-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
106 lines (82 loc) · 2.25 KB
/
function.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
size_t getFileSize(FILE *fp){
size_t file_size = 0;
// Get File Size
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
return file_size;
}
/*
String Replace Function
Source: http://stackoverflow.com/a/32413923
*/
void str_replace(char *target, const char *needle, const char *replacement){
char buffer[1024] = { 0 };
char *insert_point = &buffer[0];
const char *tmp = target;
size_t needle_len = strlen(needle);
size_t repl_len = strlen(replacement);
while (1) {
const char *p = strstr(tmp, needle);
// walked past last occurrence of needle; copy remaining part
if (p == NULL) {
strcpy(insert_point, tmp);
break;
}
// copy part before needle
memcpy(insert_point, tmp, p - tmp);
insert_point += p - tmp;
// copy replacement string
memcpy(insert_point, replacement, repl_len);
insert_point += repl_len;
// adjust pointers, move on
tmp = p + needle_len;
}
// write altered string back to target
strcpy(target, buffer);
}
/*
URL Decoding
Source: https://www.rosettacode.org/wiki/URL_decoding#C
*/
int ishex(int x){
return (x >= '0' && x <= '9') ||
(x >= 'a' && x <= 'f') ||
(x >= 'A' && x <= 'F');
}
int url_decode(const char *s, char *dec){
char *o;
const char *end = s + strlen(s);
int c;
for (o = dec; s <= end; o++) {
c = *s++;
if (c == '+') c = ' ';
else if (c == '%' && ( !ishex(*s++) ||
!ishex(*s++) ||
!sscanf(s - 2, "%2x", &c)))
return -1;
if (dec) *o = c;
}
return o - dec;
}
/*
Generating RFC 1123 Time Stamp
Source: http://stackoverflow.com/a/2727122
*/
/*
static const char *DAY_NAMES[] =
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char *MONTH_NAMES[] =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
char *Rfc1123_DateTime(time_t t){
const int RFC1123_TIME_LEN = 29;
struct tm tm;
char * buf = malloc(RFC1123_TIME_LEN+1);
gmtime_s(&tm, &t);
strftime(buf, RFC1123_TIME_LEN+1, "---, %d --- %Y %H:%M:%S GMT", &tm);
memcpy(buf, DAY_NAMES[tm.tm_wday], 3);
memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
return buf;
}
*/