00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include "IOFunctions.h"
00050 #include <string.h>
00051
00052 int util_ignore(int iSize, char cDelimiter, FILE* hFileHandle)
00053 {
00054 char cChar;
00055 for(int i = 0; i < iSize; i++)
00056 {
00057 cChar = fgetc(hFileHandle);
00058 if(cChar == EOF)
00059 return -1;
00060 if(cChar == cDelimiter)
00061 return 0;
00062 }
00063 return 0;
00064 }
00065
00066 int util_skipWhiteSpace(FILE* hFileHandle)
00067 {
00068 char cChar;
00069 do
00070 {
00071 cChar = fgetc(hFileHandle);
00072 if(cChar == EOF)
00073 return -1;
00074 if(cChar != ' ' && cChar != '"' && cChar != '\t')
00075 {
00076 ungetc(cChar, hFileHandle);
00077 return 0;
00078 }
00079 }while(1);
00080 return 0;
00081 }
00082
00083 int util_getStringCutWhiteSpace(char* acReturnString, int iSize, FILE* hFileHandle)
00084 {
00085 char cChar;
00086 fgets(acReturnString, iSize, hFileHandle);
00087 for(int i = 0; i < iSize; i++)
00088 {
00089 cChar = acReturnString[i];
00090 if(cChar == ' ' || cChar == '"' || cChar == '#' || cChar == ';' || cChar == '\t' || cChar == '\r' || cChar == '\n' || cChar == '\0')
00091 {
00092 acReturnString[i] = '\0';
00093 break;
00094 }
00095 }
00096 return 0;
00097 }
00098
00099 int util_searchSection(const char* acSectionName, FILE* hFileHandle)
00100 {
00101 int iRetVal = 1;
00102 int iSectionLength;
00103 char cChar;
00104 char acBuffer[512];
00105 iSectionLength = strlen(acSectionName);
00106 do
00107 {
00108 cChar = fgetc(hFileHandle);
00109 if(cChar == EOF)
00110 iRetVal = -1;
00111 else if(cChar == '#' || cChar == ';')
00112 {
00113 iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);
00114 if(iRetVal == 0)
00115 iRetVal = 1;
00116 }
00117 else if(cChar == '[')
00118 {
00119 fgets(acBuffer, iSectionLength+1, hFileHandle);
00120
00121 if(strncmp(acBuffer, acSectionName, iSectionLength) == 0)
00122 {
00123 cChar = fgetc(hFileHandle);
00124 if(cChar == ']')
00125 iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);
00126 else
00127 iRetVal = -1;
00128 }
00129 }
00130 }
00131 while(iRetVal > 0);
00132 return iRetVal;
00133 }
00134
00135 int util_searchKey(const char* acKeyName, FILE* hFileHandle)
00136 {
00137 int iRetVal = 1;
00138 int iKeyLength, iBufferLength;
00139 char cChar;
00140 char acBuffer[512];
00141 iKeyLength = strlen(acKeyName);
00142 do
00143 {
00144 cChar = fgetc(hFileHandle);
00145 if(cChar == EOF)
00146 iRetVal = -1;
00147 else if(cChar == '[')
00148 {
00149 ungetc(cChar, hFileHandle);
00150 iRetVal = -1;
00151 }
00152 else if(cChar == '#' || cChar == ';')
00153 {
00154 iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);
00155 if(iRetVal == 0)
00156 iRetVal = 1;
00157 }
00158 else if(cChar != ' ' && cChar != '\t' && cChar != '\r' && cChar != '\n')
00159 {
00160 acBuffer[0] = cChar;
00161 if (iKeyLength > 1)
00162 fgets(acBuffer + 1, iKeyLength, hFileHandle);
00163
00164 iBufferLength = strlen(acBuffer);
00165 if(iBufferLength > iKeyLength || (iBufferLength == iKeyLength && acBuffer[iBufferLength - 1] != '\n'))
00166 {
00167 if(strncmp(acBuffer, acKeyName, iKeyLength) == 0)
00168 {
00169 iRetVal = util_skipWhiteSpace(hFileHandle);
00170 cChar = fgetc(hFileHandle);
00171 if(cChar == '=')
00172 iRetVal = util_skipWhiteSpace(hFileHandle);
00173 else
00174 return -1;
00175 }
00176 else
00177 {
00178 iRetVal = util_ignore(0x7FFF,'\n', hFileHandle);
00179 if(iRetVal == 0)
00180 iRetVal = 1;
00181 }
00182 }
00183 }
00184 }
00185 while(iRetVal > 0);
00186 return iRetVal;
00187 }
00188
00189 int util_searchString(const char* acSectionName, const char* acKeyName, const char* acDefaultString, char* acReturnString, int iSize, const char* acFileName)
00190 {
00191 FILE* hFileHandle = fopen( acFileName, "r" );
00192 if(hFileHandle <= 0)
00193 {
00194 strncpy(acReturnString, acDefaultString, iSize);
00195 return -1;
00196 }
00197
00198 if(util_searchSection(acSectionName, hFileHandle) < 0)
00199 {
00200 strncpy(acReturnString, acDefaultString, iSize);
00201 fclose(hFileHandle);
00202 return 0;
00203 }
00204 if(util_searchKey(acKeyName, hFileHandle) < 0)
00205 {
00206 strncpy(acReturnString, acDefaultString, iSize);
00207 fclose(hFileHandle);
00208 return 0;
00209 }
00210 util_getStringCutWhiteSpace(acReturnString, iSize, hFileHandle);
00211 fclose(hFileHandle);
00212 return strlen(acReturnString);
00213 }
00214
00215 int util_setSection(const char* acSectionName, FILE* hFileHandle)
00216 {
00217 int iRetVal = fseek(hFileHandle,0,SEEK_CUR);
00218 if(iRetVal < 0)
00219 {
00220
00221 return -1;
00222 }
00223 iRetVal = fprintf(hFileHandle, "\n\n[%s]", acSectionName);
00224 if(iRetVal == strlen(acSectionName) + 4)
00225 {
00226
00227 fseek(hFileHandle,0,SEEK_CUR);
00228 return 0;
00229 }
00230 else
00231 {
00232
00233 return -1;
00234 }
00235 }
00236
00237 int util_setKey(const char* acKeyName, FILE* hFileHandle)
00238 {
00239 int iRetVal = fseek(hFileHandle,0,SEEK_CUR);
00240 if(iRetVal < 0)
00241 {
00242
00243 return -1;
00244 }
00245 iRetVal = fprintf(hFileHandle, "\n%s = ", acKeyName);
00246 if(iRetVal == strlen(acKeyName) + 4)
00247 {
00248
00249 fseek(hFileHandle,0,SEEK_CUR);
00250 return 0;
00251 }
00252 else
00253 {
00254
00255 return -1;
00256 }
00257 }
00258
00259 int util_setString(const char* acSectionName, const char* acKeyName, const char* acString, const char* acFileName)
00260 {
00261 int iRetVal = 0;
00262 int iLength = 0;
00263 char* acBuffer = NULL;
00264 FILE* hFileHandle = fopen( acFileName, "r+" );
00265 if(hFileHandle <= 0)
00266 {
00267 hFileHandle = fopen( acFileName, "w+" );
00268 if(hFileHandle <= 0)
00269 {
00270
00271 return -1;
00272 }
00273 }
00274
00275 if(util_searchSection(acSectionName, hFileHandle) < 0)
00276 {
00277
00278 iRetVal = util_setSection(acSectionName, hFileHandle);
00279 if(iRetVal < 0)
00280 {
00281 fclose(hFileHandle);
00282 return -1;
00283 }
00284 }
00285
00286
00287 if(util_searchKey(acKeyName, hFileHandle) < 0)
00288 {
00289
00290 fpos_t fposRest;
00291 iRetVal = fgetpos(hFileHandle, &fposRest);
00292 if(iRetVal < 0)
00293 {
00294
00295 fclose(hFileHandle);
00296 return -1;
00297 }
00298 char cChar;
00299 do
00300 {
00301 #if defined(__LINUX__)
00302 fposRest.__pos--;
00303 #else
00304 fposRest--;
00305 #endif
00306 iRetVal = fsetpos(hFileHandle, &fposRest);
00307 if(iRetVal < 0)
00308 {
00309
00310 fclose(hFileHandle);
00311 return -1;
00312 }
00313 cChar = fgetc(hFileHandle);
00314 if(cChar != '\n')
00315 {
00316 #if defined(__LINUX__)
00317 fposRest.__pos++;
00318 #else
00319 fposRest++;
00320 #endif
00321 iRetVal = fsetpos(hFileHandle, &fposRest);
00322 if(iRetVal < 0)
00323 {
00324
00325 fclose(hFileHandle);
00326 return -1;
00327 }
00328 break;
00329 }
00330 }while(1);
00331
00332 do
00333 {
00334 cChar = fgetc(hFileHandle);
00335 if(cChar == EOF)
00336 break;
00337 }while(1);
00338 fpos_t fposEnd;
00339 iRetVal = fgetpos(hFileHandle, &fposEnd);
00340 if(iRetVal < 0)
00341 {
00342
00343 fclose(hFileHandle);
00344 return -1;
00345 }
00346 #if defined(__LINUX__)
00347 iLength = fposEnd.__pos - fposRest.__pos;
00348 #else
00349 iLength = fposEnd - fposRest;
00350 #endif
00351 if(iLength > 0)
00352 {
00353 acBuffer = new char[iLength];
00354
00355
00356 iRetVal = fsetpos(hFileHandle, &fposRest);
00357 if(iRetVal < 0)
00358 {
00359
00360 fclose(hFileHandle);
00361 if(acBuffer != NULL)
00362 delete[] acBuffer;
00363 return -1;
00364 }
00365
00366 iLength = fread(acBuffer, sizeof(char), iLength, hFileHandle);
00367 if(iLength < 0)
00368 {
00369
00370 fclose(hFileHandle);
00371 if(acBuffer != NULL)
00372 delete[] acBuffer;
00373 return -1;
00374 }
00375 acBuffer[iLength] = '\0';
00376 }
00377
00378 iRetVal = fsetpos(hFileHandle, &fposRest);
00379 if(iRetVal < 0)
00380 {
00381
00382 fclose(hFileHandle);
00383 if(acBuffer != NULL)
00384 delete[] acBuffer;
00385 return -1;
00386 }
00387 iRetVal = util_setKey(acKeyName, hFileHandle);
00388 if(iRetVal < 0)
00389 {
00390 fclose(hFileHandle);
00391 if(acBuffer != NULL)
00392 delete[] acBuffer;
00393 return -1;
00394 }
00395 iRetVal = fprintf(hFileHandle, "%s", acString);
00396 if(iRetVal != strlen(acString))
00397 {
00398
00399 fclose(hFileHandle);
00400 if(acBuffer != NULL)
00401 delete[] acBuffer;
00402 return -1;
00403 }
00404
00405
00406 if(iLength > 0)
00407 {
00408 iLength = fwrite(acBuffer, sizeof(char), iLength, hFileHandle);
00409 if(iLength != iLength)
00410 {
00411
00412 fclose(hFileHandle);
00413 if(acBuffer != NULL)
00414 delete[] acBuffer;
00415 return -1;
00416 }
00417 if(acBuffer != NULL)
00418 delete[] acBuffer;
00419 }
00420 }
00421 else
00422 {
00423
00424 fpos_t fposString;
00425 iRetVal = fgetpos(hFileHandle, &fposString);
00426 if(iRetVal < 0)
00427 {
00428
00429 fclose(hFileHandle);
00430 return -1;
00431 }
00432
00433 char cChar;
00434 int iErase = 0;
00435 do
00436 {
00437 cChar = fgetc(hFileHandle);
00438 if(cChar == EOF || cChar == '\n' || cChar == ';' || cChar == '#')
00439 {
00440 ungetc(cChar, hFileHandle);
00441 break;
00442 }
00443 iErase++;
00444 }while(1);
00445 fpos_t fposRest;
00446 iRetVal = fgetpos(hFileHandle, &fposRest);
00447 if(iRetVal < 0)
00448 {
00449
00450 fclose(hFileHandle);
00451 return -1;
00452 }
00453
00454 do
00455 {
00456 cChar = fgetc(hFileHandle);
00457 if(cChar == EOF)
00458 break;
00459 }while(1);
00460 fpos_t fposEnd;
00461 iRetVal = fgetpos(hFileHandle, &fposEnd);
00462 if(iRetVal < 0)
00463 {
00464 std::cout << "get End pos error" << std::endl;
00465 fclose(hFileHandle);
00466 return -1;
00467 }
00468 #if defined(__LINUX__)
00469 iLength = fposEnd.__pos - fposRest.__pos;
00470 #else
00471 iLength = fposEnd - fposRest;
00472 #endif
00473 if(iLength > 0)
00474 {
00475 acBuffer = new char[iLength];
00476
00477
00478 iRetVal = fsetpos(hFileHandle, &fposRest);
00479 if(iRetVal < 0)
00480 {
00481
00482 fclose(hFileHandle);
00483 if(acBuffer != NULL)
00484 delete[] acBuffer;
00485 return -1;
00486 }
00487
00488 iLength = fread(acBuffer, sizeof(char), iLength, hFileHandle);
00489 if(iLength < 0)
00490 {
00491
00492 fclose(hFileHandle);
00493 if(acBuffer != NULL)
00494 delete[] acBuffer;
00495 return -1;
00496 }
00497 acBuffer[iLength] = '\0';
00498 }
00499
00500 iRetVal = fsetpos(hFileHandle, &fposString);
00501 if(iRetVal < 0)
00502 {
00503
00504 fclose(hFileHandle);
00505 if(acBuffer != NULL)
00506 delete[] acBuffer;
00507 return -1;
00508 }
00509
00510 iRetVal = fprintf(hFileHandle, "%s ", acString);
00511 iErase -= strlen(acString) + 1;
00512 if(iRetVal != strlen(acString) + 1)
00513 {
00514
00515 fclose(hFileHandle);
00516 if(acBuffer != NULL)
00517 delete[] acBuffer;
00518 return -1;
00519 }
00520
00521
00522
00523 if(iLength > 0)
00524 {
00525 iLength = fwrite(acBuffer, sizeof(char), iLength, hFileHandle);
00526 if(iLength != iLength)
00527 {
00528
00529 fclose(hFileHandle);
00530 if(acBuffer != NULL)
00531 delete[] acBuffer;
00532 return -1;
00533 }
00534
00535 for(int i = 0; i < iErase; i++)
00536 {
00537 cChar = fputc('\0', hFileHandle);
00538 if(cChar == EOF)
00539 break;
00540 }
00541 if(acBuffer != NULL)
00542 delete[] acBuffer;
00543 }
00544 }
00545 fclose(hFileHandle);
00546 return 0;
00547 }
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601 #ifdef WITHSTREAMS
00602 int util_posArgForKey(
00603 std::istream& in,
00604 const char* key,
00605 int number,
00606 char delim)
00607 {
00608 static char buf[BUFFER_LENGTH];
00609 char cL;
00610 int count;
00611
00612 while( !in.eof() )
00613 {
00614 in >> cL;
00615
00616 if(cL == '#' || cL == ';')
00617 in.ignore(0x7FFF,'\n');
00618 else
00619 {
00620 buf[0] = cL;
00621
00622 if (strlen(key) > 1)
00623 in.get(buf+1, strlen(key), '\n');
00624
00625 if(strncmp(buf, key, strlen(key)) == 0)
00626 {
00627 if (number >= 0)
00628 {
00629 in >> count;
00630
00631 if (count != number)
00632 return KEY_BUT_WRONG_NUMBER;
00633 };
00634
00635 in >> std::ws;
00636 in >> cL;
00637 if(cL == delim)
00638 {
00639
00640
00641 in >> std::ws;
00642
00643
00644
00645 return OKAY;
00646 }
00647 else
00648 return KEY_BUT_NO_EQUAL;
00649 }
00650 else
00651 return NO_KEY;
00652 };
00653 };
00654
00655 return FOUND_EOF;
00656 };
00657
00658
00659
00660
00661
00662 void util_parseError(
00663 int status,
00664 const char* key,
00665 int number)
00666 {
00667 switch(status)
00668 {
00669 case OKAY:
00670 break;
00671 case KEY_BUT_NO_EQUAL:
00672 std::cerr << "\nread(in) parse error : '=' expected behind";
00673 std::cerr << key;
00674 if (number >= 0)
00675 std::cerr << " " << number;
00676 std::cerr << " !";
00677 break;
00678 case NO_KEY:
00679 std::cerr << "\nread(in) parse error : '";
00680 std::cerr << key;
00681 if (number >= 0)
00682 std::cerr << " " << number;
00683 std::cerr << "' expected !";
00684 break;
00685 case FOUND_EOF:
00686 std::cerr << "\nread(in) parse error : premature EOF '";
00687 std::cerr << key;
00688 if (number >= 0)
00689 std::cerr << " " << number;
00690 std::cerr << "' expected !";
00691 break;
00692 case NO_OPEN_BRACKET:
00693 std::cerr << "\nread(in) parse error : '[' expected before";
00694 std::cerr << key;
00695 if (number >= 0)
00696 std::cerr << " " << number;
00697 std::cerr << " argument !";
00698 break;
00699 case NO_SEPERATOR:
00700 std::cerr << "\nread(in) parse error : ', ' expected ";
00701 std::cerr << " between components of " << key;
00702 if (number >= 0)
00703 std::cerr << " " << number;
00704 std::cerr << " argument !";
00705 break;
00706 case NO_CLOSED_BRACKET:
00707 std::cerr << "\nread(in) parse error : ']' expected behind";
00708 std::cerr << key;
00709 if (number >= 0)
00710 std::cerr << " " << number;
00711 std::cerr << " argument !";
00712 break;
00713 default:
00714 std::cerr << "\nread(in) : unknown error !?!?!?!?!?!?!?!?!";
00715 break;
00716 };
00717 };
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732 void util_posArgForKeyWithCheck(
00733 std::istream& in,
00734 const char* key,
00735 int number,
00736 char delim )
00737 {
00738 int status;
00739
00740 status = util_posArgForKey(in, key, number, delim);
00741
00742 if(status != OKAY)
00743 util_parseError(status, key);
00744 };
00745
00746 #endif