00001 /*********************************************************************** 00002 * 00003 * File Name: WSxM_header.C 00004 * 00005 * Description: functions for managing the WSxM_HEADER structure defined in Header.h 00006 * 00007 ***********************************************************************/ 00008 00009 /* this is not a main PlugIn file and here is no Docu to scan... 00010 % PlugInModuleIgnore 00011 */ 00012 00013 //#include <malloc.h> 00014 #include <string.h> 00015 #include <stdlib.h> 00016 #include <math.h> 00017 #include <ctype.h> 00018 #include "WSxM_header.h" 00019 00020 // Define the max. number of chars in a header argument if not done in WSxM_io.C 00021 #ifndef WSXM_MAXCHARS 00022 #define WSXM_MAXCHARS 1000 00023 #endif 00024 /*********************************************************************** 00025 * 00026 * Function void HeaderInit (WSxM_HEADER *pHeader); 00027 * 00028 * Description: Initializes the values of the WSxM_HEADER structure. It should 00029 * be called always before using any of the other functions 00030 * 00031 ***********************************************************************/ 00032 00033 void HeaderInit (WSxM_HEADER *pHeader) 00034 { 00035 if (pHeader == NULL) 00036 return; 00037 00038 pHeader->tszTitles = NULL; 00039 pHeader->tszLabels = NULL; 00040 pHeader->tszValues = NULL; 00041 00042 pHeader->iNumFields = 0; 00043 } 00044 00045 /*********************************************************************** 00046 * 00047 * Function int HeaderRead (WSxM_HEADER *pHeader, FILE *pFile); 00048 * 00049 * Description: Reads a WSxM_HEADER from a file. It is important that the file 00050 * should have been opened for reading, and the WSxM_HEADER should 00051 * have been initializated calling HeaderInit 00052 * 00053 * Inputs: 00054 * - pHeader: pointer to the WSxM_HEADER object to store the data in 00055 * - pFile: File pointer to read the header from 00056 * 00057 * Outputs: 00058 * - pHeader: will be filled with the data read from pFile 00059 * - pFile: the file pointer will go to the end of the header 00060 * 00061 * Return value: 00062 * 0 if the header was correctly read, -1 elsewhere 00063 * 00064 ***********************************************************************/ 00065 00066 int HeaderRead (WSxM_HEADER *pHeader, FILE *pFile) 00067 { 00068 int iStatus = 0; 00069 00070 if ((pHeader == NULL) || (pFile == NULL)) 00071 return -1; 00072 00073 if (pHeader->iNumFields != 0) 00074 return -1; 00075 00076 /* Read the file line by line */ 00077 00078 while (iStatus == 0) 00079 { 00080 iStatus = HeaderReadLine (pHeader,pFile); 00081 } 00082 00083 if (iStatus == -1) /* Error */ 00084 { 00085 return -1; 00086 } 00087 00088 return 0; 00089 } 00090 00091 /* Header write */ 00092 00093 /*********************************************************************** 00094 * 00095 * Function int HeaderWrite (WSxM_HEADER *pHeader, FILE *pFile); 00096 * 00097 * Description: Writes the WSxM_HEADER object to a file. It is important that the file 00098 * should have been opened for writing (not append). 00099 * 00100 * Inputs: 00101 * - pHeader: pointer to the WSxM_HEADER object with the data 00102 * - pFile: File pointer to write the data header 00103 * 00104 * 00105 * Return value: 00106 * 0 if the header was correctly write, -1 elsewhere 00107 * 00108 ***********************************************************************/ 00109 00110 int HeaderWrite (WSxM_HEADER *pHeader, FILE *pFile) 00111 { 00112 int i, j; 00113 char *pCharAux; /* for swapping in bubble sort */ 00114 char szAuxTitle[100], szAuxLabel[100], szAuxValue[WSXM_MAXCHARS]; /* for writting in file */ 00115 00116 /* sorts the WSxM_HEADER object by title and label using bubble sort algorithm */ 00117 00118 for (i=0; i < pHeader->iNumFields; i++) 00119 { 00120 for (j = 0; j < (pHeader->iNumFields-i-1); j++) 00121 { 00122 if ((strcmp (pHeader->tszTitles[j], pHeader->tszTitles[j+1]) > 0)|| 00123 (strcmp (pHeader->tszTitles[j], pHeader->tszTitles[j+1]) == 0)&& 00124 (strcmp (pHeader->tszLabels[j], pHeader->tszLabels[j+1]) > 0)) 00125 { 00126 /* the element j is bigger than j+1 one so we swap them */ 00127 /* we must swap the elements j and j+1 in the three arrays */ 00128 00129 /* swaps the titles */ 00130 pCharAux = pHeader->tszTitles[j]; 00131 pHeader->tszTitles[j] = pHeader->tszTitles[j+1]; 00132 pHeader->tszTitles[j+1] = pCharAux; 00133 00134 /* swaps the labels */ 00135 pCharAux = pHeader->tszLabels[j]; 00136 pHeader->tszLabels[j] = pHeader->tszLabels[j+1]; 00137 pHeader->tszLabels[j+1] = pCharAux; 00138 00139 /* swaps the values */ 00140 pCharAux = pHeader->tszValues[j]; 00141 pHeader->tszValues[j] = pHeader->tszValues[j+1]; 00142 pHeader->tszValues[j+1] = pCharAux; 00143 } 00144 } 00145 } 00146 00147 /* writes the pre-header info in the file */ 00148 fprintf (pFile, "%s", TEXT_COPYRIGHT_NANOTEC); 00149 fprintf (pFile, "%s", STM_IMAGE_FILE_ID); 00150 fprintf (pFile, "%s%d\n", IMAGE_HEADER_SIZE_TEXT, HeaderGetSize (pHeader)); 00151 00152 /* In the first time there is no previous title */ 00153 strcpy (szAuxTitle, ""); 00154 00155 /* writes all the fields in the file */ 00156 for (i=0; i<pHeader->iNumFields; i++) 00157 { 00158 /* if the title is diferent than the previous one we write the title in the file */ 00159 if (strcmp (szAuxTitle, pHeader->tszTitles[i]) != 0) 00160 { 00161 /* Special characteres in the title */ 00162 strcpy (szAuxTitle, pHeader->tszTitles[i]); 00163 ReplaceStringInString (szAuxTitle, "\\","\\\\"); 00164 ReplaceStringInString (szAuxTitle, "\r\n","\\n"); 00165 00166 /* writes the title in the file */ 00167 fprintf (pFile, "\n[%s]\n\n", szAuxTitle); 00168 } 00169 00170 /* Special characteres in the label */ 00171 strcpy (szAuxLabel, pHeader->tszLabels[i]); 00172 ReplaceStringInString (szAuxLabel, "\\","\\\\"); 00173 ReplaceStringInString (szAuxLabel, "\r\n","\\n"); 00174 00175 /* Special characteres in the value */ 00176 strcpy (szAuxValue, pHeader->tszValues[i]); 00177 ReplaceStringInString (szAuxValue, "\\","\\\\"); 00178 ReplaceStringInString (szAuxValue, "\r\n","\\n"); 00179 RemoveLeftAndRightWhitesFromString (szAuxValue); 00180 00181 /* writes the label and the value in the file */ 00182 fprintf (pFile, " %s: %s\n", szAuxLabel, szAuxValue); 00183 } 00184 00185 /* writes the header end */ 00186 fprintf (pFile, "\n[%s]\n",IMAGE_HEADER_END_TEXT); 00187 00188 return 0; 00189 } 00190 00191 /* Header access to one field */ 00192 00193 /* Read */ 00194 00195 /*********************************************************************** 00196 * 00197 * Function double HeaderGetAsNumber (WSxM_HEADER *pHeader, char *szTitle, char *szLabel); 00198 * 00199 * Description: gets the title-label couple value as a number from the 00200 * WSxM_HEADER object, the WSxM_HEADER should have been initializated 00201 * calling HeaderInit 00202 * 00203 * Inputs: 00204 * - pHeader: pointer to the WSxM_HEADER object 00205 * - szTitle: title of the field to be accesed 00206 * - szLabel: label of the field to be accesed 00207 * 00208 * Return value: 00209 * a double with the title-label couple value, 0 on error 00210 * 00211 ***********************************************************************/ 00212 00213 double HeaderGetAsNumber (WSxM_HEADER *pHeader, char *szTitle, char *szLabel) 00214 { 00215 char szValue[WSXM_MAXCHARS] = ""; // the default size of 100 should be okey, but to be consistent .. 00216 if (HeaderGetAsString (pHeader, szTitle, szLabel, szValue) != 0) /* error */ 00217 { 00218 return 0; 00219 } 00220 return atof (szValue); 00221 } 00222 00223 /*********************************************************************** 00224 * 00225 * Function int HeaderGetAsString (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, char *szValue); 00226 * 00227 * Description: gets the title-label couple value as a string from the 00228 * WSxM_HEADER object 00229 * 00230 * Inputs: 00231 * - pHeader: pointer to the WSxM_HEADER object 00232 * - szTitle: title of the field to be accesed 00233 * - szLabel: label of the field to be accesed 00234 * 00235 * Outputs: 00236 * - szValue: will be filled with the title-label couple value as a string 00237 * (this must be allocated) 00238 * 00239 * Return value: 00240 * 0 if the value was correctly accessed, -1 elsewhere 00241 * 00242 ***********************************************************************/ 00243 00244 int HeaderGetAsString (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, char *szValue) 00245 { 00246 int iIndex; 00247 00248 /* seeks the title-label couple */ 00249 for (iIndex=0; iIndex<pHeader->iNumFields; iIndex++) 00250 { 00251 if ((strcmp (szTitle, pHeader->tszTitles[iIndex]) == 0)&&(strcmp (szLabel, pHeader->tszLabels[iIndex]) == 0)) 00252 { 00253 /* title-label couple found at position iIndex */ 00254 break; 00255 } 00256 } 00257 00258 if (iIndex == pHeader->iNumFields) 00259 { 00260 /* the for loop didn't found the title-label couple in WSxM_HEADER object */ 00261 return -1; 00262 } 00263 00264 /* asign the value of the title-label couple to the pointer szValue */ 00265 strcpy (szValue, pHeader->tszValues[iIndex]); 00266 00267 /* access succesful */ 00268 return 0; 00269 } 00270 00271 /*********************************************************************** 00272 * 00273 * Function void HeaderReadTitle (char *szLine, char *szTitle); 00274 * 00275 * Description: reads a title from a line 00276 * 00277 * Inputs: 00278 * - szLine: line with the title 00279 * 00280 * Outputs: 00281 * - szTitle: title readed from the line (this must be allocated), 00282 * szTitle=="" if there is no title in the line. 00283 * 00284 * Return value: 00285 * none 00286 * 00287 ***********************************************************************/ 00288 00289 void HeaderReadTitle (char *szLine, char *szTitle) 00290 { 00291 int iIndex=0; 00292 00293 /* If the line don't begin by a left square bracket it isn't a title line */ 00294 if (szLine[0] != '[') 00295 { 00296 strcpy (szTitle, ""); 00297 return; 00298 } 00299 00300 /* we seek the right square bracket */ 00301 while (szLine[iIndex] != ']') 00302 { 00303 iIndex++; 00304 } 00305 00306 /* at this point iIndex is the position of the right square bracket */ 00307 00308 /* szLine[1] to skip the left square bracket */ 00309 strncpy (szTitle, &szLine[1], iIndex-1); 00310 szTitle[iIndex-1] = '\0'; 00311 } 00312 00313 /*********************************************************************** 00314 * 00315 * Function void HeaderReadLabel (szLine, szLabel); 00316 * 00317 * Description: reads a label from a line 00318 * 00319 * Inputs: 00320 * - szLine: line with the label 00321 * 00322 * Outputs: 00323 * - szLabel: label readed from the line (this must be allocated), 00324 * szValue=="" if there is no label in the line. 00325 * 00326 * Return value: 00327 * none 00328 * 00329 ***********************************************************************/ 00330 00331 void HeaderReadLabel (char *szLine, char *szLabel) 00332 { 00333 unsigned int iIndex=0; 00334 00335 /* we seek the ':' character */ 00336 while (szLine[iIndex] != ':') 00337 { 00338 if (iIndex == strlen (szLine)) 00339 { 00340 /* there is no label in this line */ 00341 strcpy (szLabel, ""); 00342 return; 00343 } 00344 iIndex++; 00345 } 00346 00347 /* at this point iIndex is the position of the ':' character */ 00348 00349 /* we copy the label from the line */ 00350 strncpy (szLabel, szLine, iIndex); 00351 szLabel[iIndex] = '\0'; 00352 } 00353 00354 /*********************************************************************** 00355 * 00356 * Function void HeaderReadValue (char *szLine, char *szValue); 00357 * 00358 * Description: reads a value from a line 00359 * 00360 * Inputs: 00361 * - szLine: line with the value 00362 * 00363 * Outputs: 00364 * - szLabel: value readed from the line (this must be allocated), 00365 * szValue=="" if there is no value in the line. 00366 * 00367 * Return value: 00368 * none 00369 * 00370 ***********************************************************************/ 00371 00372 void HeaderReadValue (char *szLine, char *szValue) 00373 { 00374 unsigned int iIndex=0; 00375 00376 /* we seek the ':' character and skip the blank after */ 00377 while (szLine[iIndex] != ':') 00378 { 00379 if (iIndex == strlen (szLine)) 00380 { 00381 /* there is no value in this line */ 00382 strcpy (szValue, ""); 00383 return; 00384 } 00385 iIndex++; 00386 } 00387 00388 /* skips the ': ' */ 00389 iIndex+=2; 00390 00391 /* at this point iIndex is the position of the first value character */ 00392 00393 /* szLine[iIndex] to skip the line before ': ' and -1 not to copy return carriage */ 00394 strncpy (szValue, &szLine[iIndex], strlen (szLine)-iIndex-1); 00395 szValue[strlen (szLine)-iIndex-1] = '\0'; 00396 } 00397 00398 /* Write */ 00399 00400 /*********************************************************************** 00401 * 00402 * Function void HeaderSetAsFloating (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, double lfValue); 00403 * 00404 * Description: sets a title-label couple value in double format to the 00405 * WSxM_HEADER object 00406 * 00407 * Inputs: 00408 * - pHeader: pointer to the WSxM_HEADER object to set the value in 00409 * - szTitle: title of the field to be set 00410 * - szLabel: label of the field to be set 00411 * - lfValue: value to be set 00412 * 00413 * Return value: 00414 * none 00415 * 00416 ***********************************************************************/ 00417 00418 void HeaderSetAsFloating (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, double lfValue) 00419 { 00420 char szValue[WSXM_MAXCHARS] = ""; 00421 00422 /* converts the double value to a string value */ 00423 sprintf (szValue, "%.61f",lfValue); 00424 00425 /* sets the value in the WSxM_HEADER object as a string */ 00426 HeaderSetAsString (pHeader, szTitle, szLabel, szValue); 00427 } 00428 00429 /*********************************************************************** 00430 * 00431 * Function void HeaderSetAsInt (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, int iValue); 00432 * 00433 * Description: sets a title-label couple value in int format to the 00434 * WSxM_HEADER object 00435 * 00436 * Inputs: 00437 * - pHeader: pointer to the WSxM_HEADER object to set the value in 00438 * - szTitle: title of the field to be set 00439 * - szLabel: label of the field to be set 00440 * - iValue: value to be set 00441 * 00442 * Return value: 00443 * none 00444 * 00445 ***********************************************************************/ 00446 00447 void HeaderSetAsInt (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, int iValue) 00448 { 00449 char szValue[WSXM_MAXCHARS] = ""; 00450 00451 /* converts the int value to a string value */ 00452 sprintf (szValue, "%d", iValue); 00453 00454 /* sets the value in the WSxM_HEADER object as a string */ 00455 HeaderSetAsString (pHeader, szTitle, szLabel, szValue); 00456 } 00457 00458 /*********************************************************************** 00459 * 00460 * Function void HeaderSetAsInt (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, int iValue); 00461 * 00462 * Description: sets a title-label couple value in string format to the 00463 * WSxM_HEADER object 00464 * 00465 * Inputs: 00466 * - pHeader: pointer to the WSxM_HEADER object to set the value in 00467 * - szTitle: title of the field to be set 00468 * - szLabel: label of the field to be set 00469 * - szValue: string value to be set 00470 * 00471 * Return value: 00472 * none 00473 * 00474 ***********************************************************************/ 00475 00476 void HeaderSetAsString (WSxM_HEADER *pHeader, char *szTitle, char *szLabel, char *szValue) 00477 { 00478 int iIndex; 00479 00480 /* sets the value in the WSxM_HEADER object */ 00481 00482 /* seeks the title-label couple */ 00483 for (iIndex=0; iIndex<pHeader->iNumFields; iIndex++) 00484 { 00485 if ((strcmp (szTitle, pHeader->tszTitles[iIndex]) == 0)&&(strcmp (szLabel, pHeader->tszLabels[iIndex]) == 0)) 00486 { 00487 /* title-label couple found at position iIndex */ 00488 /* updates the title-label couple value */ 00489 pHeader->tszValues[iIndex] = (char *) realloc (pHeader->tszValues[iIndex], (strlen (szValue)+1) * sizeof(char)); 00490 strcpy (pHeader->tszValues[iIndex], szValue); 00491 00492 return; 00493 } 00494 } 00495 00496 /* title-label couple not found, is a new field */ 00497 HeaderAddValue (pHeader, szTitle, szLabel, szValue); 00498 } 00499 00500 /*********************************************************************** 00501 * 00502 * Function void HeaderDestroy (WSxM_HEADER *pHeader); 00503 * 00504 * Description: frees all the allocated memory in the Header 00505 * 00506 ***********************************************************************/ 00507 00508 void HeaderDestroy (WSxM_HEADER *pHeader) 00509 { 00510 int i; /* Counter */ 00511 00512 if (pHeader->iNumFields == 0) /* No need to destroy */ 00513 { 00514 return; 00515 } 00516 00517 for (i=0; i < pHeader->iNumFields; i++) 00518 { 00519 free (pHeader->tszTitles[i]); 00520 free (pHeader->tszLabels[i]); 00521 free (pHeader->tszValues[i]); 00522 } 00523 00524 free (pHeader->tszTitles); 00525 free (pHeader->tszLabels); 00526 free (pHeader->tszValues); 00527 00528 pHeader->tszTitles = NULL; 00529 pHeader->tszLabels = NULL; 00530 pHeader->tszValues = NULL; 00531 00532 pHeader->iNumFields = 0; 00533 } 00534 00535 /* Internally used functions */ 00536 00537 /*********************************************************************** 00538 * 00539 * Function int HeaderReadLine (WSxM_HEADER *pHeader, FILE *pFile); 00540 * 00541 * Description: Reads a text line from a file. It is important that the file 00542 * should have been opened for reading, and the WSxM_HEADER should 00543 * have been initializated calling HeaderInit 00544 * 00545 * Inputs: 00546 * - pHeader: pointer to the WSxM_HEADER object to store the data in 00547 * - pFile: File pointer to read the header from 00548 * 00549 * Outputs: 00550 * - pHeader: will be filled with the data read from pFile 00551 * - pFile: the file pointer will go to the end of the header 00552 * 00553 * Return value: 00554 * 0 if the header was correctly read, -1 elsewhere 00555 * 00556 ***********************************************************************/ 00557 00558 int HeaderReadLine (WSxM_HEADER *pHeader, FILE *pFile) 00559 { 00560 /* multiplication in the string sizes is because the string can grow later */ 00561 00562 static char szTitle[WSXM_MAXCHARS*2] = ""; 00563 char szLine[WSXM_MAXCHARS*2]; 00564 char szTryTitle[WSXM_MAXCHARS*2]; 00565 char szLabel[WSXM_MAXCHARS*2]; 00566 char szValue[WSXM_MAXCHARS*2]; 00567 char *szReturn; 00568 00569 szReturn = fgets (szLine,WSXM_MAXCHARS,pFile); 00570 00571 if (szReturn == NULL) 00572 return -1; /* File Error */ 00573 00574 /* Line read. We see the data it has */ 00575 00576 RemoveLeftAndRightWhitesFromString (szLine); 00577 00578 /* Special characteres */ 00579 00580 ReplaceStringInString (szLine,"\\n","\r\n"); 00581 ReplaceStringInString (szLine,"\\\\","\\"); 00582 00583 /* We try to read a label-value couple */ 00584 00585 HeaderReadLabel (szLine,szLabel); 00586 HeaderReadValue (szLine,szValue); 00587 00588 /* We try to read it as a title */ 00589 00590 HeaderReadTitle (szLine,szTryTitle); 00591 00592 if (strlen (szTryTitle) > 0) /* It is a title */ 00593 { 00594 /* If it indicates the header end, we will return 1 */ 00595 00596 if (strcmp (szTryTitle,IMAGE_HEADER_END_TEXT) == 0) 00597 return 1; 00598 else 00599 { 00600 strcpy (szTitle,szTryTitle); 00601 return 0; 00602 } 00603 } 00604 00605 /* If it is not a header field (we have not read any title yet) we ignore it */ 00606 00607 if (strlen (szTitle) == 0) 00608 { 00609 return 0; 00610 } 00611 00612 /* If it is not a couple label: value, we don't mind about this line */ 00613 00614 if (strlen (szLabel) != 0) 00615 { 00616 /* We have a new value. We add it to the header */ 00617 00618 HeaderAddValue (pHeader,szTitle,szLabel,szValue); 00619 } 00620 00621 return 0; 00622 } 00623 00624 /*********************************************************************** 00625 * 00626 * Function int HeaderGetSize (WSxM_HEADER *pHeader); 00627 * 00628 * Description: Gets the size of the image header in the file. 00629 * 00630 * Inputs: 00631 * - pHeader: pointer to the WSxM_HEADER object end of the header 00632 * 00633 * Return value: 00634 * the size of the header, -1 on error 00635 * 00636 ***********************************************************************/ 00637 00638 int HeaderGetSize (WSxM_HEADER *pHeader) 00639 { 00640 int i; 00641 int iSize = 0; /* size at this moment */ 00642 int iNumOfTitles = 0; /* Number of titles written in the file */ 00643 int iNumDigits; /* Number of digits for the image header size */ 00644 char szTitle[100] = "", szLabel[100], szValue[WSXM_MAXCHARS]; 00645 00646 /* size of the pre-header and the carriage return */ 00647 iSize = sizeof (TEXT_COPYRIGHT_NANOTEC) + sizeof (STM_IMAGE_FILE_ID) + sizeof (IMAGE_HEADER_SIZE_TEXT); 00648 00649 /* size of all the fields in the HEADER object */ 00650 for (i=0; i<pHeader->iNumFields; i++) 00651 { 00652 /* if there is a new title we increase the number of titles and adds its size */ 00653 if (strcmp (szTitle, pHeader->tszTitles[i]) != 0) 00654 { 00655 strcpy (szTitle, pHeader->tszTitles[i]); 00656 iNumOfTitles++; 00657 iSize += strlen (szTitle); 00658 } 00659 00660 strcpy (szLabel, pHeader->tszLabels[i]); 00661 00662 /* Special characteres in the szLabel string */ 00663 ReplaceStringInString (szLabel,"\\n","\r\n"); 00664 ReplaceStringInString (szLabel,"\\\\","\\"); 00665 00666 strcpy (szValue, pHeader->tszValues[i]); 00667 00668 /* Special characteres in the szValue string */ 00669 ReplaceStringInString (szValue,"\\n","\r\n"); 00670 ReplaceStringInString (szValue,"\\\\","\\"); 00671 RemoveLeftAndRightWhitesFromString (szValue); 00672 00673 /* adds the size of the label and the value */ 00674 iSize += strlen (szLabel) + strlen (szValue); 00675 } 00676 00677 /* adds the size of the square brackets (2 bytes), the carriage return (3*2 bytes */ 00678 /* for each title) */ 00679 iSize += iNumOfTitles * (6 + 2); 00680 00681 /* adds the size of the carriage return (2 bytes for each label) and the blanks (6 bytes for each label) */ 00682 iSize += pHeader->iNumFields * (6 + 2); 00683 00684 /* adds the size of the header end text, the square brackets and the carriage return */ 00685 iSize += sizeof (IMAGE_HEADER_END_TEXT) + 2 + 4; 00686 00687 /* adds the size of the characteres for the image header size in pre-header */ 00688 iNumDigits = (int)floor(log10(iSize)) + 1; 00689 iSize += iNumDigits; 00690 if (iNumDigits != (int)floor((log10(iSize)) + 1)) 00691 iSize++; 00692 00693 return iSize; 00694 } 00695 00696 /*********************************************************************** 00697 * 00698 * Function int HeaderAddValue (WSxM_HEADER *pHeader, char *szTitle, char *szLabel,char *szValue); 00699 * 00700 * Description: adds the tripla title-label-value to the WSxM_HEADER object 00701 * 00702 * Inputs: 00703 * - pHeader: pointer to the WSxM_HEADER object to store the data in 00704 * - szTitle: title of the field to be added 00705 * - szLabel: label of the field to be added 00706 * - szValue: string value to be added 00707 * 00708 * Return value: 00709 * 0 if the header was correctly updated, -1 elsewhere 00710 * 00711 ***********************************************************************/ 00712 00713 int HeaderAddValue (WSxM_HEADER *pHeader, char *szTitle, char *szLabel,char *szValue) 00714 { 00715 /* grows the three arrays for the new title-label-value tripla */ 00716 pHeader->tszTitles = (char **) realloc (pHeader->tszTitles, (pHeader->iNumFields+1)*sizeof (char *)); 00717 pHeader->tszLabels = (char **) realloc (pHeader->tszLabels, (pHeader->iNumFields+1)*sizeof (char *)); 00718 pHeader->tszValues = (char **) realloc (pHeader->tszValues, (pHeader->iNumFields+1)*sizeof (char *)); 00719 00720 /* adds the title in the last position of the titles array allocating memory */ 00721 pHeader->tszTitles[pHeader->iNumFields] = (char *) calloc (strlen (szTitle)+1, sizeof(char)); 00722 if (pHeader->tszTitles[pHeader->iNumFields] == NULL) /* error */ 00723 { 00724 return -1; 00725 } 00726 strcpy (pHeader->tszTitles[pHeader->iNumFields], szTitle); 00727 00728 /* adds the label in the last position of the labels array allocating memory */ 00729 pHeader->tszLabels[pHeader->iNumFields] = (char *) calloc (strlen (szLabel)+1, sizeof(char)); 00730 if (pHeader->tszLabels[pHeader->iNumFields] == NULL) /* error */ 00731 { 00732 /* frees the memory allocated in this function */ 00733 free (pHeader->tszTitles[pHeader->iNumFields]); 00734 00735 return -1; 00736 } 00737 strcpy (pHeader->tszLabels[pHeader->iNumFields], szLabel); 00738 00739 /* adds the value in the last position of the values array allocating memory */ 00740 pHeader->tszValues[pHeader->iNumFields] = (char *) calloc (strlen (szValue)+1, sizeof(char)); 00741 if (pHeader->tszValues[pHeader->iNumFields] == NULL) /* error */ 00742 { 00743 /* frees the memory allocated in this function */ 00744 free (pHeader->tszTitles[pHeader->iNumFields]); 00745 free (pHeader->tszLabels[pHeader->iNumFields]); 00746 00747 return -1; 00748 } 00749 strcpy (pHeader->tszValues[pHeader->iNumFields], szValue); 00750 00751 /* increments the number of fields */ 00752 pHeader->iNumFields++; 00753 00754 return 0; 00755 } 00756 00757 /*********************************************************************** 00758 * 00759 * Function void RemoveLeftAndRightWhitesFromString (char *szString); 00760 * 00761 * Description: removes the white-space characteres (0x09, 0x0D or 0x20) 00762 * at the right and left of the string szString. 00763 * 00764 * Inputs: 00765 * - szString: the string we want to remove the white-space characteres 00766 * 00767 * Outputs: 00768 * - szString: the string without white-space character 00769 * 00770 * Return value: 00771 * none 00772 * 00773 ***********************************************************************/ 00774 00775 void RemoveLeftAndRightWhitesFromString (char *szString) 00776 { 00777 char szAuxString[WSXM_MAXCHARS]; 00778 int iIndex = 0; 00779 00780 /* Seeks the first non white-space character (0x09, 0x0D or 0x20) */ 00781 while (isspace (szString[iIndex])) 00782 { 00783 iIndex++; 00784 } 00785 00786 /* We copy the string without white-space characters at the left */ 00787 strcpy (szAuxString, &szString[iIndex]); 00788 00789 /* Seeks the last non white-space character (0x09, 0x0D or 0x20) */ 00790 iIndex = strlen (szAuxString); 00791 while (isspace (szString[iIndex])) 00792 { 00793 iIndex--; 00794 } 00795 00796 /* Puts the zero (mark of end of string) after the last non white-space character */ 00797 szString[iIndex+1] = '\0'; 00798 00799 /* We copy the new szString without white-space characters */ 00800 strcpy (szString, szAuxString); 00801 } 00802 00803 /*********************************************************************** 00804 * 00805 * Function void ReplaceStringInString (char *szDest, const char *szOld, const char *szNew); 00806 * 00807 * Description: replaces instances of the substring szOld with 00808 * instances of the string szNew. 00809 * 00810 * Inputs: 00811 * - szDest: the string where will be made the replacemets 00812 * - szOld: the substring to be replaced by lpszNew 00813 * - szNew: the substring replacing lpszOld 00814 * 00815 * Outputs: 00816 * - szDest: the string with the replacements 00817 * 00818 * Return value: 00819 * none 00820 * 00821 ***********************************************************************/ 00822 00823 void ReplaceStringInString (char *szDest, const char *szOld, const char *szNew) 00824 { 00825 int iIndex=0, iAuxStringIndex=0; 00826 char szAuxString[WSXM_MAXCHARS]; 00827 strcpy (szAuxString,""); 00828 /* We copy the string with the replacements to the auxiliar string */ 00829 00830 /* Searches for the substring szOld */ 00831 while (szDest[iIndex]!='\0') /* while not end of string */ 00832 { 00833 if (strncmp (&szDest[iIndex], szOld, strlen(szOld)) == 0) 00834 { 00835 /* Writes the substring szNew in the substring szOld position */ 00836 strcpy (&szAuxString[iAuxStringIndex], szNew); 00837 iAuxStringIndex += strlen (szNew); 00838 iIndex += strlen (szOld); 00839 } 00840 else 00841 { 00842 /* We copy the character without any change */ 00843 szAuxString[iAuxStringIndex] = szDest [iIndex]; 00844 iAuxStringIndex++; 00845 iIndex++; 00846 } 00847 } 00848 00849 /* Puts the zero (mark of end of string) to the auxiliar string */ 00850 szAuxString[iAuxStringIndex] = '\0'; 00851 00852 /* We copy the new string to szDest */ 00853 strcpy (szDest, szAuxString); 00854 }