OSDN Git Service

First version
[st-ro/stro.git] / src / common / utils.c
1 // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2 // For more information, see LICENCE in the main folder
3
4 #include "cbasetypes.h"
5 #include "showmsg.h"
6 #include "socket.h"
7 #include "utils.h"
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h> // floor()
12
13 #ifdef WIN32
14         #include "winapi.h"
15         #ifndef F_OK
16                 #define F_OK   0x0
17         #endif  /* F_OK */
18 #else
19         #include <unistd.h>
20         #include <dirent.h>
21         #include <sys/stat.h>
22 #endif
23
24
25 /// Dumps given buffer into file pointed to by a handle.
26 void WriteDump(FILE* fp, const void* buffer, size_t length)
27 {
28         size_t i;
29         char hex[48+1], ascii[16+1];
30
31         fprintf(fp, "--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F   0123456789ABCDEF\n");
32         ascii[16] = 0;
33
34         for( i = 0; i < length; i++ )
35         {
36                 char c = RBUFB(buffer,i);
37
38                 ascii[i%16] = ISCNTRL(c) ? '.' : c;
39                 sprintf(hex+(i%16)*3, "%02X ", RBUFB(buffer,i));
40
41                 if( (i%16) == 15 )
42                 {
43                         fprintf(fp, "%03X %s  %s\n", (unsigned int)(i/16), hex, ascii);
44                 }
45         }
46
47         if( (i%16) != 0 )
48         {
49                 ascii[i%16] = 0;
50                 fprintf(fp, "%03X %-48s  %-16s\n", (unsigned int)(i/16), hex, ascii);
51         }
52 }
53
54
55 /// Dumps given buffer on the console.
56 void ShowDump(const void* buffer, size_t length)
57 {
58         size_t i;
59         char hex[48+1], ascii[16+1];
60
61         ShowDebug("--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F   0123456789ABCDEF\n");
62         ascii[16] = 0;
63         
64         for( i = 0; i < length; i++ )
65         {
66                 char c = RBUFB(buffer,i);
67
68                 ascii[i%16] = ISCNTRL(c) ? '.' : c;
69                 sprintf(hex+(i%16)*3, "%02X ", RBUFB(buffer,i));
70
71                 if( (i%16) == 15 )
72                 {
73                         ShowDebug("%03X %s  %s\n", i/16, hex, ascii);
74                 }
75         }
76
77         if( (i%16) != 0 )
78         {
79                 ascii[i%16] = 0;
80                 ShowDebug("%03X %-48s  %-16s\n", i/16, hex, ascii);
81         }
82 }
83
84
85 #ifdef WIN32
86
87 static char* checkpath(char *path, const char *srcpath)
88 {       // just make sure the char*path is not const
89         char *p=path;
90         if(NULL!=path && NULL!=srcpath)
91         while(*srcpath) {
92                 if (*srcpath=='/') {
93                         *p++ = '\\';
94                         srcpath++;
95                 }
96                 else
97                         *p++ = *srcpath++;
98         }
99         *p = *srcpath; //EOS
100         return path;
101 }
102
103 void findfile(const char *p, const char *pat, void (func)(const char*))
104 {
105         WIN32_FIND_DATAA FindFileData;
106         HANDLE hFind;
107         char tmppath[MAX_PATH+1];
108
109         const char *path    = (p  ==NULL)? "." : p;
110         const char *pattern = (pat==NULL)? "" : pat;
111
112         checkpath(tmppath,path);
113         if( PATHSEP != tmppath[strlen(tmppath)-1])
114                 strcat(tmppath, "\\*");
115         else
116                 strcat(tmppath, "*");
117
118         hFind = FindFirstFileA(tmppath, &FindFileData);
119         if (hFind != INVALID_HANDLE_VALUE)
120         {
121                 do
122                 {
123                         if (strcmp(FindFileData.cFileName, ".") == 0)
124                                 continue;
125                         if (strcmp(FindFileData.cFileName, "..") == 0)
126                                 continue;
127
128                         sprintf(tmppath,"%s%c%s",path,PATHSEP,FindFileData.cFileName);
129
130                         if (FindFileData.cFileName && strstr(FindFileData.cFileName, pattern)) {
131                                 func( tmppath );
132                         }
133
134
135                         if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
136                         {
137                                 findfile(tmppath, pat, func);
138                         }
139                 }while (FindNextFileA(hFind, &FindFileData) != 0);
140                 FindClose(hFind);
141         }
142         return;
143 }
144
145 /**
146  * Check if the path is a directory or file
147  * @param filepath: Location of file
148  * @return 0 = Error
149  *         1 = Directory
150  *         2 = File
151  *         3 = File but doesn't exist
152  */
153 int check_filepath(const char* filepath)
154 {
155         DWORD Attribute;
156
157         if (Attribute = GetFileAttributes(filepath)) {
158                 if ((Attribute&INVALID_FILE_ATTRIBUTES) && GetLastError() == ERROR_FILE_NOT_FOUND) {
159                         SetLastError(0);
160                         return 3;
161                 } else if (Attribute&FILE_ATTRIBUTE_DIRECTORY)
162                         return 1;
163                 else
164                         return 2;
165         }
166
167         return 0;
168 }
169
170 #else
171
172 #define MAX_DIR_PATH 2048
173
174 /**
175  * Check if the path is a directory or file
176  * @param filepath: Location of file
177  * @return 0 = Error
178  *         1 = Directory
179  *         2 = File
180  *         3 = Neither a file or directory
181  */
182 int check_filepath(const char* filepath)
183 {
184         struct stat s;
185
186         if (stat(filepath, &s) == 0) {
187                 if (s.st_mode&S_IFDIR)
188                         return 1;
189                 else if (s.st_mode&S_IFREG)
190                         return 2;
191                 else
192                         return 3;
193         }
194
195         return 0;
196 }
197
198 static char* checkpath(char *path, const char*srcpath)
199 {       // just make sure the char*path is not const
200         char *p=path;
201         if(NULL!=path && NULL!=srcpath)
202         while(*srcpath) {
203                 if (*srcpath=='\\') {
204                         *p++ = '/';
205                         srcpath++;
206                 }
207                 else
208                         *p++ = *srcpath++;
209         }
210         *p = *srcpath; //EOS
211         return path;
212 }
213
214 void findfile(const char *p, const char *pat, void (func)(const char*))
215 {
216         DIR* dir;                                       // pointer to the scanned directory.
217         struct dirent* entry;           // pointer to one directory entry.
218         struct stat dir_stat;       // used by stat().
219         char tmppath[MAX_DIR_PATH+1];
220         char path[MAX_DIR_PATH+1]= ".";
221         const char *pattern = (pat==NULL)? "" : pat;
222         if(p!=NULL) strcpy(path,p);
223
224         // open the directory for reading
225         dir = opendir( checkpath(path, path) );
226         if (!dir) {
227                 ShowError("Cannot read directory '%s'\n", path);
228                 return;
229         }
230
231         // scan the directory, traversing each sub-directory
232         // matching the pattern for each file name.
233         while ((entry = readdir(dir))) {
234                 // skip the "." and ".." entries.
235                 if (strcmp(entry->d_name, ".") == 0)
236                         continue;
237                 if (strcmp(entry->d_name, "..") == 0)
238                         continue;
239
240                 sprintf(tmppath,"%s%c%s",path, PATHSEP, entry->d_name);
241
242                 // check if the pattern matchs.
243                 if (entry->d_name && strstr(entry->d_name, pattern)) {
244                         func( tmppath );
245                 }
246                 // check if it is a directory.
247                 if (stat(tmppath, &dir_stat) == -1) {
248                         ShowError("stat error %s\n': ", tmppath);
249                         continue;
250                 }
251                 // is this a directory?
252                 if (S_ISDIR(dir_stat.st_mode)) {
253                         // decent recursivly
254                         findfile(tmppath, pat, func);
255                 }
256         }//end while
257
258         closedir(dir);
259 }
260 #endif
261
262 bool exists(const char* filename)
263 {
264         return !access(filename, F_OK);
265 }
266
267 uint8 GetByte(uint32 val, int idx)
268 {
269         switch( idx )
270         {
271         case 0: return (uint8)( (val & 0x000000FF)         );
272         case 1: return (uint8)( (val & 0x0000FF00) >> 0x08 );
273         case 2: return (uint8)( (val & 0x00FF0000) >> 0x10 );
274         case 3: return (uint8)( (val & 0xFF000000) >> 0x18 );
275         default:
276 #if defined(DEBUG)
277                 ShowDebug("GetByte: invalid index (idx=%d)\n", idx);
278 #endif
279                 return 0;
280         }
281 }
282
283 uint16 GetWord(uint32 val, int idx)
284 {
285         switch( idx )
286         {
287         case 0: return (uint16)( (val & 0x0000FFFF)         );
288         case 1: return (uint16)( (val & 0xFFFF0000) >> 0x10 );
289         default:
290 #if defined(DEBUG)
291                 ShowDebug("GetWord: invalid index (idx=%d)\n", idx);
292 #endif
293                 return 0;
294         }
295 }
296 uint16 MakeWord(uint8 byte0, uint8 byte1)
297 {
298         return byte0 | (byte1 << 0x08);
299 }
300
301 uint32 MakeDWord(uint16 word0, uint16 word1)
302 {
303         return
304                 ( (uint32)(word0        ) )|
305                 ( (uint32)(word1 << 0x10) );
306 }
307
308 /*************************************
309 * Big-endian compatibility functions *
310 *************************************/
311
312 // Converts an int16 from current machine order to little-endian
313 int16 MakeShortLE(int16 val)
314 {
315         unsigned char buf[2];
316         buf[0] = (unsigned char)( (val & 0x00FF)         );
317         buf[1] = (unsigned char)( (val & 0xFF00) >> 0x08 );
318         return *((int16*)buf);
319 }
320
321 // Converts an int32 from current machine order to little-endian
322 int32 MakeLongLE(int32 val)
323 {
324         unsigned char buf[4];
325         buf[0] = (unsigned char)( (val & 0x000000FF)         );
326         buf[1] = (unsigned char)( (val & 0x0000FF00) >> 0x08 );
327         buf[2] = (unsigned char)( (val & 0x00FF0000) >> 0x10 );
328         buf[3] = (unsigned char)( (val & 0xFF000000) >> 0x18 );
329         return *((int32*)buf);
330 }
331
332 // Reads an uint16 in little-endian from the buffer
333 uint16 GetUShort(const unsigned char* buf)
334 {
335         return   ( ((uint16)(buf[0]))         )
336                         |( ((uint16)(buf[1])) << 0x08 );
337 }
338
339 // Reads an uint32 in little-endian from the buffer
340 uint32 GetULong(const unsigned char* buf)
341 {
342         return   ( ((uint32)(buf[0]))         )
343                         |( ((uint32)(buf[1])) << 0x08 )
344                         |( ((uint32)(buf[2])) << 0x10 )
345                         |( ((uint32)(buf[3])) << 0x18 );
346 }
347
348 // Reads an int32 in little-endian from the buffer
349 int32 GetLong(const unsigned char* buf)
350 {
351         return (int32)GetULong(buf);
352 }
353
354 // Reads a float (32 bits) from the buffer
355 float GetFloat(const unsigned char* buf)
356 {
357         uint32 val = GetULong(buf);
358         return *((float*)(void*)&val);
359 }
360
361 /// calculates the value of A / B, in percent (rounded down)
362 unsigned int get_percentage(const unsigned int A, const unsigned int B)
363 {
364         double result;
365
366         if( B == 0 )
367         {
368                 ShowError("get_percentage(): divison by zero! (A=%u,B=%u)\n", A, B);
369                 return ~0U;
370         }
371
372         result = 100 * ((double)A / (double)B);
373
374         if( result > UINT_MAX )
375         {
376                 ShowError("get_percentage(): result percentage too high! (A=%u,B=%u,result=%g)\n", A, B, result);
377                 return UINT_MAX;
378         }
379
380         return (unsigned int)floor(result);
381 }
382
383 /**
384  * Calculates the Levenshtein distance of two strings.
385  * @author http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C
386  */
387 int levenshtein(const char *s1, const char *s2) {
388         unsigned int s1len, s2len, x, y, lastdiag, olddiag, i;
389         unsigned int *column;
390         s1len = strlen(s1);
391         s2len = strlen(s2);
392         column = malloc((s1len+1) * sizeof(unsigned int));
393         for (y = 1; y <= s1len; y++)
394                 column[y] = y;
395         for (x = 1; x <= s2len; x++) {
396                 column[0] = x;
397                 for (y = 1, lastdiag = x-1; y <= s1len; y++) {
398                         olddiag = column[y];
399                         column[y] = min(min(column[y] + 1, column[y-1] + 1), lastdiag + (s1[y-1] == s2[x-1] ? 0 : 1));
400                         lastdiag = olddiag;
401                 }
402         }
403         i = column[s1len];
404         free(column);
405         return i;
406 }