Richard Levitte | bb09fd2 | 2004-09-23 22:11:39 +0000 | [diff] [blame] | 1 | /* $LP: LPlib/source/LPdir_win.c,v 1.10 2004/08/26 13:36:05 _cvs_levitte Exp $ */ |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 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. |
Richard Levitte | bb09fd2 | 2004-09-23 22:11:39 +0000 | [diff] [blame] | 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. |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 26 | */ |
| 27 | #include <windows.h> |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 28 | #include <tchar.h> |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 29 | #ifndef LPDIR_H |
| 30 | #include "LPdir.h" |
| 31 | #endif |
| 32 | |
Richard Levitte | 75f134c | 2004-07-22 13:00:14 +0000 | [diff] [blame] | 33 | /* We're most likely overcautious here, but let's reserve for |
| 34 | broken WinCE headers and explicitly opt for UNICODE call. |
| 35 | Keep in mind that our WinCE builds are compiled with -DUNICODE |
| 36 | [as well as -D_UNICODE]. */ |
Richard Levitte | 765e231 | 2004-07-21 21:16:21 +0000 | [diff] [blame] | 37 | #if defined(LP_SYS_WINCE) && !defined(FindFirstFile) |
| 38 | # define FindFirstFile FindFirstFileW |
| 39 | #endif |
Richard Levitte | 75f134c | 2004-07-22 13:00:14 +0000 | [diff] [blame] | 40 | #if defined(LP_SYS_WINCE) && !defined(FindFirstFile) |
| 41 | # define FindNextFile FindNextFileW |
| 42 | #endif |
Richard Levitte | 765e231 | 2004-07-21 21:16:21 +0000 | [diff] [blame] | 43 | |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 44 | #ifndef NAME_MAX |
| 45 | #define NAME_MAX 255 |
| 46 | #endif |
| 47 | |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 48 | struct LP_dir_context_st |
| 49 | { |
| 50 | WIN32_FIND_DATA ctx; |
| 51 | HANDLE handle; |
| 52 | char entry_name[NAME_MAX+1]; |
| 53 | }; |
| 54 | |
| 55 | const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory) |
| 56 | { |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 57 | if (ctx == NULL || directory == NULL) |
| 58 | { |
| 59 | errno = EINVAL; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | errno = 0; |
| 64 | if (*ctx == NULL) |
| 65 | { |
| 66 | *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX)); |
| 67 | if (*ctx == NULL) |
| 68 | { |
| 69 | errno = ENOMEM; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | memset(*ctx, '\0', sizeof(LP_DIR_CTX)); |
| 73 | |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 74 | if (sizeof(TCHAR) != sizeof(char)) |
| 75 | { |
| 76 | TCHAR *wdir = NULL; |
Richard Levitte | f744f92 | 2004-07-22 18:34:06 +0000 | [diff] [blame] | 77 | /* len_0 denotes string length *with* trailing 0 */ |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 78 | size_t index = 0,len_0 = strlen(directory) + 1; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 79 | |
Richard Levitte | f744f92 | 2004-07-22 18:34:06 +0000 | [diff] [blame] | 80 | wdir = (TCHAR *)malloc(len_0 * sizeof(TCHAR)); |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 81 | if (wdir == NULL) |
| 82 | { |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 83 | free(*ctx); |
Richard Levitte | 765e231 | 2004-07-21 21:16:21 +0000 | [diff] [blame] | 84 | *ctx = NULL; |
| 85 | errno = ENOMEM; |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | } |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 88 | |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 89 | #ifdef LP_MULTIBYTE_AVAILABLE |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 90 | if (!MultiByteToWideChar(CP_ACP, 0, directory, len_0, (WCHAR *)wdir, len_0)) |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 91 | #endif |
Richard Levitte | f744f92 | 2004-07-22 18:34:06 +0000 | [diff] [blame] | 92 | for (index = 0; index < len_0; index++) |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 93 | wdir[index] = (TCHAR)directory[index]; |
| 94 | |
| 95 | (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx); |
| 96 | |
| 97 | free(wdir); |
| 98 | } |
| 99 | else |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 100 | (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx); |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 101 | |
| 102 | if ((*ctx)->handle == INVALID_HANDLE_VALUE) |
| 103 | { |
| 104 | free(*ctx); |
| 105 | *ctx = NULL; |
| 106 | errno = EINVAL; |
| 107 | return 0; |
| 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 112 | if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 113 | { |
| 114 | return 0; |
| 115 | } |
| 116 | } |
| 117 | |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 118 | if (sizeof(TCHAR) != sizeof(char)) |
| 119 | { |
Richard Levitte | 765e231 | 2004-07-21 21:16:21 +0000 | [diff] [blame] | 120 | TCHAR *wdir = (*ctx)->ctx.cFileName; |
Richard Levitte | f744f92 | 2004-07-22 18:34:06 +0000 | [diff] [blame] | 121 | size_t index, len_0 = 0; |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 122 | |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 123 | while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1)) len_0++; |
Richard Levitte | f744f92 | 2004-07-22 18:34:06 +0000 | [diff] [blame] | 124 | len_0++; |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 125 | |
| 126 | #ifdef LP_MULTIBYTE_AVAILABLE |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 127 | if (!WideCharToMultiByte(CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name, |
Richard Levitte | f744f92 | 2004-07-22 18:34:06 +0000 | [diff] [blame] | 128 | sizeof((*ctx)->entry_name), NULL, 0)) |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 129 | #endif |
Richard Levitte | f744f92 | 2004-07-22 18:34:06 +0000 | [diff] [blame] | 130 | for (index = 0; index < len_0; index++) |
Richard Levitte | 765e231 | 2004-07-21 21:16:21 +0000 | [diff] [blame] | 131 | (*ctx)->entry_name[index] = (char)wdir[index]; |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 132 | } |
| 133 | else |
Andy Polyakov | da2ee71 | 2004-07-25 16:48:28 +0000 | [diff] [blame] | 134 | strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName, |
Richard Levitte | 64ba6cf | 2004-07-20 21:24:43 +0000 | [diff] [blame] | 135 | sizeof((*ctx)->entry_name)-1); |
| 136 | |
| 137 | (*ctx)->entry_name[sizeof((*ctx)->entry_name)-1] = '\0'; |
| 138 | |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 139 | return (*ctx)->entry_name; |
| 140 | } |
| 141 | |
| 142 | int LP_find_file_end(LP_DIR_CTX **ctx) |
| 143 | { |
| 144 | if (ctx != NULL && *ctx != NULL) |
| 145 | { |
| 146 | FindClose((*ctx)->handle); |
| 147 | free(*ctx); |
Richard Levitte | 765e231 | 2004-07-21 21:16:21 +0000 | [diff] [blame] | 148 | *ctx = NULL; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 149 | return 1; |
| 150 | } |
| 151 | errno = EINVAL; |
| 152 | return 0; |
| 153 | } |