From 8242ade95f772dd5b91786bca5a30864d59412b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Fri, 1 Apr 2016 03:09:45 +0200 Subject: [PATCH] Version 15.171 --- swad_changelog.h | 3 ++- swad_file.c | 15 ----------- swad_parameter.c | 70 +++++++----------------------------------------- swad_string.c | 62 ++++++++++++++++++++++++------------------ swad_string.h | 6 +++-- swad_syllabus.c | 9 ++++--- 6 files changed, 56 insertions(+), 109 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 3812c738..8ed688a3 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -138,13 +138,14 @@ /****************************** Public constants *****************************/ /*****************************************************************************/ -#define Log_PLATFORM_VERSION "SWAD 15.170 (2016-04-01)" +#define Log_PLATFORM_VERSION "SWAD 15.171 (2016-04-01)" #define CSS_FILE "swad15.165.5.css" #define JS_FILE "swad15.131.3.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.171: Apr 01, 2016 Code refactoring in function to get a parameter. (197259 lines) Version 15.170: Apr 01, 2016 Code refactoring in function to receive file. (197310 lines) Version 15.169.6: Mar 31, 2016 Code refactoring in function to get a parameter. (197313 lines) Version 15.169.5: Mar 31, 2016 Code refactoring in function to get a parameter. (197292 lines) diff --git a/swad_file.c b/swad_file.c index efa16395..436df886 100644 --- a/swad_file.c +++ b/swad_file.c @@ -165,21 +165,6 @@ bool Fil_ReadStdinIntoTmpFile (void) } rewind (Gbl.F.Tmp); -/* - FILE *FileTgt; - - ***** Open destination file ***** - if ((FileTgt = fopen ("/tmp/borrame.txt","wb")) == NULL) - Lay_ShowErrorAndExit ("Can not open target file."); - - ***** Copy source file into destination file ***** - Fil_FastCopyOfOpenFiles (Gbl.F.Tmp,FileTgt); - - ***** Close the files ***** - rewind (Gbl.F.Tmp); - fclose (FileTgt); -*/ - return true; } diff --git a/swad_parameter.c b/swad_parameter.c index 1d9de4ad..ad645723 100644 --- a/swad_parameter.c +++ b/swad_parameter.c @@ -63,7 +63,6 @@ static void Par_GetBoundary (void); static void Par_CreateListOfParamsFromQueryString (void); static void Par_CreateListOfParamsFromTmpFile (void); -static bool Par_ReadTmpFileUntilDelimitStr (const char *BoundaryStr,unsigned LengthBoundaryStr); static int Par_ReadTmpFileUntilQuote (void); static int Par_ReadTmpFileUntilReturn (void); @@ -305,8 +304,11 @@ static void Par_CreateListOfParamsFromTmpFile (void) /***** Go over the file getting start positions and lengths of parameters *****/ - if (Par_ReadTmpFileUntilDelimitStr (Gbl.Boundary.StrWithoutCRLF, - Gbl.Boundary.LengthWithoutCRLF)) // Delimiter string found + if (Str_ReadFileUntilBoundaryStr (Gbl.F.Tmp,NULL, + Gbl.Boundary.StrWithoutCRLF, + Gbl.Boundary.LengthWithoutCRLF, + Fil_MAX_FILE_SIZE) == 1) // Delimiter string found + for (CurPos = 0; CurPos < Gbl.Params.ContentLength; ) @@ -392,8 +394,10 @@ static void Par_CreateListOfParamsFromTmpFile (void) /***** Get parameter value or file content *****/ CurPos = (unsigned long) ftell (Gbl.F.Tmp); // At start of value or file content - if (!Par_ReadTmpFileUntilDelimitStr (Gbl.Boundary.StrWithCRLF, - Gbl.Boundary.LengthWithCRLF)) break; // Delimiter string not found + if (Str_ReadFileUntilBoundaryStr (Gbl.F.Tmp,NULL, + Gbl.Boundary.StrWithCRLF, + Gbl.Boundary.LengthWithCRLF, + Fil_MAX_FILE_SIZE) != 1) break; // Boundary string not found // Delimiter string found Param->Value.Start = CurPos; @@ -404,62 +408,6 @@ static void Par_CreateListOfParamsFromTmpFile (void) } } -/*****************************************************************************/ -/******************** Read from file until quote '\"' ************************/ -/*****************************************************************************/ -// Return true if boundary string is found. -// File is positioned just after the last character in boundary string - -static bool Par_ReadTmpFileUntilDelimitStr (const char *BoundaryStr,unsigned LengthBoundaryStr) - { - unsigned NumBytesIdentical; // Number of characters identical in each iteration of the loop - unsigned NumBytesReadButNoDiscarded; // Number of characters read from the source file... - // ...and not fully discarded in search - int Buffer[Par_MAX_LENGTH_BOUNDARY_WITH_CR_LF+1]; - unsigned StartIndex; - unsigned i; - bool Found; - - for (StartIndex = 0, - NumBytesReadButNoDiscarded = 0, - Found = false; - !Found; - StartIndex = (StartIndex + 1) % LengthBoundaryStr, - NumBytesReadButNoDiscarded--) - { - if (!NumBytesReadButNoDiscarded) - { // Read next character - Buffer[StartIndex] = fgetc (Gbl.F.Tmp); - if (feof (Gbl.F.Tmp)) - return false; - NumBytesReadButNoDiscarded++; - } - if (Buffer[StartIndex] == (int) BoundaryStr[0]) // First character identical - { - for (NumBytesIdentical = 1, - i = (StartIndex + 1) % LengthBoundaryStr; - NumBytesIdentical < LengthBoundaryStr; - NumBytesIdentical++, - i = (i + 1) % LengthBoundaryStr) - { - if (NumBytesReadButNoDiscarded == NumBytesIdentical) // Last character is identical - { - Buffer[i] = fgetc (Gbl.F.Tmp); // Read next character - if (feof (Gbl.F.Tmp)) - return false; - NumBytesReadButNoDiscarded++; - } - if (Buffer[i] != (int) BoundaryStr[NumBytesIdentical]) // Next character is different - break; - } - if (NumBytesIdentical == LengthBoundaryStr) // Boundary found - Found = true; - } - } - - return true; - } - /*****************************************************************************/ /******************** Read from file until quote '\"' ************************/ /*****************************************************************************/ diff --git a/swad_string.c b/swad_string.c index 91ee79b1..84913471 100644 --- a/swad_string.c +++ b/swad_string.c @@ -2495,38 +2495,46 @@ void Str_FilePrintStrChangingBRToRetAndNBSPToSpace (FILE *FileTgt,const char *St /* Search in the file FileSrc the string StrDelimit. Write in the file FileTgt and/or StrDst the characters read from FileSrc, not including StrDelimit!. -FileTgt and StrDst can be NULL if you don't want to use them. +StrDst can be NULL if you don't want to use them. If StrDelimit is found, return 1. If what is read exceed MaxLength, abort and return 0. If StrDelimit is not found, return -1. */ -#define MAX_LENGTH_STR_DELIMIT 100 +#define MAX_LENGTH_BOUNDARY_STR 100 -int Str_ReceiveFileUntilDelimitStr (FILE *FileSrc, FILE *FileTgt, char *StrDst, const char *StrDelimit, unsigned long long MaxLength) +int Str_ReadFileUntilBoundaryStr (FILE *FileSrc,char *StrDst, + const char *BoundaryStr, + unsigned LengthBoundaryStr, + unsigned long long MaxLength) { - int NumBytesIdentical, // Number of characters identical in each iteration of the loop - NumBytesReadButNoWritten = 0, // Number of characters read from the source file - // and not written in the destination file - LengthStrDelimit = strlen (StrDelimit); - int Buffer[MAX_LENGTH_STR_DELIMIT+1]; - unsigned long long LengthDst = 0; - int StartIndex = 0, i; + unsigned NumBytesIdentical; // Number of characters identical in each iteration of the loop + unsigned NumBytesReadButNotDiscarded; // Number of characters read from the source file... + // ...and not fully discarded in search + int Buffer[MAX_LENGTH_BOUNDARY_STR+1]; + unsigned StartIndex; + unsigned i; char *Ptr; // Pointer used to go through StrDst writing characters + unsigned long long LengthDst; - if (!LengthStrDelimit) + /***** Checkings on boundary string *****/ + if (!LengthBoundaryStr) { if (StrDst != NULL) *StrDst = '\0'; return 1; } - if (strlen (StrDelimit) > MAX_LENGTH_STR_DELIMIT) + if (LengthBoundaryStr > MAX_LENGTH_BOUNDARY_STR) Lay_ShowErrorAndExit ("Delimiter string too large."); Ptr = StrDst; + StartIndex = 0; + NumBytesReadButNotDiscarded = 0; + LengthDst = 0; + for (;;) { - if (!NumBytesReadButNoWritten) + if (!NumBytesReadButNotDiscarded) { // Read next character Buffer[StartIndex] = fgetc (FileSrc); if (feof (FileSrc)) @@ -2535,15 +2543,16 @@ int Str_ReceiveFileUntilDelimitStr (FILE *FileSrc, FILE *FileTgt, char *StrDst, *Ptr = '\0'; return -1; } - NumBytesReadButNoWritten++; + NumBytesReadButNotDiscarded++; } - if (Buffer[StartIndex] == (int) StrDelimit[0]) // First character identical + + if (Buffer[StartIndex] == (int) BoundaryStr[0]) // First character identical { - for (NumBytesIdentical = 1, i = (StartIndex + 1) % LengthStrDelimit; - NumBytesIdentical < LengthStrDelimit; - NumBytesIdentical++, i = (i + 1) % LengthStrDelimit) + for (NumBytesIdentical = 1, i = (StartIndex + 1) % LengthBoundaryStr; + NumBytesIdentical < LengthBoundaryStr; + NumBytesIdentical++, i = (i + 1) % LengthBoundaryStr) { - if (NumBytesReadButNoWritten == NumBytesIdentical) // Next character identical + if (NumBytesReadButNotDiscarded == NumBytesIdentical) // Last character is identical { Buffer[i] = fgetc (FileSrc); // Read next character if (feof (FileSrc)) @@ -2552,31 +2561,32 @@ int Str_ReceiveFileUntilDelimitStr (FILE *FileSrc, FILE *FileTgt, char *StrDst, *Ptr = '\0'; return -1; } - NumBytesReadButNoWritten++; + NumBytesReadButNotDiscarded++; } - if (Buffer[i] != (int) StrDelimit[NumBytesIdentical]) // Next different character + if (Buffer[i] != (int) BoundaryStr[NumBytesIdentical]) // Next character is different break; } - if (NumBytesIdentical == LengthStrDelimit) // Str found + if (NumBytesIdentical == LengthBoundaryStr) // Boundary found { if (StrDst != NULL) *Ptr = '\0'; return 1; } } + if (LengthDst == MaxLength) { if (StrDst != NULL) *Ptr = '\0'; return 0; } - if (FileTgt != NULL) - fputc (Buffer[StartIndex],FileTgt); // Add the first character to the destination file + if (StrDst != NULL) *Ptr++ = (char) Buffer[StartIndex]; + + StartIndex = (StartIndex + 1) % LengthBoundaryStr; + NumBytesReadButNotDiscarded--; LengthDst++; - NumBytesReadButNoWritten--; - StartIndex = (StartIndex+1) % LengthStrDelimit; } return 0; // Not reached diff --git a/swad_string.h b/swad_string.h index 922e0f82..e5eba6c7 100644 --- a/swad_string.h +++ b/swad_string.h @@ -108,8 +108,10 @@ bool Str_FileIsHTML (const char *FileName); bool Str_Path1BeginsByPath2 (const char *Path1,const char *Path2); void Str_SkipSpacesInFile (FILE *FileSrc); void Str_FilePrintStrChangingBRToRetAndNBSPToSpace (FILE *FileTgt,const char *Str); -int Str_ReceiveFileUntilDelimitStr (FILE *FileSrc, FILE *FileTgt, char *StrDst, const char *StrDelimit, unsigned long long MaxLength); -int Str_SkipFileUntilDelimitStr (FILE *FileSrc, const char *StrDelimit); +int Str_ReadFileUntilBoundaryStr (FILE *FileSrc,char *StrDst, + const char *BoundaryStr, + unsigned LengthBoundaryStr, + unsigned long long MaxLength); bool Str_ConvertFilFolLnkNameToValid (char *FileName); void Str_ConvertToValidFileName (char *Str); void Str_WriteSizeInBytesBrief (double SizeInBytes); diff --git a/swad_syllabus.c b/swad_syllabus.c index cbe01ba4..45db5f7f 100644 --- a/swad_syllabus.c +++ b/swad_syllabus.c @@ -399,8 +399,9 @@ static void Syl_LoadToMemory (void) LstItemsSyllabus.Lst[NumItem].CodItem[N] = CodItem[N]; /* Get the text of the item */ - Result = Str_ReceiveFileUntilDelimitStr (Gbl.F.XML, NULL, LstItemsSyllabus.Lst[NumItem].Text,"", - (unsigned long long) Syl_MAX_BYTES_TEXT_ITEM); + Result = Str_ReadFileUntilBoundaryStr (Gbl.F.XML,LstItemsSyllabus.Lst[NumItem].Text, + "",strlen (""), + (unsigned long long) Syl_MAX_BYTES_TEXT_ITEM); if (Result == 0) // Str too long { if (!Str_FindStrInFile (Gbl.F.XML,"",Str_NO_SKIP_HTML_COMMENTS)) // End the search @@ -459,8 +460,8 @@ int Syl_ReadLevelItemSyllabus (void) if (!Str_FindStrInFile (Gbl.F.XML,"nivel=\"",Str_NO_SKIP_HTML_COMMENTS)) Lay_ShowErrorAndExit ("Wrong syllabus format."); - if (Str_ReceiveFileUntilDelimitStr (Gbl.F.XML,NULL,StrlLevel,"\"", - (unsigned long long) (11+1)) != 1) + if (Str_ReadFileUntilBoundaryStr (Gbl.F.XML,StrlLevel,"\"",1, + (unsigned long long) (11+1)) != 1) Lay_ShowErrorAndExit ("Wrong syllabus format."); if (sscanf (StrlLevel,"%d",&Level) != 1) Lay_ShowErrorAndExit ("Wrong syllabus format.");