2
0

LPdir_win.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* $LP: LPlib/source/LPdir_win.c,v 1.10 2004/08/26 13:36:05 _cvs_levitte Exp $ */
  2. /*
  3. * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <windows.h>
  28. #include <tchar.h>
  29. #ifndef LPDIR_H
  30. #include "LPdir.h"
  31. #endif
  32. /* We're most likely overcautious here, but let's reserve for
  33. broken WinCE headers and explicitly opt for UNICODE call.
  34. Keep in mind that our WinCE builds are compiled with -DUNICODE
  35. [as well as -D_UNICODE]. */
  36. #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
  37. # define FindFirstFile FindFirstFileW
  38. #endif
  39. #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
  40. # define FindNextFile FindNextFileW
  41. #endif
  42. #ifndef NAME_MAX
  43. #define NAME_MAX 255
  44. #endif
  45. struct LP_dir_context_st
  46. {
  47. WIN32_FIND_DATA ctx;
  48. HANDLE handle;
  49. char entry_name[NAME_MAX+1];
  50. };
  51. const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
  52. {
  53. if (ctx == NULL || directory == NULL)
  54. {
  55. errno = EINVAL;
  56. return 0;
  57. }
  58. errno = 0;
  59. if (*ctx == NULL)
  60. {
  61. *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
  62. if (*ctx == NULL)
  63. {
  64. errno = ENOMEM;
  65. return 0;
  66. }
  67. memset(*ctx, '\0', sizeof(LP_DIR_CTX));
  68. if (sizeof(TCHAR) != sizeof(char))
  69. {
  70. TCHAR *wdir = NULL;
  71. /* len_0 denotes string length *with* trailing 0 */
  72. size_t index = 0,len_0 = strlen(directory) + 1;
  73. wdir = (TCHAR *)malloc(len_0 * sizeof(TCHAR));
  74. if (wdir == NULL)
  75. {
  76. free(*ctx);
  77. *ctx = NULL;
  78. errno = ENOMEM;
  79. return 0;
  80. }
  81. #ifdef LP_MULTIBYTE_AVAILABLE
  82. if (!MultiByteToWideChar(CP_ACP, 0, directory, len_0, (WCHAR *)wdir, len_0))
  83. #endif
  84. for (index = 0; index < len_0; index++)
  85. wdir[index] = (TCHAR)directory[index];
  86. (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
  87. free(wdir);
  88. }
  89. else
  90. (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
  91. if ((*ctx)->handle == INVALID_HANDLE_VALUE)
  92. {
  93. free(*ctx);
  94. *ctx = NULL;
  95. errno = EINVAL;
  96. return 0;
  97. }
  98. }
  99. else
  100. {
  101. if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE)
  102. {
  103. return 0;
  104. }
  105. }
  106. if (sizeof(TCHAR) != sizeof(char))
  107. {
  108. TCHAR *wdir = (*ctx)->ctx.cFileName;
  109. size_t index, len_0 = 0;
  110. while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1)) len_0++;
  111. len_0++;
  112. #ifdef LP_MULTIBYTE_AVAILABLE
  113. if (!WideCharToMultiByte(CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
  114. sizeof((*ctx)->entry_name), NULL, 0))
  115. #endif
  116. for (index = 0; index < len_0; index++)
  117. (*ctx)->entry_name[index] = (char)wdir[index];
  118. }
  119. else
  120. strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
  121. sizeof((*ctx)->entry_name)-1);
  122. (*ctx)->entry_name[sizeof((*ctx)->entry_name)-1] = '\0';
  123. return (*ctx)->entry_name;
  124. }
  125. int LP_find_file_end(LP_DIR_CTX **ctx)
  126. {
  127. if (ctx != NULL && *ctx != NULL)
  128. {
  129. FindClose((*ctx)->handle);
  130. free(*ctx);
  131. *ctx = NULL;
  132. return 1;
  133. }
  134. errno = EINVAL;
  135. return 0;
  136. }