-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
188 lines (154 loc) · 4.34 KB
/
misc.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
#include <stdlib.h>
#include "misc.h"
char PROGVERSION[] = "\tGetFast v%.1f by Ramazan AYYILDIZ\n";
void reverse_string(char *str) {
char *p, *s;
int i;
int size;
if ((size = (int) strlen(str)) == 0)
return;
p = (char *) calloc(size, sizeof(char));
s = p;
for (i = size; i >= 0; i--, s++)
*s = *(str + i - 1);
*s = '\0';
memset(str, 0, (size_t) size);
strncpy(str, p, (size_t) size);
}/*end of the reverse_string*/
void usage() {
printf("\n\t-----------------------------------------------------------\n");
printf(PROGVERSION, VERSION);
printf("\n\tUsage : \n\t-------\n\n");
printf("\t GetFast -[d/u] [-n PART_NUMBER] [-v] [-h/--help] url \n\n");
printf("\t -d : Downlaod a file \n");
printf("\t -u : Downlaod remote url and save \n");
printf("\t -n : Split the remote file into parts PART_NUMBER while\n");
printf("\t downloading the file. Default=5 parts \n");
printf("\t -v : Display version of GetFast\n");
printf("\t -h : Get help\n");
printf("\t --help : Get help\n");
printf("\t url : Remote url. Url must be HTTP protocol\n");
printf("\t-----------------------------------------------------------\n\n");
}/*end of the usage */
BOOL parse_string(char *out, char *atr) {
char *s;
char *tok;
int a_link = 0, img_link = 0;
#ifdef DEBUG
printf("\t[Misc.c ] Parse String start \n");
#endif
if (strlen(atr) == 0) {
return FALSE;
}
if (atr[0] == '/') {
return FALSE;
}
if (atr[0] == 'a' || atr[0] == 'A')
a_link = 1;
else if (strncmp(atr, "img", 3) == 0)
img_link = 1;
else {
return FALSE;
}
s = atr;
while (TRUE) {
tok = strtok(s, " ");
if (tok == NULL) break;
if (a_link && (strncmp(tok, "href=\"", 6)) == 0) {
char *t = strtok(tok + 6, "\"");
strcpy(out, t);
return TRUE;
} else if (img_link && (strncmp(tok, "src=\"", 5)) == 0) {
char *t = strtok(tok + 5, "\"");
strcpy(out, t);
return TRUE;
}
s = s + strlen(tok) + 1;
}
#ifdef DEBUG
printf("\t[Misc.c ] Parse String finish \n");
#endif
return FALSE;
}
void progress_bar(float cur, float tot) {
static float last = 0.0;
float rate = (float) ((cur / tot) * 100.0);
if (rate == 0) rate = 1.0;
int i = 0, dot = 0;
dot = (int) (rate / 2.0);
if (rate < last + 5.0 || rate > 100) return;
for (i = 0; i < dot; i++) {
printf(".");
}
for (i = 0; i < (52 - dot); i++) {
printf(" ");
}
printf("%.3f %s\n", rate, "%");
last = rate;
}
BOOL parse_url(struct url_info *u, char *h) {
char *s;
char buf[FILE_SIZE];
int i;
#ifdef DEBUG
printf("\t[Misc.c ] Parse URL start \n");
#endif
if (!u) {
u = (struct url_info *) malloc(sizeof(struct url_info));
if (!u) {
#ifdef DEBUG
fprintf(stderr,"\t[Misc.c ]Memory is full\n");
#endif
return FALSE;
}
}/*if url has no area at memory */
if (strncmp(h, "http://", 7) != 0) {
fprintf(stderr, "The url must start with >> http://... \n");
usage();
u = NULL;
exit((int) FAIL);
}
strcpy(u->url, h);/*Copy all arg[1] to url */
#ifdef DEBUG
printf("\t[Misc.c ] strcpy(u->url,h) Now url : %s \n",u->url);
#endif
reverse_string(u->url);
i = 0;
for (s = u->url; *s != '/'; s++) {
buf[i++] = *s;
buf[i] = '\0';
}
reverse_string(buf);
strcpy(u->file, buf);
reverse_string(u->url);
s = u->url + 7;
i = 0;
for (; *s != '\0'; s++) {
if (*s == '/') break;
buf[i++] = *s;
buf[i] = '\0';
}
strcpy(u->host, buf);
#ifdef DEBUG
printf("\t[Misc.c ] Hostname: %s\n",buf);
#endif
u->port = 80;
#ifdef DEBUG
printf("\t[Misc.c ] After file Now url->port:%d url->file:%s \n",u->port,u->file);
#endif
i = 0;
s = h + 7 + strlen(u->host);
for (; *s != '\0'; s++) {
if (*s == '\0') break;
buf[i++] = *s;
buf[i] = '\0';
}
strcpy(u->identifer, buf);
#ifdef DEBUG
printf("\t[Misc.c ] Identifer: %s \n",u->identifer);
#endif
#ifdef DEBUG
printf("\t[Misc.c ] Parse URL finish\n");
#endif
return TRUE;
}/*end of the parse_url */