-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
191 lines (154 loc) · 3.9 KB
/
utils.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <strings.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <regex.h>
#include <pcre.h>
// size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
// {
// int written = fwrite(ptr, size, nmemb, file);
// return written;
// }
int curl(char *url, char *path)
{
FILE *file;
CURL *curl;
CURLcode ret;
struct curl_slist *headers = NULL;
if (path == NULL)
{
path = index(url, '/');
path++;
}
/* First step, init curl */
curl = curl_easy_init();
if (!curl)
{
return -1;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
/* HTTP HEADER */
headers = curl_slist_append(headers, "Connection: close");
headers = curl_slist_append(headers, "Accept-Charset: utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
/* 可以通过 CURLOPT_WRITEDATA属性给默认回调函数传递一个已经打开的文件指针,用于将数据输出到文件里。 */
file = fopen(path, "w");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)file);
//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
/* Allow curl to perform the action */
ret = curl_easy_perform(curl);
long response_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
fclose(file);
/* ret == 0 means everything is OK. */
if (ret != 0 || response_code != 200)
{
return -1;
}
return 0;
}
char *strlink(char *str1, ...)
{
char *str = malloc(sizeof(char));
memset(str, 0, 1);
// char *str = NULL;
va_list arg_ptr; // 参数列表指针
va_start(arg_ptr, str1); // 指向固定参数
// va_copy(dest, src)
char *strtemp = str1;
char *p;
while(abs(strcmp(strtemp, "__Last")))
{
p = realloc(str, strlen(str) + strlen(strtemp) + 1);
if (p == NULL)
{
return NULL;
}
str = p;
strcat(str, strtemp);
strtemp = va_arg(arg_ptr, char *); // 返回char*参数
}
va_end(arg_ptr); // va_start配套
// printf("%s\n", str);
return str;
}
char *substr(const char *str, int start, int end)
{
char *sub, *subtemp;
int n;
if ((n = end - start) < 0)
{
return NULL;
}
sub = malloc((n + 1) * sizeof(char));
strncpy(sub, str + start, n);
sub[n] = 0;
return sub;
}
/*
* 匹配字符串
*
* test: 需要匹配的字符串
* pattern: 模式
* rm_eo: 匹配尾偏移量
* add: 左侧忽略的字符数
* sub: 右侧忽略的字符数
*/
char *match(char *text, const char *pattern, int *rm_eo, int add, int sub)
{
char *str;
regex_t preg;
regmatch_t pmatch;
if (text == NULL || pattern == NULL) { return NULL; }
if (regcomp(&preg, pattern, 0)) { return NULL; }
if (regexec(&preg, text, 1, &pmatch, 0)) { return NULL; }
*rm_eo = pmatch.rm_eo;
str = substr(text, pmatch.rm_so + add, pmatch.rm_eo - sub);
return str;
}
char *match_pcre(char *text, const char *pattern, int *rm_eo, int index, int add, int sub)
{
char *str;
pcre *pc;
int pe;
const char *errptr;
int erroffset;
int ovector[30];
if (text == NULL || pattern == NULL) { return NULL; }
/* PCRE_DOTALL . matches anything including NL */
pc = pcre_compile(pattern, PCRE_DOTALL, &errptr, &erroffset, NULL);
pe = pcre_exec(pc, NULL, text, strlen(text), 0, 0, ovector, 30);
if (pe <= index)
{
return NULL;
}
*rm_eo = ovector[2 * index + 1];
str = substr(text, ovector[2 * index] + add, ovector[2 * index + 1] - sub);
return str;
}
int match_pcre2(char *text, const char *pattern, int *start, int *end)
{
char *str;
pcre *pc;
int pe;
const char *errptr;
int erroffset;
int ovector[30];
if (text == NULL || pattern == NULL) { return 0; }
/* PCRE_DOTALL . matches anything including NL */
pc = pcre_compile(pattern, PCRE_DOTALL, &errptr, &erroffset, NULL);
pe = pcre_exec(pc, NULL, text, strlen(text), 0, 0, ovector, 30);
if (pe <= 1)
{
return 0;
}
*start = ovector[2];
*end = ovector[3];
return 1;
}