-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurlextract.c
68 lines (61 loc) · 1.47 KB
/
urlextract.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
#include <string.h>
#include <stdio.h>
#include <regex.h>
#include <sys/types.h>
#include <stdlib.h>
int getnjpg(size_t n, const char *src, char *dest);
static int ngetmatches(size_t n, char *reg, const char *src, char *dest)
{
regex_t regex;
regmatch_t pmatch[2];
size_t i;
size_t count;
const char *cursor;
const char *old;
size_t start;
size_t end;
size_t size;
char *write;
count = 0;
write = dest;
if (regcomp(®ex, reg, 0))
{
printf("Error compiling regex\n");
return -1;
}
cursor = src;
old = cursor;
for (i = 0; i < n; i ++)
{
/*printf("cursor location %s\n",cursor);*/
if (regexec(®ex, cursor, 2, (regmatch_t *)&pmatch, 0) != 0)
{
/*printf("No more matches: stopped at %ld\n", i);*/
break;
}
else{
start = pmatch[0].rm_so;
end = pmatch[0].rm_eo;
size = end - start;
/*printf("Match data: %ld, %ld\n", start, end);*/
cursor = old + start;
strncpy(write, cursor, size);
*(write + size) = '\0';
/*printf("Found match: %s\n", write);*/
write += MAX_MATCH;
cursor = old + end;
old = cursor;
/*printf("end value: %ld\n", end);
printf("cursor value: %ld\n", (size_t)cursor);*/
count += 1;
}
}
regfree(®ex);
/*printf("Found %ld matches\n", count);*/
return count;
}
int getnjpg(size_t n, const char *src, char *dest)
{
/* TODO match more with nicer regex */
return ngetmatches(n, "https://i[^ ]*.jpg", src, dest);
}