csvfile.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. //---------------------------------------------------------------------------
  2. //
  3. // csvfile.cpp - This file contains the class declaration for the CSV Files
  4. //
  5. // The CSV file is an Excel csv style file.
  6. //
  7. // MechCommander 2
  8. //
  9. //---------------------------------------------------------------------------//
  10. // Copyright (C) Microsoft Corporation. All rights reserved. //
  11. //===========================================================================//
  12. //---------------------------------------------------------------------------
  13. // Include files
  14. #ifndef CSVFILE_H
  15. #include "csvfile.h"
  16. #endif
  17. #ifndef HEAP_H
  18. #include "heap.H"
  19. #endif
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #ifndef _MBCS
  26. #include <gameos.hpp>
  27. #else
  28. #include <assert.h>
  29. #define gosASSERT assert
  30. #define gos_Malloc malloc
  31. #define gos_Free free
  32. #endif
  33. //---------------------------------------------------------------------------
  34. // Static Globals
  35. #undef isspace //Macro Chokes under Intel Compiler!!
  36. //---------------------------------------------------------------------------
  37. // class CSVIniFile
  38. CSVFile::CSVFile (void) : File()
  39. {
  40. totalRows = totalCols = 0L;
  41. }
  42. //---------------------------------------------------------------------------
  43. CSVFile::~CSVFile (void)
  44. {
  45. close();
  46. }
  47. //---------------------------------------------------------------------------
  48. long CSVFile::countRows (void)
  49. {
  50. long count = 0;
  51. long oldPosition = logicalPosition;
  52. seek(0); //Start at the top.
  53. char tmp[2048];
  54. readLine((MemoryPtr)tmp,2047);
  55. while (!eof())
  56. {
  57. count++;
  58. readLine((MemoryPtr)tmp,2047);
  59. }
  60. //----------------------------------
  61. // Move back to where we were.
  62. seek(oldPosition);
  63. return(count);
  64. }
  65. //---------------------------------------------------------------------------
  66. long CSVFile::countCols (void)
  67. {
  68. long count = 0, maxCols = 0;
  69. long oldPosition = logicalPosition;
  70. seek(0); //Start at the top.
  71. char tmp[2048];
  72. char *currentChk = tmp;
  73. readLine((MemoryPtr)tmp,2047);
  74. currentChk = strstr(tmp,",");
  75. while (currentChk && (*currentChk != '\n') && (*currentChk != '\r'))
  76. {
  77. count++;
  78. currentChk++;
  79. currentChk = strstr(currentChk,",");
  80. }
  81. if (count > maxCols)
  82. maxCols = count;
  83. readLine((MemoryPtr)tmp,2047);
  84. //----------------------------------
  85. // Move back to where we were.
  86. seek(oldPosition);
  87. return(maxCols);
  88. }
  89. //---------------------------------------------------------------------------
  90. long CSVFile::getNextWord (char *&line, char *buffer, unsigned long bufLen)
  91. {
  92. //--------------------------------------------------
  93. // Check to see if we are at end of line
  94. if (*line == '\0')
  95. return(-1);
  96. //--------------------------------------------------
  97. // Check to see if the rest of the line is comments
  98. if (*line == '/')
  99. return(-1);
  100. if ( *line == ',' ) // empty column, move on
  101. return -1;
  102. //------------------------------------------
  103. // Find start of word from current location
  104. while ((*line != '\0') && ((*line == ' ') || (*line == '\t') || (*line == ',')))
  105. {
  106. line++;
  107. }
  108. //--------------------------------------------------
  109. // Check to see if we are at end of line
  110. if (*line == '\0')
  111. return(-1);
  112. //--------------------------------------------------
  113. // Check to see if the rest of the line is comments
  114. if (*line == '/')
  115. return(-1);
  116. //-------------------------------------------
  117. // Find length of word from current location
  118. char *startOfWord = line;
  119. unsigned long wordLength = 0;
  120. while ((*line != '\0') && ((*line != ',')))
  121. {
  122. line++;
  123. wordLength++;
  124. }
  125. if (wordLength > bufLen)
  126. return(-2);
  127. strncpy(buffer, startOfWord, wordLength);
  128. buffer[wordLength] = '\0';
  129. return(NO_ERR);
  130. }
  131. //---------------------------------------------------------------------------
  132. long CSVFile::afterOpen (void)
  133. {
  134. //-------------------------------------------------------
  135. // Check if we opened this with CREATE and write the
  136. // FITini Header and position to Write Start.
  137. if (fileMode == CREATE && parent == NULL)
  138. {
  139. STOP(("Cannot write CSV files at present."));
  140. }
  141. else
  142. {
  143. //------------------------------------------------------
  144. // Find out how many Rows and cols we have
  145. totalRows = countRows();
  146. totalCols = countCols();
  147. }
  148. return(NO_ERR);
  149. }
  150. //---------------------------------------------------------------------------
  151. void CSVFile::atClose (void)
  152. {
  153. //------------------------------------------------------------
  154. // Check if we are in create mode and if so, write the footer
  155. if (fileMode == CREATE)
  156. {
  157. STOP(("Cannot write CSV files at present."));
  158. }
  159. totalRows = totalCols = 0;
  160. }
  161. //---------------------------------------------------------------------------
  162. float CSVFile::textToFloat (char *num)
  163. {
  164. float result = atof(num);
  165. return(result);
  166. }
  167. //---------------------------------------------------------------------------
  168. long CSVFile::textToLong (char *num)
  169. {
  170. long result = 0;
  171. //------------------------------------
  172. // Check if Hex Number
  173. char *hexOffset = strstr(num,"0x");
  174. if (hexOffset == NULL)
  175. {
  176. result = atol(num);
  177. }
  178. else
  179. {
  180. hexOffset += 2;
  181. long numDigits = strlen(hexOffset)-1;
  182. for (int i=0; i<=numDigits; i++)
  183. {
  184. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  185. {
  186. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  187. break;
  188. }
  189. }
  190. numDigits = strlen(hexOffset)-1;
  191. long power = 0;
  192. for (long count = numDigits;count >= 0;count--,power++)
  193. {
  194. unsigned char currentDigit = toupper(hexOffset[count]);
  195. if (currentDigit >= 'A' && currentDigit <= 'F')
  196. {
  197. result += (currentDigit - 'A' + 10)<<(4*power);
  198. }
  199. else if (currentDigit >= '0' && currentDigit <= '9')
  200. {
  201. result += (currentDigit - '0')<<(4*power);
  202. }
  203. else
  204. {
  205. //---------------------------------------------------------
  206. // There is a digit in here I don't understand. Return 0.
  207. result = 0;
  208. break;
  209. }
  210. }
  211. }
  212. return(result);
  213. }
  214. //---------------------------------------------------------------------------
  215. short CSVFile::textToShort (char *num)
  216. {
  217. short result = 0;
  218. //------------------------------------
  219. // Check if Hex Number
  220. char *hexOffset = strstr(num,"0x");
  221. if (hexOffset == NULL)
  222. {
  223. result = atol(num);
  224. }
  225. else
  226. {
  227. hexOffset += 2;
  228. long numDigits = strlen(hexOffset)-1;
  229. for (int i=0; i<=numDigits; i++)
  230. {
  231. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  232. {
  233. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  234. break;
  235. }
  236. }
  237. numDigits = strlen(hexOffset)-1;
  238. long power = 0;
  239. for (long count = numDigits;count >= 0;count--,power++)
  240. {
  241. unsigned char currentDigit = toupper(hexOffset[count]);
  242. if (currentDigit >= 'A' && currentDigit <= 'F')
  243. {
  244. result += (currentDigit - 'A' + 10)<<(4*power);
  245. }
  246. else if (currentDigit >= '0' && currentDigit <= '9')
  247. {
  248. result += (currentDigit - '0')<<(4*power);
  249. }
  250. else
  251. {
  252. //---------------------------------------------------------
  253. // There is a digit in here I don't understand. Return 0.
  254. result = 0;
  255. break;
  256. }
  257. }
  258. }
  259. return(result);
  260. }
  261. //---------------------------------------------------------------------------
  262. char CSVFile::textToChar (char *num)
  263. {
  264. char result = 0;
  265. //------------------------------------
  266. // Check if Hex Number
  267. char *hexOffset = strstr(num,"0x");
  268. if (hexOffset == NULL)
  269. {
  270. result = atol(num);
  271. }
  272. else
  273. {
  274. hexOffset += 2;
  275. long numDigits = strlen(hexOffset)-1;
  276. for (int i=0; i<=numDigits; i++)
  277. {
  278. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  279. {
  280. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  281. break;
  282. }
  283. }
  284. numDigits = strlen(hexOffset)-1;
  285. long power = 0;
  286. for (long count = numDigits;count >= 0;count--,power++)
  287. {
  288. unsigned char currentDigit = toupper(hexOffset[count]);
  289. if (currentDigit >= 'A' && currentDigit <= 'F')
  290. {
  291. result += (currentDigit - 'A' + 10)<<(4*power);
  292. }
  293. else if (currentDigit >= '0' && currentDigit <= '9')
  294. {
  295. result += (currentDigit - '0')<<(4*power);
  296. }
  297. else
  298. {
  299. //---------------------------------------------------------
  300. // There is a digit in here I don't understand. Return 0.
  301. result = 0;
  302. break;
  303. }
  304. }
  305. }
  306. return(result);
  307. }
  308. //---------------------------------------------------------------------------
  309. unsigned long CSVFile::textToULong (char *num)
  310. {
  311. unsigned long result = 0;
  312. //------------------------------------
  313. // Check if Hex Number
  314. char *hexOffset = strstr(num,"0x");
  315. if (hexOffset == NULL)
  316. {
  317. result = atol(num);
  318. }
  319. else
  320. {
  321. hexOffset += 2;
  322. long numDigits = strlen(hexOffset)-1;
  323. for (int i=0; i<=numDigits; i++)
  324. {
  325. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  326. {
  327. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  328. break;
  329. }
  330. }
  331. numDigits = strlen(hexOffset)-1;
  332. long power = 0;
  333. for (long count = numDigits;count >= 0;count--,power++)
  334. {
  335. unsigned char currentDigit = toupper(hexOffset[count]);
  336. if (currentDigit >= 'A' && currentDigit <= 'F')
  337. {
  338. result += (currentDigit - 'A' + 10)<<(4*power);
  339. }
  340. else if (currentDigit >= '0' && currentDigit <= '9')
  341. {
  342. result += (currentDigit - '0')<<(4*power);
  343. }
  344. else
  345. {
  346. //---------------------------------------------------------
  347. // There is a digit in here I don't understand. Return 0.
  348. result = 0;
  349. break;
  350. }
  351. }
  352. }
  353. return(result);
  354. }
  355. //---------------------------------------------------------------------------
  356. unsigned short CSVFile::textToUShort (char *num)
  357. {
  358. unsigned short result = 0;
  359. //------------------------------------
  360. // Check if Hex Number
  361. char *hexOffset = strstr(num,"0x");
  362. if (hexOffset == NULL)
  363. {
  364. result = atol(num);
  365. }
  366. else
  367. {
  368. hexOffset += 2;
  369. long numDigits = strlen(hexOffset)-1;
  370. for (int i=0; i<=numDigits; i++)
  371. {
  372. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  373. {
  374. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  375. break;
  376. }
  377. }
  378. numDigits = strlen(hexOffset)-1;
  379. long power = 0;
  380. for (long count = numDigits;count >= 0;count--,power++)
  381. {
  382. unsigned char currentDigit = toupper(hexOffset[count]);
  383. if (currentDigit >= 'A' && currentDigit <= 'F')
  384. {
  385. result += (currentDigit - 'A' + 10)<<(4*power);
  386. }
  387. else if (currentDigit >= '0' && currentDigit <= '9')
  388. {
  389. result += (currentDigit - '0')<<(4*power);
  390. }
  391. else
  392. {
  393. //---------------------------------------------------------
  394. // There is a digit in here I don't understand. Return 0.
  395. result = 0;
  396. break;
  397. }
  398. }
  399. }
  400. return(result);
  401. }
  402. //---------------------------------------------------------------------------
  403. unsigned char CSVFile::textToUChar (char *num)
  404. {
  405. unsigned char result = 0;
  406. //------------------------------------
  407. // Check if Hex Number
  408. char *hexOffset = strstr(num,"0x");
  409. if (hexOffset == NULL)
  410. {
  411. result = atol(num);
  412. }
  413. else
  414. {
  415. hexOffset += 2;
  416. long numDigits = strlen(hexOffset)-1;
  417. for (int i=0; i<=numDigits; i++)
  418. {
  419. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  420. {
  421. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  422. break;
  423. }
  424. }
  425. numDigits = strlen(hexOffset)-1;
  426. long power = 0;
  427. for (long count = numDigits;count >= 0;count--,power++)
  428. {
  429. unsigned char currentDigit = toupper(hexOffset[count]);
  430. if (currentDigit >= 'A' && currentDigit <= 'F')
  431. {
  432. result += (currentDigit - 'A' + 10)<<(4*power);
  433. }
  434. else if (currentDigit >= '0' && currentDigit <= '9')
  435. {
  436. result += (currentDigit - '0')<<(4*power);
  437. }
  438. else
  439. {
  440. //---------------------------------------------------------
  441. // There is a digit in here I don't understand. Return 0.
  442. result = 0;
  443. break;
  444. }
  445. }
  446. }
  447. return(result);
  448. }
  449. //---------------------------------------------------------------------------
  450. bool CSVFile::booleanToLong (char *num)
  451. {
  452. char testChar = 0;
  453. while (num[testChar] && isspace(num[testChar]))
  454. testChar++;
  455. // 'N' == NO if you can believe that
  456. if ((toupper(num[testChar]) == 'F') || (toupper(num[testChar]) == '0') || (toupper(num[testChar]) == 'N') )
  457. return FALSE;
  458. else
  459. return(TRUE);
  460. }
  461. //---------------------------------------------------------------------------
  462. long CSVFile::floatToText (char *result, float num, unsigned long bufLen)
  463. {
  464. char temp[250];
  465. sprintf(temp,"%f4",num);
  466. unsigned long numLength = strlen(temp);
  467. if (numLength >= bufLen)
  468. return(-2);
  469. strncpy(result,temp,numLength);
  470. result[numLength] = '\0';
  471. return(NO_ERR);
  472. }
  473. //---------------------------------------------------------------------------
  474. long CSVFile::longToTextDec (char *result, long num, unsigned long bufLen)
  475. {
  476. char temp[250];
  477. sprintf(temp,"%d",num);
  478. unsigned long numLength = strlen(temp);
  479. if (numLength >= bufLen)
  480. return(-2);
  481. strncpy(result,temp,numLength);
  482. result[numLength] = '\0';
  483. return(NO_ERR);
  484. }
  485. //---------------------------------------------------------------------------
  486. long CSVFile::longToTextHex (char *result, long num, unsigned long bufLen)
  487. {
  488. char temp[250];
  489. sprintf(temp,"0x%x",num);
  490. unsigned long numLength = strlen(temp);
  491. if (numLength >= bufLen)
  492. return(-2);
  493. strncpy(result,temp,numLength);
  494. result[numLength] = '\0';
  495. return(NO_ERR);
  496. }
  497. //---------------------------------------------------------------------------
  498. long CSVFile::shortToTextDec (char *result, short num, unsigned long bufLen)
  499. {
  500. char temp[250];
  501. sprintf(temp,"%d",num);
  502. unsigned long numLength = strlen(temp);
  503. if (numLength >= bufLen)
  504. return(-2);
  505. strncpy(result,temp,numLength);
  506. result[numLength] = '\0';
  507. return(NO_ERR);
  508. }
  509. //---------------------------------------------------------------------------
  510. long CSVFile::shortToTextHex (char *result, short num, unsigned long bufLen)
  511. {
  512. char temp[250];
  513. sprintf(temp,"0x%x",num);
  514. unsigned long numLength = strlen(temp);
  515. if (numLength >= bufLen)
  516. return(-2);
  517. strncpy(result,temp,numLength);
  518. result[numLength] = '\0';
  519. return(NO_ERR);
  520. }
  521. //---------------------------------------------------------------------------
  522. long CSVFile::byteToTextDec (char *result, byte num, unsigned long bufLen)
  523. {
  524. char temp[250];
  525. sprintf(temp,"%d",num);
  526. unsigned long numLength = strlen(temp);
  527. if (numLength >= bufLen)
  528. return(-2);
  529. strncpy(result,temp,numLength);
  530. result[numLength] = '\0';
  531. return(NO_ERR);
  532. }
  533. //---------------------------------------------------------------------------
  534. long CSVFile::byteToTextHex (char *result, byte num, unsigned long bufLen)
  535. {
  536. char temp[250];
  537. sprintf(temp,"0x%x",num);
  538. unsigned long numLength = strlen(temp);
  539. if (numLength >= bufLen)
  540. return(-2);
  541. strncpy(result,temp,numLength);
  542. result[numLength] = '\0';
  543. return(NO_ERR);
  544. }
  545. //---------------------------------------------------------------------------
  546. long CSVFile::open (const char* fName, FileMode _mode, long numChild)
  547. {
  548. long result = File::open(fName,_mode,numChild);
  549. if (result != NO_ERR)
  550. return(result);
  551. seek(0);
  552. result = afterOpen();
  553. return(result);
  554. }
  555. //---------------------------------------------------------------------------
  556. long CSVFile::open (FilePtr _parent, unsigned long fileSize, long numChild)
  557. {
  558. numChild = -1; //Force all parented CSVs to load from RAM.
  559. long result = File::open(_parent,fileSize,numChild);
  560. if (result != NO_ERR)
  561. return(result);
  562. result = afterOpen();
  563. return(result);
  564. }
  565. //---------------------------------------------------------------------------
  566. long CSVFile::create (char* fName)
  567. {
  568. fName;
  569. //STOP(("CSV file write is not supported %s",fName));
  570. return(-1);
  571. }
  572. //---------------------------------------------------------------------------
  573. void CSVFile::close (void)
  574. {
  575. if (isOpen())
  576. {
  577. atClose();
  578. File::close();
  579. }
  580. }
  581. //---------------------------------------------------------------------------
  582. long CSVFile::seekRowCol (DWORD row, DWORD col)
  583. {
  584. if ((row > totalRows) || (col > totalCols))
  585. return -1;
  586. DWORD rowCount = 0;
  587. seek(0); //Start at the top.
  588. char tmp[2048];
  589. do
  590. {
  591. rowCount++;
  592. readLine((MemoryPtr)tmp,2047);
  593. } while (rowCount != row);
  594. char *currentChk = tmp;
  595. if (col)
  596. {
  597. DWORD colCount = 1;
  598. while (currentChk && (colCount != col))
  599. {
  600. colCount++;
  601. currentChk = strstr(currentChk,",");
  602. if ( currentChk ) // if we increment to one, bad things happen
  603. currentChk++;
  604. }
  605. }
  606. //---------------------------------------------------
  607. // We are now pointing at the row and col specified.
  608. if (currentChk)
  609. {
  610. char *data = dataBuffer;
  611. return getNextWord(currentChk,data,2047);
  612. }
  613. else
  614. {
  615. return -1;
  616. // CAN'T do this, for some reason excel writes out empty rows.
  617. //STOP(("Unable to parse CSV %s, ROW %d, COL %D, ERROR: NULL",getFilename(),row,col));
  618. }
  619. //return(NO_ERR);
  620. }
  621. //---------------------------------------------------------------------------
  622. long CSVFile::readFloat (DWORD row, DWORD col, float &value)
  623. {
  624. long result = seekRowCol(row,col);
  625. if (result == NO_ERR)
  626. {
  627. value = textToFloat(dataBuffer);
  628. }
  629. else
  630. value = 0.0f;
  631. return(NO_ERR);
  632. }
  633. //---------------------------------------------------------------------------
  634. long CSVFile::readLong (DWORD row, DWORD col, long &value)
  635. {
  636. long result = seekRowCol(row,col);
  637. if (result == NO_ERR)
  638. {
  639. value = textToLong(dataBuffer);
  640. }
  641. else
  642. value = 0.0f;
  643. return(NO_ERR);
  644. }
  645. //---------------------------------------------------------------------------
  646. long CSVFile::readBoolean (DWORD row, DWORD col, bool &value)
  647. {
  648. long result = seekRowCol(row,col);
  649. if (result == NO_ERR)
  650. {
  651. value = booleanToLong(dataBuffer);
  652. }
  653. else
  654. value = 0;
  655. return(NO_ERR);
  656. }
  657. //---------------------------------------------------------------------------
  658. long CSVFile::readShort (DWORD row, DWORD col, short &value)
  659. {
  660. long result = seekRowCol(row,col);
  661. if (result == NO_ERR)
  662. {
  663. value = textToShort(dataBuffer);
  664. }
  665. else
  666. value = 0.0f;
  667. return(NO_ERR);
  668. }
  669. //---------------------------------------------------------------------------
  670. long CSVFile::readChar (DWORD row, DWORD col, char &value)
  671. {
  672. long result = seekRowCol(row,col);
  673. if (result == NO_ERR)
  674. {
  675. value = textToChar(dataBuffer);
  676. }
  677. else
  678. value = 0.0f;
  679. return(NO_ERR);
  680. }
  681. //---------------------------------------------------------------------------
  682. long CSVFile::readULong (DWORD row, DWORD col, unsigned long &value)
  683. {
  684. long result = seekRowCol(row,col);
  685. if (result == NO_ERR)
  686. {
  687. value = textToULong(dataBuffer);
  688. }
  689. else
  690. value = 0.0f;
  691. return(NO_ERR);
  692. }
  693. //---------------------------------------------------------------------------
  694. long CSVFile::readUShort (DWORD row, DWORD col, unsigned short &value)
  695. {
  696. long result = seekRowCol(row,col);
  697. if (result == NO_ERR)
  698. {
  699. value = textToUShort(dataBuffer);
  700. }
  701. else
  702. value = 0.0f;
  703. return(NO_ERR);
  704. }
  705. //---------------------------------------------------------------------------
  706. long CSVFile::readUChar (DWORD row, DWORD col, unsigned char &value)
  707. {
  708. long result = seekRowCol(row,col);
  709. if (result == NO_ERR)
  710. {
  711. value = textToUChar(dataBuffer);
  712. }
  713. else
  714. value = 0.0f;
  715. return(NO_ERR);
  716. }
  717. //---------------------------------------------------------------------------
  718. long CSVFile::copyString (char *dest, char *src, unsigned long bufLen)
  719. {
  720. unsigned long offset = 0;
  721. //---------------------------------------
  722. // Copy each character until close quote
  723. while (*src != '"' && *src != '\0' && offset < bufLen)
  724. {
  725. dest[offset] = *src;
  726. src++;
  727. offset++;
  728. }
  729. //----------------------------------------------------
  730. // If this string is longer than buffer, let em know.
  731. if (offset == bufLen)
  732. {
  733. return(-2);
  734. }
  735. //---------------------------------
  736. // otherwise, NULL term and return
  737. dest[offset] = '\0';
  738. return(NO_ERR);
  739. }
  740. //---------------------------------------------------------------------------
  741. long CSVFile::readString (DWORD row, DWORD col, char *result, unsigned long bufferSize)
  742. {
  743. long res = seekRowCol(row,col);
  744. if (res == NO_ERR)
  745. {
  746. long errorCode = copyString(result,dataBuffer,bufferSize);
  747. if (errorCode != NO_ERR)
  748. return(errorCode);
  749. }
  750. else
  751. return 1;
  752. return(0); // gotta return some kind of error!
  753. }
  754. //---------------------------------------------------------------------------
  755. //
  756. // Edit log
  757. //
  758. //---------------------------------------------------------------------------