/xbmc/visualizations/XBMCProjectM/libprojectM/win32-dirent.cpp

http://github.com/xbmc/xbmc · C++ · 231 lines · 163 code · 35 blank · 33 comment · 37 complexity · 68bc19ea194fc50c024c06f509d7a9d6 MD5 · raw file

  1. /*
  2. Implementation of POSIX directory browsing functions and types for Win32.
  3. Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
  4. History: Created March 1997. Updated June 2003.
  5. Rights: See end of file.
  6. */
  7. #include "win32-dirent.h"
  8. #include <errno.h>
  9. #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #ifdef __cplusplus
  13. extern "C"
  14. {
  15. #endif
  16. struct DIR
  17. {
  18. long handle; /* -1 for failed rewind */
  19. struct _finddata_t info;
  20. struct dirent result; /* d_name null iff first time */
  21. char *name; /* null-terminated char string */
  22. };
  23. DIR *opendir(const char *name)
  24. {
  25. DIR *dir = 0;
  26. if(name && name[0])
  27. {
  28. size_t base_length = strlen(name);
  29. const char *all = /* search pattern must end with suitable wildcard */
  30. strchr("/\\", name[base_length - 1]) ? "" : "/";
  31. if((dir = (DIR *) malloc(sizeof *dir)) != 0 &&
  32. (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0)
  33. {
  34. strcat(strcpy(dir->name, name), all);
  35. if((dir->handle = (long) _findfirst(dir->name, &dir->info)) != -1)
  36. {
  37. dir->result.d_name = 0;
  38. }
  39. else /* rollback */
  40. {
  41. free(dir->name);
  42. free(dir);
  43. dir = 0;
  44. }
  45. }
  46. else /* rollback */
  47. {
  48. free(dir);
  49. dir = 0;
  50. errno = ENOMEM;
  51. }
  52. }
  53. else
  54. {
  55. errno = EINVAL;
  56. }
  57. return dir;
  58. }
  59. int closedir(DIR *dir)
  60. {
  61. int result = -1;
  62. if(dir)
  63. {
  64. if(dir->handle != -1)
  65. {
  66. result = _findclose(dir->handle);
  67. }
  68. free(dir->name);
  69. free(dir);
  70. }
  71. if(result == -1) /* map all errors to EBADF */
  72. {
  73. errno = EBADF;
  74. }
  75. return result;
  76. }
  77. struct dirent *readdir(DIR *dir)
  78. {
  79. struct dirent *result = 0;
  80. if(dir && dir->handle != -1)
  81. {
  82. if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
  83. {
  84. result = &dir->result;
  85. result->d_name = dir->info.name;
  86. }
  87. }
  88. else
  89. {
  90. errno = EBADF;
  91. }
  92. return result;
  93. }
  94. void rewinddir(DIR *dir)
  95. {
  96. if(dir && dir->handle != -1)
  97. {
  98. _findclose(dir->handle);
  99. dir->handle = (long) _findfirst(dir->name, &dir->info);
  100. dir->result.d_name = 0;
  101. }
  102. else
  103. {
  104. errno = EBADF;
  105. }
  106. }
  107. // helper for scandir below
  108. static void scandir_free_dir_entries(struct dirent*** namelist, int entries) {
  109. int i;
  110. if (!*namelist) return;
  111. for (i = 0; i < entries; ++i) {
  112. free((*namelist)[i]);
  113. }
  114. free(*namelist);
  115. *namelist = 0;
  116. }
  117. // returns the number of directory entries select or -1 if an error occurs
  118. int scandir(
  119. const char* dir,
  120. struct dirent*** namelist,
  121. int(*filter)(const struct dirent*),
  122. int(*compar)(const void*, const void*)
  123. ) {
  124. int entries = 0;
  125. int max_entries = 1024; // assume 2*512 = 1024 entries (used for allocation)
  126. DIR* d;
  127. *namelist = 0;
  128. // open directory
  129. d = opendir(dir);
  130. if (!d) return -1;
  131. // iterate
  132. while (1) {
  133. struct dirent* ent = readdir(d);
  134. if (!ent) break;
  135. // add if no filter or filter returns non-zero
  136. if (filter && (0 == filter(ent))) continue;
  137. // resize our buffer if there is not enough room
  138. if (!*namelist || entries >= max_entries) {
  139. struct dirent** new_entries;
  140. max_entries *= 2;
  141. new_entries = (struct dirent **)realloc(*namelist, max_entries);
  142. if (!new_entries) {
  143. scandir_free_dir_entries(namelist, entries);
  144. closedir(d);
  145. errno = ENOMEM;
  146. return -1;
  147. }
  148. *namelist = new_entries;
  149. }
  150. // allocate new entry
  151. (*namelist)[entries] = (struct dirent *)malloc(sizeof(struct dirent) + strlen(ent->d_name) + 1);
  152. if (!(*namelist)[entries]) {
  153. scandir_free_dir_entries(namelist, entries);
  154. closedir(d);
  155. errno = ENOMEM;
  156. return -1;
  157. }
  158. // copy entry info
  159. *(*namelist)[entries] = *ent;
  160. // and then we tack the string onto the end
  161. {
  162. char* dest = (char*)((*namelist)[entries]) + sizeof(struct dirent);
  163. strcpy(dest, ent->d_name);
  164. (*namelist)[entries]->d_name = dest;
  165. }
  166. ++entries;
  167. }
  168. // sort
  169. if (*namelist && compar) qsort(*namelist, entries, sizeof((*namelist)[0]), compar);
  170. return entries;
  171. }
  172. int alphasort(const void* lhs, const void* rhs) {
  173. const struct dirent* lhs_ent = *(struct dirent**)lhs;
  174. const struct dirent* rhs_ent = *(struct dirent**)rhs;
  175. return _strcmpi(lhs_ent->d_name, rhs_ent->d_name);
  176. }
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. /*
  181. Copyright Kevlin Henney, 1997, 2003. All rights reserved.
  182. Permission to use, copy, modify, and distribute this software and its
  183. documentation for any purpose is hereby granted without fee, provided
  184. that this copyright and permissions notice appear in all copies and
  185. derivatives.
  186. This software is supplied "as is" without express or implied warranty.
  187. But that said, if there are any problems please get in touch.
  188. */