Fossil SCM

Refactor the previous commit by adding a wrapper for `freopen()', and use the Cygwin-aware routines to convert path names to/from UTF-16.

florian 2021-08-31 10:39 win32-temppath-mojibake
Commit 963de841f2e2dfd54d977f745eba44c4df9d6b9f1c3b50740d2f7714ed10b6e1
3 files changed +2 -11 +16 +2 -2
+2 -11
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -280,22 +280,13 @@
280280
**
281281
** For --browser and --webpage, output the HTML header.
282282
*/
283283
void diff_begin(u64 diffFlags){
284284
if( (diffFlags & DIFF_BROWSER)!=0 ){
285
-#ifdef _WIN32
286
- LPWSTR tempDiffFilenameW;
287
-#endif
288285
tempDiffFilename = fossil_temp_filename();
289286
tempDiffFilename = sqlite3_mprintf("%z.html", tempDiffFilename);
290
-#ifndef _WIN32
291
- diffOut = freopen(tempDiffFilename,"wb",stdout);
292
-#else
293
- tempDiffFilenameW = fossil_utf8_to_unicode(tempDiffFilename);
294
- diffOut = _wfreopen(tempDiffFilenameW,L"wb",stdout);
295
- fossil_unicode_free(tempDiffFilenameW);
296
-#endif
287
+ diffOut = fossil_freopen(tempDiffFilename,"wb",stdout);
297288
if( diffOut==0 ){
298289
fossil_fatal("unable to create temporary file \"%s\"",
299290
tempDiffFilename);
300291
}
301292
#ifndef _WIN32
@@ -335,11 +326,11 @@
335326
fossil_print("%s", zWebpageEnd);
336327
}
337328
if( (diffFlags & DIFF_BROWSER)!=0 && nErr==0 ){
338329
char *zCmd = mprintf("%s %$", fossil_web_browser(), tempDiffFilename);
339330
fclose(diffOut);
340
- diffOut = freopen(NULL_DEVICE, "wb", stdout);
331
+ diffOut = fossil_freopen(NULL_DEVICE, "wb", stdout);
341332
fossil_system(zCmd);
342333
fossil_free(zCmd);
343334
diffOut = 0;
344335
sqlite3_sleep(FOSSIL_BROWSER_DIFF_DELAY);
345336
file_delete(tempDiffFilename);
346337
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -280,22 +280,13 @@
280 **
281 ** For --browser and --webpage, output the HTML header.
282 */
283 void diff_begin(u64 diffFlags){
284 if( (diffFlags & DIFF_BROWSER)!=0 ){
285 #ifdef _WIN32
286 LPWSTR tempDiffFilenameW;
287 #endif
288 tempDiffFilename = fossil_temp_filename();
289 tempDiffFilename = sqlite3_mprintf("%z.html", tempDiffFilename);
290 #ifndef _WIN32
291 diffOut = freopen(tempDiffFilename,"wb",stdout);
292 #else
293 tempDiffFilenameW = fossil_utf8_to_unicode(tempDiffFilename);
294 diffOut = _wfreopen(tempDiffFilenameW,L"wb",stdout);
295 fossil_unicode_free(tempDiffFilenameW);
296 #endif
297 if( diffOut==0 ){
298 fossil_fatal("unable to create temporary file \"%s\"",
299 tempDiffFilename);
300 }
301 #ifndef _WIN32
@@ -335,11 +326,11 @@
335 fossil_print("%s", zWebpageEnd);
336 }
337 if( (diffFlags & DIFF_BROWSER)!=0 && nErr==0 ){
338 char *zCmd = mprintf("%s %$", fossil_web_browser(), tempDiffFilename);
339 fclose(diffOut);
340 diffOut = freopen(NULL_DEVICE, "wb", stdout);
341 fossil_system(zCmd);
342 fossil_free(zCmd);
343 diffOut = 0;
344 sqlite3_sleep(FOSSIL_BROWSER_DIFF_DELAY);
345 file_delete(tempDiffFilename);
346
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -280,22 +280,13 @@
280 **
281 ** For --browser and --webpage, output the HTML header.
282 */
283 void diff_begin(u64 diffFlags){
284 if( (diffFlags & DIFF_BROWSER)!=0 ){
 
 
 
285 tempDiffFilename = fossil_temp_filename();
286 tempDiffFilename = sqlite3_mprintf("%z.html", tempDiffFilename);
287 diffOut = fossil_freopen(tempDiffFilename,"wb",stdout);
 
 
 
 
 
 
288 if( diffOut==0 ){
289 fossil_fatal("unable to create temporary file \"%s\"",
290 tempDiffFilename);
291 }
292 #ifndef _WIN32
@@ -335,11 +326,11 @@
326 fossil_print("%s", zWebpageEnd);
327 }
328 if( (diffFlags & DIFF_BROWSER)!=0 && nErr==0 ){
329 char *zCmd = mprintf("%s %$", fossil_web_browser(), tempDiffFilename);
330 fclose(diffOut);
331 diffOut = fossil_freopen(NULL_DEVICE, "wb", stdout);
332 fossil_system(zCmd);
333 fossil_free(zCmd);
334 diffOut = 0;
335 sqlite3_sleep(FOSSIL_BROWSER_DIFF_DELAY);
336 file_delete(tempDiffFilename);
337
+16
--- src/file.c
+++ src/file.c
@@ -2160,10 +2160,26 @@
21602160
#else
21612161
FILE *f = fopen(zName, zMode);
21622162
#endif
21632163
return f;
21642164
}
2165
+
2166
+/*
2167
+** Wrapper for freopen() that understands UTF8 arguments.
2168
+*/
2169
+FILE *fossil_freopen(const char *zName, const char *zMode, FILE *stream){
2170
+#ifdef _WIN32
2171
+ wchar_t *uMode = fossil_utf8_to_unicode(zMode);
2172
+ wchar_t *uName = fossil_utf8_to_path(zName, 0);
2173
+ FILE *f = _wfreopen(uName, uMode, stream);
2174
+ fossil_path_free(uName);
2175
+ fossil_unicode_free(uMode);
2176
+#else
2177
+ FILE *f = freopen(zName, zMode, stream);
2178
+#endif
2179
+ return f;
2180
+}
21652181
21662182
/*
21672183
** Works like fclose() except that:
21682184
**
21692185
** 1) is a no-op if f is 0 or if it is stdin.
21702186
--- src/file.c
+++ src/file.c
@@ -2160,10 +2160,26 @@
2160 #else
2161 FILE *f = fopen(zName, zMode);
2162 #endif
2163 return f;
2164 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2165
2166 /*
2167 ** Works like fclose() except that:
2168 **
2169 ** 1) is a no-op if f is 0 or if it is stdin.
2170
--- src/file.c
+++ src/file.c
@@ -2160,10 +2160,26 @@
2160 #else
2161 FILE *f = fopen(zName, zMode);
2162 #endif
2163 return f;
2164 }
2165
2166 /*
2167 ** Wrapper for freopen() that understands UTF8 arguments.
2168 */
2169 FILE *fossil_freopen(const char *zName, const char *zMode, FILE *stream){
2170 #ifdef _WIN32
2171 wchar_t *uMode = fossil_utf8_to_unicode(zMode);
2172 wchar_t *uName = fossil_utf8_to_path(zName, 0);
2173 FILE *f = _wfreopen(uName, uMode, stream);
2174 fossil_path_free(uName);
2175 fossil_unicode_free(uMode);
2176 #else
2177 FILE *f = freopen(zName, zMode, stream);
2178 #endif
2179 return f;
2180 }
2181
2182 /*
2183 ** Works like fclose() except that:
2184 **
2185 ** 1) is a no-op if f is 0 or if it is stdin.
2186
+2 -2
--- src/util.c
+++ src/util.c
@@ -671,11 +671,11 @@
671671
sqlite3_randomness(sizeof(r), &r);
672672
#if _WIN32
673673
cDirSep = '\\';
674674
dwTempLenW = GetTempPathW(dwTempSizeW, zTempDirW);
675675
if( dwTempLenW>0 && dwTempLenW<dwTempSizeW
676
- && ( zTempDirA = fossil_unicode_to_utf8(zTempDirW) )){
676
+ && ( zTempDirA = fossil_path_to_utf8(zTempDirW) )){
677677
zDir = zTempDirA;
678678
}else{
679679
zDir = fossil_getenv("LOCALAPPDATA");
680680
if( zDir==0 ) zDir = ".";
681681
}
@@ -693,11 +693,11 @@
693693
nDir = strlen(zDir);
694694
zSep[1] = 0;
695695
zSep[0] = (nDir && zDir[nDir-1]==cDirSep) ? 0 : cDirSep;
696696
zTFile = sqlite3_mprintf("%s%sfossil%016llx%016llx", zDir,zSep,r[0],r[1]);
697697
#ifdef _WIN32
698
- if( zTempDirA ) fossil_unicode_free(zTempDirA);
698
+ if( zTempDirA ) fossil_path_free(zTempDirA);
699699
#endif
700700
return zTFile;
701701
}
702702
703703
/*
704704
--- src/util.c
+++ src/util.c
@@ -671,11 +671,11 @@
671 sqlite3_randomness(sizeof(r), &r);
672 #if _WIN32
673 cDirSep = '\\';
674 dwTempLenW = GetTempPathW(dwTempSizeW, zTempDirW);
675 if( dwTempLenW>0 && dwTempLenW<dwTempSizeW
676 && ( zTempDirA = fossil_unicode_to_utf8(zTempDirW) )){
677 zDir = zTempDirA;
678 }else{
679 zDir = fossil_getenv("LOCALAPPDATA");
680 if( zDir==0 ) zDir = ".";
681 }
@@ -693,11 +693,11 @@
693 nDir = strlen(zDir);
694 zSep[1] = 0;
695 zSep[0] = (nDir && zDir[nDir-1]==cDirSep) ? 0 : cDirSep;
696 zTFile = sqlite3_mprintf("%s%sfossil%016llx%016llx", zDir,zSep,r[0],r[1]);
697 #ifdef _WIN32
698 if( zTempDirA ) fossil_unicode_free(zTempDirA);
699 #endif
700 return zTFile;
701 }
702
703 /*
704
--- src/util.c
+++ src/util.c
@@ -671,11 +671,11 @@
671 sqlite3_randomness(sizeof(r), &r);
672 #if _WIN32
673 cDirSep = '\\';
674 dwTempLenW = GetTempPathW(dwTempSizeW, zTempDirW);
675 if( dwTempLenW>0 && dwTempLenW<dwTempSizeW
676 && ( zTempDirA = fossil_path_to_utf8(zTempDirW) )){
677 zDir = zTempDirA;
678 }else{
679 zDir = fossil_getenv("LOCALAPPDATA");
680 if( zDir==0 ) zDir = ".";
681 }
@@ -693,11 +693,11 @@
693 nDir = strlen(zDir);
694 zSep[1] = 0;
695 zSep[0] = (nDir && zDir[nDir-1]==cDirSep) ? 0 : cDirSep;
696 zTFile = sqlite3_mprintf("%s%sfossil%016llx%016llx", zDir,zSep,r[0],r[1]);
697 #ifdef _WIN32
698 if( zTempDirA ) fossil_path_free(zTempDirA);
699 #endif
700 return zTFile;
701 }
702
703 /*
704

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button