From 78cac96e4a8c873cfb94f48c0a828506ecd81a42 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Thu, 21 Jul 2011 22:44:45 +1000 Subject: [PATCH] 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(). --- src/fileio.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index 255e244..f9dfa01 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -145,6 +145,7 @@ bool load_game (int num) char *buf, *filename; FILE *file; int saved_errno, lineno; + char *prev_locale; int crypt_key; int n, i, j; @@ -196,6 +197,10 @@ bool load_game (int num) } // 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"); // Read the game file header @@ -283,11 +288,12 @@ bool load_game (int num) errno_exit("%s", filename); } - // Restore the user-supplied locale - setlocale(LC_ALL, ""); + // Change the formatting of numbers back to the user-supplied locale + setlocale(LC_NUMERIC, prev_locale); free(buf); free(filename); + free(prev_locale); return true; } @@ -301,6 +307,7 @@ bool save_game (int num) char *buf, *filename; FILE *file; int saved_errno; + char *prev_locale; struct stat statbuf; int crypt_key; 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 + prev_locale = strdup(setlocale(LC_NUMERIC, NULL)); + if (prev_locale == NULL) { + err_exit_nomem(); + } setlocale(LC_NUMERIC, "C"); // Write out the game file header and encryption key @@ -429,11 +440,12 @@ bool save_game (int num) errno_exit("%s", filename); } - // Restore the user-supplied locale - setlocale(LC_ALL, ""); + // Change the formatting of numbers back to the user-supplied locale + setlocale(LC_NUMERIC, prev_locale); free(buf); free(filename); + free(prev_locale); return true; }