Inifile.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  1. //---------------------------------------------------------------------------
  2. //
  3. // inifile.cpp - This file contains the class declaration for the FitIni Files
  4. //
  5. // The FitIni file is a ini style file.
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. //---------------------------------------------------------------------------
  11. // Include files
  12. #ifndef INIFILE_H
  13. #include "inifile.h"
  14. #endif
  15. #ifndef HEAP_H
  16. #include "heap.H"
  17. #endif
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include "err.h"
  24. #ifndef _MBCS
  25. #include <gameos.hpp>
  26. #else
  27. #include <assert.h>
  28. #define gosASSERT assert
  29. #define gos_Malloc malloc
  30. #define gos_Free free
  31. #endif
  32. //---------------------------------------------------------------------------
  33. // Static Globals
  34. char fitIniHeader[7] = "FITini";
  35. char fitIniFooter[7] = "FITend";
  36. #undef isspace //Macro Chokes under Intel Compiler!!
  37. //---------------------------------------------------------------------------
  38. // class FitIniFile
  39. FitIniFile::FitIniFile (void) : File()
  40. {
  41. totalBlocks = 0;
  42. fileBlocks = NULL;
  43. currentBlockId = NULL;
  44. currentBlockOffset = 0;
  45. currentBlockSize = 0;
  46. }
  47. //---------------------------------------------------------------------------
  48. FitIniFile::~FitIniFile (void)
  49. {
  50. close();
  51. }
  52. //---------------------------------------------------------------------------
  53. long FitIniFile::findNextBlockStart (char *line, unsigned long lineLen)
  54. {
  55. char thisLine[255];
  56. char *common = NULL;
  57. do
  58. {
  59. if (line)
  60. {
  61. readLine((MemoryPtr)line,lineLen);
  62. common = line;
  63. }
  64. else
  65. {
  66. readLine((MemoryPtr)thisLine,254);
  67. common = thisLine;
  68. }
  69. }
  70. while(!eof() && (common[0] != '['));
  71. if (eof())
  72. {
  73. return(NO_MORE_BLOCKS);
  74. }
  75. return(NO_ERR);
  76. }
  77. //---------------------------------------------------------------------------
  78. long FitIniFile::countBlocks (void)
  79. {
  80. long count = 0;
  81. long oldPosition = logicalPosition;
  82. while (findNextBlockStart() != NO_MORE_BLOCKS)
  83. {
  84. count++;
  85. }
  86. //----------------------------------
  87. // Move back to where we were.
  88. seek(oldPosition);
  89. return(count);
  90. }
  91. //---------------------------------------------------------------------------
  92. long FitIniFile::getNextWord (char *&line, char *buffer, unsigned long bufLen)
  93. {
  94. //--------------------------------------------------
  95. // Check to see if we are at end of line
  96. if (*line == '\0')
  97. return(GET_NEXT_LINE);
  98. //--------------------------------------------------
  99. // Check to see if the rest of the line is comments
  100. if (*line == '/')
  101. return(GET_NEXT_LINE);
  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(GET_NEXT_LINE);
  112. //--------------------------------------------------
  113. // Check to see if the rest of the line is comments
  114. if (*line == '/')
  115. return(GET_NEXT_LINE);
  116. //-------------------------------------------
  117. // Find length of word from current location
  118. char *startOfWord = line;
  119. unsigned long wordLength = 0;
  120. while ((*line != '\0') && ((*line != ' ') && (*line != '\t') && (*line != ',')))
  121. {
  122. line++;
  123. wordLength++;
  124. }
  125. if (wordLength > bufLen)
  126. return(BUFFER_TOO_SMALL);
  127. strncpy(buffer, startOfWord, wordLength);
  128. buffer[wordLength] = '\0';
  129. return(NO_ERR);
  130. }
  131. //---------------------------------------------------------------------------
  132. long FitIniFile::afterOpen (void)
  133. {
  134. char line[255];
  135. //-------------------------------------------------------
  136. // Check if we opened this with CREATE and write the
  137. // FITini Header and position to Write Start.
  138. if (fileMode == CREATE && parent == NULL)
  139. {
  140. sprintf(line,"%s \r\n",fitIniHeader);
  141. write((MemoryPtr)line,strlen(line));
  142. totalBlocks = 0;
  143. }
  144. else
  145. {
  146. //------------------------------------------------------
  147. // Check if this is a FitIniFile by looking for header.
  148. char chkHeader[12];
  149. readLine((MemoryPtr)chkHeader,11);
  150. if (strstr(chkHeader,fitIniHeader) == NULL)
  151. return(NOT_A_FITINIFILE);
  152. //------------------------------------------------------
  153. // Find out how many blocks we have
  154. totalBlocks = countBlocks();
  155. //--------------------------------------------------------------------------
  156. // Allocate RAM for the BlockInfoNodes. Check if system Heap is available
  157. fileBlocks = (IniBlockNode *)systemHeap->Malloc(sizeof(IniBlockNode) * totalBlocks);
  158. gosASSERT(fileBlocks != NULL);
  159. memset(fileBlocks,0,sizeof(IniBlockNode) * totalBlocks);
  160. //--------------------------------------------------------------------------
  161. // Put Info into fileBlocks.
  162. unsigned long currentBlockNum = 0;
  163. while (findNextBlockStart(line,254) != NO_MORE_BLOCKS)
  164. {
  165. //----------------------------------------------------
  166. // If we write too many fileBlocks, we will trash RAM
  167. // Shouldn't be able to happen but...
  168. if (currentBlockNum == totalBlocks)
  169. return(TOO_MANY_BLOCKS);
  170. long count = 1;
  171. while (line[count] != ']' && line[count] != '\n')
  172. {
  173. fileBlocks[currentBlockNum].blockId[count-1] = line[count];
  174. count++;
  175. }
  176. if (count >= 49)
  177. STOP(("BlockId To large in Fit File %s",fileName));
  178. if (line[count] == '\n')
  179. {
  180. char error[256];
  181. sprintf( error, "couldn't resolve block %s in file %s", line, getFilename() );
  182. Assert( 0, 0, error );
  183. return SYNTAX_ERROR;
  184. }
  185. fileBlocks[currentBlockNum].blockId[count-1] = '\0';
  186. //----------------------------------------------------------------------
  187. // Since we just read all of last line, we now point to start of data
  188. fileBlocks[currentBlockNum].blockOffset = logicalPosition;
  189. currentBlockNum++;
  190. }
  191. //------------------------------------------------------
  192. // If we didn't read in enough, CD-ROM error?
  193. if (currentBlockNum != totalBlocks)
  194. return(NOT_ENOUGH_BLOCKS);
  195. }
  196. return(NO_ERR);
  197. }
  198. //---------------------------------------------------------------------------
  199. void FitIniFile::atClose (void)
  200. {
  201. //------------------------------------------------------------
  202. // Check if we are in create mode and if so, write the footer
  203. char line[200];
  204. if (fileMode == CREATE)
  205. {
  206. seek(0,SEEK_END);
  207. sprintf(line,"%s \r\n",fitIniFooter);
  208. write((MemoryPtr)line,strlen(line));
  209. }
  210. //-----------------------------
  211. // Free up the fileBlocks
  212. systemHeap->Free(fileBlocks);
  213. fileBlocks = NULL;
  214. }
  215. //---------------------------------------------------------------------------
  216. float FitIniFile::textToFloat (const char *num)
  217. {
  218. float result = atof(num);
  219. return(result);
  220. }
  221. //---------------------------------------------------------------------------
  222. double FitIniFile::textToDouble (const char *num)
  223. {
  224. double result = atof(num);
  225. return(result);
  226. }
  227. //---------------------------------------------------------------------------
  228. long FitIniFile::textToLong (const char *num)
  229. {
  230. long result = 0;
  231. //------------------------------------
  232. // Check if Hex Number
  233. char *hexOffset = (char *)strstr(num,"0x");
  234. if (hexOffset == NULL)
  235. {
  236. result = atol(num);
  237. }
  238. else
  239. {
  240. hexOffset += 2;
  241. long numDigits = strlen(hexOffset)-1;
  242. for (int i=0; i<=numDigits; i++)
  243. {
  244. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  245. {
  246. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  247. break;
  248. }
  249. }
  250. numDigits = strlen(hexOffset)-1;
  251. long power = 0;
  252. for (long count = numDigits;count >= 0;count--,power++)
  253. {
  254. unsigned char currentDigit = toupper(hexOffset[count]);
  255. if (currentDigit >= 'A' && currentDigit <= 'F')
  256. {
  257. result += (currentDigit - 'A' + 10)<<(4*power);
  258. }
  259. else if (currentDigit >= '0' && currentDigit <= '9')
  260. {
  261. result += (currentDigit - '0')<<(4*power);
  262. }
  263. else
  264. {
  265. //---------------------------------------------------------
  266. // There is a digit in here I don't understand. Return 0.
  267. result = 0;
  268. break;
  269. }
  270. }
  271. }
  272. return(result);
  273. }
  274. //---------------------------------------------------------------------------
  275. short FitIniFile::textToShort (const char *num)
  276. {
  277. short result = 0;
  278. //------------------------------------
  279. // Check if Hex Number
  280. char *hexOffset = (char *)strstr(num,"0x");
  281. if (hexOffset == NULL)
  282. {
  283. result = atol(num);
  284. }
  285. else
  286. {
  287. hexOffset += 2;
  288. long numDigits = strlen(hexOffset)-1;
  289. for (int i=0; i<=numDigits; i++)
  290. {
  291. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  292. {
  293. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  294. break;
  295. }
  296. }
  297. numDigits = strlen(hexOffset)-1;
  298. long power = 0;
  299. for (long count = numDigits;count >= 0;count--,power++)
  300. {
  301. unsigned char currentDigit = toupper(hexOffset[count]);
  302. if (currentDigit >= 'A' && currentDigit <= 'F')
  303. {
  304. result += (currentDigit - 'A' + 10)<<(4*power);
  305. }
  306. else if (currentDigit >= '0' && currentDigit <= '9')
  307. {
  308. result += (currentDigit - '0')<<(4*power);
  309. }
  310. else
  311. {
  312. //---------------------------------------------------------
  313. // There is a digit in here I don't understand. Return 0.
  314. result = 0;
  315. break;
  316. }
  317. }
  318. }
  319. return(result);
  320. }
  321. //---------------------------------------------------------------------------
  322. char FitIniFile::textToChar (const char *num)
  323. {
  324. char result = 0;
  325. //------------------------------------
  326. // Check if Hex Number
  327. char *hexOffset = (char *)strstr(num,"0x");
  328. if (hexOffset == NULL)
  329. {
  330. result = atol(num);
  331. }
  332. else
  333. {
  334. hexOffset += 2;
  335. long numDigits = strlen(hexOffset)-1;
  336. for (int i=0; i<=numDigits; i++)
  337. {
  338. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  339. {
  340. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  341. break;
  342. }
  343. }
  344. numDigits = strlen(hexOffset)-1;
  345. long power = 0;
  346. for (long count = numDigits;count >= 0;count--,power++)
  347. {
  348. unsigned char currentDigit = toupper(hexOffset[count]);
  349. if (currentDigit >= 'A' && currentDigit <= 'F')
  350. {
  351. result += (currentDigit - 'A' + 10)<<(4*power);
  352. }
  353. else if (currentDigit >= '0' && currentDigit <= '9')
  354. {
  355. result += (currentDigit - '0')<<(4*power);
  356. }
  357. else
  358. {
  359. //---------------------------------------------------------
  360. // There is a digit in here I don't understand. Return 0.
  361. result = 0;
  362. break;
  363. }
  364. }
  365. }
  366. return(result);
  367. }
  368. //---------------------------------------------------------------------------
  369. unsigned long FitIniFile::textToULong (const char *num)
  370. {
  371. unsigned long result = 0;
  372. //------------------------------------
  373. // Check if Hex Number
  374. char *hexOffset = (char *)strstr(num,"0x");
  375. if (hexOffset == NULL)
  376. {
  377. result = atol(num);
  378. }
  379. else
  380. {
  381. hexOffset += 2;
  382. long numDigits = strlen(hexOffset)-1;
  383. for (int i=0; i<=numDigits; i++)
  384. {
  385. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  386. {
  387. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  388. break;
  389. }
  390. }
  391. numDigits = strlen(hexOffset)-1;
  392. long power = 0;
  393. for (long count = numDigits;count >= 0;count--,power++)
  394. {
  395. unsigned char currentDigit = toupper(hexOffset[count]);
  396. if (currentDigit >= 'A' && currentDigit <= 'F')
  397. {
  398. result += (currentDigit - 'A' + 10)<<(4*power);
  399. }
  400. else if (currentDigit >= '0' && currentDigit <= '9')
  401. {
  402. result += (currentDigit - '0')<<(4*power);
  403. }
  404. else
  405. {
  406. //---------------------------------------------------------
  407. // There is a digit in here I don't understand. Return 0.
  408. result = 0;
  409. break;
  410. }
  411. }
  412. }
  413. return(result);
  414. }
  415. //---------------------------------------------------------------------------
  416. unsigned short FitIniFile::textToUShort (const char *num)
  417. {
  418. unsigned short result = 0;
  419. //------------------------------------
  420. // Check if Hex Number
  421. char *hexOffset = (char *)strstr(num,"0x");
  422. if (hexOffset == NULL)
  423. {
  424. result = atol(num);
  425. }
  426. else
  427. {
  428. hexOffset += 2;
  429. long numDigits = strlen(hexOffset)-1;
  430. for (int i=0; i<=numDigits; i++)
  431. {
  432. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  433. {
  434. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  435. break;
  436. }
  437. }
  438. numDigits = strlen(hexOffset)-1;
  439. long power = 0;
  440. for (long count = numDigits;count >= 0;count--,power++)
  441. {
  442. unsigned char currentDigit = toupper(hexOffset[count]);
  443. if (currentDigit >= 'A' && currentDigit <= 'F')
  444. {
  445. result += (currentDigit - 'A' + 10)<<(4*power);
  446. }
  447. else if (currentDigit >= '0' && currentDigit <= '9')
  448. {
  449. result += (currentDigit - '0')<<(4*power);
  450. }
  451. else
  452. {
  453. //---------------------------------------------------------
  454. // There is a digit in here I don't understand. Return 0.
  455. result = 0;
  456. break;
  457. }
  458. }
  459. }
  460. return(result);
  461. }
  462. //---------------------------------------------------------------------------
  463. unsigned char FitIniFile::textToUChar (const char *num)
  464. {
  465. unsigned char result = 0;
  466. //------------------------------------
  467. // Check if Hex Number
  468. char *hexOffset = (char *)strstr(num,"0x");
  469. if (hexOffset == NULL)
  470. {
  471. result = atol(num);
  472. }
  473. else
  474. {
  475. hexOffset += 2;
  476. long numDigits = strlen(hexOffset)-1;
  477. for (int i=0; i<=numDigits; i++)
  478. {
  479. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  480. {
  481. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  482. break;
  483. }
  484. }
  485. numDigits = strlen(hexOffset)-1;
  486. long power = 0;
  487. for (long count = numDigits;count >= 0;count--,power++)
  488. {
  489. unsigned char currentDigit = toupper(hexOffset[count]);
  490. if (currentDigit >= 'A' && currentDigit <= 'F')
  491. {
  492. result += (currentDigit - 'A' + 10)<<(4*power);
  493. }
  494. else if (currentDigit >= '0' && currentDigit <= '9')
  495. {
  496. result += (currentDigit - '0')<<(4*power);
  497. }
  498. else
  499. {
  500. //---------------------------------------------------------
  501. // There is a digit in here I don't understand. Return 0.
  502. result = 0;
  503. break;
  504. }
  505. }
  506. }
  507. return(result);
  508. }
  509. //---------------------------------------------------------------------------
  510. bool FitIniFile::booleanToLong (const char *num)
  511. {
  512. char testChar = 0;
  513. while (num[testChar] && isspace(num[testChar]))
  514. testChar++;
  515. if ((toupper(num[testChar]) == 'F') || (toupper(num[testChar]) == '0'))
  516. return FALSE;
  517. else
  518. return(TRUE);
  519. }
  520. //---------------------------------------------------------------------------
  521. long FitIniFile::floatToText (char *result, float num, unsigned long bufLen)
  522. {
  523. char temp[250];
  524. sprintf(temp,"%f4",num);
  525. unsigned long numLength = strlen(temp);
  526. if (numLength >= bufLen)
  527. return(BUFFER_TOO_SMALL);
  528. strncpy(result,temp,numLength);
  529. result[numLength] = '\0';
  530. return(NO_ERR);
  531. }
  532. //---------------------------------------------------------------------------
  533. long FitIniFile::longToTextDec (char *result, long num, unsigned long bufLen)
  534. {
  535. char temp[250];
  536. sprintf(temp,"%d",num);
  537. unsigned long numLength = strlen(temp);
  538. if (numLength >= bufLen)
  539. return(BUFFER_TOO_SMALL);
  540. strncpy(result,temp,numLength);
  541. result[numLength] = '\0';
  542. return(NO_ERR);
  543. }
  544. //---------------------------------------------------------------------------
  545. long FitIniFile::longToTextHex (char *result, long num, unsigned long bufLen)
  546. {
  547. char temp[250];
  548. sprintf(temp,"0x%x",num);
  549. unsigned long numLength = strlen(temp);
  550. if (numLength >= bufLen)
  551. return(BUFFER_TOO_SMALL);
  552. strncpy(result,temp,numLength);
  553. result[numLength] = '\0';
  554. return(NO_ERR);
  555. }
  556. //---------------------------------------------------------------------------
  557. long FitIniFile::shortToTextDec (char *result, short num, unsigned long bufLen)
  558. {
  559. char temp[250];
  560. sprintf(temp,"%d",num);
  561. unsigned long numLength = strlen(temp);
  562. if (numLength >= bufLen)
  563. return(BUFFER_TOO_SMALL);
  564. strncpy(result,temp,numLength);
  565. result[numLength] = '\0';
  566. return(NO_ERR);
  567. }
  568. //---------------------------------------------------------------------------
  569. long FitIniFile::shortToTextHex (char *result, short num, unsigned long bufLen)
  570. {
  571. char temp[250];
  572. sprintf(temp,"0x%x",num);
  573. unsigned long numLength = strlen(temp);
  574. if (numLength >= bufLen)
  575. return(BUFFER_TOO_SMALL);
  576. strncpy(result,temp,numLength);
  577. result[numLength] = '\0';
  578. return(NO_ERR);
  579. }
  580. //---------------------------------------------------------------------------
  581. long FitIniFile::byteToTextDec (char *result, byte num, unsigned long bufLen)
  582. {
  583. char temp[250];
  584. sprintf(temp,"%d",num);
  585. unsigned long numLength = strlen(temp);
  586. if (numLength >= bufLen)
  587. return(BUFFER_TOO_SMALL);
  588. strncpy(result,temp,numLength);
  589. result[numLength] = '\0';
  590. return(NO_ERR);
  591. }
  592. //---------------------------------------------------------------------------
  593. long FitIniFile::byteToTextHex (char *result, byte num, unsigned long bufLen)
  594. {
  595. char temp[250];
  596. sprintf(temp,"0x%x",num);
  597. unsigned long numLength = strlen(temp);
  598. if (numLength >= bufLen)
  599. return(BUFFER_TOO_SMALL);
  600. strncpy(result,temp,numLength);
  601. result[numLength] = '\0';
  602. return(NO_ERR);
  603. }
  604. //---------------------------------------------------------------------------
  605. long FitIniFile::open (const char* fName, FileMode _mode, long numChild)
  606. {
  607. long result = File::open(fName,_mode,numChild);
  608. if (result != NO_ERR)
  609. return(result);
  610. seek(0);
  611. result = afterOpen();
  612. return(result);
  613. }
  614. //---------------------------------------------------------------------------
  615. long FitIniFile::open (FilePtr _parent, unsigned long fileSize, long numChild)
  616. {
  617. numChild = -1; //Force all parented FitINIs to load from RAM.
  618. long result = File::open(_parent,fileSize,numChild);
  619. if (result != NO_ERR)
  620. return(result);
  621. result = afterOpen();
  622. return(result);
  623. }
  624. //---------------------------------------------------------------------------
  625. long FitIniFile::create (char* fName)
  626. {
  627. long result = File::create(fName);
  628. afterOpen();
  629. return(result);
  630. }
  631. long FitIniFile::createWithCase( char* fName )
  632. {
  633. long result = File::createWithCase( fName );
  634. afterOpen();
  635. return result;
  636. }
  637. //---------------------------------------------------------------------------
  638. void FitIniFile::close (void)
  639. {
  640. if (isOpen())
  641. {
  642. atClose();
  643. File::close();
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. long FitIniFile::seekBlock (const char *blockId)
  648. {
  649. unsigned long blockNum = 0;
  650. while ((blockNum < totalBlocks) && (strcmp(fileBlocks[blockNum].blockId,blockId) != 0))
  651. {
  652. blockNum++;
  653. }
  654. if (blockNum == totalBlocks)
  655. {
  656. return(BLOCK_NOT_FOUND);
  657. }
  658. //----------------------------------------
  659. // Block was found, seek to that position
  660. seek(fileBlocks[blockNum].blockOffset);
  661. //----------------------------------------
  662. // Setup all current Block Info
  663. currentBlockId = fileBlocks[blockNum].blockId;
  664. currentBlockOffset = fileBlocks[blockNum].blockOffset;
  665. blockNum++;
  666. if (blockNum == totalBlocks)
  667. {
  668. currentBlockSize = getLength() - currentBlockOffset;
  669. }
  670. else
  671. {
  672. currentBlockSize = fileBlocks[blockNum].blockOffset - currentBlockOffset;
  673. }
  674. return(NO_ERR);
  675. }
  676. //---------------------------------------------------------------------------
  677. long FitIniFile::readIdFloat (const char *varName, float &value)
  678. {
  679. char line[255];
  680. char searchString[255];
  681. //--------------------------------
  682. // Always read from top of Block.
  683. seek(currentBlockOffset);
  684. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  685. //------------------------
  686. // Put prefix on varName.
  687. sprintf(searchString,"f %s",varName);
  688. //--------------------------------
  689. // Search line by line for varName
  690. int testy;
  691. do
  692. {
  693. readLine((MemoryPtr)line,254);
  694. testy = strnicmp(line, searchString, strlen(searchString));
  695. if (testy == 0)
  696. {
  697. char* tc = &line[strlen(searchString)];
  698. while (isspace(*tc))
  699. tc++;
  700. if (*tc != '=')
  701. testy = 1;
  702. }
  703. }
  704. while((testy != 0) && (logicalPosition < endOfBlock));
  705. if (logicalPosition >= endOfBlock)
  706. {
  707. value = 0.0;
  708. return(VARIABLE_NOT_FOUND);
  709. }
  710. char *equalSign = strstr(line, "=");
  711. if (equalSign)
  712. {
  713. equalSign++;
  714. value = textToFloat(equalSign);
  715. }
  716. else
  717. {
  718. return(SYNTAX_ERROR);
  719. }
  720. return(NO_ERR);
  721. }
  722. //---------------------------------------------------------------------------
  723. long FitIniFile::readIdDouble (const char *varName, double &value)
  724. {
  725. char line[255];
  726. char searchString[255];
  727. //--------------------------------
  728. // Always read from top of Block.
  729. seek(currentBlockOffset);
  730. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  731. //------------------------
  732. // Put prefix on varName.
  733. sprintf(searchString,"f %s",varName);
  734. //--------------------------------
  735. // Search line by line for varName
  736. int testy;
  737. do
  738. {
  739. readLine((MemoryPtr)line,254);
  740. testy = strnicmp(line, searchString, strlen(searchString));
  741. if (testy == 0)
  742. {
  743. char* tc = &line[strlen(searchString)];
  744. while (isspace(*tc))
  745. tc++;
  746. if (*tc != '=')
  747. testy = 1;
  748. }
  749. }
  750. while((testy != 0) && (logicalPosition < endOfBlock));
  751. if (logicalPosition >= endOfBlock)
  752. {
  753. value = 0.0;
  754. return(VARIABLE_NOT_FOUND);
  755. }
  756. char *equalSign = strstr(line, "=");
  757. if (equalSign)
  758. {
  759. equalSign++;
  760. value = textToDouble(equalSign);
  761. }
  762. else
  763. {
  764. return(SYNTAX_ERROR);
  765. }
  766. return(NO_ERR);
  767. }
  768. //---------------------------------------------------------------------------
  769. long FitIniFile::readIdLong (const char *varName, long &value)
  770. {
  771. char line[255];
  772. char searchString[255];
  773. //--------------------------------
  774. // Always read from top of Block.
  775. seek(currentBlockOffset);
  776. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  777. //------------------------
  778. // Put prefix on varName.
  779. sprintf(searchString,"l %s",varName);
  780. //--------------------------------
  781. // Search line by line for varName
  782. int testy;
  783. do
  784. {
  785. readLine((MemoryPtr)line,254);
  786. testy = strnicmp(line, searchString, strlen(searchString));
  787. if (testy == 0)
  788. {
  789. char* tc = &line[strlen(searchString)];
  790. while (isspace(*tc))
  791. tc++;
  792. if (*tc != '=')
  793. testy = 1;
  794. }
  795. }
  796. while((testy != 0) && (logicalPosition < endOfBlock));
  797. if (logicalPosition >= endOfBlock)
  798. {
  799. value = 0;
  800. return(VARIABLE_NOT_FOUND);
  801. }
  802. char *equalSign = strstr(line, "=");
  803. if (equalSign)
  804. {
  805. equalSign++;
  806. value = textToLong(equalSign);
  807. }
  808. else
  809. {
  810. return(SYNTAX_ERROR);
  811. }
  812. return(NO_ERR);
  813. }
  814. //---------------------------------------------------------------------------
  815. long FitIniFile::readIdBoolean (const char *varName, bool &value)
  816. {
  817. char line[255];
  818. char searchString[255];
  819. //--------------------------------
  820. // Always read from top of Block.
  821. seek(currentBlockOffset);
  822. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  823. //------------------------
  824. // Put prefix on varName.
  825. sprintf(searchString,"b %s",varName);
  826. //--------------------------------
  827. // Search line by line for varName
  828. int testy;
  829. do
  830. {
  831. readLine((MemoryPtr)line,254);
  832. testy = strnicmp(line, searchString, strlen(searchString));
  833. if (testy == 0)
  834. {
  835. char* tc = &line[strlen(searchString)];
  836. while (isspace(*tc))
  837. tc++;
  838. if (*tc != '=')
  839. testy = 1;
  840. }
  841. }
  842. while((testy != 0) && (logicalPosition < endOfBlock));
  843. if (logicalPosition >= endOfBlock)
  844. {
  845. value = 0;
  846. return(VARIABLE_NOT_FOUND);
  847. }
  848. char *equalSign = strstr(line, "=");
  849. if (equalSign)
  850. {
  851. equalSign++;
  852. value = booleanToLong(equalSign);
  853. }
  854. else
  855. {
  856. return(SYNTAX_ERROR);
  857. }
  858. return(NO_ERR);
  859. }
  860. //---------------------------------------------------------------------------
  861. long FitIniFile::readIdShort (const char *varName, short &value)
  862. {
  863. char line[255];
  864. char searchString[255];
  865. //--------------------------------
  866. // Always read from top of Block.
  867. seek(currentBlockOffset);
  868. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  869. //------------------------
  870. // Put prefix on varName.
  871. sprintf(searchString,"s %s",varName);
  872. //--------------------------------
  873. // Search line by line for varName
  874. int testy;
  875. do
  876. {
  877. readLine((MemoryPtr)line,254);
  878. testy = strnicmp(line, searchString, strlen(searchString));
  879. if (testy == 0)
  880. {
  881. char* tc = &line[strlen(searchString)];
  882. while (isspace(*tc))
  883. tc++;
  884. if (*tc != '=')
  885. testy = 1;
  886. }
  887. }
  888. while((testy != 0) && (logicalPosition < endOfBlock));
  889. if (logicalPosition >= endOfBlock)
  890. {
  891. value = 0;
  892. return(VARIABLE_NOT_FOUND);
  893. }
  894. char *equalSign = strstr(line, "=");
  895. if (equalSign)
  896. {
  897. equalSign++;
  898. value = textToShort(equalSign);
  899. }
  900. else
  901. {
  902. return(SYNTAX_ERROR);
  903. }
  904. return(NO_ERR);
  905. }
  906. //---------------------------------------------------------------------------
  907. long FitIniFile::readIdChar (const char *varName, char &value)
  908. {
  909. char line[255];
  910. char searchString[255];
  911. //--------------------------------
  912. // Always read from top of Block.
  913. seek(currentBlockOffset);
  914. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  915. //------------------------
  916. // Put prefix on varName.
  917. sprintf(searchString,"c %s",varName);
  918. //--------------------------------
  919. // Search line by line for varName
  920. int testy;
  921. do
  922. {
  923. readLine((MemoryPtr)line,254);
  924. testy = strnicmp(line, searchString, strlen(searchString));
  925. if (testy == 0)
  926. {
  927. char* tc = &line[strlen(searchString)];
  928. while (isspace(*tc))
  929. tc++;
  930. if (*tc != '=')
  931. testy = 1;
  932. }
  933. }
  934. while((testy != 0) && (logicalPosition < endOfBlock));
  935. if (logicalPosition >= endOfBlock)
  936. {
  937. value = 0;
  938. return(VARIABLE_NOT_FOUND);
  939. }
  940. char *equalSign = strstr(line, "=");
  941. if (equalSign)
  942. {
  943. equalSign++;
  944. value = textToChar(equalSign);
  945. }
  946. else
  947. {
  948. return(SYNTAX_ERROR);
  949. }
  950. return(NO_ERR);
  951. }
  952. //---------------------------------------------------------------------------
  953. long FitIniFile::readIdULong (const char *varName, unsigned long &value)
  954. {
  955. char line[255];
  956. char searchString[255];
  957. //--------------------------------
  958. // Always read from top of Block.
  959. seek(currentBlockOffset);
  960. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  961. //------------------------
  962. // Put prefix on varName.
  963. sprintf(searchString,"ul %s",varName);
  964. //--------------------------------
  965. // Search line by line for varName
  966. int testy;
  967. do
  968. {
  969. readLine((MemoryPtr)line,254);
  970. testy = strnicmp(line, searchString, strlen(searchString));
  971. if (testy == 0)
  972. {
  973. char* tc = &line[strlen(searchString)];
  974. while (isspace(*tc))
  975. tc++;
  976. if (*tc != '=')
  977. testy = 1;
  978. }
  979. }
  980. while((testy != 0) && (logicalPosition < endOfBlock));
  981. if (logicalPosition >= endOfBlock)
  982. {
  983. value = 0;
  984. return(VARIABLE_NOT_FOUND);
  985. }
  986. char *equalSign = strstr(line, "=");
  987. if (equalSign)
  988. {
  989. equalSign++;
  990. value = textToULong(equalSign);
  991. }
  992. else
  993. {
  994. return(SYNTAX_ERROR);
  995. }
  996. return(NO_ERR);
  997. }
  998. //---------------------------------------------------------------------------
  999. long FitIniFile::readIdUShort (const char *varName, unsigned short &value)
  1000. {
  1001. char line[255];
  1002. char searchString[255];
  1003. //--------------------------------
  1004. // Always read from top of Block.
  1005. seek(currentBlockOffset);
  1006. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1007. //------------------------
  1008. // Put prefix on varName.
  1009. sprintf(searchString,"us %s",varName);
  1010. //--------------------------------
  1011. // Search line by line for varName
  1012. int testy;
  1013. do
  1014. {
  1015. readLine((MemoryPtr)line,254);
  1016. testy = strnicmp(line, searchString, strlen(searchString));
  1017. if (testy == 0)
  1018. {
  1019. char* tc = &line[strlen(searchString)];
  1020. while (isspace(*tc))
  1021. tc++;
  1022. if (*tc != '=')
  1023. testy = 1;
  1024. }
  1025. }
  1026. while((testy != 0) && (logicalPosition < endOfBlock));
  1027. if (logicalPosition >= endOfBlock)
  1028. {
  1029. value = 0;
  1030. return(VARIABLE_NOT_FOUND);
  1031. }
  1032. char *equalSign = strstr(line, "=");
  1033. if (equalSign)
  1034. {
  1035. equalSign++;
  1036. value = textToUShort(equalSign);
  1037. }
  1038. else
  1039. {
  1040. return(SYNTAX_ERROR);
  1041. }
  1042. return(NO_ERR);
  1043. }
  1044. //---------------------------------------------------------------------------
  1045. long FitIniFile::readIdUChar (const char *varName, unsigned char &value)
  1046. {
  1047. char line[255];
  1048. char searchString[255];
  1049. //--------------------------------
  1050. // Always read from top of Block.
  1051. seek(currentBlockOffset);
  1052. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1053. //------------------------
  1054. // Put prefix on varName.
  1055. sprintf(searchString,"uc %s",varName);
  1056. //--------------------------------
  1057. // Search line by line for varName
  1058. int testy;
  1059. do
  1060. {
  1061. readLine((MemoryPtr)line,254);
  1062. testy = strnicmp(line, searchString, strlen(searchString));
  1063. if (testy == 0)
  1064. {
  1065. char* tc = &line[strlen(searchString)];
  1066. while (isspace(*tc))
  1067. tc++;
  1068. if (*tc != '=')
  1069. testy = 1;
  1070. }
  1071. }
  1072. while((testy != 0) && (logicalPosition < endOfBlock));
  1073. if (logicalPosition >= endOfBlock)
  1074. {
  1075. value = 0;
  1076. return(VARIABLE_NOT_FOUND);
  1077. }
  1078. char *equalSign = strstr(line, "=");
  1079. if (equalSign)
  1080. {
  1081. equalSign++;
  1082. value = textToUChar(equalSign);
  1083. }
  1084. else
  1085. {
  1086. return(SYNTAX_ERROR);
  1087. }
  1088. return(NO_ERR);
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. long FitIniFile::copyString (char *dest, char *src, unsigned long bufLen)
  1092. {
  1093. unsigned long offset = 0;
  1094. //---------------------
  1095. // Find starting Quote
  1096. while (*src != '"')
  1097. {
  1098. src++;
  1099. }
  1100. src++; //One more to get to first character past quote.
  1101. //---------------------------------------
  1102. // Copy each character until close quote
  1103. while (*src != '"' && offset < bufLen)
  1104. {
  1105. dest[offset] = *src;
  1106. src++;
  1107. offset++;
  1108. }
  1109. //----------------------------------------------------
  1110. // If this string is longer than buffer, let em know.
  1111. if (offset == bufLen)
  1112. {
  1113. return(BUFFER_TOO_SMALL);
  1114. }
  1115. //---------------------------------
  1116. // otherwise, NULL term and return
  1117. dest[offset] = '\0';
  1118. return(NO_ERR);
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. long FitIniFile::readIdString (const char *varName, char *result, unsigned long bufferSize)
  1122. {
  1123. char line[2048];
  1124. char searchString[255];
  1125. //--------------------------------
  1126. // Always read from top of Block.
  1127. seek(currentBlockOffset);
  1128. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1129. //------------------------
  1130. // Put prefix on varName.
  1131. sprintf(searchString,"st %s",varName);
  1132. //--------------------------------
  1133. // Search line by line for varName
  1134. int testy;
  1135. do
  1136. {
  1137. readLine((MemoryPtr)line,2047);
  1138. testy = strnicmp(line, searchString, strlen(searchString));
  1139. if (testy == 0)
  1140. {
  1141. char* tc = &line[strlen(searchString)];
  1142. while (isspace(*tc))
  1143. tc++;
  1144. if (*tc != '=')
  1145. testy = 1;
  1146. }
  1147. }
  1148. while((testy != 0) && (logicalPosition < endOfBlock));
  1149. if (logicalPosition >= endOfBlock)
  1150. {
  1151. return(VARIABLE_NOT_FOUND);
  1152. }
  1153. char *equalSign = strstr(line, "=");
  1154. if (equalSign)
  1155. {
  1156. equalSign++;
  1157. char* pFound = NULL;
  1158. char* pFirstEqual = strstr( equalSign, "\"" );
  1159. // strings can span more than one line, make sure there is another equal sign
  1160. if ( pFirstEqual && !strstr( pFirstEqual+1, "\"" ) )
  1161. {
  1162. long curLen = strlen( equalSign );
  1163. char tmpLine[2047];
  1164. do
  1165. {
  1166. readLine((MemoryPtr)tmpLine,2047);
  1167. pFound = strstr( tmpLine, "\"" );
  1168. long addedLen = strlen( tmpLine ) + 1;
  1169. if ( curLen + addedLen < 2048 )
  1170. {
  1171. strcat( equalSign, tmpLine );
  1172. strcat( equalSign, "\n" );
  1173. curLen += addedLen;
  1174. }
  1175. else
  1176. break;
  1177. }while( !pFound && (logicalPosition < endOfBlock) );
  1178. }
  1179. long errorCode = copyString(result,equalSign,bufferSize);
  1180. if (errorCode != NO_ERR)
  1181. return(errorCode);
  1182. }
  1183. else
  1184. {
  1185. return(SYNTAX_ERROR);
  1186. }
  1187. return(NO_ERR);
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. long FitIniFile::getIdStringLength (const char *varName)
  1191. {
  1192. char line[255];
  1193. char searchString[255];
  1194. //--------------------------------
  1195. // Always read from top of Block.
  1196. seek(currentBlockOffset);
  1197. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1198. //------------------------
  1199. // Put prefix on varName.
  1200. sprintf(searchString,"st %s",varName);
  1201. //--------------------------------
  1202. // Search line by line for varName
  1203. int testy;
  1204. do
  1205. {
  1206. readLine((MemoryPtr)line,254);
  1207. testy = strnicmp(line, searchString, strlen(searchString));
  1208. if (testy == 0)
  1209. {
  1210. char* tc = &line[strlen(searchString)];
  1211. while (isspace(*tc))
  1212. tc++;
  1213. if (*tc != '=')
  1214. testy = 1;
  1215. }
  1216. }
  1217. while((testy != 0) && (logicalPosition < endOfBlock));
  1218. if (logicalPosition >= endOfBlock)
  1219. {
  1220. return(VARIABLE_NOT_FOUND);
  1221. }
  1222. char *equalSign = strchr(line, '"');
  1223. if (equalSign)
  1224. {
  1225. equalSign++;
  1226. char* end = equalSign;
  1227. while (*end != '"' && *end != 0)
  1228. end++;
  1229. if (*end)
  1230. return (end - equalSign + 1); // + for terminator
  1231. else
  1232. return(SYNTAX_ERROR);
  1233. }
  1234. return(SYNTAX_ERROR);
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. long FitIniFile::readIdFloatArray (const char *varName, float *result, unsigned long numElements)
  1238. {
  1239. char line[255];
  1240. char frontSearch[10];
  1241. char searchString[255];
  1242. //--------------------------------
  1243. // Always read from top of Block.
  1244. seek(currentBlockOffset);
  1245. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1246. //------------------------------------------------------------------
  1247. // Create two search strings so that we can match any number in []
  1248. sprintf(frontSearch,"f[");
  1249. sprintf(searchString,"] %s",varName);
  1250. //--------------------------------
  1251. // Search line by line for varName
  1252. char *fSearch = NULL;
  1253. char *bSearch = NULL;
  1254. do
  1255. {
  1256. readLine((MemoryPtr)line,254);
  1257. fSearch = strstr(line,frontSearch);
  1258. bSearch = strstr(line,searchString);
  1259. }
  1260. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1261. if (logicalPosition >= endOfBlock)
  1262. {
  1263. return(VARIABLE_NOT_FOUND);
  1264. }
  1265. //--------------------------------------
  1266. // Get number of elements in array.
  1267. char elementString[10];
  1268. unsigned long actualElements;
  1269. fSearch += 2; //Move pointer to first number in brackets.
  1270. long numDigits = bSearch - fSearch;
  1271. if (numDigits > 9)
  1272. return(TOO_MANY_ELEMENTS);
  1273. strncpy(elementString,fSearch,numDigits);
  1274. elementString[numDigits] = '\0';
  1275. actualElements = textToULong(elementString);
  1276. if (actualElements > numElements)
  1277. return(USER_ARRAY_TOO_SMALL);
  1278. //------------------------------
  1279. // Parse out the elements here.
  1280. char *equalSign = strstr(line, "=");
  1281. unsigned long elementsRead = 0;
  1282. if (equalSign)
  1283. {
  1284. equalSign++; //Move to char past equal sign.
  1285. //--------------------------------------------------------------------------------
  1286. // Now, loop until we reach the end of block or we've read in all of the elements
  1287. while ((logicalPosition < endOfBlock) && (elementsRead < actualElements))
  1288. {
  1289. long errorCode = getNextWord(equalSign,elementString,9);
  1290. if (errorCode == GET_NEXT_LINE)
  1291. {
  1292. readLine((MemoryPtr)line,254);
  1293. equalSign = line;
  1294. continue;
  1295. }
  1296. if (errorCode != NO_ERR)
  1297. {
  1298. return(errorCode);
  1299. }
  1300. result[elementsRead] = textToFloat(elementString);
  1301. elementsRead++;
  1302. }
  1303. if (logicalPosition >= endOfBlock && elementsRead < actualElements)
  1304. return(NOT_ENOUGH_ELEMENTS_FOR_ARRAY);
  1305. }
  1306. else
  1307. {
  1308. return(SYNTAX_ERROR);
  1309. }
  1310. return(NO_ERR);
  1311. }
  1312. //---------------------------------------------------------------------------
  1313. long FitIniFile::readIdLongArray (const char *varName, long *result, unsigned long numElements)
  1314. {
  1315. char line[255];
  1316. char frontSearch[10];
  1317. char searchString[255];
  1318. //--------------------------------
  1319. // Always read from top of Block.
  1320. seek(currentBlockOffset);
  1321. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1322. //------------------------------------------------------------------
  1323. // Create two search strings so that we can match any number in []
  1324. sprintf(frontSearch,"l[");
  1325. sprintf(searchString,"] %s",varName);
  1326. //--------------------------------
  1327. // Search line by line for varName
  1328. char *fSearch = NULL;
  1329. char *bSearch = NULL;
  1330. do
  1331. {
  1332. readLine((MemoryPtr)line,254);
  1333. fSearch = strstr(line,frontSearch);
  1334. bSearch = strstr(line,searchString);
  1335. }
  1336. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1337. if (logicalPosition >= endOfBlock)
  1338. {
  1339. return(VARIABLE_NOT_FOUND);
  1340. }
  1341. //--------------------------------------
  1342. // Get number of elements in array.
  1343. char elementString[10];
  1344. unsigned long actualElements;
  1345. fSearch += 2; //Move pointer to first number in brackets.
  1346. long numDigits = bSearch - fSearch;
  1347. if (numDigits > 9)
  1348. return(TOO_MANY_ELEMENTS);
  1349. strncpy(elementString,fSearch,numDigits);
  1350. elementString[numDigits] = '\0';
  1351. actualElements = textToULong(elementString);
  1352. if (actualElements > numElements)
  1353. return(USER_ARRAY_TOO_SMALL);
  1354. //------------------------------
  1355. // Parse out the elements here.
  1356. char *equalSign = strstr(line, "=");
  1357. unsigned long elementsRead = 0;
  1358. if (equalSign)
  1359. {
  1360. equalSign++; //Move to char past equal sign.
  1361. //--------------------------------------------------------------------------------
  1362. // Now, loop until we reach the end of block or we've read in all of the elements
  1363. while ((logicalPosition < endOfBlock) && (elementsRead < actualElements))
  1364. {
  1365. long errorCode = getNextWord(equalSign,elementString,9);
  1366. if (errorCode == GET_NEXT_LINE)
  1367. {
  1368. readLine((MemoryPtr)line,254);
  1369. equalSign = line;
  1370. continue;
  1371. }
  1372. if (errorCode != NO_ERR)
  1373. {
  1374. return(errorCode);
  1375. }
  1376. result[elementsRead] = textToLong(elementString);
  1377. elementsRead++;
  1378. }
  1379. if (logicalPosition >= endOfBlock && elementsRead < actualElements)
  1380. return(NOT_ENOUGH_ELEMENTS_FOR_ARRAY);
  1381. }
  1382. else
  1383. {
  1384. return(SYNTAX_ERROR);
  1385. }
  1386. return(NO_ERR);
  1387. }
  1388. //---------------------------------------------------------------------------
  1389. long FitIniFile::readIdULongArray (const char *varName, unsigned long *result, unsigned long numElements)
  1390. {
  1391. char line[255];
  1392. char frontSearch[10];
  1393. char searchString[255];
  1394. //--------------------------------
  1395. // Always read from top of Block.
  1396. seek(currentBlockOffset);
  1397. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1398. //------------------------------------------------------------------
  1399. // Create two search strings so that we can match any number in []
  1400. sprintf(frontSearch,"ul[");
  1401. sprintf(searchString,"] %s",varName);
  1402. //--------------------------------
  1403. // Search line by line for varName
  1404. char *fSearch = NULL;
  1405. char *bSearch = NULL;
  1406. do
  1407. {
  1408. readLine((MemoryPtr)line,254);
  1409. fSearch = strstr(line,frontSearch);
  1410. bSearch = strstr(line,searchString);
  1411. }
  1412. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1413. if (logicalPosition >= endOfBlock)
  1414. {
  1415. return(VARIABLE_NOT_FOUND);
  1416. }
  1417. //--------------------------------------
  1418. // Get number of elements in array.
  1419. char elementString[10];
  1420. unsigned long actualElements;
  1421. fSearch += 3; //Move pointer to first number in brackets.
  1422. long numDigits = bSearch - fSearch;
  1423. if (numDigits > 9)
  1424. return(TOO_MANY_ELEMENTS);
  1425. strncpy(elementString,fSearch,numDigits);
  1426. elementString[numDigits] = '\0';
  1427. actualElements = textToULong(elementString);
  1428. if (actualElements > numElements)
  1429. return(USER_ARRAY_TOO_SMALL);
  1430. //------------------------------
  1431. // Parse out the elements here.
  1432. char *equalSign = strstr(line, "=");
  1433. unsigned long elementsRead = 0;
  1434. if (equalSign)
  1435. {
  1436. equalSign++; //Move to char past equal sign.
  1437. //--------------------------------------------------------------------------------
  1438. // Now, loop until we reach the end of block or we've read in all of the elements
  1439. while ((logicalPosition < endOfBlock) && (elementsRead < actualElements))
  1440. {
  1441. long errorCode = getNextWord(equalSign,elementString,9);
  1442. if (errorCode == GET_NEXT_LINE)
  1443. {
  1444. readLine((MemoryPtr)line,254);
  1445. equalSign = line;
  1446. continue;
  1447. }
  1448. if (errorCode != NO_ERR)
  1449. {
  1450. return(errorCode);
  1451. }
  1452. result[elementsRead] = textToULong(elementString);
  1453. elementsRead++;
  1454. }
  1455. if (logicalPosition >= endOfBlock && elementsRead < actualElements)
  1456. return(NOT_ENOUGH_ELEMENTS_FOR_ARRAY);
  1457. }
  1458. else
  1459. {
  1460. return(SYNTAX_ERROR);
  1461. }
  1462. return(NO_ERR);
  1463. }
  1464. //---------------------------------------------------------------------------
  1465. long FitIniFile::readIdShortArray (const char *varName, short *result, unsigned long numElements)
  1466. {
  1467. char line[255];
  1468. char frontSearch[10];
  1469. char searchString[255];
  1470. //--------------------------------
  1471. // Always read from top of Block.
  1472. seek(currentBlockOffset);
  1473. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1474. //------------------------------------------------------------------
  1475. // Create two search strings so that we can match any number in []
  1476. sprintf(frontSearch,"s[");
  1477. sprintf(searchString,"] %s",varName);
  1478. //--------------------------------
  1479. // Search line by line for varName
  1480. char *fSearch = NULL;
  1481. char *bSearch = NULL;
  1482. do
  1483. {
  1484. readLine((MemoryPtr)line,254);
  1485. fSearch = strstr(line,frontSearch);
  1486. bSearch = strstr(line,searchString);
  1487. }
  1488. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1489. if (logicalPosition >= endOfBlock)
  1490. {
  1491. return(VARIABLE_NOT_FOUND);
  1492. }
  1493. //--------------------------------------
  1494. // Get number of elements in array.
  1495. char elementString[10];
  1496. unsigned long actualElements;
  1497. fSearch += 2; //Move pointer to first number in brackets.
  1498. long numDigits = bSearch - fSearch;
  1499. if (numDigits > 9)
  1500. return(TOO_MANY_ELEMENTS);
  1501. strncpy(elementString,fSearch,numDigits);
  1502. elementString[numDigits] = '\0';
  1503. actualElements = textToULong(elementString);
  1504. if (actualElements > numElements)
  1505. return(USER_ARRAY_TOO_SMALL);
  1506. //------------------------------
  1507. // Parse out the elements here.
  1508. char *equalSign = strstr(line, "=");
  1509. unsigned long elementsRead = 0;
  1510. if (equalSign)
  1511. {
  1512. equalSign++; //Move to char past equal sign.
  1513. //--------------------------------------------------------------------------------
  1514. // Now, loop until we reach the end of block or we've read in all of the elements
  1515. while ((logicalPosition < endOfBlock) && (elementsRead < actualElements))
  1516. {
  1517. long errorCode = getNextWord(equalSign,elementString,9);
  1518. if (errorCode == GET_NEXT_LINE)
  1519. {
  1520. readLine((MemoryPtr)line,254);
  1521. equalSign = line;
  1522. continue;
  1523. }
  1524. if (errorCode != NO_ERR)
  1525. {
  1526. return(errorCode);
  1527. }
  1528. result[elementsRead] = textToShort(elementString);
  1529. elementsRead++;
  1530. }
  1531. if (logicalPosition >= endOfBlock && elementsRead < actualElements)
  1532. return(NOT_ENOUGH_ELEMENTS_FOR_ARRAY);
  1533. }
  1534. else
  1535. {
  1536. return(SYNTAX_ERROR);
  1537. }
  1538. return(NO_ERR);
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. long FitIniFile::readIdUShortArray (const char *varName, unsigned short *result, unsigned long numElements)
  1542. {
  1543. char line[255];
  1544. char frontSearch[10];
  1545. char searchString[255];
  1546. //--------------------------------
  1547. // Always read from top of Block.
  1548. seek(currentBlockOffset);
  1549. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1550. //------------------------------------------------------------------
  1551. // Create two search strings so that we can match any number in []
  1552. sprintf(frontSearch,"us[");
  1553. sprintf(searchString,"] %s",varName);
  1554. //--------------------------------
  1555. // Search line by line for varName
  1556. char *fSearch = NULL;
  1557. char *bSearch = NULL;
  1558. do
  1559. {
  1560. readLine((MemoryPtr)line,254);
  1561. fSearch = strstr(line,frontSearch);
  1562. bSearch = strstr(line,searchString);
  1563. }
  1564. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1565. if (logicalPosition >= endOfBlock)
  1566. {
  1567. return(VARIABLE_NOT_FOUND);
  1568. }
  1569. //--------------------------------------
  1570. // Get number of elements in array.
  1571. char elementString[10];
  1572. unsigned long actualElements;
  1573. fSearch += 3; //Move pointer to first number in brackets.
  1574. long numDigits = bSearch - fSearch;
  1575. if (numDigits > 9)
  1576. return(TOO_MANY_ELEMENTS);
  1577. strncpy(elementString,fSearch,numDigits);
  1578. elementString[numDigits] = '\0';
  1579. actualElements = textToULong(elementString);
  1580. if (actualElements > numElements)
  1581. return(USER_ARRAY_TOO_SMALL);
  1582. //------------------------------
  1583. // Parse out the elements here.
  1584. char *equalSign = strstr(line, "=");
  1585. unsigned long elementsRead = 0;
  1586. if (equalSign)
  1587. {
  1588. equalSign++; //Move to char past equal sign.
  1589. //--------------------------------------------------------------------------------
  1590. // Now, loop until we reach the end of block or we've read in all of the elements
  1591. while ((logicalPosition < endOfBlock) && (elementsRead < actualElements))
  1592. {
  1593. long errorCode = getNextWord(equalSign,elementString,9);
  1594. if (errorCode == GET_NEXT_LINE)
  1595. {
  1596. readLine((MemoryPtr)line,254);
  1597. equalSign = line;
  1598. continue;
  1599. }
  1600. if (errorCode != NO_ERR)
  1601. {
  1602. return(errorCode);
  1603. }
  1604. result[elementsRead] = textToUShort(elementString);
  1605. elementsRead++;
  1606. }
  1607. if (logicalPosition >= endOfBlock && elementsRead < actualElements)
  1608. return(NOT_ENOUGH_ELEMENTS_FOR_ARRAY);
  1609. }
  1610. else
  1611. {
  1612. return(SYNTAX_ERROR);
  1613. }
  1614. return(NO_ERR);
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. long FitIniFile::readIdCharArray (const char *varName, char *result, unsigned long numElements)
  1618. {
  1619. char line[255];
  1620. char frontSearch[10];
  1621. char searchString[255];
  1622. //--------------------------------
  1623. // Always read from top of Block.
  1624. seek(currentBlockOffset);
  1625. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1626. //------------------------------------------------------------------
  1627. // Create two search strings so that we can match any number in []
  1628. sprintf(frontSearch,"c[");
  1629. sprintf(searchString,"] %s",varName);
  1630. //--------------------------------
  1631. // Search line by line for varName
  1632. char *fSearch = NULL;
  1633. char *bSearch = NULL;
  1634. do
  1635. {
  1636. readLine((MemoryPtr)line,254);
  1637. fSearch = strstr(line,frontSearch);
  1638. bSearch = strstr(line,searchString);
  1639. }
  1640. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1641. if (logicalPosition >= endOfBlock)
  1642. {
  1643. return(VARIABLE_NOT_FOUND);
  1644. }
  1645. //--------------------------------------
  1646. // Get number of elements in array.
  1647. char elementString[10];
  1648. unsigned long actualElements;
  1649. fSearch += 2; //Move pointer to first number in brackets.
  1650. long numDigits = bSearch - fSearch;
  1651. if (numDigits > 9)
  1652. return(TOO_MANY_ELEMENTS);
  1653. strncpy(elementString,fSearch,numDigits);
  1654. elementString[numDigits] = '\0';
  1655. actualElements = textToULong(elementString);
  1656. if (actualElements > numElements)
  1657. return(USER_ARRAY_TOO_SMALL);
  1658. //------------------------------
  1659. // Parse out the elements here.
  1660. char *equalSign = strstr(line, "=");
  1661. unsigned long elementsRead = 0;
  1662. if (equalSign)
  1663. {
  1664. equalSign++; //Move to char past equal sign.
  1665. //--------------------------------------------------------------------------------
  1666. // Now, loop until we reach the end of block or we've read in all of the elements
  1667. while ((logicalPosition < endOfBlock) && (elementsRead < actualElements))
  1668. {
  1669. long errorCode = getNextWord(equalSign,elementString,9);
  1670. if (errorCode == GET_NEXT_LINE)
  1671. {
  1672. readLine((MemoryPtr)line,254);
  1673. equalSign = line;
  1674. continue;
  1675. }
  1676. if (errorCode != NO_ERR)
  1677. {
  1678. return(errorCode);
  1679. }
  1680. result[elementsRead] = textToChar(elementString);
  1681. elementsRead++;
  1682. }
  1683. if (logicalPosition >= endOfBlock && elementsRead < actualElements)
  1684. return(NOT_ENOUGH_ELEMENTS_FOR_ARRAY);
  1685. }
  1686. else
  1687. {
  1688. return(SYNTAX_ERROR);
  1689. }
  1690. return(NO_ERR);
  1691. }
  1692. //---------------------------------------------------------------------------
  1693. long FitIniFile::readIdUCharArray (const char *varName, unsigned char *result, unsigned long numElements)
  1694. {
  1695. char line[255];
  1696. char frontSearch[10];
  1697. char searchString[255];
  1698. //--------------------------------
  1699. // Always read from top of Block.
  1700. seek(currentBlockOffset);
  1701. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1702. //------------------------------------------------------------------
  1703. // Create two search strings so that we can match any number in []
  1704. sprintf(frontSearch,"uc[");
  1705. sprintf(searchString,"] %s",varName);
  1706. //--------------------------------
  1707. // Search line by line for varName
  1708. char *fSearch = NULL;
  1709. char *bSearch = NULL;
  1710. do
  1711. {
  1712. readLine((MemoryPtr)line,254);
  1713. fSearch = strstr(line,frontSearch);
  1714. bSearch = strstr(line,searchString);
  1715. }
  1716. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1717. if (logicalPosition >= endOfBlock)
  1718. {
  1719. return(VARIABLE_NOT_FOUND);
  1720. }
  1721. //--------------------------------------
  1722. // Get number of elements in array.
  1723. char elementString[10];
  1724. unsigned long actualElements;
  1725. fSearch += 3; //Move pointer to first number in brackets.
  1726. long numDigits = bSearch - fSearch;
  1727. if (numDigits > 9)
  1728. return(TOO_MANY_ELEMENTS);
  1729. strncpy(elementString,fSearch,numDigits);
  1730. elementString[numDigits] = '\0';
  1731. actualElements = textToULong(elementString);
  1732. if (actualElements > numElements)
  1733. return(USER_ARRAY_TOO_SMALL);
  1734. //------------------------------
  1735. // Parse out the elements here.
  1736. char *equalSign = strstr(line, "=");
  1737. unsigned long elementsRead = 0;
  1738. if (equalSign)
  1739. {
  1740. equalSign++; //Move to char past equal sign.
  1741. //--------------------------------------------------------------------------------
  1742. // Now, loop until we reach the end of block or we've read in all of the elements
  1743. while ((logicalPosition < endOfBlock) && (elementsRead < actualElements))
  1744. {
  1745. long errorCode = getNextWord(equalSign,elementString,9);
  1746. if (errorCode == GET_NEXT_LINE)
  1747. {
  1748. readLine((MemoryPtr)line,254);
  1749. equalSign = line;
  1750. continue;
  1751. }
  1752. if (errorCode != NO_ERR)
  1753. {
  1754. return(errorCode);
  1755. }
  1756. result[elementsRead] = textToUChar(elementString);
  1757. elementsRead++;
  1758. }
  1759. if (logicalPosition >= endOfBlock && elementsRead < actualElements)
  1760. return(NOT_ENOUGH_ELEMENTS_FOR_ARRAY);
  1761. }
  1762. else
  1763. {
  1764. return(SYNTAX_ERROR);
  1765. }
  1766. return(NO_ERR);
  1767. }
  1768. //---------------------------------------------------------------------------
  1769. unsigned long FitIniFile::getIdFloatArrayElements (const char *varName)
  1770. {
  1771. char line[255];
  1772. char frontSearch[10];
  1773. char searchString[255];
  1774. //--------------------------------
  1775. // Always read from top of Block.
  1776. seek(currentBlockOffset);
  1777. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1778. //------------------------------------------------------------------
  1779. // Create two search strings so that we can match any number in []
  1780. sprintf(frontSearch,"f[");
  1781. sprintf(searchString,"] %s",varName);
  1782. //--------------------------------
  1783. // Search line by line for varName
  1784. char *fSearch = NULL;
  1785. char *bSearch = NULL;
  1786. do
  1787. {
  1788. readLine((MemoryPtr)line,254);
  1789. fSearch = strstr(line,frontSearch);
  1790. bSearch = strstr(line,searchString);
  1791. }
  1792. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1793. if (logicalPosition >= endOfBlock)
  1794. {
  1795. return(VARIABLE_NOT_FOUND);
  1796. }
  1797. //--------------------------------------
  1798. // Get number of elements in array.
  1799. char elementString[10];
  1800. unsigned long actualElements;
  1801. fSearch += 2; //Move pointer to first number in brackets.
  1802. long numDigits = bSearch - fSearch;
  1803. if (numDigits > 9)
  1804. return(TOO_MANY_ELEMENTS);
  1805. strncpy(elementString,fSearch,numDigits);
  1806. elementString[numDigits] = '\0';
  1807. actualElements = textToULong(elementString);
  1808. return(actualElements);
  1809. }
  1810. //---------------------------------------------------------------------------
  1811. unsigned long FitIniFile::getIdLongArrayElements (const char *varName)
  1812. {
  1813. char line[255];
  1814. char frontSearch[10];
  1815. char searchString[255];
  1816. //--------------------------------
  1817. // Always read from top of Block.
  1818. seek(currentBlockOffset);
  1819. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1820. //------------------------------------------------------------------
  1821. // Create two search strings so that we can match any number in []
  1822. sprintf(frontSearch,"l[");
  1823. sprintf(searchString,"] %s",varName);
  1824. //--------------------------------
  1825. // Search line by line for varName
  1826. char *fSearch = NULL;
  1827. char *bSearch = NULL;
  1828. do
  1829. {
  1830. readLine((MemoryPtr)line,254);
  1831. fSearch = strstr(line,frontSearch);
  1832. bSearch = strstr(line,searchString);
  1833. }
  1834. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1835. if (logicalPosition >= endOfBlock)
  1836. {
  1837. return(VARIABLE_NOT_FOUND);
  1838. }
  1839. //--------------------------------------
  1840. // Get number of elements in array.
  1841. char elementString[10];
  1842. unsigned long actualElements;
  1843. fSearch += 2; //Move pointer to first number in brackets.
  1844. long numDigits = bSearch - fSearch;
  1845. if (numDigits > 9)
  1846. return(TOO_MANY_ELEMENTS);
  1847. strncpy(elementString,fSearch,numDigits);
  1848. elementString[numDigits] = '\0';
  1849. actualElements = textToULong(elementString);
  1850. return(actualElements);
  1851. }
  1852. //---------------------------------------------------------------------------
  1853. unsigned long FitIniFile::getIdULongArrayElements (const char *varName)
  1854. {
  1855. char line[255];
  1856. char frontSearch[10];
  1857. char searchString[255];
  1858. //--------------------------------
  1859. // Always read from top of Block.
  1860. seek(currentBlockOffset);
  1861. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1862. //------------------------------------------------------------------
  1863. // Create two search strings so that we can match any number in []
  1864. sprintf(frontSearch,"ul[");
  1865. sprintf(searchString,"] %s",varName);
  1866. //--------------------------------
  1867. // Search line by line for varName
  1868. char *fSearch = NULL;
  1869. char *bSearch = NULL;
  1870. do
  1871. {
  1872. readLine((MemoryPtr)line,254);
  1873. fSearch = strstr(line,frontSearch);
  1874. bSearch = strstr(line,searchString);
  1875. }
  1876. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1877. if (logicalPosition >= endOfBlock)
  1878. {
  1879. return(VARIABLE_NOT_FOUND);
  1880. }
  1881. //--------------------------------------
  1882. // Get number of elements in array.
  1883. char elementString[10];
  1884. unsigned long actualElements;
  1885. fSearch += 3; //Move pointer to first number in brackets.
  1886. long numDigits = bSearch - fSearch;
  1887. if (numDigits > 9)
  1888. return(TOO_MANY_ELEMENTS);
  1889. strncpy(elementString,fSearch,numDigits);
  1890. elementString[numDigits] = '\0';
  1891. actualElements = textToULong(elementString);
  1892. return(actualElements);
  1893. }
  1894. //---------------------------------------------------------------------------
  1895. unsigned long FitIniFile::getIdShortArrayElements (const char *varName)
  1896. {
  1897. char line[255];
  1898. char frontSearch[10];
  1899. char searchString[255];
  1900. //--------------------------------
  1901. // Always read from top of Block.
  1902. seek(currentBlockOffset);
  1903. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1904. //------------------------------------------------------------------
  1905. // Create two search strings so that we can match any number in []
  1906. sprintf(frontSearch,"s[");
  1907. sprintf(searchString,"] %s",varName);
  1908. //--------------------------------
  1909. // Search line by line for varName
  1910. char *fSearch = NULL;
  1911. char *bSearch = NULL;
  1912. do
  1913. {
  1914. readLine((MemoryPtr)line,254);
  1915. fSearch = strstr(line,frontSearch);
  1916. bSearch = strstr(line,searchString);
  1917. }
  1918. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1919. if (logicalPosition >= endOfBlock)
  1920. {
  1921. return(VARIABLE_NOT_FOUND);
  1922. }
  1923. //--------------------------------------
  1924. // Get number of elements in array.
  1925. char elementString[10];
  1926. unsigned long actualElements;
  1927. fSearch += 2; //Move pointer to first number in brackets.
  1928. long numDigits = bSearch - fSearch;
  1929. if (numDigits > 9)
  1930. return(TOO_MANY_ELEMENTS);
  1931. strncpy(elementString,fSearch,numDigits);
  1932. elementString[numDigits] = '\0';
  1933. actualElements = textToULong(elementString);
  1934. return(actualElements);
  1935. }
  1936. //---------------------------------------------------------------------------
  1937. unsigned long FitIniFile::getIdUShortArrayElements (const char *varName)
  1938. {
  1939. char line[255];
  1940. char frontSearch[10];
  1941. char searchString[255];
  1942. //--------------------------------
  1943. // Always read from top of Block.
  1944. seek(currentBlockOffset);
  1945. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1946. //------------------------------------------------------------------
  1947. // Create two search strings so that we can match any number in []
  1948. sprintf(frontSearch,"us[");
  1949. sprintf(searchString,"] %s",varName);
  1950. //--------------------------------
  1951. // Search line by line for varName
  1952. char *fSearch = NULL;
  1953. char *bSearch = NULL;
  1954. do
  1955. {
  1956. readLine((MemoryPtr)line,254);
  1957. fSearch = strstr(line,frontSearch);
  1958. bSearch = strstr(line,searchString);
  1959. }
  1960. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  1961. if (logicalPosition >= endOfBlock)
  1962. {
  1963. return(VARIABLE_NOT_FOUND);
  1964. }
  1965. //--------------------------------------
  1966. // Get number of elements in array.
  1967. char elementString[10];
  1968. unsigned long actualElements;
  1969. fSearch += 3; //Move pointer to first number in brackets.
  1970. long numDigits = bSearch - fSearch;
  1971. if (numDigits > 9)
  1972. return(TOO_MANY_ELEMENTS);
  1973. strncpy(elementString,fSearch,numDigits);
  1974. elementString[numDigits] = '\0';
  1975. actualElements = textToULong(elementString);
  1976. return(actualElements);
  1977. }
  1978. //---------------------------------------------------------------------------
  1979. unsigned long FitIniFile::getIdCharArrayElements (const char *varName)
  1980. {
  1981. char line[255];
  1982. char frontSearch[10];
  1983. char searchString[255];
  1984. //--------------------------------
  1985. // Always read from top of Block.
  1986. seek(currentBlockOffset);
  1987. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  1988. //------------------------------------------------------------------
  1989. // Create two search strings so that we can match any number in []
  1990. sprintf(frontSearch,"c[");
  1991. sprintf(searchString,"] %s",varName);
  1992. //--------------------------------
  1993. // Search line by line for varName
  1994. char *fSearch = NULL;
  1995. char *bSearch = NULL;
  1996. do
  1997. {
  1998. readLine((MemoryPtr)line,254);
  1999. fSearch = strstr(line,frontSearch);
  2000. bSearch = strstr(line,searchString);
  2001. }
  2002. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  2003. if (logicalPosition >= endOfBlock)
  2004. {
  2005. return(VARIABLE_NOT_FOUND);
  2006. }
  2007. //--------------------------------------
  2008. // Get number of elements in array.
  2009. char elementString[10];
  2010. unsigned long actualElements;
  2011. fSearch += 2; //Move pointer to first number in brackets.
  2012. long numDigits = bSearch - fSearch;
  2013. if (numDigits > 9)
  2014. return(TOO_MANY_ELEMENTS);
  2015. strncpy(elementString,fSearch,numDigits);
  2016. elementString[numDigits] = '\0';
  2017. actualElements = textToULong(elementString);
  2018. return(actualElements);
  2019. }
  2020. //---------------------------------------------------------------------------
  2021. unsigned long FitIniFile::getIdUCharArrayElements (const char *varName)
  2022. {
  2023. char line[255];
  2024. char frontSearch[10];
  2025. char searchString[255];
  2026. //--------------------------------
  2027. // Always read from top of Block.
  2028. seek(currentBlockOffset);
  2029. unsigned long endOfBlock = currentBlockOffset+currentBlockSize;
  2030. //------------------------------------------------------------------
  2031. // Create two search strings so that we can match any number in []
  2032. sprintf(frontSearch,"uc[");
  2033. sprintf(searchString,"] %s",varName);
  2034. //--------------------------------
  2035. // Search line by line for varName
  2036. char *fSearch = NULL;
  2037. char *bSearch = NULL;
  2038. do
  2039. {
  2040. readLine((MemoryPtr)line,254);
  2041. fSearch = strstr(line,frontSearch);
  2042. bSearch = strstr(line,searchString);
  2043. }
  2044. while(((fSearch == NULL) || (bSearch == NULL)) && (logicalPosition < endOfBlock));
  2045. if (logicalPosition >= endOfBlock)
  2046. {
  2047. return(VARIABLE_NOT_FOUND);
  2048. }
  2049. //--------------------------------------
  2050. // Get number of elements in array.
  2051. char elementString[10];
  2052. unsigned long actualElements;
  2053. fSearch += 3; //Move pointer to first number in brackets.
  2054. long numDigits = bSearch - fSearch;
  2055. if (numDigits > 9)
  2056. return(TOO_MANY_ELEMENTS);
  2057. strncpy(elementString,fSearch,numDigits);
  2058. elementString[numDigits] = '\0';
  2059. actualElements = textToULong(elementString);
  2060. return(actualElements);
  2061. }
  2062. //---------------------------------------------------------------------------
  2063. long FitIniFile::writeBlock (const char *blockId)
  2064. {
  2065. char thisLine[255];
  2066. sprintf(thisLine,"\r\n[%s]\r\n",blockId);
  2067. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2068. return(result);
  2069. }
  2070. //---------------------------------------------------------------------------
  2071. long FitIniFile::writeIdFloat (const char *varName, float value)
  2072. {
  2073. char thisLine[255];
  2074. sprintf(thisLine,"f %s = %f\r\n",varName,value);
  2075. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2076. return(result);
  2077. }
  2078. //---------------------------------------------------------------------------
  2079. long FitIniFile::writeIdBoolean (const char *varName, bool value)
  2080. {
  2081. char thisLine[255];
  2082. if (value)
  2083. sprintf(thisLine,"b %s = %s\r\n",varName,"TRUE");
  2084. else
  2085. sprintf(thisLine,"b %s = %s\r\n",varName,"FALSE");
  2086. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2087. return(result);
  2088. }
  2089. //---------------------------------------------------------------------------
  2090. long FitIniFile::writeIdLong (const char *varName, long value)
  2091. {
  2092. char thisLine[255];
  2093. sprintf(thisLine,"l %s = %d\r\n",varName,value);
  2094. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2095. return(result);
  2096. }
  2097. //---------------------------------------------------------------------------
  2098. long FitIniFile::writeIdShort (const char *varName, short value)
  2099. {
  2100. char thisLine[255];
  2101. sprintf(thisLine,"s %s = %d\r\n",varName,value);
  2102. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2103. return(result);
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. long FitIniFile::writeIdChar (const char *varName, char value)
  2107. {
  2108. char thisLine[255];
  2109. sprintf(thisLine,"c %s = %d\r\n",varName,value);
  2110. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2111. return(result);
  2112. }
  2113. //---------------------------------------------------------------------------
  2114. long FitIniFile::writeIdULong (const char *varName, unsigned long value)
  2115. {
  2116. char thisLine[255];
  2117. sprintf(thisLine,"ul %s = %d\r\n",varName,value);
  2118. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2119. return(result);
  2120. }
  2121. //---------------------------------------------------------------------------
  2122. long FitIniFile::writeIdUShort (const char *varName, unsigned short value)
  2123. {
  2124. char thisLine[255];
  2125. sprintf(thisLine,"us %s = %d\r\n",varName,value);
  2126. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2127. return(result);
  2128. }
  2129. //---------------------------------------------------------------------------
  2130. long FitIniFile::writeIdUChar (const char *varName, unsigned char value)
  2131. {
  2132. char thisLine[255];
  2133. sprintf(thisLine,"uc %s = %d\r\n",varName,value);
  2134. long result = write((MemoryPtr)thisLine,strlen(thisLine));
  2135. return(result);
  2136. }
  2137. //---------------------------------------------------------------------------
  2138. long FitIniFile::writeIdString (const char *varName, const char *result)
  2139. {
  2140. char thisLine[4096];
  2141. char tmpString[4000];
  2142. memset(tmpString,0,4000);
  2143. strncpy(tmpString,result,3999);
  2144. if (strlen(result) >= 4000)
  2145. PAUSE(("String passed to WriteIdString is longer then 4000 characters"));
  2146. sprintf(thisLine,"st %s = \"%s\"\r\n",varName,result);
  2147. long bytesWritten = write((MemoryPtr)thisLine,strlen(thisLine));
  2148. return(bytesWritten);
  2149. }
  2150. //---------------------------------------------------------------------------
  2151. long FitIniFile::writeIdUShortArray (const char *varName, unsigned short *array, unsigned long numElements)
  2152. {
  2153. char thisLine[255];
  2154. sprintf(thisLine,"us[%d] %s = %d,",numElements,varName,array[0]);
  2155. long bytesWritten = write((MemoryPtr)thisLine,strlen(thisLine));
  2156. for (long i=1;i<(long)numElements;i++)
  2157. {
  2158. sprintf(thisLine,"%d,",array[i]);
  2159. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2160. }
  2161. sprintf(thisLine,"\r\n");
  2162. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2163. return (bytesWritten);
  2164. }
  2165. //---------------------------------------------------------------------------
  2166. long FitIniFile::writeIdLongArray (const char *varName, long *array, unsigned long numElements)
  2167. {
  2168. char thisLine[255];
  2169. sprintf(thisLine,"l[%d] %s = %d,",numElements,varName,array[0]);
  2170. long bytesWritten = write((MemoryPtr)thisLine,strlen(thisLine));
  2171. for (long i=1;i<(long)numElements;i++)
  2172. {
  2173. sprintf(thisLine,"%d,",array[i]);
  2174. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2175. }
  2176. sprintf(thisLine,"\r\n");
  2177. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2178. return (bytesWritten);
  2179. }
  2180. //---------------------------------------------------------------------------
  2181. long FitIniFile::writeIdFloatArray (const char *varName, float *array, unsigned long numElements)
  2182. {
  2183. char thisLine[255];
  2184. sprintf(thisLine,"f[%d] %s = %.2f,",numElements,varName,array[0]);
  2185. long bytesWritten = write((MemoryPtr)thisLine,strlen(thisLine));
  2186. for (long i=1;i<(long)numElements;i++)
  2187. {
  2188. sprintf(thisLine," %.2f,",array[i]);
  2189. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2190. }
  2191. sprintf(thisLine,"\r\n");
  2192. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2193. return (bytesWritten);
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. long FitIniFile::writeIdUCharArray (const char *varName, unsigned char *array, unsigned long numElements)
  2197. {
  2198. char thisLine[255];
  2199. sprintf(thisLine,"uc[%d] %s = %d,",numElements,varName,array[0]);
  2200. long bytesWritten = write((MemoryPtr)thisLine,strlen(thisLine));
  2201. for (long i=1;i<(long)numElements;i++)
  2202. {
  2203. sprintf(thisLine," %d,",array[i]);
  2204. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2205. }
  2206. sprintf(thisLine,"\r\n");
  2207. bytesWritten += write((MemoryPtr)thisLine,strlen(thisLine));
  2208. return (bytesWritten);
  2209. }
  2210. //---------------------------------------------------------------------------
  2211. //
  2212. // Edit log
  2213. //
  2214. //---------------------------------------------------------------------------