]> icculus.org git repositories - btb/d2x.git/blob - arch/win32/findfile.c
remove needless sleep on linux hmiplay startup (d1x r1.6)
[btb/d2x.git] / arch / win32 / findfile.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #define WIN32_LEAN_AND_MEAN
19 #include <windows.h>
20 #include "findfile.h"
21
22
23 //      Global Variables        ----------------------------------------------------------
24
25 static HANDLE   _FindFileHandle = INVALID_HANDLE_VALUE;
26
27 #ifdef _WIN32_WCE
28 static char *UnicodeToAsc(const wchar_t *w_str)
29 {
30         char *str = NULL;
31
32         if (w_str != NULL)
33     {
34                 int len = wcslen(w_str) + 1;
35                 str = (char *)malloc(len);
36
37                 if (WideCharToMultiByte(CP_ACP, 0, w_str, -1, str, len, NULL, NULL) == 0)
38                 {   //Conversion failed
39                         free(str);
40                         return NULL;
41                 }
42                 else
43                 {   //Conversion successful
44                         return(str);
45                 }
46         }
47         else
48         {   //Given NULL string
49                 return NULL;
50         }
51 }
52
53 static wchar_t *AscToUnicode(const char *str)
54 {
55         wchar_t *w_str = NULL;
56         if (str != NULL)
57         {
58                 int len = strlen(str) + 1;
59                 w_str = (wchar_t *)malloc(sizeof(wchar_t) * len);
60                 if (MultiByteToWideChar(CP_ACP, 0, str, -1, w_str, len) == 0)
61                 {
62                         free(w_str);
63                         return NULL;
64                 }
65                 else
66                 {
67                         return(w_str);
68                 }
69         }
70         else
71         {
72                 return NULL;
73         }
74 }
75 #else
76 # define UnicodeToAsc(x) (x)
77 # define AscToUnicode(x) ((LPSTR)x)
78 #endif
79
80 //      Functions
81
82 int     FileFindFirst(char *search_str, FILEFINDSTRUCT *ffstruct)
83 {
84         WIN32_FIND_DATA find;
85
86         _FindFileHandle = FindFirstFile(AscToUnicode(search_str), &find);
87         if (_FindFileHandle == INVALID_HANDLE_VALUE) return 1;
88         else {
89                 ffstruct->size = find.nFileSizeLow;
90                 strcpy(ffstruct->name, UnicodeToAsc(find.cFileName));
91                 return 0; 
92         }
93 }
94
95
96 int     FileFindNext(FILEFINDSTRUCT *ffstruct)
97 {
98         WIN32_FIND_DATA find;
99
100         if (!FindNextFile(_FindFileHandle, &find)) return 1;
101         else {
102                 ffstruct->size = find.nFileSizeLow;
103                 strcpy(ffstruct->name, UnicodeToAsc(find.cFileName));
104                 return 0;
105         }
106 }
107  
108
109 int     FileFindClose(void)
110 {
111         if (!FindClose(_FindFileHandle)) return 1;
112         else return 0;
113 }
114
115
116 #if 0
117 int GetFileDateTime(int filehandle, FILETIMESTRUCT *ftstruct)
118 {
119         FILETIME filetime;
120         int retval;
121
122         retval = GetFileTime((HANDLE)filehandle, NULL, NULL, &filetime);
123         if (retval) {
124                 FileTimeToDosDateTime(&filetime, &ftstruct->date, &ftstruct->time);
125                 return 0;
126         }
127         else return 1;
128 }
129
130
131 //returns 0 if no error
132 int SetFileDateTime(int filehandle, FILETIMESTRUCT *ftstruct)
133 {
134         FILETIME ft;
135         int retval;
136
137         DosDateTimeToFileTime(ftstruct->date, ftstruct->time, &ft);
138         retval = SetFileTime((HANDLE)filehandle, NULL, NULL, &ft);      
139         if (retval) return 0;
140         else return 1;
141 }
142 #endif