From: <ai...@us...> - 2009-09-08 02:46:05
|
Revision: 10382 http://plplot.svn.sourceforge.net/plplot/?rev=10382&view=rev Author: airwin Date: 2009-09-08 02:45:49 +0000 (Tue, 08 Sep 2009) Log Message: ----------- Make plsave_set_locale() and plrestore_locale reentrant (no more dependence on global variable). This means this pair of routines must be called from the same routine, but in practice that fine grain use of these routines is required in any case, and the reentrancy is required in any case by some of the device driver pldebug calls which recursively call the device driver. Modified Paths: -------------- trunk/include/plplotP.h trunk/src/plctrl.c Modified: trunk/include/plplotP.h =================================================================== --- trunk/include/plplotP.h 2009-09-08 02:37:58 UTC (rev 10381) +++ trunk/include/plplotP.h 2009-09-08 02:45:49 UTC (rev 10382) @@ -566,19 +566,16 @@ plP_text(PLINT base, PLFLT just, PLFLT *xform, PLINT x, PLINT y, PLINT refx, PLINT refy, const char *string); - /* Used to save locale string to be used for later restore of locale. */ -extern PLDLLIMPEXP_DATA(char *)plsaved_lc_numeric_locale; - - /* For LC_NUMERIC save current locale string, then set "C" locale to protect + /* Save LC_NUMERIC locale string, then set "C" locale to protect parts of PLplot which absolutely demand the LC_NUMERIC "C" locale. */ -PLDLLIMPEXP void +PLDLLIMPEXP char * plsave_set_locale(void); /* Restore LC_NUMERIC locale that was determined by plsave_set_locale. */ PLDLLIMPEXP void -plrestore_locale(void); +plrestore_locale(char * save_lc_numeric_locale); /* where should structure definitions that must be seen by drivers and core source files, be? */ Modified: trunk/src/plctrl.c =================================================================== --- trunk/src/plctrl.c 2009-09-08 02:37:58 UTC (rev 10381) +++ trunk/src/plctrl.c 2009-09-08 02:45:49 UTC (rev 10382) @@ -51,10 +51,6 @@ #include <errno.h> #endif -/* extern declaration in plplotP.h, but defined here once for all of - libplplot. */ -char * plsaved_lc_numeric_locale; - /* Random number generator (Mersenne Twister) */ #include "mt19937ar.h" @@ -1210,9 +1206,8 @@ char color_info[30]; char msgbuf[1024]; FILE *fp; + char * save_locale = plsave_set_locale(); - plsave_set_locale(); - if(strlen(filename) == 0) { fp = plLibOpen(PL_DEFAULT_CMAP0_FILE); if (fp == NULL) { @@ -1310,7 +1305,7 @@ } } - plrestore_locale(); + plrestore_locale(save_locale); } /*--------------------------------------------------------------------------*\ @@ -1380,9 +1375,8 @@ PLBOOL *rev; FILE *fp; char msgbuf[1024]; + char * save_locale = plsave_set_locale(); - plsave_set_locale(); - rgb = TRUE; err = 0; format_version = 0; @@ -1550,7 +1544,7 @@ free(pos); free(rev); -finish: plrestore_locale(); +finish: plrestore_locale(save_locale); } /*--------------------------------------------------------------------------*\ @@ -2556,8 +2550,8 @@ /*--------------------------------------------------------------------------*\ * plsave_set_locale() * - * For LC_NUMERIC save current locale string in the global - * pointer, plsaved_lc_numeric_locale, then set "C" locale. + * Save LC_NUMERIC locale in a string. The pointer to that string is + * returned. Then set LC_NUMERIC to "C" locale. * n.b. plsave_set_locale and plrestore_locale should always be used as * a pair to surround PLplot code that absolutely requires the * LC_NUMERIC "C" locale to be in effect. It is one of plrestore_locale's @@ -2565,46 +2559,60 @@ * string. \*--------------------------------------------------------------------------*/ -void +char * plsave_set_locale(void) { char * setlocale_ptr; - char msgbuf[1024]; + char * saved_lc_numeric_locale; - if(!(plsaved_lc_numeric_locale = (char *) malloc(100*sizeof(char)))) { + if(!(saved_lc_numeric_locale = (char *) malloc(100*sizeof(char)))) { plexit("plsave_set_locale: out of memory"); } /*save original LC_NUMERIC locale for restore below. */ if(!(setlocale_ptr = setlocale(LC_NUMERIC, NULL))) { - snprintf(msgbuf,1024,"plsave_set_locale: LC_NUMERIC locale could not be determined for NULL locale.\n"); - plexit(msgbuf); + plexit("plsave_set_locale: LC_NUMERIC locale could not be determined for NULL locale.\n"); } - strncpy(plsaved_lc_numeric_locale, setlocale_ptr, 100); - plsaved_lc_numeric_locale[99] = '\0'; - pldebug("plsave_set_locale", "LC_NUMERIC locale to be restored is \"%s\"\n", plsaved_lc_numeric_locale); + strncpy(saved_lc_numeric_locale, setlocale_ptr, 100); + saved_lc_numeric_locale[99] = '\0'; + + /* Do not use pldebug since get overflowed stack (infinite recursion) + if device is interactive (i.e., pls->termin is set). */ + /* comment out fprintf (unless there is some emergency debugging to do) + because output is too voluminous. */ + /* +fprintf(stderr, "plsave_set_locale: saved LC_NUMERIC locale is \"%s\"\n", saved_lc_numeric_locale); + */ + if(!(setlocale(LC_NUMERIC, "C"))) { plexit("plsave_set_locale: LC_NUMERIC locale could not be set to \"C\""); } + return saved_lc_numeric_locale; } /*--------------------------------------------------------------------------*\ * plrestore_locale() * - * For LC_NUMERIC restore the locale string that was determined by - * plsave_set_locale with a pointer to that string stored in the global - * variable plsaved_lc_numeric_locale and free the memory for that string. + * Restore LC_NUMERIC locale string that was determined by + * plsave_set_locale with the pointer to that string as the argument. + * Also, free the memory for that string. \*--------------------------------------------------------------------------*/ void -plrestore_locale(void) { - char msgbuf[1024]; +plrestore_locale(char *saved_lc_numeric_locale) { - pldebug("plrestore_locale", "LC_NUMERIC locale to be restored is \"%s\"\n", plsaved_lc_numeric_locale); + /* Do not use pldebug since get overflowed stack (infinite recursion) + if device is interactive (i.e., pls->termin is set). */ + /* comment out fprintf (unless there is some emergency debugging to do) + because output is too voluminous. */ + /* + fprintf(stderr, "plrestore_locale: restored LC_NUMERIC locale is \"%s\"\n", saved_lc_numeric_locale); + */ - if(!(setlocale(LC_NUMERIC, plsaved_lc_numeric_locale))) { - snprintf(msgbuf,1024,"plrestore_locale: LC_NUMERIC could not be restored to the default \"%s\" locale.\n", plsaved_lc_numeric_locale); + if(!(setlocale(LC_NUMERIC, saved_lc_numeric_locale))) { + char msgbuf[1024]; + snprintf(msgbuf,1024,"plrestore_locale: LC_NUMERIC could not be restored to the default \"%s\" locale.\n", saved_lc_numeric_locale); plexit(msgbuf); } - free(plsaved_lc_numeric_locale); + free(saved_lc_numeric_locale); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |