win32.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Win32 support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #if (defined __STRICT_ANSI__)
  9. #define __PHYSFS_DOING_STRICT_ANSI__
  10. #endif
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #define __PHYSICSFS_INTERNAL__
  14. #include "physfs_internal.h"
  15. const char *__PHYSFS_platformDirSeparator = "\\";
  16. static const char *win32strerror(void)
  17. {
  18. static TCHAR msgbuf[255];
  19. FormatMessage(
  20. FORMAT_MESSAGE_FROM_SYSTEM |
  21. FORMAT_MESSAGE_IGNORE_INSERTS,
  22. NULL,
  23. GetLastError(),
  24. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  25. msgbuf,
  26. sizeof (msgbuf) / sizeof (TCHAR),
  27. NULL
  28. );
  29. return((const char *) msgbuf);
  30. } /* win32strerror */
  31. char **__PHYSFS_platformDetectAvailableCDs(void)
  32. {
  33. char **retval = (char **) malloc(sizeof (char *));
  34. int cd_count = 1; /* We count the NULL entry. */
  35. char drive_str[4] = "x:\\";
  36. for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
  37. {
  38. if (GetDriveType(drive_str) == DRIVE_CDROM)
  39. {
  40. char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
  41. if (tmp)
  42. {
  43. retval = tmp;
  44. retval[cd_count-1] = (char *) malloc(4);
  45. if (retval[cd_count-1])
  46. {
  47. strcpy(retval[cd_count-1], drive_str);
  48. cd_count++;
  49. } /* if */
  50. } /* if */
  51. } /* if */
  52. } /* for */
  53. retval[cd_count - 1] = NULL;
  54. return(retval);
  55. } /* __PHYSFS_detectAvailableCDs */
  56. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  57. {
  58. DWORD buflen = 0;
  59. char *retval = NULL;
  60. char *filepart = NULL;
  61. if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
  62. return(NULL);
  63. buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
  64. retval = (char *) malloc(buflen);
  65. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  66. SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
  67. return(retval);
  68. } /* __PHYSFS_platformCalcBaseDir */
  69. char *__PHYSFS_platformGetUserName(void)
  70. {
  71. DWORD bufsize = 0;
  72. LPTSTR retval = NULL;
  73. if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
  74. {
  75. retval = (LPTSTR) malloc(bufsize);
  76. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  77. if (GetUserName(retval, &bufsize) == 0) /* ?! */
  78. {
  79. free(retval);
  80. retval = NULL;
  81. } /* if */
  82. } /* if */
  83. return((char *) retval);
  84. } /* __PHYSFS_platformGetUserName */
  85. char *__PHYSFS_platformGetUserDir(void)
  86. {
  87. return(NULL); /* no user dir on win32. */
  88. } /* __PHYSFS_platformGetUserDir */
  89. int __PHYSFS_platformGetThreadID(void)
  90. {
  91. return((int) GetCurrentThreadId());
  92. } /* __PHYSFS_platformGetThreadID */
  93. /* ...make this Cygwin AND Visual C friendly... */
  94. int __PHYSFS_platformStricmp(const char *x, const char *y)
  95. {
  96. int ux, uy;
  97. do
  98. {
  99. ux = toupper((int) *x);
  100. uy = toupper((int) *y);
  101. if (ux > uy)
  102. return(1);
  103. else if (ux < uy)
  104. return(-1);
  105. x++;
  106. y++;
  107. } while ((ux) && (uy));
  108. return(0);
  109. } /* __PHYSFS_platformStricmp */
  110. int __PHYSFS_platformExists(const char *fname)
  111. {
  112. return(GetFileAttributes(fname) != 0xffffffff);
  113. } /* __PHYSFS_platformExists */
  114. int __PHYSFS_platformIsSymLink(const char *fname)
  115. {
  116. return(0); /* no symlinks on win32. */
  117. } /* __PHYSFS_platformIsSymlink */
  118. int __PHYSFS_platformIsDirectory(const char *fname)
  119. {
  120. return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  121. } /* __PHYSFS_platformIsDirectory */
  122. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  123. const char *dirName,
  124. const char *append)
  125. {
  126. int len = ((prepend) ? strlen(prepend) : 0) +
  127. ((append) ? strlen(append) : 0) +
  128. strlen(dirName) + 1;
  129. char *retval = malloc(len);
  130. char *p;
  131. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  132. if (prepend)
  133. strcpy(retval, prepend);
  134. else
  135. retval[0] = '\0';
  136. strcat(retval, dirName);
  137. if (append)
  138. strcat(retval, append);
  139. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  140. *p = '\\';
  141. return(retval);
  142. } /* __PHYSFS_platformCvtToDependent */
  143. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  144. void __PHYSFS_platformTimeslice(void)
  145. {
  146. Sleep(10);
  147. } /* __PHYSFS_platformTimeslice */
  148. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  149. int omitSymLinks)
  150. {
  151. LinkedStringList *retval = NULL;
  152. LinkedStringList *l = NULL;
  153. LinkedStringList *prev = NULL;
  154. HANDLE dir;
  155. WIN32_FIND_DATA ent;
  156. dir = FindFirstFile(dirname, &ent);
  157. BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  158. for (; FindNextFile(dir, &ent) != 0; )
  159. {
  160. if (strcmp(ent.cFileName, ".") == 0)
  161. continue;
  162. if (strcmp(ent.cFileName, "..") == 0)
  163. continue;
  164. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  165. if (l == NULL)
  166. break;
  167. l->str = (char *) malloc(strlen(ent.cFileName) + 1);
  168. if (l->str == NULL)
  169. {
  170. free(l);
  171. break;
  172. } /* if */
  173. strcpy(l->str, ent.cFileName);
  174. if (retval == NULL)
  175. retval = l;
  176. else
  177. prev->next = l;
  178. prev = l;
  179. l->next = NULL;
  180. } /* while */
  181. FindClose(dir);
  182. return(retval);
  183. } /* __PHYSFS_platformEnumerateFiles */
  184. int __PHYSFS_platformFileLength(FILE *handle)
  185. {
  186. fpos_t curpos;
  187. int retval;
  188. fgetpos(handle, &curpos);
  189. fseek(handle, 0, SEEK_END);
  190. retval = ftell(handle);
  191. fsetpos(handle, &curpos);
  192. return(retval);
  193. } /* __PHYSFS_platformFileLength */
  194. char *__PHYSFS_platformCurrentDir(void)
  195. {
  196. LPTSTR retval;
  197. DWORD buflen = 0;
  198. buflen = GetCurrentDirectory(buflen, NULL);
  199. retval = (LPTSTR) malloc(buflen);
  200. GetCurrentDirectory(buflen, retval);
  201. return((char *) retval);
  202. } /* __PHYSFS_platformCurrentDir */
  203. char *__PHYSFS_platformRealPath(const char *path)
  204. {
  205. /* !!! FIXME: This isn't supported on CygWin! */
  206. return(_fullpath(NULL, path, _MAX_PATH));
  207. } /* __PHYSFS_platformRealPath */
  208. int __PHYSFS_platformMkDir(const char *path)
  209. {
  210. DWORD rc = CreateDirectory(path, NULL);
  211. BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
  212. return(1);
  213. } /* __PHYSFS_platformMkDir */
  214. /* end of win32.c ... */