688
|
1 #include <wchar.h>
|
|
2 #include <string.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdarg.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <ctype.h>
|
|
7 #include "fmt.h"
|
|
8 #include <curl/curl.h>
|
|
9
|
|
10 char *fmt_escape(char *str)
|
|
11 {
|
|
12 return curl_escape(str, 0);
|
|
13 }
|
|
14
|
|
15 char *fmt_unescape(char *str)
|
|
16 {
|
|
17 return curl_unescape(str, 0);
|
|
18 }
|
|
19
|
|
20 char *fmt_timestr(time_t t, int gmt)
|
|
21 {
|
|
22 struct tm *tm;
|
|
23 static char buf[30];
|
|
24
|
|
25 tm = gmt ? gmtime(&t) : localtime(&t);
|
|
26 snprintf(buf, sizeof(buf), "%d-%.2d-%.2d %.2d:%.2d:%.2d",
|
|
27 tm->tm_year + 1900,
|
|
28 tm->tm_mon + 1,
|
|
29 tm->tm_mday,
|
|
30 tm->tm_hour,
|
|
31 tm->tm_min,
|
|
32 tm->tm_sec);
|
|
33 return buf;
|
|
34 }
|
|
35
|
|
36 char *fmt_vastr(char *fmt, ...)
|
|
37 {
|
|
38 va_list ap;
|
|
39 static char buf[4096];
|
|
40 va_start(ap, fmt);
|
|
41 vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
42 va_end(ap);
|
|
43 return buf;
|
|
44 }
|
|
45
|
|
46 void fmt_debug(char *file, const char *fun, char *str)
|
|
47 {
|
|
48 fprintf(stderr, "%s [%s] %s: %s\n", fmt_timestr(time(NULL), 0),
|
|
49 file, fun, str);
|
|
50 }
|
|
51
|
|
52 char *fmt_string_pack(char *string, char *fmt, ...)
|
|
53 {
|
|
54 int buflen = 0, stringlen = 0;
|
|
55 char buf[4096];
|
|
56 va_list ap;
|
|
57
|
|
58 va_start(ap, fmt);
|
|
59 vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
60 va_end(ap);
|
|
61 if(string != NULL) stringlen = strlen(string);
|
|
62 buflen = strlen(buf);
|
|
63
|
|
64 string = realloc(string, stringlen + buflen + 1);
|
|
65 memcpy(string + stringlen, buf, buflen);
|
|
66 *(string + stringlen + buflen) = 0;
|
|
67 return string;
|
|
68 }
|
|
69
|
|
70 int fmt_strcasecmp(const char *s1, const char *s2)
|
|
71 {
|
|
72 while (toupper(*s1) == toupper(*s2++))
|
|
73 if (!*s1++)
|
|
74 return 0;
|
|
75 return toupper(s1[0]) - toupper(s2[-1]);
|
|
76 }
|
|
77
|
|
78 int fmt_strncasecmp(const char *s1, const char *s2, size_t n)
|
|
79 {
|
|
80 while (toupper(*s1) == toupper(*s2++) && --n)
|
|
81 if(!*s1++)
|
|
82 return 0;
|
|
83 return n ? toupper(s1[0]) - toupper(s2[-1]) : 0;
|
|
84 }
|