diff --git a/swad_action.c b/swad_action.c index 5ad27635..f3f9e31c 100644 --- a/swad_action.c +++ b/swad_action.c @@ -26,7 +26,6 @@ /*****************************************************************************/ #include // For NULL -#include // For setlocale #include // For fprintf #include // For malloc and free #include // For string functions diff --git a/swad_changelog.h b/swad_changelog.h index 9c42675f..47d1ece0 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -133,13 +133,14 @@ /****************************** Public constants *****************************/ /*****************************************************************************/ -#define Log_PLATFORM_VERSION "SWAD 15.218.2 (2016-06-04)" +#define Log_PLATFORM_VERSION "SWAD 15.218.3 (2016-06-04)" #define CSS_FILE "swad15.218.css" #define JS_FILE "swad15.216.js" // Number of lines (includes comments but not blank lines) has been got with the following command: // nl swad*.c swad*.h css/swad*.css py/swad*.py js/swad*.js soap/swad*.h sql/swad*.sql | tail -1 /* + Version 15.218.3: Jun 04, 2016 Code refactoring related to setlocale. (201952 lines) Version 15.218.2: Jun 04, 2016 Checked value returned by setlocale in all calls. (201957 lines) Version 15.218.1: Jun 04, 2016 Change in listing of courses. (201934 lines) Version 15.218: Jun 03, 2016 New module swad_MFU for most frequently used actions. diff --git a/swad_global.c b/swad_global.c index 1f63004b..44c0ee45 100644 --- a/swad_global.c +++ b/swad_global.c @@ -25,9 +25,9 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -#include // For setlocale #include // For INT_MAX, LONG_MAX #include // For NULL +#include // For setlocale #include // For exit, system, malloc, calloc, free, etc #include // For string functions #include // For gettimeofday @@ -83,7 +83,12 @@ void Gbl_InitializeGlobals (void) extern const unsigned Txt_Current_CGI_SWAD_Language; Txt_Language_t Lan; - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) + Gbl.Layout.WritingHTMLStart = + Gbl.Layout.HTMLStartWritten = + Gbl.Layout.DivsEndWritten = + Gbl.Layout.HTMLEndWritten = false; + + if (!setlocale (LC_ALL,"es_ES.utf8")) // TODO: this should be internationalized!!!!!!! exit (1); gettimeofday (&Gbl.tvStart, &Gbl.tz); @@ -116,11 +121,6 @@ void Gbl_InitializeGlobals (void) Gbl.Error = false; - Gbl.Layout.WritingHTMLStart = - Gbl.Layout.HTMLStartWritten = - Gbl.Layout.DivsEndWritten = - Gbl.Layout.HTMLEndWritten = false; - Gbl.DB.DatabaseIsOpen = false; Gbl.DB.LockedTables = false; diff --git a/swad_string.c b/swad_string.c index 6d6f68ec..5a6fd55d 100644 --- a/swad_string.c +++ b/swad_string.c @@ -867,12 +867,10 @@ float Str_GetFloatNumFromStr (const char *Str) if (Str) { - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To get the decimal point - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To get the decimal point as a dot if (sscanf (Str,"%f",&Num) != 1) Lay_ShowErrorAndExit ("Bad floating point format."); - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system } else // Str == NULL Num = 0.0; @@ -880,6 +878,29 @@ float Str_GetFloatNumFromStr (const char *Str) return Num; } +/*****************************************************************************/ +/**** Change decimal point to US system in order to get/print it as a dot ****/ +/*****************************************************************************/ + +void Str_SetDecimalPointToUS (void) + { + if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To get/print the floating point as a dot + if (Gbl.Layout.HTMLStartWritten) + Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + } + +/*****************************************************************************/ +/****************** Change decimal point to local system *********************/ +/*****************************************************************************/ + +void Str_SetDecimalPointToLocal (void) + { + // TODO: this should be internationalized!!!!!!! + if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to local system + if (Gbl.Layout.HTMLStartWritten) + Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + } + /*****************************************************************************/ /*************** Add a string to a Query, changing ' for ' ***************/ /*****************************************************************************/ diff --git a/swad_string.h b/swad_string.h index e5eba6c7..3de6e1e7 100644 --- a/swad_string.h +++ b/swad_string.h @@ -79,6 +79,8 @@ char Str_ConvertToLowerLetter (char Ch); void Str_WriteFloatNum (float Number); void Str_ConvertStrFloatCommaToStrFloatPoint (char *Str); float Str_GetFloatNumFromStr (const char *Str); +void Str_SetDecimalPointToUS (void); +void Str_SetDecimalPointToLocal (void); void Str_AddStrToQuery (char *Query,const char *Str,size_t SizeOfQuery); void Str_ChangeFormat (Str_ChangeFrom_t ChangeFrom,Str_ChangeTo_t ChangeTo, diff --git a/swad_test.c b/swad_test.c index f975b675..1d935e6c 100644 --- a/swad_test.c +++ b/swad_test.c @@ -28,7 +28,6 @@ #include // For UINT_MAX #include // For PATH_MAX #include // For NULL -#include // For setlocale, LC_NUMERIC... #include // To access MySQL databases #include // For boolean type #include // For fprintf, etc. @@ -1172,7 +1171,7 @@ static void Tst_UpdateScoreQst (long QstCod,float ScoreThisQst,bool AnswerIsNotB char Query[512]; /***** Update number of clicks and score of the question *****/ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To print the floating point as a dot + Str_SetDecimalPointToUS (); // To print the floating point as a dot Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); if (AnswerIsNotBlank) sprintf (Query,"UPDATE tst_questions" @@ -1184,8 +1183,7 @@ static void Tst_UpdateScoreQst (long QstCod,float ScoreThisQst,bool AnswerIsNotB " SET NumHits=NumHits+1" " WHERE QstCod='%ld'", QstCod); - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system DB_QueryUPDATE (Query,"can not update the score of a question"); } @@ -2844,12 +2842,10 @@ static void Tst_ListOneOrMoreQuestionsToEdit (unsigned long NumRows,MYSQL_RES *m Lay_ShowErrorAndExit ("Wrong number of hits not blank to a question."); /* Get the acumulated score of the question (row[11]) */ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To get the decimal point - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To get the decimal point as a dot if (sscanf (row[11],"%lf",&TotalScoreThisQst) != 1) Lay_ShowErrorAndExit ("Wrong score of a question."); - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system /* Write number of times this question has been answered */ fprintf (Gbl.F.Out,"" @@ -5626,15 +5622,13 @@ double Tst_GetFloatAnsFromStr (char *Str) Str_ConvertStrFloatCommaToStrFloatPoint (Str); /***** The string is "scanned" in floating point (it must have a point, not a colon as decimal separator) *****/ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To get the decimal point - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To get the decimal point as a dot if (sscanf (Str,"%lg",&DoubleNum) != 1) // If the string does not hold a valid floating point number... { DoubleNum = 0.0; // ...the number is reset to 0 Str[0] = '\0'; // ...and the string is reset to "" } - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system return DoubleNum; } @@ -6052,8 +6046,7 @@ static void Tst_InsertAnswersIntoDB (void) DB_QueryINSERT (Query,"can not create answer"); break; case Tst_ANS_FLOAT: - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To print the floating point as a dot - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To print the floating point as a dot for (i = 0; i < 2; i++) @@ -6066,8 +6059,7 @@ static void Tst_InsertAnswersIntoDB (void) Gbl.Test.Answer.FloatingPoint[i]); DB_QueryINSERT (Query,"can not create answer"); } - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system break; case Tst_ANS_TRUE_FALSE: sprintf (Query,"INSERT INTO tst_answers" @@ -6490,12 +6482,10 @@ static unsigned Tst_GetNumTstQuestions (Sco_Scope_t Scope,Tst_AnswerType_t AnsTy if (sscanf (row[1],"%lu",&(Stats->NumHits)) != 1) Lay_ShowErrorAndExit ("Error when getting total number of hits in test questions."); - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To get the decimal point - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To get the decimal point as a dot if (sscanf (row[2],"%lf",&(Stats->TotalScore)) != 1) Lay_ShowErrorAndExit ("Error when getting total score in test questions."); - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system } else { @@ -6948,15 +6938,13 @@ static void Tst_StoreScoreOfTestExamInDB (long TstCod, char Query[256]; /***** Update score in test exam *****/ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To print the floating point as a dot - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To print the floating point as a dot sprintf (Query,"UPDATE tst_exams" " SET NumQstsNotBlank='%u',Score='%lf'" " WHERE TstCod='%ld'", NumQstsNotBlank,Score, TstCod); - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system DB_QueryUPDATE (Query,"can not update result of test exam"); } @@ -7186,8 +7174,7 @@ static void Tst_ShowResultsOfTestExams (struct UsrData *UsrDat) NumQstsNotBlankInThisExam = 0; /* Get score (row[5]) */ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To get the decimal point - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To get the decimal point as a dot if (sscanf (row[5],"%lf",&ScoreInThisExam) == 1) { if (Gbl.Test.AllowTeachers) @@ -7195,8 +7182,7 @@ static void Tst_ShowResultsOfTestExams (struct UsrData *UsrDat) } else ScoreInThisExam = 0.0; - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system /* Write number of questions */ fprintf (Gbl.F.Out,"", @@ -7701,12 +7687,10 @@ static void Tst_GetExamDataByTstCod (long TstCod,time_t *TstTimeUTC, *NumQstsNotBlank = 0; /* Get score (row[5]) */ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To get the decimal point - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To get the decimal point as a dot if (sscanf (row[5],"%lf",Score) != 1) *Score = 0.0; - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system } /***** Free structure that stores the query result *****/ @@ -7729,8 +7713,7 @@ static void Tst_StoreOneExamQstInDB (long TstCod,long QstCod,unsigned NumQst,dou Par_ReplaceSeparatorMultipleByComma (Gbl.Test.StrAnswersOneQst[NumQst],Answers); /***** Insert question and user's answers into database *****/ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To print the floating point as a dot - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To print the floating point as a dot sprintf (Query,"INSERT INTO tst_exam_questions" " (TstCod,QstCod,QstInd,Score,Indexes,Answers)" " VALUES ('%ld','%ld','%u','%lf','%s','%s')", @@ -7739,8 +7722,7 @@ static void Tst_StoreOneExamQstInDB (long TstCod,long QstCod,unsigned NumQst,dou Score, Indexes, Answers); - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system DB_QueryINSERT (Query,"can not insert a question of an exam"); } diff --git a/swad_web_service.c b/swad_web_service.c index 7b80142f..cab856bc 100644 --- a/swad_web_service.c +++ b/swad_web_service.c @@ -94,7 +94,6 @@ cp -f /home/acanas/swad/swad/swad /var/www/cgi-bin/ #include // For scandir, etc. #include // For PATH_MAX #include // For NULL -#include // For setlocale, LC_NUMERIC... #include // For lstat #include #include @@ -3736,8 +3735,7 @@ int swad__getTrivialQuestion (struct soap *soap, "lowerScore or upperScore values not valid"); /***** Start query *****/ - if (!setlocale (LC_NUMERIC,"en_US.utf8")) // To print the floating point as a dot - Lay_ShowAlert (Lay_ERROR,"Can not set locale to en_US."); + Str_SetDecimalPointToUS (); // To print the floating point as a dot sprintf (Query,"SELECT DISTINCTROW tst_questions.QstCod," "tst_questions.AnsType,tst_questions.Shuffle," "tst_questions.Stem,tst_questions.Feedback," @@ -3758,8 +3756,7 @@ int swad__getTrivialQuestion (struct soap *soap, " ORDER BY RAND(NOW()) LIMIT 1", DegreesStr,DegreesStr, lowerScore,upperScore); - if (!setlocale (LC_NUMERIC,"es_ES.utf8")) // Return to spanish system (TODO: this should be internationalized!!!!!!!) - Lay_ShowAlert (Lay_ERROR,"Can not set locale to es_ES."); + Str_SetDecimalPointToLocal (); // Return to local system NumRows = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get test questions");