1
0

y_ini.inc 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /*----------------------------------------------------------------------------*-
  2. ===========================
  3. Y Sever Includes - INI Core
  4. ===========================
  5. Description:
  6. Reads the INI and also exports a number of functions to other "classes" for
  7. easy reading of data files there.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the SA:MP script information include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2008
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk
  24. Thanks:
  25. Peter, Cam - Support.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN.
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 1.0
  44. Changelog:
  45. 08/09/10:
  46. Started adding sscanf and file plugin compatibility.
  47. Added tagless data at the start of a file (dini compatible).
  48. Added new INI:file[tag]() syntax.
  49. Added options to default file load.
  50. Fixed bugs in default file load configuration.
  51. Modified to be stand alone.
  52. 20/02/08:
  53. Added INI_RemoveEntry.
  54. 18/08/07:
  55. Fixed bug reading identifiers starting with a tag (i.e. names).
  56. Added local file reading for non-serverwide broadcasting.
  57. Added tag passing instead of tag based functions option.
  58. Increased default pool size.
  59. 30/07/07:
  60. Added auto creation of non-existant files.
  61. 13/07/07:
  62. Fixed INI writing to actually work.
  63. Added support for blank lines in INIs decently and quickly.
  64. 25/06/07:
  65. Altered file write options to use lists.
  66. Added buffer overwriting for updating values.
  67. 24/06/07:
  68. Added file write options.
  69. 21/06/07:
  70. Added INI_NEW_LINE for future writing functions.
  71. 20/06/07:
  72. Added support for an optional parameter in broadcast data.
  73. 15/04/07:
  74. Updated for more whitespaces.
  75. Added INI comment code.
  76. Added support for value-less entries.
  77. Modified entry extraction to use end of name location parameter.
  78. Removed INI_GetTagName, now done via INI_GetEntryName.
  79. 14/04/07:
  80. Updated header documentation with more than changelog.
  81. 24/03/07:
  82. First version.
  83. Functions:
  84. Public:
  85. -
  86. Core:
  87. -
  88. Stock:
  89. INI_Load - Loads an INI file using standard features.
  90. INI_ParseFile - Loads a file as an ini and distributes data.
  91. INI_GetEntryName - Gets the name of an INI item.
  92. INI_GetEntryText - Gets the value of an INI item.
  93. INI_Open - Opens an INI for writing.
  94. INI_Close - Closes an INI being written to.
  95. INI_SetTag - Sets a subheading in an INI fo subsequent writes.
  96. INI_WriteString - Writes a string to an INI.
  97. INI_WriteInt - Writes an int to an INI.
  98. INI_WriteFloat - Writes a float to an INI.
  99. INI_WriteHex - Writes a hex to an INI.
  100. INI_WriteBin - Writes a binary to an INI.
  101. INI_WriteBool - Writes a boolean to an INI.
  102. INI_RemoveEntry - Remove an entry from a file.
  103. Static:
  104. INI_WriteBuffer - Writes an INI's buffer to the file.
  105. INI_AddToBuffer - Adds a string to an INI buffer.
  106. Inline:
  107. INI_Int - Parse an integer INI entry.
  108. INI_Float - Parse a float INI entry.
  109. INI_Hex - Parse a hex INI entry.
  110. INI_Bin - Parse a binary INI entry.
  111. INI_Bool - Parse a binary INI entry.
  112. INI_String - Parse a string INI entry.
  113. API:
  114. -
  115. Callbacks:
  116. -
  117. Definitions:
  118. MAX_INI_TAG - Maximum length of an INI tagname.
  119. MAX_INI_ENTRY_NAME - Maximum length of an INI entry name.
  120. MAX_INI_ENTRY_TEXT - Maximum length of an INI's entries' value.
  121. MAX_INI_LINE - Maximum length of a line in a file.
  122. INI_NEW_LINE - String for new lines.
  123. INI_MAX_WRITES - Maximum concurrent files open for writing.
  124. MAX_INI_TAGS - Number of tags the buffer can hold data for at once.
  125. Enums:
  126. E_INI_WRITE - Storage for entries to be written.
  127. E_INI_TAGS - Data for tags with data.
  128. Macros:
  129. INI_Parse - Header for ini parsing functions.
  130. Tags:
  131. INI - Handle to an INI file being written to.
  132. Variables:
  133. Global:
  134. -
  135. Static:
  136. YSI_g_sINIWriteBuffer - Basic data to be written.
  137. YSI_g_sINIWritePos - Next slot to write to.
  138. YSI_g_sINITagPos - Next slot to add a tag to.
  139. YSI_g_sINICurrentTag - Pointer to the tag been writen to.
  140. YSI_g_sINIWriteTag - Data for tags,
  141. YSI_g_sINIWriteFile - Current files been written to.
  142. Commands:
  143. -
  144. Compile options:
  145. -
  146. Operators:
  147. -
  148. -*----------------------------------------------------------------------------*/
  149. // Ini file reading is not distributed. User file reading may be, though I
  150. // doubt it.
  151. #include <YSI\internal\y_version>
  152. //#tryinclude <sscanf>
  153. #include <YSI\y_utils>
  154. #include <YSI\y_bit>
  155. #include <YSI\y_debug>
  156. #if !defined MAX_INI_TAG
  157. #define MAX_INI_TAG (16)
  158. #endif
  159. #if !defined MAX_INI_ENTRY_NAME
  160. #define MAX_INI_ENTRY_NAME (32)
  161. #endif
  162. #if !defined MAX_INI_ENTRY_TEXT
  163. #define MAX_INI_ENTRY_TEXT 128
  164. #endif
  165. #define MAX_INI_LINE (MAX_INI_ENTRY_NAME + MAX_INI_ENTRY_TEXT + 32)
  166. #define INI_NO_FILE (INI:-1)
  167. #if !defined INI_NEW_LINE
  168. #define INI_NEW_LINE "\r\n"
  169. #endif
  170. #if !defined INI_MAX_WRITES
  171. // I doubt many people will use the multiple ini function.
  172. #define INI_MAX_WRITES (2)
  173. #endif
  174. #if !defined INI_BUFFER_SIZE
  175. #define INI_BUFFER_SIZE (64)
  176. #endif
  177. /*#if INI_BUFFER_SIZE <= 32
  178. #define INI_BUFFER_BITS 2
  179. #else
  180. #define INI_BUFFER_BITS Bit_Bits(INI_BUFFER_SIZE)
  181. #endif*/
  182. #if !defined MAX_INI_TAGS
  183. #define MAX_INI_TAGS 3
  184. /*#else
  185. #if MAX_INI_TAGS > 32
  186. #error Current code only supports up to 32 buffer tags
  187. #endif*/
  188. #endif
  189. #define MAX_INI_STRING (64)
  190. enum E_INI_WRITE
  191. {
  192. E_INI_WRITE_NAME[MAX_INI_ENTRY_NAME],
  193. E_INI_WRITE_TEXT[MAX_INI_ENTRY_TEXT],
  194. E_INI_WRITE_NEXT
  195. }
  196. enum E_INI_TAGS
  197. {
  198. E_INI_TAGS_NAME[MAX_INI_TAG char],
  199. E_INI_TAGS_START,
  200. E_INI_TAGS_LAST
  201. }
  202. static stock
  203. YSI_g_sINIWriteBuffer[INI_MAX_WRITES][INI_BUFFER_SIZE][E_INI_WRITE],
  204. YSI_g_sINIWritePos[INI_MAX_WRITES],
  205. YSI_g_sINITagPos[INI_MAX_WRITES],
  206. YSI_g_sINICurrentTag[INI_MAX_WRITES],
  207. YSI_g_sINIWriteTag[INI_MAX_WRITES][MAX_INI_TAGS][E_INI_TAGS],
  208. YSI_g_sINIWriteFile[INI_MAX_WRITES][128];
  209. #define INI_Parse(%1,%2) \
  210. forward @INI_%1_%2(name[], value[]); \
  211. @INI_%1_%2(name[], value[])
  212. #define INI:%0[%1](%2) \
  213. forward @INI_%0_%1(%2); \
  214. @INI_%0_%1(%2)
  215. #if defined _inc_sscanf && 0
  216. /*------------------------------------------------------------------------*-
  217. Function:
  218. INI_Int
  219. Params:
  220. name[] - Name of the INI textual identifier.
  221. function - Function to call with integer value.
  222. Return:
  223. function().
  224. Notes:
  225. -
  226. -*------------------------------------------------------------------------*/
  227. #define INI_Int(%1,%2) \
  228. if(!strcmp((%1),name,true)&&!sscanf(value,"d",%2))return;
  229. //if(!strcmp(){%2=floatstr(value);return;}
  230. //if (!strcmp((%1), name, true)) return %2(strval(value))
  231. /*------------------------------------------------------------------------*-
  232. Function:
  233. INI_Float
  234. Params:
  235. name[] - Name of the INI textual identifier.
  236. function - Function to call with float value.
  237. Return:
  238. function().
  239. Notes:
  240. -
  241. -*------------------------------------------------------------------------*/
  242. #define INI_Float(%1,%2) \
  243. if(!strcmp((%1),name,true)&&!sscanf(value,"f",%2))return;
  244. //if (!strcmp((%1), name, true)) return %2(floatstr(value))
  245. /*------------------------------------------------------------------------*-
  246. Function:
  247. INI_Hex
  248. Params:
  249. name[] - Name of the INI textual identifier.
  250. function - Function to call with hex value.
  251. Return:
  252. function().
  253. Notes:
  254. -
  255. -*------------------------------------------------------------------------*/
  256. #define INI_Hex(%1,%2) \
  257. if(!strcmp((%1),name,true)&&!sscanf(value,"h",%2))return;
  258. //if (!strcmp((%1), name, true)) return %2(hexstr(value))
  259. /*------------------------------------------------------------------------*-
  260. Function:
  261. INI_Bin
  262. Params:
  263. name[] - Name of the INI textual identifier.
  264. function - Function to call with binary value.
  265. Return:
  266. function().
  267. Notes:
  268. -
  269. -*------------------------------------------------------------------------*/
  270. #define INI_Bin(%1,%2) \
  271. if(!strcmp((%1),name,true)&&!sscanf(value,"l",%2))return;
  272. //if (!strcmp((%1), name, true)) return %2(binstr(value))
  273. /*------------------------------------------------------------------------*-
  274. Function:
  275. INI_String
  276. Params:
  277. name[] - Name of the INI textual identifier.
  278. function - Function to call with string value.
  279. Return:
  280. function().
  281. Notes:
  282. -
  283. -*------------------------------------------------------------------------*/
  284. #define INI_String(%1,%2,%3) \
  285. if(!strcmp((%1),name,true)&&!sscanf(value,"s["#%3"]",%2))return;
  286. //if (!strcmp((%1), name, true)) return %2(value)
  287. #else
  288. #define INI_Int(%1,%2) \
  289. if(!strcmp((%1),name,true))return %2=strval(value)
  290. #define INI_Float(%1,%2) \
  291. if(!strcmp((%1),name,true))return _:(%2=floatstr(value))
  292. #define INI_Hex(%1,%2) \
  293. if(!strcmp((%1),name,true))return %2=hexstr(value)
  294. #define INI_Bin(%1,%2) \
  295. if(!strcmp((%1),name,true))return %2=binstr(value)
  296. #define INI_Bool(%1,%2) \
  297. if(!strcmp((%1),name,true))return %2=boolstr(value)
  298. #define INI_String(%1,%2,%3) \
  299. if(!strcmp((%1),name,true))return strcpy(%2,value,%3)
  300. #endif
  301. /*----------------------------------------------------------------------------*-
  302. Function:
  303. INI_GetEntryName
  304. Params:
  305. source - The string you want to get an entry name from.
  306. dest - The place you want to store the entry name to
  307. Return:
  308. bool: Found the name correctly.
  309. Notes:
  310. -
  311. -*----------------------------------------------------------------------------*/
  312. stock bool:INI_GetEntryName(source[], dest[], &i)
  313. {
  314. new
  315. j;
  316. while (source[j] && source[j] <= ' ') j++;
  317. i = j;
  318. while (source[i] > ' ' && source[i] != '=') i++;
  319. if (i == j) return false;
  320. i -= j;
  321. memcpy(dest, source, j, i * 4, MAX_INI_ENTRY_NAME);
  322. dest[i] = '\0';
  323. return true;
  324. }
  325. /*----------------------------------------------------------------------------*-
  326. Function:
  327. INI_GetEntryText
  328. Params:
  329. source - The string you want to get an entry from.
  330. dest - The place you want to store the entry to
  331. Return:
  332. -
  333. Notes:
  334. -*----------------------------------------------------------------------------*/
  335. stock INI_GetEntryText(source[], dest[], i)
  336. {
  337. while (source[i] && (source[i] <= ' ' || source[i] == '=')) i++;
  338. dest[0] = 1;
  339. dest[1] = '\0';
  340. if (!source[i]) return;
  341. strcpy(dest, source[i], MAX_INI_ENTRY_TEXT);
  342. }
  343. /*----------------------------------------------------------------------------*-
  344. Function:
  345. INI_ParseFile
  346. Params:
  347. filename[] - The file to load.
  348. remoteFormat[] - The format string to generate the remote function to
  349. pass the data to once loaded.
  350. bool:bFileFirst - The order of the remoteFormat parameters.
  351. bool:bExtra - Send additional data.
  352. extra - Additional data to send.
  353. bLocal - Call local functions instead of gloabal ones.
  354. bPassTag - Pass the tag as an extra parameter not the function name.
  355. Return:
  356. -
  357. Notes:
  358. bFileFirst sets the order and inclusion of the possible remoteFormat
  359. parameters. If true the format will add the filename first then the
  360. current tag, if false the order will be reversed. This can also be used
  361. to exclude one or the other from the function name by setting the required
  362. parameter to be entered first and then only haing one %s in the format
  363. sting. The default order is tag first for languages compatability.
  364. -*----------------------------------------------------------------------------*/
  365. stock bool:INI_ParseFile(filename[], remoteFormat[], bool:bFileFirst = false, bool:bExtra = false, extra = 0, bool:bLocal = true, bool:bPassTag = false)
  366. {
  367. new
  368. File:f;
  369. if (!(f = fopen(filename, io_read))) return false;
  370. new
  371. line[MAX_INI_LINE],
  372. tagName[MAX_INI_STRING],
  373. function[MAX_INI_STRING],
  374. comment,
  375. pos;
  376. // Strip the extension from the filename.
  377. comment = chrfind('.', filename);
  378. if (comment != -1) filename[comment] = '\0';
  379. // Now reduce it to only the filename, no path.
  380. while (comment != -1)
  381. {
  382. if (filename[comment] == '\\' || filename[comment] == '/')
  383. {
  384. //strcpy(filename, filename[comment + 1], MAX_INI_STRING);
  385. ++comment;
  386. new
  387. i = 0;
  388. while ((filename[i] = filename[comment + i]))
  389. {
  390. ++i;
  391. }
  392. break;
  393. }
  394. --comment;
  395. }
  396. // Set the default tag value.
  397. if (bFileFirst)
  398. {
  399. format(function, sizeof (function), remoteFormat, filename, "");
  400. }
  401. else
  402. {
  403. format(function, sizeof (function), remoteFormat, "", filename);
  404. }
  405. // Now read in the whole data.
  406. while (fread(f, line))
  407. {
  408. StripNL(line);
  409. if (!line[0]) continue;
  410. new
  411. stringIdent[MAX_INI_ENTRY_NAME];
  412. comment = chrfind(';', line);
  413. if (comment != -1)
  414. {
  415. line[comment] = '\0';
  416. }
  417. if (!INI_GetEntryName(line, stringIdent, pos))
  418. {
  419. continue;
  420. }
  421. if (stringIdent[0] == '[' && (comment = chrfind(']', stringIdent)) != -1 && !stringIdent[comment + 1])
  422. {
  423. // Got a tag, update the function to call.
  424. stringIdent[comment] = '\0';
  425. if (bFileFirst)
  426. {
  427. format(function, sizeof (function), remoteFormat, filename, stringIdent[1]);
  428. }
  429. else
  430. {
  431. format(function, sizeof (function), remoteFormat, stringIdent[1], filename);
  432. }
  433. // Skip complex checks where possible.
  434. if (bLocal && funcidx(function) == -1)
  435. {
  436. function[0] = '\0';
  437. }
  438. else if (bPassTag)
  439. {
  440. strcpy(tagName, stringIdent[1], MAX_INI_STRING);
  441. }
  442. }
  443. else if (function[0])
  444. {
  445. new
  446. stringText[MAX_INI_ENTRY_TEXT];
  447. INI_GetEntryText(line, stringText, pos);
  448. // Read in a value - distribute it as required.
  449. if (bLocal)
  450. {
  451. if (bExtra)
  452. {
  453. if (bPassTag)
  454. {
  455. CallLocalFunction(function, "isss", extra, tagName, stringIdent, stringText);
  456. }
  457. else
  458. {
  459. CallLocalFunction(function, "iss", extra, stringIdent, stringText);
  460. }
  461. }
  462. else
  463. {
  464. if (bPassTag)
  465. {
  466. CallLocalFunction(function, "sss", tagName, stringIdent, stringText);
  467. }
  468. else
  469. {
  470. CallLocalFunction(function, "ss", stringIdent, stringText);
  471. }
  472. }
  473. }
  474. else
  475. {
  476. if (bExtra)
  477. {
  478. if (bPassTag)
  479. {
  480. CallRemoteFunction(function, "isss", extra, tagName, stringIdent, stringText);
  481. }
  482. else
  483. {
  484. CallRemoteFunction(function, "iss", extra, stringIdent, stringText);
  485. }
  486. }
  487. else
  488. {
  489. if (bPassTag)
  490. {
  491. CallRemoteFunction(function, "sss", tagName, stringIdent, stringText);
  492. }
  493. else
  494. {
  495. CallRemoteFunction(function, "ss", stringIdent, stringText);
  496. }
  497. }
  498. }
  499. }
  500. }
  501. fclose(f);
  502. return true;
  503. }
  504. /*----------------------------------------------------------------------------*-
  505. Function:
  506. INI_Load
  507. Params:
  508. filename[] - The file to load.
  509. bool:bExtra - Send additional data.
  510. extra - Additional data to send.
  511. bLocal - Call local functions instead of gloabal ones.
  512. Return:
  513. INI_ParseFile
  514. Notes:
  515. Wrapper for INI_ParseFile to use standard API features so people can
  516. worry even less. Designed for use with INI_Parse.
  517. -*----------------------------------------------------------------------------*/
  518. stock bool:INI_Load(filename[], bool:bExtra = false, extra = 0, bool:bLocal = true)
  519. {
  520. return INI_ParseFile(filename, "@INI_%s_%s", true, bExtra, extra, bLocal, false);
  521. }
  522. /*----------------------------------------------------------------------------*-
  523. Function:
  524. INI_Open
  525. Params:
  526. filename[] - INI file to open.
  527. Return:
  528. INI - handle to the file or INI_NO_FILE.
  529. Notes:
  530. Doesn't actually open the file, just starts a new buffer if possible.
  531. -*----------------------------------------------------------------------------*/
  532. stock INI:INI_Open(filename[])
  533. {
  534. new
  535. i;
  536. for (i = 0; i < INI_MAX_WRITES; i++)
  537. {
  538. if (!YSI_g_sINIWriteFile[i][0]) break;
  539. }
  540. if (i == INI_MAX_WRITES)
  541. {
  542. return INI_NO_FILE;
  543. }
  544. strcpy(YSI_g_sINIWriteFile[i], filename, sizeof (YSI_g_sINIWriteFile[]));
  545. YSI_g_sINIWritePos[i] = 0;
  546. YSI_g_sINITagPos[i] = 0;
  547. // Reset tags.
  548. YSI_g_sINICurrentTag[i] = 0;
  549. YSI_g_sINIWriteTag[i][0][E_INI_TAGS_NAME][0] = '\0';
  550. YSI_g_sINIWriteTag[i][0][E_INI_TAGS_START] = -1;
  551. /* if (!fexist(filename))
  552. {
  553. new
  554. File:fHnd = fopen(filename, io_write);
  555. if (fHnd)
  556. {
  557. fclose(fHnd);
  558. }
  559. C:1(else printf("*** Internal Error: Could not create target file"););
  560. }*/
  561. return INI:i;
  562. }
  563. /*----------------------------------------------------------------------------*-
  564. Function:
  565. INI_Close
  566. Params:
  567. INI:file - Handle to the ini to close.
  568. Return:
  569. -
  570. Notes:
  571. Writes any outstanding buffer data to the file and ends the stream.
  572. -*----------------------------------------------------------------------------*/
  573. stock INI_Close(INI:file)
  574. {
  575. if (YSI_g_sINIWritePos[_:file]) INI_WriteBuffer(file);
  576. YSI_g_sINIWriteFile[_:file][0] = '\0';
  577. }
  578. /*----------------------------------------------------------------------------*-
  579. Function:
  580. INI_SetTag
  581. Params:
  582. INI:file - INI file handle to write to.
  583. tag[] - Name of the new file subsection for subsequent data to write to.
  584. Return:
  585. -
  586. Notes:
  587. Sets a new [tag] section header. Subsequent data is written under this
  588. header. Uses lists for constant tag switching and checks the tag doesn't
  589. already exist.
  590. -*----------------------------------------------------------------------------*/
  591. stock INI_SetTag(INI:file, tag[])
  592. {
  593. if (file < INI:0 || file >= INI:INI_MAX_WRITES) return;
  594. new
  595. pos = YSI_g_sINITagPos[_:file];
  596. for (new i = 0; i < pos; i++)
  597. {
  598. if (YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_NAME][0] && !strcmp(tag, YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_NAME], true))
  599. {
  600. YSI_g_sINICurrentTag[_:file] = i;
  601. return;
  602. }
  603. }
  604. // Allow untagged data (not sure how it will go down...).
  605. /*if (pos == 0 && YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_START] != -1)
  606. {
  607. ++pos;
  608. ++YSI_g_sINITagPos[_:file];
  609. }*/
  610. if (pos >= MAX_INI_TAGS)
  611. {
  612. if (!INI_WriteBuffer(file)) return;
  613. // Loop.
  614. pos = 0;
  615. }
  616. //strcpy(YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_NAME], tag, MAX_INI_TAG);
  617. strpack(YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_NAME], tag, MAX_INI_TAG);
  618. YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_START] = -1;
  619. YSI_g_sINICurrentTag[_:file] = pos;
  620. ++YSI_g_sINITagPos[_:file];
  621. }
  622. /*----------------------------------------------------------------------------*-
  623. Function:
  624. INI_AddToBuffer
  625. Params:
  626. INI:file - INI file to write to.
  627. name[] - Data name to write.
  628. data[] - Data to write.
  629. Return:
  630. -
  631. Notes:
  632. First checks the name doesn't already exist under the current tag header
  633. and if it does overwrites the current value. If not checks there's room
  634. in the buffer to write to and purges the buffer if not. Finally saves the
  635. data in the buffer for writing when required and adds the data to the
  636. relevant list for tag inclusion.
  637. -*----------------------------------------------------------------------------*/
  638. static stock INI_AddToBuffer(INI:file, name[], data[])
  639. {
  640. if (file < INI:0 || file >= INI:INI_MAX_WRITES)
  641. {
  642. return 0;
  643. }
  644. if (!YSI_g_sINITagPos[_:file])
  645. {
  646. // Tagless data.
  647. YSI_g_sINITagPos[_:file] = 1;
  648. YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_START] = -1;
  649. YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_NAME][0] = 0;
  650. }
  651. new
  652. pos = YSI_g_sINIWritePos[_:file],
  653. tmptag = YSI_g_sINICurrentTag[_:file],
  654. start = YSI_g_sINIWriteTag[_:file][tmptag][E_INI_TAGS_START];
  655. while (start != -1)
  656. {
  657. if (!strcmp(name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NAME], true))
  658. {
  659. strcpy(YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT], data, MAX_INI_ENTRY_TEXT);
  660. //strpack(YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT], data, MAX_INI_ENTRY_TEXT);
  661. return 1;
  662. }
  663. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  664. }
  665. if (pos >= INI_BUFFER_SIZE)
  666. {
  667. if (!INI_WriteBuffer(file))
  668. {
  669. return 0;
  670. }
  671. INI_SetTag(file, YSI_g_sINIWriteTag[_:file][tmptag][E_INI_TAGS_NAME]);
  672. pos = 0;
  673. }
  674. new
  675. curtag = YSI_g_sINICurrentTag[_:file];
  676. if (YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_START] == -1)
  677. {
  678. YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_START] = pos;
  679. }
  680. else
  681. {
  682. YSI_g_sINIWriteBuffer[_:file][YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_LAST]][E_INI_WRITE_NEXT] = pos;
  683. }
  684. strcpy(YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_NAME], name, MAX_INI_ENTRY_NAME);
  685. //strpack(YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_NAME], name, MAX_INI_ENTRY_NAME);
  686. strcpy(YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_TEXT], data, MAX_INI_ENTRY_TEXT);
  687. YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_NEXT] = -1;
  688. YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_LAST] = pos;
  689. YSI_g_sINIWritePos[_:file]++;
  690. return 1;
  691. }
  692. /*----------------------------------------------------------------------------*-
  693. Function:
  694. INI_RemoveEntry
  695. Params:
  696. INI:file - File to write to.
  697. name[] - Item to remove.
  698. Return:
  699. -
  700. Notes:
  701. Wrapper for INI_AddToBuffer for removing data.
  702. -*----------------------------------------------------------------------------*/
  703. stock INI_RemoveEntry(INI:file, name[])
  704. {
  705. static
  706. sData[2] = {-1, 0};
  707. INI_AddToBuffer(file, name, sData);
  708. }
  709. /*----------------------------------------------------------------------------*-
  710. Function:
  711. INI_WriteString
  712. Params:
  713. INI:file - File to write to.
  714. name[] - Data name.
  715. data[] - Data.
  716. Return:
  717. -
  718. Notes:
  719. Wrapper for INI_AddToBuffer for strings.
  720. -*----------------------------------------------------------------------------*/
  721. stock INI_WriteString(INI:file, name[], data[])
  722. {
  723. INI_AddToBuffer(file, name, data);
  724. }
  725. /*----------------------------------------------------------------------------*-
  726. Function:
  727. INI_WriteInt
  728. Params:
  729. INI:file - File to write to.
  730. name[] - Data name.
  731. data - Integer data.
  732. Return:
  733. -
  734. Notes:
  735. Wrapper for INI_AddToBuffer for integers.
  736. -*----------------------------------------------------------------------------*/
  737. stock INI_WriteInt(INI:file, name[], data)
  738. {
  739. new
  740. str[12];
  741. valstr(str, data);
  742. INI_AddToBuffer(file, name, str);
  743. }
  744. /*----------------------------------------------------------------------------*-
  745. Function:
  746. INI_WriteHex
  747. Params:
  748. INI:file - File to write to.
  749. name[] - Data name.
  750. data - Hex data.
  751. Return:
  752. -
  753. Notes:
  754. Wrapper for INI_AddToBuffer for integers to be written as hex values.
  755. -*----------------------------------------------------------------------------*/
  756. stock INI_WriteHex(INI:file, name[], data)
  757. {
  758. static const
  759. sc_values[] = !"0123456789ABCDEF";
  760. new
  761. str[11],
  762. i = 9;
  763. do
  764. {
  765. str[i--] = sc_values{data & 0x0F};
  766. data >>>= 4;
  767. }
  768. while (data);
  769. str[i--] = 'x';
  770. str[i] = '0';
  771. INI_AddToBuffer(file, name, str[i]);
  772. }
  773. /*----------------------------------------------------------------------------*-
  774. Function:
  775. INI_WriteBin
  776. Params:
  777. INI:file - File to write to.
  778. name[] - Data name.
  779. data - Binary data.
  780. Return:
  781. -
  782. Notes:
  783. Wrapper for INI_AddToBuffer for integers to be written as binary values.
  784. -*----------------------------------------------------------------------------*/
  785. stock INI_WriteBin(INI:file, name[], data)
  786. {
  787. // Do four values at once for speed. This uses a packed string and unpacks
  788. // it so that we can quickly write four values at once.
  789. static const
  790. sc_values[] = !"0000000100100011010001010110011110001001101010111100110111101111";
  791. new
  792. str[35],
  793. i = 10;
  794. do
  795. {
  796. str[--i] = sc_values[data & 0x0F];
  797. data >>>= 4;
  798. }
  799. while (data);
  800. // Convert the coalesced values to individual values.
  801. strunpack(str[i], str[i], 33);
  802. str[--i] = 'b';
  803. str[--i] = '0';
  804. INI_AddToBuffer(file, name, str[i]);
  805. }
  806. /*----------------------------------------------------------------------------*-
  807. Function:
  808. INI_WriteBool
  809. Params:
  810. INI:file - File to write to.
  811. name[] - Data name.
  812. data - Boolean data.
  813. Return:
  814. -
  815. Notes:
  816. Wrapper for INI_AddToBuffer for booleans.
  817. -*----------------------------------------------------------------------------*/
  818. stock INI_WriteBool(INI:file, name[], bool:data)
  819. {
  820. if (data)
  821. {
  822. INI_AddToBuffer(file, name, "true");
  823. }
  824. else
  825. {
  826. INI_AddToBuffer(file, name, "false");
  827. }
  828. }
  829. /*----------------------------------------------------------------------------*-
  830. Function:
  831. INI_WriteFloat
  832. Params:
  833. INI:file - File to write to.
  834. name[] - Data name.
  835. Float:data - Float data.
  836. accuracy - number of decimal places to write.
  837. Return:
  838. -
  839. Notes:
  840. Wrapper for INI_AddToBuffer for floats. Uses custom code instead of
  841. format() as it's actually faster for something simple like this.
  842. -*----------------------------------------------------------------------------*/
  843. stock INI_WriteFloat(INI:file, name[], Float:data, accuracy = 6)
  844. {
  845. new
  846. str[32];
  847. format(str, sizeof (str), "%.*f", accuracy, data);
  848. INI_AddToBuffer(file, name, str);
  849. }
  850. /*----------------------------------------------------------------------------*-
  851. Function:
  852. INI_WriteBuffer
  853. Params:
  854. INI:file - INI stream to write to file.
  855. Return:
  856. Success/fail.
  857. Notes:
  858. Opens the required file for reading and a temp file for read/writing. Goes
  859. through the entire file reading all contained data. If it reaches a tag
  860. line ([tag_name]) it dumps any unwritten data from the last tag (if there
  861. was one) and starts processing the new tag. While a tag is being processed
  862. every line is compared against the UNWRITTEN new data for that tag in the
  863. buffer, if they're the same it writes the new data instead (it also writes
  864. any comments which were after the data in the original line back), else it
  865. writes the original line back.
  866. Once all the new data is written to the temp file any tags which haven't
  867. been processed at all (i.e. were not found in the original file) are
  868. written to the temp file along with all their data. The original file is
  869. then destroyed and reopend and all the data copied out from the temp file
  870. to the newly opened original file, closed and saved.
  871. -*----------------------------------------------------------------------------*/
  872. static stock INI_WriteBuffer(INI:file)
  873. {
  874. if (_:file < 0 || _:file >= INI_MAX_WRITES) return 0;
  875. new
  876. //File:buffer = fopen("_temp_ysi_user_file_.ysi", io_write),
  877. File:buffer = ftemp(),
  878. File:source = fopen(YSI_g_sINIWriteFile[_:file], io_read);
  879. if (buffer)
  880. {
  881. new
  882. line[MAX_INI_LINE],
  883. BitArray:read<INI_BUFFER_SIZE>, //:read[INI_BUFFER_BITS],
  884. writing = 0,
  885. //Bit:tagswritten,
  886. BitArray:tagswritten<MAX_INI_TAGS>,
  887. tagpos = YSI_g_sINITagPos[_:file],
  888. start = -1,
  889. blank;
  890. if (source)
  891. {
  892. // Write tagless data.
  893. if (!YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_NAME][0])
  894. {
  895. start = YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_START];
  896. // Will be -1 if there is no tagless data.
  897. if (start != -1)
  898. {
  899. writing = 1;
  900. Bit_Set(tagswritten, 0, true);
  901. }
  902. }
  903. while (fread(source, line))
  904. {
  905. new
  906. pos = 1;
  907. for (new i = 0; line[i]; i++)
  908. {
  909. if (line[i] == ';')
  910. {
  911. goto INI_WriteBuffer_cont1;
  912. }
  913. else if (line[i] > ' ')
  914. {
  915. // Determine that the line is not blank.
  916. pos = 0;
  917. break;
  918. }
  919. }
  920. if (pos)
  921. {
  922. blank++;
  923. continue;
  924. }
  925. if (line[0] == '[' && (pos = chrfind(']', line)) != -1 && endofline(line, pos + 1))
  926. {
  927. pos--;
  928. writing = 0;
  929. new
  930. form[MAX_INI_LINE];
  931. // Reached a new tag - flush the rest of the last tag.
  932. while (start != -1)
  933. {
  934. if (!Bit_GetBit(read, start))
  935. {
  936. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] != -1)
  937. {
  938. format(form, sizeof (form), "%s = %s" INI_NEW_LINE, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NAME], YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT]);
  939. fwrite(buffer, form);
  940. }
  941. Bit_Set(read, start, true);
  942. }
  943. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  944. }
  945. while (blank--) fwrite(buffer, INI_NEW_LINE);
  946. blank = 0;
  947. // Note the start of the new tag.
  948. for (new j = 0; j < tagpos; j++)
  949. {
  950. //if (!Bit_Get(tagswritten, j) && !YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME][pos] && !strcmp(YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME], line[1], true, pos))
  951. if (!Bit_Get(tagswritten, j) && !strcmp(YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME], line[1], true, pos))
  952. {
  953. writing = 1;
  954. start = YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_START];
  955. //tagswritten |= Bit:(1 << j);
  956. Bit_Set(tagswritten, j, true);
  957. break;
  958. }
  959. }
  960. }
  961. else if (writing)
  962. {
  963. new
  964. name[MAX_INI_ENTRY_NAME],
  965. temp,
  966. liststart = start;
  967. INI_GetEntryName(line, name, temp);
  968. pos = chrfind(';', line, temp);
  969. while (blank--) fwrite(buffer, INI_NEW_LINE);
  970. blank = 0;
  971. while (start != -1)
  972. {
  973. if (!Bit_GetBit(read, start) && !strcmp(name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NAME]))
  974. {
  975. Bit_Set(read, start, true, INI_BUFFER_SIZE);
  976. // Delete the current entry from the file, as per
  977. // our request.
  978. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] == -1)
  979. {
  980. // Can't use "continue" as we're in an inner
  981. // loop.
  982. goto INI_WriteBuffer_cont2;
  983. }
  984. if (pos != -1)
  985. {
  986. format(line, sizeof (line), "%s = %s %s", name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT], line[pos]);
  987. }
  988. else
  989. {
  990. format(line, sizeof (line), "%s = %s" INI_NEW_LINE, name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT]);
  991. }
  992. //start = -1;
  993. break;
  994. }
  995. else
  996. {
  997. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  998. }
  999. }
  1000. /*if (start == -1)
  1001. {
  1002. // Wasn't found.
  1003. start = strlen(line);
  1004. if (line[start - 1] > ' ')
  1005. {
  1006. strcat(line, INI_NEW_LINE);
  1007. }
  1008. }*/
  1009. start = liststart;
  1010. }
  1011. INI_WriteBuffer_cont1:
  1012. fwrite(buffer, line);
  1013. INI_WriteBuffer_cont2:
  1014. }
  1015. // Write any data from this tag group not in the original file.
  1016. // I.e. flush the final read tag.
  1017. while (start != -1)
  1018. {
  1019. if (!Bit_GetBit(read, start))
  1020. {
  1021. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] != -1)
  1022. {
  1023. format(line, sizeof (line), "%s = %s" INI_NEW_LINE, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NAME], YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT]);
  1024. fwrite(buffer, line);
  1025. }
  1026. }
  1027. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  1028. }
  1029. fclose(source);
  1030. }
  1031. // Write any tag groups not found in the original file.
  1032. for (new j = 0; j < tagpos; j++)
  1033. {
  1034. //if (!(tagswritten & Bit:(1 << j)))
  1035. if (!Bit_Get(tagswritten, j))
  1036. {
  1037. start = YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_START];
  1038. if (j || YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_NAME][0])
  1039. {
  1040. strunpack(line, YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME]);
  1041. format(line, sizeof (line), "[%s]" INI_NEW_LINE, line);
  1042. fwrite(buffer, line);
  1043. }
  1044. while (start != -1)
  1045. {
  1046. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] != -1)
  1047. {
  1048. format(line, sizeof (line), "%s = %s" INI_NEW_LINE, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NAME], YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT]);
  1049. fwrite(buffer, line);
  1050. }
  1051. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  1052. }
  1053. }
  1054. }
  1055. //fclose(buffer);
  1056. // This renames the file back (by copying all the data).
  1057. fremove(YSI_g_sINIWriteFile[_:file]);
  1058. #if defined _inc_TODO_COPY_INC
  1059. #else
  1060. #if defined _int_TODO_OTHER_COPY_INC
  1061. #else
  1062. source = fopen(YSI_g_sINIWriteFile[_:file], io_write);
  1063. //buffer = fopen("_temp_ysi_user_file_.ysi", io_read);
  1064. fseek(buffer);
  1065. if (source) // && buffer)
  1066. {
  1067. while (fread(buffer, line)) fwrite(source, line);
  1068. /*new
  1069. val;
  1070. printf("start: %d", _:E_INI_WRITE * INI_BUFFER_SIZE);
  1071. while ((val = fblockread(buffer, YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME], _:E_INI_WRITE * INI_BUFFER_SIZE)))
  1072. {
  1073. printf("%d %d", val, strlen(YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME]));
  1074. printf("%s", YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME]);
  1075. fblockwrite(source, YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME], val);
  1076. }*/
  1077. fclose(buffer);
  1078. fclose(source);
  1079. }
  1080. //fremove("_temp_ysi_user_file_.ysi");
  1081. #endif
  1082. #endif
  1083. // Reset the write buffer.
  1084. YSI_g_sINITagPos[_:file] = 0;
  1085. YSI_g_sINIWritePos[_:file] = 0;
  1086. YSI_g_sINICurrentTag[_:file] = 0;
  1087. return 1;
  1088. }
  1089. return 0;
  1090. }