1 /* $LP: LPlib/source/LPdir_win.c,v 1.10 2004/08/26 13:36:05 _cvs_levitte Exp $ */
3 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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]. */
37 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
38 # define FindFirstFile FindFirstFileW
40 #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
41 # define FindNextFile FindNextFileW
48 struct LP_dir_context_st
52 char entry_name
[NAME_MAX
+1];
55 const char *LP_find_file(LP_DIR_CTX
**ctx
, const char *directory
)
57 if (ctx
== NULL
|| directory
== NULL
)
66 *ctx
= (LP_DIR_CTX
*)malloc(sizeof(LP_DIR_CTX
));
72 memset(*ctx
, '\0', sizeof(LP_DIR_CTX
));
74 if (sizeof(TCHAR
) != sizeof(char))
77 /* len_0 denotes string length *with* trailing 0 */
78 size_t index
= 0,len_0
= strlen(directory
) + 1;
80 wdir
= (TCHAR
*)malloc(len_0
* sizeof(TCHAR
));
89 #ifdef LP_MULTIBYTE_AVAILABLE
90 if (!MultiByteToWideChar(CP_ACP
, 0, directory
, len_0
, (WCHAR
*)wdir
, len_0
))
92 for (index
= 0; index
< len_0
; index
++)
93 wdir
[index
] = (TCHAR
)directory
[index
];
95 (*ctx
)->handle
= FindFirstFile(wdir
, &(*ctx
)->ctx
);
100 (*ctx
)->handle
= FindFirstFile((TCHAR
*)directory
, &(*ctx
)->ctx
);
102 if ((*ctx
)->handle
== INVALID_HANDLE_VALUE
)
112 if (FindNextFile((*ctx
)->handle
, &(*ctx
)->ctx
) == FALSE
)
118 if (sizeof(TCHAR
) != sizeof(char))
120 TCHAR
*wdir
= (*ctx
)->ctx
.cFileName
;
121 size_t index
, len_0
= 0;
123 while (wdir
[len_0
] && len_0
< (sizeof((*ctx
)->entry_name
) - 1)) len_0
++;
126 #ifdef LP_MULTIBYTE_AVAILABLE
127 if (!WideCharToMultiByte(CP_ACP
, 0, (WCHAR
*)wdir
, len_0
, (*ctx
)->entry_name
,
128 sizeof((*ctx
)->entry_name
), NULL
, 0))
130 for (index
= 0; index
< len_0
; index
++)
131 (*ctx
)->entry_name
[index
] = (char)wdir
[index
];
134 strncpy((*ctx
)->entry_name
, (const char *)(*ctx
)->ctx
.cFileName
,
135 sizeof((*ctx
)->entry_name
)-1);
137 (*ctx
)->entry_name
[sizeof((*ctx
)->entry_name
)-1] = '\0';
139 return (*ctx
)->entry_name
;
142 int LP_find_file_end(LP_DIR_CTX
**ctx
)
144 if (ctx
!= NULL
&& *ctx
!= NULL
)
146 FindClose((*ctx
)->handle
);