1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-03 16:53:36 +03:00

Set binary mode for output on Windows when writing a quoted string that

might contain newline characters.

FossilOrigin-Name: 7096e6c06d9a3e48d3f0d134f5f3275dde796be2
This commit is contained in:
drh
2015-01-18 20:30:23 +00:00
parent 5d907be5c9
commit 047d4538e3
3 changed files with 32 additions and 18 deletions

View File

@@ -106,6 +106,26 @@ extern int pclose(FILE*);
#define IsDigit(X) isdigit((unsigned char)X)
#define ToLower(X) (char)tolower((unsigned char)X)
/* On Windows, we normally run with output mode of TEXT so that \n characters
** are automatically translated into \r\n. However, this behavior needs
** to be disabled in some cases (ex: when generating CSV output and when
** rendering quoted strings that contain \n characters). The following
** routines take care of that.
*/
#if defined(_WIN32) || defined(WIN32)
static setBinaryMode(FILE *out){
fflush(out);
_setmode(_fileno(out), _O_BINARY);
}
static setTextMode(FILE *out){
fflush(out);
_setmode(_fileno(out), _O_TEXT);
}
#else
# define setBinaryMode(X)
# define setTextMode(X)
#endif
/* True if the timer is enabled */
static int enableTimer = 0;
@@ -584,6 +604,7 @@ static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
static void output_quoted_string(FILE *out, const char *z){
int i;
int nSingle = 0;
setBinaryMode(out);
for(i=0; z[i]; i++){
if( z[i]=='\'' ) nSingle++;
}
@@ -606,6 +627,7 @@ static void output_quoted_string(FILE *out, const char *z){
}
fprintf(out,"'");
}
setTextMode(out);
}
/*
@@ -908,10 +930,7 @@ static int shell_callback(
break;
}
case MODE_Csv: {
#if defined(WIN32) || defined(_WIN32)
fflush(p->out);
_setmode(_fileno(p->out), _O_BINARY);
#endif
setBinaryMode(p->out);
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
@@ -924,10 +943,7 @@ static int shell_callback(
}
fprintf(p->out, "%s", p->rowSeparator);
}
#if defined(WIN32) || defined(_WIN32)
fflush(p->out);
_setmode(_fileno(p->out), _O_TEXT);
#endif
setTextMode(p->out);
break;
}
case MODE_Insert: {
@@ -4176,9 +4192,7 @@ int main(int argc, char **argv){
exit(1);
}
#endif
#if defined(WIN32) || defined(_WIN32)
_setmode(0, _O_BINARY);
#endif
setBinaryMode(stdin);
Argv0 = argv[0];
main_init(&data);
stdin_is_interactive = isatty(0);