Fossil SCM

Make sure the argument to fopen() is MBCS instead of UTF8.

drh 2011-04-28 13:53 windows-i18n
Commit a3c252f237c93ddfce292a0d99f8d8720764eef9
+1 -1
--- src/blob.c
+++ src/blob.c
@@ -690,11 +690,11 @@
690690
}
691691
if( size==0 ){
692692
return 0;
693693
}
694694
blob_resize(pBlob, size);
695
- in = fopen(zFilename, "rb");
695
+ in = fossil_fopen(zFilename, "rb");
696696
if( in==0 ){
697697
fossil_panic("cannot open %s for reading", zFilename);
698698
}
699699
got = fread(blob_buffer(pBlob), 1, size, in);
700700
fclose(in);
701701
--- src/blob.c
+++ src/blob.c
@@ -690,11 +690,11 @@
690 }
691 if( size==0 ){
692 return 0;
693 }
694 blob_resize(pBlob, size);
695 in = fopen(zFilename, "rb");
696 if( in==0 ){
697 fossil_panic("cannot open %s for reading", zFilename);
698 }
699 got = fread(blob_buffer(pBlob), 1, size, in);
700 fclose(in);
701
--- src/blob.c
+++ src/blob.c
@@ -690,11 +690,11 @@
690 }
691 if( size==0 ){
692 return 0;
693 }
694 blob_resize(pBlob, size);
695 in = fossil_fopen(zFilename, "rb");
696 if( in==0 ){
697 fossil_panic("cannot open %s for reading", zFilename);
698 }
699 got = fread(blob_buffer(pBlob), 1, size, in);
700 fclose(in);
701
+55 -2
--- src/file.c
+++ src/file.c
@@ -166,13 +166,13 @@
166166
*/
167167
void file_copy(const char *zFrom, const char *zTo){
168168
FILE *in, *out;
169169
int got;
170170
char zBuf[8192];
171
- in = fopen(zFrom, "rb");
171
+ in = fossil_fopen(zFrom, "rb");
172172
if( in==0 ) fossil_fatal("cannot open \"%s\" for reading", zFrom);
173
- out = fopen(zTo, "wb");
173
+ out = fossil_fopen(zTo, "wb");
174174
if( out==0 ) fossil_fatal("cannot open \"%s\" for writing", zTo);
175175
while( (got=fread(zBuf, 1, sizeof(zBuf), in))>0 ){
176176
fwrite(zBuf, 1, got, out);
177177
}
178178
fclose(in);
@@ -666,5 +666,58 @@
666666
blob_read_from_file(&onDisk, zName);
667667
rc = blob_compare(&onDisk, pContent);
668668
blob_reset(&onDisk);
669669
return rc==0;
670670
}
671
+
672
+
673
+/**************************************************************************
674
+** The following routines translate between MBCS and UTF8 on windows.
675
+** Since everything is always UTF8 on unix, these routines are no-ops
676
+** there.
677
+*/
678
+
679
+/*
680
+** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
681
+** to deallocate any memory used to store the returned pointer when done.
682
+*/
683
+char *fossil_mbcs_to_utf8(const char *zMbcs){
684
+#ifdef _WIN32
685
+ return sqlite3_win32_mbcs_to_utf8(zMbcs);
686
+#else
687
+ return (char*)zMbcs; /* No-op on unix */
688
+#endif
689
+}
690
+
691
+/*
692
+** Translate UTF8 to MBCS. Return a pointer. Call fossil_mbcs_free()
693
+** to deallocate any memory used to store the returned pointer when done.
694
+*/
695
+char *fossil_utf8_to_mbcs(const char *zUtf8){
696
+#ifdef _WIN32
697
+ return sqlite3_win32_utf8_to_mbcs(zUtf8);
698
+#else
699
+ return (char*)zUtf8; /* No-op on unix */
700
+#endif
701
+}
702
+
703
+/*
704
+** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
705
+** to deallocate any memory used to store the returned pointer when done.
706
+*/
707
+void fossil_mbcs_free(char *zOld){
708
+#ifdef _WIN32
709
+ free(zOld);
710
+#else
711
+ /* No-op on unix */
712
+#endif
713
+}
714
+
715
+/*
716
+** Like fopen() but always takes a UTF8 argument.
717
+*/
718
+FILE *fossil_fopen(const char *zName, const char *zMode){
719
+ char *zMbcs = fossil_utf8_to_mbcs(zName);
720
+ FILE *f = fopen(zMbcs, zMode);
721
+ fossil_mbcs_free(zMbcs);
722
+ return f;
723
+}
671724
--- src/file.c
+++ src/file.c
@@ -166,13 +166,13 @@
166 */
167 void file_copy(const char *zFrom, const char *zTo){
168 FILE *in, *out;
169 int got;
170 char zBuf[8192];
171 in = fopen(zFrom, "rb");
172 if( in==0 ) fossil_fatal("cannot open \"%s\" for reading", zFrom);
173 out = fopen(zTo, "wb");
174 if( out==0 ) fossil_fatal("cannot open \"%s\" for writing", zTo);
175 while( (got=fread(zBuf, 1, sizeof(zBuf), in))>0 ){
176 fwrite(zBuf, 1, got, out);
177 }
178 fclose(in);
@@ -666,5 +666,58 @@
666 blob_read_from_file(&onDisk, zName);
667 rc = blob_compare(&onDisk, pContent);
668 blob_reset(&onDisk);
669 return rc==0;
670 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
671
--- src/file.c
+++ src/file.c
@@ -166,13 +166,13 @@
166 */
167 void file_copy(const char *zFrom, const char *zTo){
168 FILE *in, *out;
169 int got;
170 char zBuf[8192];
171 in = fossil_fopen(zFrom, "rb");
172 if( in==0 ) fossil_fatal("cannot open \"%s\" for reading", zFrom);
173 out = fossil_fopen(zTo, "wb");
174 if( out==0 ) fossil_fatal("cannot open \"%s\" for writing", zTo);
175 while( (got=fread(zBuf, 1, sizeof(zBuf), in))>0 ){
176 fwrite(zBuf, 1, got, out);
177 }
178 fclose(in);
@@ -666,5 +666,58 @@
666 blob_read_from_file(&onDisk, zName);
667 rc = blob_compare(&onDisk, pContent);
668 blob_reset(&onDisk);
669 return rc==0;
670 }
671
672
673 /**************************************************************************
674 ** The following routines translate between MBCS and UTF8 on windows.
675 ** Since everything is always UTF8 on unix, these routines are no-ops
676 ** there.
677 */
678
679 /*
680 ** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
681 ** to deallocate any memory used to store the returned pointer when done.
682 */
683 char *fossil_mbcs_to_utf8(const char *zMbcs){
684 #ifdef _WIN32
685 return sqlite3_win32_mbcs_to_utf8(zMbcs);
686 #else
687 return (char*)zMbcs; /* No-op on unix */
688 #endif
689 }
690
691 /*
692 ** Translate UTF8 to MBCS. Return a pointer. Call fossil_mbcs_free()
693 ** to deallocate any memory used to store the returned pointer when done.
694 */
695 char *fossil_utf8_to_mbcs(const char *zUtf8){
696 #ifdef _WIN32
697 return sqlite3_win32_utf8_to_mbcs(zUtf8);
698 #else
699 return (char*)zUtf8; /* No-op on unix */
700 #endif
701 }
702
703 /*
704 ** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
705 ** to deallocate any memory used to store the returned pointer when done.
706 */
707 void fossil_mbcs_free(char *zOld){
708 #ifdef _WIN32
709 free(zOld);
710 #else
711 /* No-op on unix */
712 #endif
713 }
714
715 /*
716 ** Like fopen() but always takes a UTF8 argument.
717 */
718 FILE *fossil_fopen(const char *zName, const char *zMode){
719 char *zMbcs = fossil_utf8_to_mbcs(zName);
720 FILE *f = fopen(zMbcs, zMode);
721 fossil_mbcs_free(zMbcs);
722 return f;
723 }
724
+2 -9
--- src/main.c
+++ src/main.c
@@ -227,24 +227,17 @@
227227
*/
228228
int main(int argc, char **argv){
229229
const char *zCmdName = "unknown";
230230
int idx;
231231
int rc;
232
+ int i;
232233
233234
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
234235
g.now = time(0);
235236
g.argc = argc;
236237
g.argv = argv;
237
-#if defined(_WIN32)
238
- {
239
- int i;
240
- extern char *sqlite3_win32_mbcs_to_utf8(const char*);
241
- for(i=0; i<argc; i++){
242
- g.argv[i] = sqlite3_win32_mbcs_to_utf8(argv[i]);
243
- }
244
- }
245
-#endif
238
+ for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
246239
if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
247240
zCmdName = "cgi";
248241
}else if( argc<2 ){
249242
fossil_fatal("Usage: %s COMMAND ...\n"
250243
"\"%s help\" for a list of available commands\n"
251244
--- src/main.c
+++ src/main.c
@@ -227,24 +227,17 @@
227 */
228 int main(int argc, char **argv){
229 const char *zCmdName = "unknown";
230 int idx;
231 int rc;
 
232
233 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
234 g.now = time(0);
235 g.argc = argc;
236 g.argv = argv;
237 #if defined(_WIN32)
238 {
239 int i;
240 extern char *sqlite3_win32_mbcs_to_utf8(const char*);
241 for(i=0; i<argc; i++){
242 g.argv[i] = sqlite3_win32_mbcs_to_utf8(argv[i]);
243 }
244 }
245 #endif
246 if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
247 zCmdName = "cgi";
248 }else if( argc<2 ){
249 fossil_fatal("Usage: %s COMMAND ...\n"
250 "\"%s help\" for a list of available commands\n"
251
--- src/main.c
+++ src/main.c
@@ -227,24 +227,17 @@
227 */
228 int main(int argc, char **argv){
229 const char *zCmdName = "unknown";
230 int idx;
231 int rc;
232 int i;
233
234 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
235 g.now = time(0);
236 g.argc = argc;
237 g.argv = argv;
238 for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
 
 
 
 
 
 
 
 
239 if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
240 zCmdName = "cgi";
241 }else if( argc<2 ){
242 fossil_fatal("Usage: %s COMMAND ...\n"
243 "\"%s help\" for a list of available commands\n"
244
+1 -1
--- src/vfile.c
+++ src/vfile.c
@@ -428,11 +428,11 @@
428428
const char *zName = db_column_text(&q, 1);
429429
int isSelected = db_column_int(&q, 3);
430430
431431
if( isSelected ){
432432
md5sum_step_text(zName, -1);
433
- in = fopen(zFullpath,"rb");
433
+ in = fossil_fopen(zFullpath,"rb");
434434
if( in==0 ){
435435
md5sum_step_text(" 0\n", -1);
436436
continue;
437437
}
438438
fseek(in, 0L, SEEK_END);
439439
--- src/vfile.c
+++ src/vfile.c
@@ -428,11 +428,11 @@
428 const char *zName = db_column_text(&q, 1);
429 int isSelected = db_column_int(&q, 3);
430
431 if( isSelected ){
432 md5sum_step_text(zName, -1);
433 in = fopen(zFullpath,"rb");
434 if( in==0 ){
435 md5sum_step_text(" 0\n", -1);
436 continue;
437 }
438 fseek(in, 0L, SEEK_END);
439
--- src/vfile.c
+++ src/vfile.c
@@ -428,11 +428,11 @@
428 const char *zName = db_column_text(&q, 1);
429 int isSelected = db_column_int(&q, 3);
430
431 if( isSelected ){
432 md5sum_step_text(zName, -1);
433 in = fossil_fopen(zFullpath,"rb");
434 if( in==0 ){
435 md5sum_step_text(" 0\n", -1);
436 continue;
437 }
438 fseek(in, 0L, SEEK_END);
439
+1 -1
--- src/wiki.c
+++ src/wiki.c
@@ -916,11 +916,11 @@
916916
FILE * zF;
917917
short doClose = 0;
918918
if( (1 == strlen(zFile)) && ('-'==zFile[0]) ){
919919
zF = stdout;
920920
}else{
921
- zF = fopen( zFile, "w" );
921
+ zF = fossil_fopen( zFile, "w" );
922922
doClose = zF ? 1 : 0;
923923
}
924924
if( ! zF ){
925925
fossil_fatal("wiki export could not open output file for writing.");
926926
}
927927
--- src/wiki.c
+++ src/wiki.c
@@ -916,11 +916,11 @@
916 FILE * zF;
917 short doClose = 0;
918 if( (1 == strlen(zFile)) && ('-'==zFile[0]) ){
919 zF = stdout;
920 }else{
921 zF = fopen( zFile, "w" );
922 doClose = zF ? 1 : 0;
923 }
924 if( ! zF ){
925 fossil_fatal("wiki export could not open output file for writing.");
926 }
927
--- src/wiki.c
+++ src/wiki.c
@@ -916,11 +916,11 @@
916 FILE * zF;
917 short doClose = 0;
918 if( (1 == strlen(zFile)) && ('-'==zFile[0]) ){
919 zF = stdout;
920 }else{
921 zF = fossil_fopen( zFile, "w" );
922 doClose = zF ? 1 : 0;
923 }
924 if( ! zF ){
925 fossil_fatal("wiki export could not open output file for writing.");
926 }
927
+2 -2
--- src/winhttp.c
+++ src/winhttp.c
@@ -92,11 +92,11 @@
9292
wanted = find_content_length(zHdr) + (&z[4]-zHdr) - amt;
9393
break;
9494
}
9595
}
9696
if( amt>=sizeof(zHdr) ) goto end_request;
97
- out = fopen(zRequestFName, "wb");
97
+ out = fossil_fopen(zRequestFName, "wb");
9898
if( out==0 ) goto end_request;
9999
fwrite(zHdr, 1, amt, out);
100100
while( wanted>0 ){
101101
got = recv(p->s, zHdr, sizeof(zHdr), 0);
102102
if( got==SOCKET_ERROR ) goto end_request;
@@ -112,11 +112,11 @@
112112
sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http \"%s\" %s %s %s --nossl%s",
113113
fossil_nameofexe(), g.zRepositoryName, zRequestFName, zReplyFName,
114114
inet_ntoa(p->addr.sin_addr), p->zOptions
115115
);
116116
fossil_system(zCmd);
117
- in = fopen(zReplyFName, "rb");
117
+ in = fossil_fopen(zReplyFName, "rb");
118118
if( in ){
119119
while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){
120120
send(p->s, zHdr, got, 0);
121121
}
122122
}
123123
--- src/winhttp.c
+++ src/winhttp.c
@@ -92,11 +92,11 @@
92 wanted = find_content_length(zHdr) + (&z[4]-zHdr) - amt;
93 break;
94 }
95 }
96 if( amt>=sizeof(zHdr) ) goto end_request;
97 out = fopen(zRequestFName, "wb");
98 if( out==0 ) goto end_request;
99 fwrite(zHdr, 1, amt, out);
100 while( wanted>0 ){
101 got = recv(p->s, zHdr, sizeof(zHdr), 0);
102 if( got==SOCKET_ERROR ) goto end_request;
@@ -112,11 +112,11 @@
112 sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http \"%s\" %s %s %s --nossl%s",
113 fossil_nameofexe(), g.zRepositoryName, zRequestFName, zReplyFName,
114 inet_ntoa(p->addr.sin_addr), p->zOptions
115 );
116 fossil_system(zCmd);
117 in = fopen(zReplyFName, "rb");
118 if( in ){
119 while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){
120 send(p->s, zHdr, got, 0);
121 }
122 }
123
--- src/winhttp.c
+++ src/winhttp.c
@@ -92,11 +92,11 @@
92 wanted = find_content_length(zHdr) + (&z[4]-zHdr) - amt;
93 break;
94 }
95 }
96 if( amt>=sizeof(zHdr) ) goto end_request;
97 out = fossil_fopen(zRequestFName, "wb");
98 if( out==0 ) goto end_request;
99 fwrite(zHdr, 1, amt, out);
100 while( wanted>0 ){
101 got = recv(p->s, zHdr, sizeof(zHdr), 0);
102 if( got==SOCKET_ERROR ) goto end_request;
@@ -112,11 +112,11 @@
112 sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http \"%s\" %s %s %s --nossl%s",
113 fossil_nameofexe(), g.zRepositoryName, zRequestFName, zReplyFName,
114 inet_ntoa(p->addr.sin_addr), p->zOptions
115 );
116 fossil_system(zCmd);
117 in = fossil_fopen(zReplyFName, "rb");
118 if( in ){
119 while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){
120 send(p->s, zHdr, got, 0);
121 }
122 }
123

Keyboard Shortcuts

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