| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifdef _WIN32 |
| #ifndef _SQLITE3_STDIO_H_ |
| #include "sqlite3_stdio.h" |
| #endif |
| #undef WIN32_LEAN_AND_MEAN |
| #define WIN32_LEAN_AND_MEAN |
| #include <windows.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <stdio.h> |
| #include <assert.h> |
| #include "sqlite3.h" |
| #include <ctype.h> |
| #include <stdarg.h> |
| #include <io.h> |
| #include <fcntl.h> |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #if defined(SQLITE_U8TEXT_ONLY) |
| # define UseWtextForOutput(fd) 1 |
| # define UseWtextForInput(fd) 1 |
| # define IsConsole(fd) _isatty(_fileno(fd)) |
| #elif defined(SQLITE_U8TEXT_STDIO) |
| # define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr) |
| # define UseWtextForInput(fd) ((fd)==stdin) |
| # define IsConsole(fd) _isatty(_fileno(fd)) |
| #else |
| # define UseWtextForOutput(fd) _isatty(_fileno(fd)) |
| # define UseWtextForInput(fd) _isatty(_fileno(fd)) |
| # define IsConsole(fd) 1 |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static int simBinaryStdout = 0; |
| static int simBinaryOther = 0; |
|
|
|
|
| |
| |
| |
| static int UseBinaryWText(FILE *fd){ |
| if( fd==stdout || fd==stderr ){ |
| return simBinaryStdout; |
| }else{ |
| return simBinaryOther; |
| } |
| } |
|
|
|
|
| |
| |
| |
| FILE *sqlite3_fopen(const char *zFilename, const char *zMode){ |
| FILE *fp = 0; |
| wchar_t *b1, *b2; |
| int sz1, sz2; |
|
|
| sz1 = (int)strlen(zFilename); |
| sz2 = (int)strlen(zMode); |
| b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); |
| b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); |
| if( b1 && b2 ){ |
| sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1); |
| b1[sz1] = 0; |
| sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); |
| b2[sz2] = 0; |
| fp = _wfopen(b1, b2); |
| } |
| sqlite3_free(b1); |
| sqlite3_free(b2); |
| simBinaryOther = 0; |
| return fp; |
| } |
|
|
|
|
| |
| |
| |
| FILE *sqlite3_popen(const char *zCommand, const char *zMode){ |
| FILE *fp = 0; |
| wchar_t *b1, *b2; |
| int sz1, sz2; |
|
|
| sz1 = (int)strlen(zCommand); |
| sz2 = (int)strlen(zMode); |
| b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); |
| b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); |
| if( b1 && b2 ){ |
| sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1); |
| b1[sz1] = 0; |
| sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); |
| b2[sz2] = 0; |
| fp = _wpopen(b1, b2); |
| } |
| sqlite3_free(b1); |
| sqlite3_free(b2); |
| return fp; |
| } |
|
|
| |
| |
| |
| char *sqlite3_fgets(char *buf, int sz, FILE *in){ |
| if( UseWtextForInput(in) ){ |
| |
| |
| |
| |
| |
| wchar_t *b1 = sqlite3_malloc( sz*sizeof(wchar_t) ); |
| if( b1==0 ) return 0; |
| #ifdef SQLITE_USE_W32_FOR_CONSOLE_IO |
| DWORD nRead = 0; |
| if( IsConsole(in) |
| && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz-1, &nRead, 0) |
| ){ |
| b1[nRead] = 0; |
| }else |
| #endif |
| { |
| _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT); |
| if( fgetws(b1, sz/4, in)==0 ){ |
| sqlite3_free(b1); |
| return 0; |
| } |
| } |
| WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0); |
| sqlite3_free(b1); |
| return buf; |
| }else{ |
| |
| |
| return fgets(buf, sz, in); |
| } |
| } |
|
|
| |
| |
| |
| |
| static void piecemealOutput(wchar_t *b1, int sz, FILE *out){ |
| int i; |
| wchar_t c; |
| while( sz>0 ){ |
| for(i=0; i<sz && b1[i]>=0x80; i++){} |
| if( i>0 ){ |
| c = b1[i]; |
| b1[i] = 0; |
| fflush(out); |
| _setmode(_fileno(out), _O_U8TEXT); |
| fputws(b1, out); |
| fflush(out); |
| b1 += i; |
| b1[0] = c; |
| sz -= i; |
| }else{ |
| fflush(out); |
| _setmode(_fileno(out), _O_TEXT); |
| _setmode(_fileno(out), _O_BINARY); |
| fwrite(&b1[0], 1, 1, out); |
| for(i=1; i<sz && b1[i]<0x80; i++){ |
| fwrite(&b1[i], 1, 1, out); |
| } |
| fflush(out); |
| _setmode(_fileno(out), _O_U8TEXT); |
| b1 += i; |
| sz -= i; |
| } |
| } |
| } |
|
|
| |
| |
| |
| int sqlite3_fputs(const char *z, FILE *out){ |
| if( !UseWtextForOutput(out) ){ |
| |
| |
| return fputs(z, out); |
| }else{ |
| |
| |
| |
| int sz = (int)strlen(z); |
| wchar_t *b1 = sqlite3_malloc( (sz+1)*sizeof(wchar_t) ); |
| if( b1==0 ) return 0; |
| sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz); |
| b1[sz] = 0; |
|
|
| #ifdef SQLITE_USE_W32_FOR_CONSOLE_IO |
| DWORD nWr = 0; |
| if( IsConsole(out) |
| && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0) |
| ){ |
| |
| |
| }else |
| #endif |
| { |
| |
| |
| |
| _setmode(_fileno(out), _O_U8TEXT); |
| if( UseBinaryWText(out) ){ |
| piecemealOutput(b1, sz, out); |
| }else{ |
| fputws(b1, out); |
| } |
| } |
| sqlite3_free(b1); |
| return 0; |
| } |
| } |
|
|
|
|
| |
| |
| |
| int sqlite3_fprintf(FILE *out, const char *zFormat, ...){ |
| int rc; |
| if( UseWtextForOutput(out) ){ |
| |
| |
| |
| char *z; |
| va_list ap; |
|
|
| va_start(ap, zFormat); |
| z = sqlite3_vmprintf(zFormat, ap); |
| va_end(ap); |
| sqlite3_fputs(z, out); |
| rc = (int)strlen(z); |
| sqlite3_free(z); |
| }else{ |
| |
| |
| va_list ap; |
| va_start(ap, zFormat); |
| rc = vfprintf(out, zFormat, ap); |
| va_end(ap); |
| } |
| return rc; |
| } |
| int sqlite3_vfprintf(FILE *out, const char *zFormat, va_list ap){ |
| int rc; |
| if( UseWtextForOutput(out) ){ |
| |
| |
| |
| char *z; |
| z = sqlite3_vmprintf(zFormat, ap); |
| sqlite3_fputs(z, out); |
| rc = (int)strlen(z); |
| sqlite3_free(z); |
| }else{ |
| |
| |
| rc = vfprintf(out, zFormat, ap); |
| } |
| return rc; |
| } |
|
|
| |
| |
| |
| |
| void sqlite3_fsetmode(FILE *fp, int mode){ |
| if( !UseWtextForOutput(fp) ){ |
| fflush(fp); |
| _setmode(_fileno(fp), mode); |
| }else if( fp==stdout || fp==stderr ){ |
| simBinaryStdout = (mode==_O_BINARY); |
| }else{ |
| simBinaryOther = (mode==_O_BINARY); |
| } |
| } |
|
|
| #endif |
|
|