comparison common/portpath.c @ 1157:fd7d04bb37cb

(issue36) Add encoding aware port_fopen function and use it
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 18 Sep 2014 15:43:22 +0200
parents f110a3f6e387
children 0a803c3fb5a6
comparison
equal deleted inserted replaced
1156:e986d3d4705f 1157:fd7d04bb37cb
16 #include <stdlib.h> 16 #include <stdlib.h>
17 #include <sys/stat.h> 17 #include <sys/stat.h>
18 #include <sys/types.h> 18 #include <sys/types.h>
19 #include <unistd.h> 19 #include <unistd.h>
20 #include <string.h> 20 #include <string.h>
21
22 #ifdef WIN32
23 #include <share.h>
24 #endif
21 25
22 char * 26 char *
23 port_dirname(char *path) 27 port_dirname(char *path)
24 { 28 {
25 #ifndef _WIN32 29 #ifndef _WIN32
138 if ((ret == 0) && S_ISDIR(sb.st_mode)) 142 if ((ret == 0) && S_ISDIR(sb.st_mode))
139 return true; 143 return true;
140 else 144 else
141 return false; 145 return false;
142 } 146 }
147
148 FILE*
149 port_fopen_rb(const char *path, bool exclusive)
150 {
151 FILE *f = NULL;
152 if (!path)
153 {
154 return NULL;
155 }
156 #ifdef WIN32
157 {
158 wchar_t *wFilename = utf8_to_wchar(path, strlen(path));
159 if (!wFilename)
160 {
161 ERRORPRINTF ("Invalid encoding\n");
162 return NULL;
163 }
164 /* We open and write protect the file here so that
165 as long as the file is open we can be sure that
166 it was not modified and can use it in subsequent
167 calls based on the filename. */
168 OutputDebugStringW(wFilename);
169 if (exclusive)
170 {
171 f = _wfsopen(wFilename, L"rb", _SH_DENYWR);
172 }
173 else
174 {
175 f = _wfopen(wFilename, L"rb");
176 }
177 xfree(wFilename);
178 if (f == NULL)
179 {
180 /* Fall back to local8 bit encoding */
181 if (exclusive)
182 {
183 f = _fsopen(path, "rb", _SH_DENYWR);
184 }
185 else
186 {
187 f = fopen(path, "rb");
188 }
189 }
190 }
191 #else
192 (void)(exclusive);
193 f = fopen(path, "rb");
194 #endif
195 return f;
196 }

http://wald.intevation.org/projects/trustbridge/