utils.c 5.5 KB

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