utils.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/mmo.h"
  5. #include "../common/malloc.h"
  6. #include "../common/showmsg.h"
  7. #include "socket.h"
  8. #include "utils.h"
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h> // floor()
  14. #ifdef WIN32
  15. #include "../common/winapi.h"
  16. #ifndef F_OK
  17. #define F_OK 0x0
  18. #endif /* F_OK */
  19. #else
  20. #include <unistd.h>
  21. #include <dirent.h>
  22. #include <sys/stat.h>
  23. #endif
  24. /// Dumps given buffer into file pointed to by a handle.
  25. void WriteDump(FILE* fp, const void* buffer, size_t length)
  26. {
  27. size_t i;
  28. char hex[48+1], ascii[16+1];
  29. fprintf(fp, "--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n");
  30. ascii[16] = 0;
  31. for( i = 0; i < length; i++ )
  32. {
  33. char c = RBUFB(buffer,i);
  34. ascii[i%16] = ISCNTRL(c) ? '.' : c;
  35. sprintf(hex+(i%16)*3, "%02X ", RBUFB(buffer,i));
  36. if( (i%16) == 15 )
  37. {
  38. fprintf(fp, "%03X %s %s\n", (unsigned int)(i/16), hex, ascii);
  39. }
  40. }
  41. if( (i%16) != 0 )
  42. {
  43. ascii[i%16] = 0;
  44. fprintf(fp, "%03X %-48s %-16s\n", (unsigned int)(i/16), hex, ascii);
  45. }
  46. }
  47. /// Dumps given buffer on the console.
  48. void ShowDump(const void* buffer, size_t length)
  49. {
  50. size_t i;
  51. char hex[48+1], ascii[16+1];
  52. ShowDebug("--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n");
  53. ascii[16] = 0;
  54. for( i = 0; i < length; i++ )
  55. {
  56. char c = RBUFB(buffer,i);
  57. ascii[i%16] = ISCNTRL(c) ? '.' : c;
  58. sprintf(hex+(i%16)*3, "%02X ", RBUFB(buffer,i));
  59. if( (i%16) == 15 )
  60. {
  61. ShowDebug("%03X %s %s\n", i/16, hex, ascii);
  62. }
  63. }
  64. if( (i%16) != 0 )
  65. {
  66. ascii[i%16] = 0;
  67. ShowDebug("%03X %-48s %-16s\n", i/16, hex, ascii);
  68. }
  69. }
  70. #ifdef WIN32
  71. static char* checkpath(char *path, const char *srcpath)
  72. { // just make sure the char*path is not const
  73. char *p=path;
  74. if(NULL!=path && NULL!=srcpath)
  75. while(*srcpath) {
  76. if (*srcpath=='/') {
  77. *p++ = '\\';
  78. srcpath++;
  79. }
  80. else
  81. *p++ = *srcpath++;
  82. }
  83. *p = *srcpath; //EOS
  84. return path;
  85. }
  86. void findfile(const char *p, const char *pat, void (func)(const char*))
  87. {
  88. WIN32_FIND_DATAA FindFileData;
  89. HANDLE hFind;
  90. char tmppath[MAX_PATH+1];
  91. const char *path = (p ==NULL)? "." : p;
  92. const char *pattern = (pat==NULL)? "" : pat;
  93. checkpath(tmppath,path);
  94. if( PATHSEP != tmppath[strlen(tmppath)-1])
  95. strcat(tmppath, "\\*");
  96. else
  97. strcat(tmppath, "*");
  98. hFind = FindFirstFileA(tmppath, &FindFileData);
  99. if (hFind != INVALID_HANDLE_VALUE)
  100. {
  101. do
  102. {
  103. if (strcmp(FindFileData.cFileName, ".") == 0)
  104. continue;
  105. if (strcmp(FindFileData.cFileName, "..") == 0)
  106. continue;
  107. sprintf(tmppath,"%s%c%s",path,PATHSEP,FindFileData.cFileName);
  108. if (FindFileData.cFileName && strstr(FindFileData.cFileName, pattern)) {
  109. func( tmppath );
  110. }
  111. if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  112. {
  113. findfile(tmppath, pat, func);
  114. }
  115. }while (FindNextFileA(hFind, &FindFileData) != 0);
  116. FindClose(hFind);
  117. }
  118. return;
  119. }
  120. #else
  121. #define MAX_DIR_PATH 2048
  122. static char* checkpath(char *path, const char*srcpath)
  123. { // just make sure the char*path is not const
  124. char *p=path;
  125. if(NULL!=path && NULL!=srcpath)
  126. while(*srcpath) {
  127. if (*srcpath=='\\') {
  128. *p++ = '/';
  129. srcpath++;
  130. }
  131. else
  132. *p++ = *srcpath++;
  133. }
  134. *p = *srcpath; //EOS
  135. return path;
  136. }
  137. void findfile(const char *p, const char *pat, void (func)(const char*))
  138. {
  139. DIR* dir; // pointer to the scanned directory.
  140. struct dirent* entry; // pointer to one directory entry.
  141. struct stat dir_stat; // used by stat().
  142. char tmppath[MAX_DIR_PATH+1];
  143. char path[MAX_DIR_PATH+1]= ".";
  144. const char *pattern = (pat==NULL)? "" : pat;
  145. if(p!=NULL) strcpy(path,p);
  146. // open the directory for reading
  147. dir = opendir( checkpath(path, path) );
  148. if (!dir) {
  149. ShowError("Cannot read directory '%s'\n", path);
  150. return;
  151. }
  152. // scan the directory, traversing each sub-directory
  153. // matching the pattern for each file name.
  154. while ((entry = readdir(dir))) {
  155. // skip the "." and ".." entries.
  156. if (strcmp(entry->d_name, ".") == 0)
  157. continue;
  158. if (strcmp(entry->d_name, "..") == 0)
  159. continue;
  160. sprintf(tmppath,"%s%c%s",path, PATHSEP, entry->d_name);
  161. // check if the pattern matchs.
  162. if (entry->d_name && strstr(entry->d_name, pattern)) {
  163. func( tmppath );
  164. }
  165. // check if it is a directory.
  166. if (stat(tmppath, &dir_stat) == -1) {
  167. ShowError("stat error %s\n': ", tmppath);
  168. continue;
  169. }
  170. // is this a directory?
  171. if (S_ISDIR(dir_stat.st_mode)) {
  172. // decent recursivly
  173. findfile(tmppath, pat, func);
  174. }
  175. }//end while
  176. closedir(dir);
  177. }
  178. #endif
  179. bool exists(const char* filename)
  180. {
  181. return !access(filename, F_OK);
  182. }
  183. uint8 GetByte(uint32 val, int idx)
  184. {
  185. switch( idx )
  186. {
  187. case 0: return (uint8)( (val & 0x000000FF) );
  188. case 1: return (uint8)( (val & 0x0000FF00) >> 0x08 );
  189. case 2: return (uint8)( (val & 0x00FF0000) >> 0x10 );
  190. case 3: return (uint8)( (val & 0xFF000000) >> 0x18 );
  191. default:
  192. #if defined(DEBUG)
  193. ShowDebug("GetByte: invalid index (idx=%d)\n", idx);
  194. #endif
  195. return 0;
  196. }
  197. }
  198. uint16 GetWord(uint32 val, int idx)
  199. {
  200. switch( idx )
  201. {
  202. case 0: return (uint16)( (val & 0x0000FFFF) );
  203. case 1: return (uint16)( (val & 0xFFFF0000) >> 0x10 );
  204. default:
  205. #if defined(DEBUG)
  206. ShowDebug("GetWord: invalid index (idx=%d)\n", idx);
  207. #endif
  208. return 0;
  209. }
  210. }
  211. uint16 MakeWord(uint8 byte0, uint8 byte1)
  212. {
  213. return byte0 | (byte1 << 0x08);
  214. }
  215. uint32 MakeDWord(uint16 word0, uint16 word1)
  216. {
  217. return
  218. ( (uint32)(word0 ) )|
  219. ( (uint32)(word1 << 0x10) );
  220. }
  221. /// calculates the value of A / B, in percent (rounded down)
  222. unsigned int get_percentage(const unsigned int A, const unsigned int B)
  223. {
  224. double result;
  225. if( B == 0 )
  226. {
  227. ShowError("get_percentage(): divison by zero! (A=%u,B=%u)\n", A, B);
  228. return ~0U;
  229. }
  230. result = 100 * ((double)A / (double)B);
  231. if( result > UINT_MAX )
  232. {
  233. ShowError("get_percentage(): result percentage too high! (A=%u,B=%u,result=%g)\n", A, B, result);
  234. return UINT_MAX;
  235. }
  236. return (unsigned int)floor(result);
  237. }