1 /*********************************************************************
2 *
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * ===================================================================
15 * Revision History ::
16 * YYYY.MM.DD Change ID Developer
17 * Description
18 * -------------------------------------------------------------------
19 * 2002.04.25 Vlad Skarzhevskyy
20 * Initial implementation.
21 *
22 * ===================================================================
23 *
24 ********************************************************************/
25
26 #include "pure-sfv.h"
27
getFilePath(char * filename)28 char* getFilePath(char* filename)
29 {
30 static string tmp_name;
31 char* end;
32 strncpy(tmp_name, filename, MAX_STRING_LEN);
33 /* Unix function dirname
34 dir = dirname(tmp_name);
35 Begin DIY
36 */
37 end = strrchr(tmp_name, DIR_SEP_PLATFORM);
38 if (end == NULL) {
39 return NULL;
40 } else {
41 *end = '\0';
42 }
43 /* End DIY */
44 DBUG_PRINT("dirname", ("%s", tmp_name));
45 if (strcmp(tmp_name, ".") == 0) {
46 return NULL;
47 }
48 return tmp_name;
49 }
50
getBaseName(char * filename)51 char* getBaseName(char* filename)
52 {
53 char *ret;
54 /* Unix function basename */
55 for (ret = filename; *filename; filename++) {
56 if (*filename == DIR_SEP_PLATFORM) {
57 ret = filename + 1;
58 }
59 }
60 DBUG_PRINT("basename", ("%s", ret));
61 return ret;
62 }
63
getBaseNameWinUnix(char * filename)64 char* getBaseNameWinUnix(char* filename)
65 {
66 char* unix_end;
67 char* win_end;
68 unix_end = strrchr(filename, DIR_SEP_UNIX);
69 if (unix_end == NULL) {
70 unix_end = filename;
71 } else {
72 unix_end ++;
73 }
74 win_end = strrchr(unix_end, DIR_SEP_DOS);
75 if (win_end == NULL) {
76 win_end = unix_end;
77 } else {
78 win_end ++;
79 }
80 DBUG_PRINT("basename", ("%s", win_end));
81 return win_end;
82 }
83
makeCleanName(char * src,char * dst)84 void makeCleanName(char* src, char* dst)
85 {
86 int i, k, len;
87 char c;
88
89 len = strlen(src);
90 k = 0;
91 for (i = 0; i < len; i++)
92 {
93 c = src[i];
94 if ((c == '_') || (c == ' ') || (c == '-')) {
95 continue;
96 }
97 c = tolower(c);
98 dst[k] = c;
99 k++;
100 }
101 dst[k] = '\0';
102 }
103
getFileExtension(char * filename,char * dst)104 void getFileExtension(char* filename, char* dst)
105 {
106 int i, len;
107 char* end;
108 dst[0] = '\0';
109 end = strrchr(filename, '.');
110 if (end == NULL) {
111 return;
112 }
113 end ++;
114 len = strlen(end);
115 if ((len == 0) || (len > MAX_FILE_EXTENSION)) {
116 return;
117 }
118 for(i = 0; i < len; i++) {
119 dst[i] = tolower(end[i]);
120 }
121 dst[i] = '\0';
122 return;
123 }
124
hasWildcard(const char * name)125 int hasWildcard(const char * name)
126 {
127 if (strchr(name, '*') != NULL) {
128 return 1;
129 }
130 if (strchr(name, '?') != NULL) {
131 return 1;
132 }
133 return 0;
134 }
135
136 /* http://sourceforge.net/snippet/detail.php?type=snippet&id=100891 */
137 /* eg. match("a*b","AXYZB", 1) .. true */
138 /* eg. match("a?","a:", 1) .. true */
139
140 /* Problem in mathc "*.txt" "w.txt any.txt" */
141
match(const char * p,const char * s,int ignore_case)142 int match( const char * p, const char * s, int ignore_case)
143 {
144 switch( *p ){
145 case '\0' : return ! *s ;
146 case '*' : return (match(p+1, s, ignore_case)) || (( *s ) && match(p, s+1 , ignore_case));
147 case '?' : return *s && match(p+1, s+1, ignore_case);
148 default :
149 return ( (*p == *s) ||
150 (ignore_case && (toupper(*p) == toupper(*s)))
151 ) && match(p+1, s+1, ignore_case);
152 /* *p == *s && match( p+1, s+1 ); */
153 }
154 }
155
156 #ifdef _MSC_VER
157 /* Read directory list under MS Visual C++ */
158
opendir(const char * dirname)159 DIR *opendir(const char *dirname) {
160 DIR *d = malloc(sizeof(DIR));
161 d->dirp = 0;
162 strncpy(d->name, dirname, PATH_MAX);
163 return d;
164 }
165
closedir(DIR * dirp)166 int closedir(DIR *dirp) {
167 free(dirp);
168 return 0;
169 }
170
readdir(DIR * d)171 struct dirent *readdir(DIR *d) {
172 static struct dirent udirinfo;
173 static string tmp_name;
174 struct _finddata_t dirinfo;
175 int rc;
176
177 if (d->dirp <= 0) {
178 /* Find first file in current directory */
179 if (d->name[0] == 0) {
180 strncpy(tmp_name, ".%c*.*", DIR_SEP_PLATFORM);
181 } else {
182 sprintf(tmp_name, "%s%c*.*", d->name, DIR_SEP_PLATFORM);
183 }
184 d->dirp = _findfirst(tmp_name, &dirinfo);
185 if (d->dirp == -1) {
186 return NULL;
187 }
188 } else {
189 rc = _findnext(d->dirp, &dirinfo);
190 if (errno == ENOENT) {
191 errno = 0;
192 }
193 if (rc == -1) {
194 return NULL;
195 }
196 }
197
198 strncpy(udirinfo.d_name, dirinfo.name, PATH_MAX);
199
200 return &udirinfo;
201
202 }
203
204 #endif
205
206
207 /* #ifdef __unix */
208
209 /* Read directory list under unix */
210
dirRead(char * dir_name,crc_info_type * dir_info)211 int dirRead(char* dir_name, crc_info_type* dir_info)
212 {
213 DIR *dirp;
214 struct dirent *dirinfo;
215 crc_info_item_type item;
216 char* use_dir_name;
217
218 if ((dir_name == NULL) || (dir_name[0] == '\0')) {
219 use_dir_name = ".";
220 } else {
221 use_dir_name = dir_name;
222 }
223
224 strncpy(dir_info->name, use_dir_name, MAX_STRING_LEN);
225
226 dirp = opendir(use_dir_name);
227 if (dirp == NULL) {
228 fprintf(stderr, "sfv: %s: %s\n", use_dir_name, strerror(errno));
229 return ERROR_DIR_ACCESS;
230 }
231
232 errno = 0;
233
234 while ((dirinfo = readdir(dirp)) != NULL) {
235 if ((strcmp(dirinfo->d_name, ".") == 0) ||
236 (strcmp(dirinfo->d_name, "..") == 0)) {
237 continue;
238 }
239 sfvInfoItemNULL(&item);
240 strncpy(item.file_name, dirinfo->d_name, MAX_STRING_LEN);
241 item.crc = 0;
242 if (sfvInfoAdd(dir_info, &item)) {
243 return ERROR_MEMORY;
244 }
245
246 }
247 if (errno) {
248 fprintf(stderr, "sfv: %s: %s\n", use_dir_name, strerror(errno));
249 return ERROR_DIR_ACCESS;
250 }
251 closedir(dirp);
252
253 return 0;
254 }
255
256 /* #endif */
257
258 /* #ifdef _MSC_VER */
259
260 /* Read directory list under MS Visual C++ */
261
262 /*
263 int dirRead(char* dir_name, crc_info_type* dir_info)
264 {
265 long dirp;
266 int rc;
267 struct _finddata_t dirinfo;
268 crc_info_item_type item;
269 char *use_dir_name;
270 static string tmp_name;
271
272 if ((dir_name == NULL) || (dir_name[0] == '\0')) {
273 use_dir_name = ".";
274 } else {
275 use_dir_name = dir_name;
276 }
277
278 strncpy(dir_info->name, use_dir_name, MAX_STRING_LEN);
279 sprintf(tmp_name, "%s%c*.*", use_dir_name, DIR_SEP_PLATFORM);
280
281 dirp = _findfirst(tmp_name, &dirinfo);
282 if (dirp == -1) {
283 fprintf(stderr, "sfv: %s: %s\n", use_dir_name, strerror(errno));
284 return ERROR_DIR_ACCESS;
285 }
286
287 do {
288 if ((strcmp(dirinfo.name, ".") == 0) ||
289 (strcmp(dirinfo.name, "..") == 0)) {
290 ;
291 } else {
292 sfvInfoItemNULL(&item);
293 strncpy(item.file_name, dirinfo.name, MAX_STRING_LEN);
294 item.crc = 0;
295 if (sfvInfoAdd(dir_info, &item)) {
296 return ERROR_MEMORY;
297 }
298 }
299
300 } while ((rc = _findnext(dirp, &dirinfo)) == 0);
301
302 if ((rc == -1) && (errno != ENOENT)) {
303 fprintf(stderr, "sfv: %s: %s\n", use_dir_name, strerror(errno));
304 return ERROR_DIR_ACCESS;
305 }
306
307 _findclose(dirp);
308
309 return 0;
310 }
311 #endif
312
313 */
314
315 #ifdef _MSC_VER
316
chdir(const char * path)317 int chdir(const char *path)
318 {
319 /* TODO */
320 return _chdir(path);
321 }
322
getcwd(char * buf,int size)323 char *getcwd(char *buf, int size)
324 {
325 return _getcwd(buf, size);
326 }
327
328 #endif
329
330
isFile(char * dir_name,char * file_name)331 int isFile(char* dir_name, char* file_name)
332 {
333 return 1;
334 }
335
fileReadAttributes(crc_info_item_type * file_info)336 int fileReadAttributes(crc_info_item_type* file_info)
337 {
338 int rc = 0;
339 struct stat fileinfo;
340 rc = stat(file_info->file_name, &fileinfo);
341 if (rc) {
342 fprintf(stderr, "sfv: Error reading file attributs [%s]: %s\n", file_info->file_name, strerror(errno));
343 return rc;
344 }
345 file_info->size = fileinfo.st_size;
346 file_info->time = *localtime(&fileinfo.st_mtime);
347 file_info->is_dir = S_ISDIR(fileinfo.st_mode);
348 DBUG_PRINT("file", ("%20s, %ld, %i", file_info->file_name, file_info->size, file_info->is_dir));
349 return rc;
350 }
351
dirReadAttributes(char * dir_name,crc_info_type * dir_info)352 int dirReadAttributes(char* dir_name, crc_info_type* dir_info)
353 {
354 int rc = 0;
355 int i;
356
357 for(i = 0; i < dir_info->len; i++)
358 {
359 rc = fileReadAttributes(&(dir_info->items[i]));
360 if (rc) return rc;
361 }
362 return rc;
363 }
364
dirFindFile(char * file_name,crc_info_type * dir_info)365 int dirFindFile(char* file_name, crc_info_type* dir_info)
366 {
367 int i;
368 for(i = 0; i < dir_info->len; i++)
369 {
370 if (strcmp(file_name, dir_info->items[i].file_name) == 0) {
371 return (i + 1);
372 }
373 }
374 return 0;
375 }
376
dirFindFileLike(char * file_name_clean,crc_info_type * dir_info)377 int dirFindFileLike(char* file_name_clean, crc_info_type* dir_info)
378 {
379 int i;
380 for(i = 0; i < dir_info->len; i++)
381 {
382 if (strcmp(file_name_clean, dir_info->items[i].file_name_clean) == 0) {
383 return (i + 1);
384 }
385 }
386 return 0;
387 }
388
389 /* EOF */
390
391