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