1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-11-03 17:27:29 -05:00

Change handling of setlocale(LC_NUMERIC) in load_game() and save_game()

Changing just the LC_NUMERIC part of the locale DOES work, if you read
the manual page carefully!  In particular, TWO calls need to be made, the
first with parameter NULL to get the current locale.  In addition, the
result MUST be copied to a new string, as it may be (and is!) overwritten
by the next call to setlocale().
This commit is contained in:
John Zaitseff 2011-07-21 22:44:45 +10:00
parent 71486357c1
commit 78cac96e4a

View File

@ -145,6 +145,7 @@ bool load_game (int num)
char *buf, *filename; char *buf, *filename;
FILE *file; FILE *file;
int saved_errno, lineno; int saved_errno, lineno;
char *prev_locale;
int crypt_key; int crypt_key;
int n, i, j; int n, i, j;
@ -196,6 +197,10 @@ bool load_game (int num)
} }
// Change the formatting of numbers to the POSIX locale for consistency // Change the formatting of numbers to the POSIX locale for consistency
prev_locale = strdup(setlocale(LC_NUMERIC, NULL));
if (prev_locale == NULL) {
err_exit_nomem();
}
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
// Read the game file header // Read the game file header
@ -283,11 +288,12 @@ bool load_game (int num)
errno_exit("%s", filename); errno_exit("%s", filename);
} }
// Restore the user-supplied locale // Change the formatting of numbers back to the user-supplied locale
setlocale(LC_ALL, ""); setlocale(LC_NUMERIC, prev_locale);
free(buf); free(buf);
free(filename); free(filename);
free(prev_locale);
return true; return true;
} }
@ -301,6 +307,7 @@ bool save_game (int num)
char *buf, *filename; char *buf, *filename;
FILE *file; FILE *file;
int saved_errno; int saved_errno;
char *prev_locale;
struct stat statbuf; struct stat statbuf;
int crypt_key; int crypt_key;
int i, j, x, y; int i, j, x, y;
@ -369,6 +376,10 @@ bool save_game (int num)
} }
// Change the formatting of numbers to the POSIX locale for consistency // Change the formatting of numbers to the POSIX locale for consistency
prev_locale = strdup(setlocale(LC_NUMERIC, NULL));
if (prev_locale == NULL) {
err_exit_nomem();
}
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
// Write out the game file header and encryption key // Write out the game file header and encryption key
@ -429,11 +440,12 @@ bool save_game (int num)
errno_exit("%s", filename); errno_exit("%s", filename);
} }
// Restore the user-supplied locale // Change the formatting of numbers back to the user-supplied locale
setlocale(LC_ALL, ""); setlocale(LC_NUMERIC, prev_locale);
free(buf); free(buf);
free(filename); free(filename);
free(prev_locale);
return true; return true;
} }