y_ini.inc 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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 YSI ini include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  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, whose limits continue to amaze me!
  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 broadcastfunc 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 "internal\y_version"
  152. //#tryinclude <sscanf>
  153. #include "y_utils"
  154. #include "y_bit"
  155. #include "y_debug"
  156. #include "y_inline"
  157. #include "internal\y_natives"
  158. #define MAX_INI_LINE (MAX_INI_ENTRY_NAME + MAX_INI_ENTRY_TEXT + 32)
  159. #define INI_NO_FILE (INI:-1)
  160. #if !defined INI_NEW_LINE
  161. #define INI_NEW_LINE "\r\n"
  162. #endif
  163. #if !defined MAX_INI_TAG
  164. #define MAX_INI_TAG (32)
  165. #endif
  166. #if !defined MAX_INI_ENTRY_NAME
  167. #define MAX_INI_ENTRY_NAME (32)
  168. #endif
  169. #if !defined MAX_INI_ENTRY_TEXT
  170. #define MAX_INI_ENTRY_TEXT YSI_MAX_STRING
  171. #endif
  172. #if !defined INI_MAX_WRITES
  173. // I doubt many people will use the multiple ini function.
  174. #define INI_MAX_WRITES (2)
  175. #endif
  176. #if !defined INI_BUFFER_SIZE
  177. #define INI_BUFFER_SIZE (64)
  178. #endif
  179. /*#if INI_BUFFER_SIZE <= 32
  180. #define INI_BUFFER_BITS 2
  181. #else
  182. #define INI_BUFFER_BITS Bit_Bits(INI_BUFFER_SIZE)
  183. #endif*/
  184. #if !defined MAX_INI_TAGS
  185. #define MAX_INI_TAGS 3
  186. /*#else
  187. #if MAX_INI_TAGS > 32
  188. #error Current code only supports up to 32 buffer tags
  189. #endif*/
  190. #endif
  191. //#define MAX_INI_STRING (64)
  192. #define MAX_INI_STRING FUNCTION_LENGTH
  193. #define Y_INI_WRITE_ARRAY_SIZE ((MAX_INI_ENTRY_TEXT - 1) / 16 * 16)
  194. enum E_INI_WRITE
  195. {
  196. E_INI_WRITE_NAME[MAX_INI_ENTRY_NAME],
  197. E_INI_WRITE_TEXT[MAX_INI_ENTRY_TEXT],
  198. E_INI_WRITE_NEXT
  199. }
  200. enum E_INI_TAGS
  201. {
  202. E_INI_TAGS_NAME[MAX_INI_TAG char],
  203. E_INI_TAGS_START,
  204. E_INI_TAGS_LAST
  205. }
  206. static stock
  207. YSI_g_sINIWriteBuffer[INI_MAX_WRITES][INI_BUFFER_SIZE][E_INI_WRITE],
  208. YSI_g_sINIWritePos[INI_MAX_WRITES],
  209. YSI_g_sINITagPos[INI_MAX_WRITES],
  210. YSI_g_sINICurrentTag[INI_MAX_WRITES],
  211. YSI_g_sINIWriteTag[INI_MAX_WRITES][MAX_INI_TAGS][E_INI_TAGS],
  212. YSI_g_sINIWriteFile[INI_MAX_WRITES][YSI_MAX_STRING];
  213. #define INI_Parse(%1,%2) \
  214. forward @INI_%1_%2(name[], value[]); \
  215. @INI_%1_%2(name[], value[])
  216. #define INI:%0[%1](%2) \
  217. forward @INI_%0_%1(%2); \
  218. @INI_%0_%1(%2)
  219. #if defined _inc_sscanf && 0
  220. /*------------------------------------------------------------------------*-
  221. Function:
  222. INI_Int
  223. Params:
  224. name[] - Name of the INI textual identifier.
  225. function - Function to call with integer value.
  226. Return:
  227. function().
  228. Notes:
  229. -
  230. -*------------------------------------------------------------------------*/
  231. #define INI_Int(%1,%2) \
  232. if(!strcmp((%1),name,true)&&!sscanf(value,"d",%2))return;
  233. //if(!strcmp(){%2=floatstr(value);return;}
  234. //if (!strcmp((%1), name, true)) return %2(strval(value))
  235. /*------------------------------------------------------------------------*-
  236. Function:
  237. INI_Float
  238. Params:
  239. name[] - Name of the INI textual identifier.
  240. function - Function to call with float value.
  241. Return:
  242. function().
  243. Notes:
  244. -
  245. -*------------------------------------------------------------------------*/
  246. #define INI_Float(%1,%2) \
  247. if(!strcmp((%1),name,true)&&!sscanf(value,"f",%2))return;
  248. //if (!strcmp((%1), name, true)) return %2(floatstr(value))
  249. /*------------------------------------------------------------------------*-
  250. Function:
  251. INI_Hex
  252. Params:
  253. name[] - Name of the INI textual identifier.
  254. function - Function to call with hex value.
  255. Return:
  256. function().
  257. Notes:
  258. -
  259. -*------------------------------------------------------------------------*/
  260. #define INI_Hex(%1,%2) \
  261. if(!strcmp((%1),name,true)&&!sscanf(value,"h",%2))return;
  262. //if (!strcmp((%1), name, true)) return %2(hexstr(value))
  263. /*------------------------------------------------------------------------*-
  264. Function:
  265. INI_Bin
  266. Params:
  267. name[] - Name of the INI textual identifier.
  268. function - Function to call with binary value.
  269. Return:
  270. function().
  271. Notes:
  272. -
  273. -*------------------------------------------------------------------------*/
  274. #define INI_Bin(%1,%2) \
  275. if(!strcmp((%1),name,true)&&!sscanf(value,"l",%2))return;
  276. //if (!strcmp((%1), name, true)) return %2(binstr(value))
  277. /*------------------------------------------------------------------------*-
  278. Function:
  279. INI_String
  280. Params:
  281. name[] - Name of the INI textual identifier.
  282. function - Function to call with string value.
  283. Return:
  284. function().
  285. Notes:
  286. -
  287. -*------------------------------------------------------------------------*/
  288. #define INI_String(%1,%2,%3) \
  289. if(!strcmp((%1),name,true)&&!sscanf(value,"s["#%3"]",%2))return;
  290. //if (!strcmp((%1), name, true)) return %2(value)
  291. #else
  292. #define INI_Int(%1,%2) \
  293. if(!strcmp((%1),name,true))return %2=strval(value)
  294. #define INI_Float(%1,%2) \
  295. if(!strcmp((%1),name,true))return _:(%2=floatstr(value))
  296. #define INI_Hex(%1,%2) \
  297. if(!strcmp((%1),name,true))return %2=hexstr(value)
  298. #define INI_Bin(%1,%2) \
  299. if(!strcmp((%1),name,true))return %2=binstr(value)
  300. #define INI_Bool(%1,%2) \
  301. if(!strcmp((%1),name,true))return %2=boolstr(value)
  302. #define INI_String(%1,%2,%3) \
  303. if(!strcmp((%1),name,true))return strcpy(%2,value,%3)
  304. #endif
  305. /*----------------------------------------------------------------------------*\
  306. Function:
  307. INI_GetEntryName
  308. Params:
  309. source - The string you want to get an entry name from.
  310. dest - The place you want to store the entry name to
  311. Return:
  312. bool: Found the name correctly.
  313. Notes:
  314. -
  315. \*----------------------------------------------------------------------------*/
  316. stock bool:INI_GetEntryName(source[], dest[], &i)
  317. {
  318. P:3("bool:INI_GetEntryName called: \"%s\", %i, %i", source, dest, i);
  319. new
  320. j;
  321. while (source[j] && source[j] <= ' ') j++;
  322. P:7("bool:INI_GetEntryName: progress 0");
  323. i = j;
  324. while (source[i] > ' ' && source[i] != '=') i++;
  325. P:7("bool:INI_GetEntryName: progress 1");
  326. if (i == j) return false;
  327. P:7("bool:INI_GetEntryName: progress 2");
  328. i -= j;
  329. memcpy(dest, source, j, i * 4, MAX_INI_ENTRY_NAME);
  330. dest[i] = '\0';
  331. P:7("bool:INI_GetEntryName: progress 3, %s, %i, %s", dest, i, source);
  332. return true;
  333. }
  334. /*----------------------------------------------------------------------------*\
  335. Function:
  336. INI_GetEntryText
  337. Params:
  338. source - The string you want to get an entry from.
  339. dest - The place you want to store the entry to
  340. Return:
  341. -
  342. Notes:
  343. \*----------------------------------------------------------------------------*/
  344. stock INI_GetEntryText(source[], dest[], i)
  345. {
  346. P:3("INI_GetEntryText called: \"%s\", %i, %i", source, dest, i);
  347. while ('\0' < source[i] <= ' ')
  348. {
  349. ++i;
  350. }
  351. P:7("bool:INI_GetEntryText: progress 1");
  352. if (source[i] == '=')
  353. {
  354. while ('\0' < source[++i] <= ' ')
  355. {
  356. // Do nothing.
  357. }
  358. }
  359. P:7("bool:INI_GetEntryText: progress 2");
  360. while (source[i] && (source[i] <= ' ' || source[i] == '=')) i++;
  361. //while (source[i] && (source[i] <= ' ' || source[i] == '=')) i++;
  362. P:7("bool:INI_GetEntryText: progress 3");
  363. dest[0] = 1;
  364. dest[1] = '\0';
  365. if (!source[i]) return;
  366. P:7("bool:INI_GetEntryText: progress 4, %s, %d, %s", dest, i, source);
  367. strcpy(dest, source[i], MAX_INI_ENTRY_TEXT);
  368. }
  369. /*----------------------------------------------------------------------------*\
  370. Function:
  371. INI_ParseFile
  372. Params:
  373. filename[] - The file to load.
  374. remoteFormat[] - The format string to generate the remote function to
  375. pass the data to once loaded.
  376. bool:bFileFirst - The order of the remoteFormat parameters.
  377. bool:bExtra - Send additional data.
  378. extra - Additional data to send.
  379. bLocal - Call local functions instead of gloabal ones.
  380. bPassTag - Pass the tag as an extra parameter not the function name.
  381. bFilter - Apply the tag name filter to all tags or just prefixed ones?
  382. filter - Text to use to search for which tags to load.
  383. Return:
  384. -
  385. Notes:
  386. bFileFirst sets the order and inclusion of the possible remoteFormat
  387. parameters. If true the format will add the filename first then the
  388. current tag, if false the order will be reversed. This can also be used
  389. to exclude one or the other from the function name by setting the required
  390. parameter to be entered first and then only haing one %s in the format
  391. sting. The default order is tag first for languages compatability.
  392. \*----------------------------------------------------------------------------*/
  393. stock bool:INI_ParseFile(fname[], remoteFormat[], bool:bFileFirst = false, bool:bExtra = false, extra = 0, bool:bLocal = true, bool:bPassTag = false, bool:bFilter = true, filter[] = "")
  394. {
  395. P:3("bool:INI_ParseFile called: \"%s\", \"%s\", %i, %i, %i, %i, %i", fname, remoteFormat, bFileFirst, bExtra, extra, bLocal, bPassTag);
  396. new
  397. File:f,
  398. filename[64],
  399. callbackFormat;
  400. if (bExtra)
  401. {
  402. if (bPassTag) callbackFormat = _F<isss>;
  403. else callbackFormat = _F<iss>;
  404. }
  405. else
  406. {
  407. if (bPassTag) callbackFormat = _F<sss>;
  408. else callbackFormat = _F<ss>;
  409. }
  410. strcpy(filename, fname, 64);
  411. if (!(f = fopen(filename, io_read))) return false;
  412. P:5("INI_ParseFile: open");
  413. new
  414. line[MAX_INI_LINE],
  415. tagName[MAX_INI_STRING] = "\1\0",
  416. function[MAX_INI_STRING],
  417. comment,
  418. pos,
  419. bool:bCallback,
  420. callback[E_CALLBACK_DATA];
  421. // Strip the extension from the filename.
  422. comment = chrfind('.', filename);
  423. if (comment != -1) filename[comment] = '\0';
  424. // Now reduce it to only the filename, no path.
  425. while (comment != -1)
  426. {
  427. if (filename[comment] == '\\' || filename[comment] == '/')
  428. {
  429. //strcpy(filename, filename[comment + 1], MAX_INI_STRING);
  430. ++comment;
  431. new
  432. i = 0;
  433. while ((filename[i] = filename[comment + i]))
  434. {
  435. ++i;
  436. }
  437. break;
  438. }
  439. --comment;
  440. }
  441. P:5("INI_ParseFile: filename only");
  442. // Set the default tag value.
  443. if (bFileFirst)
  444. {
  445. format(function, sizeof (function), remoteFormat, filename, "");
  446. }
  447. else
  448. {
  449. format(function, sizeof (function), remoteFormat, "", filename);
  450. }
  451. if (bLocal)
  452. {
  453. bCallback = bool:Callback_Get(callback_tag:function, callback, callbackFormat);
  454. }
  455. // Now read in the whole data.
  456. P:5("INI_ParseFile: parse");
  457. while (fread(f, line))
  458. {
  459. StripNL(line);
  460. if (!line[0]) continue;
  461. new
  462. stringIdent[MAX_INI_ENTRY_NAME];
  463. comment = chrfind(';', line);
  464. if (comment != -1)
  465. {
  466. line[comment] = '\0';
  467. }
  468. if (!INI_GetEntryName(line, stringIdent, pos))
  469. {
  470. continue;
  471. }
  472. if (stringIdent[0] == '[' && (comment = chrfind(']', stringIdent)) != -1 && !stringIdent[comment + 1])
  473. {
  474. // Got a tag, update the function to call.
  475. stringIdent[comment] = '\0';
  476. P:5("INI_ParseFile: Is checkable %d %d %d", stringIdent[1] == '@', stringIdent[2] == '@', (comment = chrfind('-', stringIdent)) != -1);
  477. if (stringIdent[1] == '@' && stringIdent[2] == '@' && (comment = chrfind('-', stringIdent)) != -1)
  478. {
  479. // We are keeping the ability to filter by tag names, but now
  480. // we are just using the existing unique tag load code in y_ini.
  481. stringIdent[comment] = '\0';
  482. P:5("INI_ParseFile: SingleTag check");
  483. if (strcmp(filter, stringIdent[3]))
  484. {
  485. P:5("INI_ParseFile: Not this time");
  486. // Skip tags we don't care about. This will not skip
  487. // anything if the passed tag is "" (here I'm (ab)using the
  488. // fact that "strcmp" returns 0 when an empty string is
  489. // involved).
  490. function[0] = '\0';
  491. continue;
  492. }
  493. stringIdent[comment] = '-';
  494. ++comment;
  495. }
  496. else
  497. {
  498. P:5("INI_ParseFile: SingleTag check");
  499. // Apply the filter to non-prefixed tags if desired.
  500. if (bFilter && strcmp(filter, stringIdent[1]))
  501. {
  502. P:5("INI_ParseFile: Not this time");
  503. // Skip tags we don't care about. This will not skip
  504. // anything if the passed tag is "" (here I'm (ab)using the
  505. // fact that "strcmp" returns 0 when an empty string is
  506. // involved).
  507. function[0] = '\0';
  508. continue;
  509. }
  510. comment = 1;
  511. }
  512. if (bFileFirst)
  513. {
  514. format(function, sizeof (function), remoteFormat, filename, stringIdent[comment]);
  515. }
  516. else
  517. {
  518. format(function, sizeof (function), remoteFormat, stringIdent[comment], filename);
  519. }
  520. if (bLocal)
  521. {
  522. bCallback = bool:Callback_Get(callback_tag:function, callback, callbackFormat);
  523. }
  524. P:5("INI_ParseFile: Call it \"%s\", %d, %d", function, bLocal, funcidx(function) == -1);
  525. // Skip complex checks where possible.
  526. if (bLocal && funcidx(function) == -1)
  527. {
  528. // This needs updating for inline functions.
  529. function[0] = '\0';
  530. }
  531. else
  532. {
  533. if (bPassTag)
  534. {
  535. strcpy(tagName, stringIdent[1], MAX_INI_STRING);
  536. }
  537. }
  538. }
  539. else if (function[0])
  540. {
  541. new
  542. stringText[MAX_INI_ENTRY_TEXT];
  543. INI_GetEntryText(line, stringText, pos);
  544. // Read in a value - distribute it as required.
  545. if (bCallback)
  546. {
  547. if (bExtra)
  548. {
  549. if (bPassTag)
  550. {
  551. Callback_Call(callback, extra, tagName, stringIdent, stringText);
  552. //CallLocalFunction(function, "isss", extra, tagName, stringIdent, stringText);
  553. }
  554. else
  555. {
  556. Callback_Call(callback, extra, stringIdent, stringText);
  557. //CallLocalFunction(function, "iss", extra, stringIdent, stringText);
  558. }
  559. }
  560. else
  561. {
  562. if (bPassTag)
  563. {
  564. Callback_Call(callback, tagName, stringIdent, stringText);
  565. //CallLocalFunction(function, "sss", tagName, stringIdent, stringText);
  566. }
  567. else
  568. {
  569. Callback_Call(callback, stringIdent, stringText);
  570. //CallLocalFunction(function, "ss", stringIdent, stringText);
  571. }
  572. }
  573. }
  574. else if (!bLocal)
  575. {
  576. // Can't use inilne functions remotely (it makes no sense at all
  577. // - inline functions come from the call point, and the call
  578. // point is in this local script.
  579. if (bExtra)
  580. {
  581. if (bPassTag)
  582. {
  583. CallRemoteFunction(function, "isss", extra, tagName, stringIdent, stringText);
  584. }
  585. else
  586. {
  587. CallRemoteFunction(function, "iss", extra, stringIdent, stringText);
  588. }
  589. }
  590. else
  591. {
  592. if (bPassTag)
  593. {
  594. CallRemoteFunction(function, "sss", tagName, stringIdent, stringText);
  595. }
  596. else
  597. {
  598. CallRemoteFunction(function, "ss", stringIdent, stringText);
  599. }
  600. }
  601. }
  602. }
  603. }
  604. fclose(f);
  605. return true;
  606. }
  607. /*----------------------------------------------------------------------------*\
  608. Function:
  609. INI_Load
  610. Params:
  611. filename[] - The file to load.
  612. bool:bExtra - Send additional data.
  613. extra - Additional data to send.
  614. bLocal - Call local functions instead of gloabal ones.
  615. Return:
  616. INI_ParseFile
  617. Notes:
  618. Wrapper for INI_ParseFile to use standard API features so people can
  619. worry even less. Designed for use with INI_Parse.
  620. \*----------------------------------------------------------------------------*/
  621. stock bool:INI_Load(filename[], bool:bExtra = false, extra = 0, bool:bLocal = true)
  622. {
  623. P:3("bool:INI_Load called: \"%s\", %i, %i, %i", filename, _:bExtra, extra, _:bLocal);
  624. return INI_ParseFile(filename, "@INI_%s_%s", true, bExtra, extra, bLocal, false);
  625. }
  626. /*----------------------------------------------------------------------------*\
  627. Function:
  628. INI_Open
  629. Params:
  630. filename[] - INI file to open.
  631. Return:
  632. INI - handle to the file or INI_NO_FILE.
  633. Notes:
  634. Doesn't actually open the file, just starts a new buffer if possible.
  635. \*----------------------------------------------------------------------------*/
  636. stock INI:INI_Open(filename[])
  637. {
  638. if (ftouch(filename) == -1)
  639. {
  640. P:E("INI_Open could not find or create file %s", filename);
  641. }
  642. P:3("INI:INI_Open called: \"%s\"", filename);
  643. new
  644. i;
  645. for (i = 0; i < INI_MAX_WRITES; i++)
  646. {
  647. if (!YSI_g_sINIWriteFile[i][0]) break;
  648. }
  649. if (i == INI_MAX_WRITES)
  650. {
  651. return INI_NO_FILE;
  652. }
  653. strcpy(YSI_g_sINIWriteFile[i], filename, sizeof (YSI_g_sINIWriteFile[]));
  654. YSI_g_sINIWritePos[i] = 0;
  655. YSI_g_sINITagPos[i] = 0;
  656. // Reset tags.
  657. YSI_g_sINICurrentTag[i] = 0;
  658. YSI_g_sINIWriteTag[i][0][E_INI_TAGS_NAME][0] = '\0';
  659. YSI_g_sINIWriteTag[i][0][E_INI_TAGS_START] = -1;
  660. /* if (!fexist(filename))
  661. {
  662. new
  663. File:fHnd = fopen(filename, io_write);
  664. if (fHnd)
  665. {
  666. fclose(fHnd);
  667. }
  668. else P:E("Could not create target file");
  669. }*/
  670. return INI:i;
  671. }
  672. /*----------------------------------------------------------------------------*\
  673. Function:
  674. INI_Close
  675. Params:
  676. INI:file - Handle to the ini to close.
  677. Return:
  678. -
  679. Notes:
  680. Writes any outstanding buffer data to the file and ends the stream.
  681. \*----------------------------------------------------------------------------*/
  682. stock INI_Close(INI:file)
  683. {
  684. P:3("INI_Close called: %i", _:file);
  685. if (YSI_g_sINIWritePos[_:file] || YSI_g_sINITagPos[_:file]) INI_WriteBuffer(file);
  686. YSI_g_sINIWriteFile[_:file][0] = '\0';
  687. }
  688. /*----------------------------------------------------------------------------*\
  689. Function:
  690. INI_SetTag
  691. Params:
  692. INI:file - INI file handle to write to.
  693. tag[] - Name of the new file subsection for subsequent data to write to.
  694. Return:
  695. -
  696. Notes:
  697. Sets a new [tag] section header. Subsequent data is written under this
  698. header. Uses lists for constant tag switching and checks the tag doesn't
  699. already exist.
  700. \*----------------------------------------------------------------------------*/
  701. stock INI_SetTag(INI:file, tag[])
  702. {
  703. P:3("INI_SetTag called: %i, \"%s\"", _:file, tag);
  704. if (file < INI:0 || file >= INI:INI_MAX_WRITES) return;
  705. new
  706. pos = YSI_g_sINITagPos[_:file];
  707. for (new i = 0; i < pos; i++)
  708. {
  709. if (YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_NAME][0] && !strcmp(tag, YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_NAME], true))
  710. {
  711. YSI_g_sINICurrentTag[_:file] = i;
  712. return;
  713. }
  714. }
  715. // Allow untagged data (not sure how it will go down...).
  716. /*if (pos == 0 && YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_START] != -1)
  717. {
  718. ++pos;
  719. ++YSI_g_sINITagPos[_:file];
  720. }*/
  721. if (pos >= MAX_INI_TAGS)
  722. {
  723. if (!INI_WriteBuffer(file)) return;
  724. // Loop.
  725. pos = 0;
  726. }
  727. //strcpy(YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_NAME], tag, MAX_INI_TAG);
  728. strpack(YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_NAME], tag, MAX_INI_TAG);
  729. YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_START] = -1;
  730. YSI_g_sINICurrentTag[_:file] = pos;
  731. ++YSI_g_sINITagPos[_:file];
  732. }
  733. /*----------------------------------------------------------------------------*\
  734. Function:
  735. INI_DeleteTag
  736. Params:
  737. INI:file - INI file handle to write to.
  738. tag[] - Name of the whole section to delete.
  739. Return:
  740. -
  741. Notes:
  742. Removes a [tag] section from a file.
  743. \*----------------------------------------------------------------------------*/
  744. stock INI_DeleteTag(INI:file, tag[])
  745. {
  746. P:3("INI_DeleteTag called: %i, \"%s\"", _:file, tag);
  747. if (file < INI:0 || file >= INI:INI_MAX_WRITES) return;
  748. new
  749. pos = YSI_g_sINITagPos[_:file];
  750. for (new i = 0; i < pos; i++)
  751. {
  752. P:6("INI_DeleteTag: %i, \"%s\", \"%s\"", i, tag, unpack(YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_NAME]));
  753. if (YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_NAME][0] && !strcmp(tag, YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_NAME], true))
  754. {
  755. YSI_g_sINICurrentTag[_:file] = i;
  756. YSI_g_sINIWriteTag[_:file][i][E_INI_TAGS_START] = cellmax;
  757. return;
  758. }
  759. }
  760. if (pos >= MAX_INI_TAGS)
  761. {
  762. if (!INI_WriteBuffer(file)) return;
  763. // Loop.
  764. pos = 0;
  765. }
  766. strpack(YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_NAME], tag, MAX_INI_TAG);
  767. YSI_g_sINIWriteTag[_:file][pos][E_INI_TAGS_START] = cellmax;
  768. YSI_g_sINICurrentTag[_:file] = pos;
  769. ++YSI_g_sINITagPos[_:file];
  770. }
  771. stock INI_WriteArray(INI:file, const name[], data[], len)
  772. {
  773. // Write 6 bits at a time, in 3 cell chunks. It takes 16 bytes to record
  774. // three cells with 6 bits per byte.
  775. P:4("INI_WriteArray called");
  776. new
  777. dname[MAX_INI_ENTRY_NAME],
  778. write[Y_INI_WRITE_ARRAY_SIZE + 1],
  779. idx,
  780. wi,
  781. iter;
  782. // Write the length first just so the data exists.
  783. //INI_WriteInt(file, name, len);
  784. valstr(write, len);
  785. INI_AddToBuffer(file, name, write);
  786. write[0] = '\0';
  787. while (idx + 3 < len)
  788. {
  789. // Store all the data fast.
  790. //write[wi++] = (((data[idx] & 0xFC000000) >>> 2) | ((data[idx] & 0x03F00000) >> 4) | ((data[idx] & 0x000FC000) >> 6) | ((data[idx] & 0x00003F00) >> 8))
  791. write[wi++] = ((data[idx] & 0xFC000000) >>> 26) + '>';
  792. write[wi++] = ((data[idx] & 0x03F00000) >>> 20) + '>';
  793. write[wi++] = ((data[idx] & 0x000FC000) >>> 14) + '>';
  794. write[wi++] = ((data[idx] & 0x00003F00) >>> 8) + '>';
  795. //write[wi++] = (((data[idx - 1] & 0x000000FC) << 22) | ((data[idx - 1] & 0x00000003) << 20) | ((data[idx] & 0xF0000000) >>> 12) | ((data[idx] & 0x0FC00000) >> 14) | ((data[idx] & 0x003F0000) >> 16))
  796. write[wi++] = ((data[idx] & 0x000000FC) >>> 2) + '>';
  797. write[wi++] = (((data[idx] & 0x00000003) << 4) | ((data[idx + 1] & 0xF0000000) >>> 28)) + '>';
  798. ++idx;
  799. write[wi++] = ((data[idx] & 0x0FC00000) >>> 22) + '>';
  800. write[wi++] = ((data[idx] & 0x003F0000) >>> 16) + '>';
  801. //write[wi++] = (((data[idx] & 0x0000FC00) << 14) | ((data[idx] & 0x000003F0) << 12) | ((data[idx] & 0x0000000F) << 10) | ((data[idx + 1] & 0xC0000000) >>> 22) | ((data[idx + 1] & 0x3F000000) >> 24))
  802. write[wi++] = ((data[idx] & 0x0000FC00) >>> 10) + '>';
  803. write[wi++] = ((data[idx] & 0x000003F0) >>> 4) + '>';
  804. write[wi++] = (((data[idx] & 0x0000000F) << 2) | ((data[idx + 1] & 0xC0000000) >>> 30)) + '>';
  805. ++idx;
  806. write[wi++] = ((data[idx] & 0x3F000000) >>> 24) + '>';
  807. //write[wi++] = (((data[idx] & 0x00FC0000) << 6) | ((data[idx] & 0x0003F000) << 4) | ((data[idx] & 0x00000FC0) << 2) | ((data[idx] & 0x0000003F) << 0))
  808. write[wi++] = ((data[idx] & 0x00FC0000) >>> 18) + '>';
  809. write[wi++] = ((data[idx] & 0x0003F000) >>> 12) + '>';
  810. write[wi++] = ((data[idx] & 0x00000FC0) >>> 6) + '>';
  811. write[wi++] = ((data[idx] & 0x0000003F) >>> 0) + '>';
  812. ++idx;
  813. if (wi == Y_INI_WRITE_ARRAY_SIZE)
  814. {
  815. format(dname, sizeof (dname), "@@%s-%d", name, iter++);
  816. //printf("%s = %s", dname, write);
  817. P:5("Uvar_WriteArray: write %s = %s", dname, write);
  818. //Player_WriteString(dname, write);
  819. write[wi] = '\0';
  820. INI_AddToBuffer(file, dname, write);
  821. write[0] = '\0';
  822. wi = 0;
  823. }
  824. }
  825. // Do the straggling bytes.
  826. if (idx != len)
  827. {
  828. write[wi++] = ((data[idx] & 0xFC000000) >>> 26) + '>';
  829. write[wi++] = ((data[idx] & 0x03F00000) >>> 20) + '>';
  830. write[wi++] = ((data[idx] & 0x000FC000) >>> 14) + '>';
  831. write[wi++] = ((data[idx] & 0x00003F00) >>> 8) + '>';
  832. //write[wi++] = (((data[idx - 1] & 0x000000FC) << 22) | ((data[idx - 1] & 0x00000003) << 20) | ((data[idx] & 0xF0000000) >>> 12) | ((data[idx] & 0x0FC00000) >> 14) | ((data[idx] & 0x003F0000) >> 16))
  833. write[wi++] = ((data[idx] & 0x000000FC) >>> 2) + '>';
  834. if (++idx == len)
  835. {
  836. write[wi++] = ((data[idx - 1] & 0x00000003) << 4) + '>';
  837. }
  838. else
  839. {
  840. write[wi++] = (((data[idx - 1] & 0x00000003) << 4) | ((data[idx] & 0xF0000000) >>> 28)) + '>';
  841. write[wi++] = ((data[idx] & 0x0FC00000) >>> 22) + '>';
  842. write[wi++] = ((data[idx] & 0x003F0000) >>> 16) + '>';
  843. //write[wi++] = (((data[idx] & 0x0000FC00) << 14) | ((data[idx] & 0x000003F0) << 12) | ((data[idx] & 0x0000000F) << 10) | ((data[idx + 1] & 0xC0000000) >>> 22) | ((data[idx + 1] & 0x3F000000) >> 24))
  844. write[wi++] = ((data[idx] & 0x0000FC00) >>> 10) + '>';
  845. write[wi++] = ((data[idx] & 0x000003F0) >>> 4) + '>';
  846. if (++idx == len)
  847. {
  848. write[wi++] = ((data[idx - 1] & 0x0000000F) << 2) + '>';
  849. }
  850. else
  851. {
  852. write[wi++] = (((data[idx - 1] & 0x0000000F) << 2) | ((data[idx] & 0xC0000000) >>> 30)) + '>';
  853. //++idx;
  854. write[wi++] = ((data[idx] & 0x3F000000) >>> 24) + '>';
  855. //write[wi++] = (((data[idx] & 0x00FC0000) << 6) | ((data[idx] & 0x0003F000) << 4) | ((data[idx] & 0x00000FC0) << 2) | ((data[idx] & 0x0000003F) << 0))
  856. write[wi++] = ((data[idx] & 0x00FC0000) >>> 18) + '>';
  857. write[wi++] = ((data[idx] & 0x0003F000) >>> 12) + '>';
  858. write[wi++] = ((data[idx] & 0x00000FC0) >>> 6) + '>';
  859. write[wi++] = ((data[idx] & 0x0000003F) >>> 0) + '>';
  860. }
  861. }
  862. format(dname, sizeof (dname), "@@%s-%d", name, iter++);
  863. //printf("%s = %s", dname, write);
  864. P:5("Uvar_WriteArray: write %s = %s", dname, write);
  865. //Player_WriteString(dname, write);
  866. write[wi] = '\0';
  867. INI_AddToBuffer(file, dname, write);
  868. write[0] = '\0';
  869. wi = 0;
  870. }
  871. return 1;
  872. }
  873. /*----------------------------------------------------------------------------*\
  874. Function:
  875. INI_AddToBuffer
  876. Params:
  877. INI:file - INI file to write to.
  878. name[] - Data name to write.
  879. data[] - Data to write.
  880. Return:
  881. -
  882. Notes:
  883. First checks the name doesn't already exist under the current tag header
  884. and if it does overwrites the current value. If not checks there's room
  885. in the buffer to write to and purges the buffer if not. Finally saves the
  886. data in the buffer for writing when required and adds the data to the
  887. relevant list for tag inclusion.
  888. \*----------------------------------------------------------------------------*/
  889. static stock INI_AddToBuffer(INI:file, const name[], data[])
  890. {
  891. P:4("INI_AddToBuffer called: %i, \"%s\", \"%s\"", _:file, name, data);
  892. if (file < INI:0 || file >= INI:INI_MAX_WRITES)
  893. {
  894. return 0;
  895. }
  896. if (!YSI_g_sINITagPos[_:file])
  897. {
  898. // Tagless data.
  899. YSI_g_sINITagPos[_:file] = 1;
  900. YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_START] = -1;
  901. YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_NAME][0] = 0;
  902. }
  903. new
  904. pos = YSI_g_sINIWritePos[_:file],
  905. tmptag = YSI_g_sINICurrentTag[_:file],
  906. start = YSI_g_sINIWriteTag[_:file][tmptag][E_INI_TAGS_START];
  907. if (start == cellmax)
  908. {
  909. // Don't write to a deleted tag.
  910. return 0;
  911. }
  912. while (start != -1)
  913. {
  914. if (!strcmp(name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NAME], true))
  915. {
  916. strcpy(YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT], data, MAX_INI_ENTRY_TEXT);
  917. //strpack(YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT], data, MAX_INI_ENTRY_TEXT);
  918. return 1;
  919. }
  920. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  921. }
  922. if (pos >= INI_BUFFER_SIZE)
  923. {
  924. if (!INI_WriteBuffer(file))
  925. {
  926. return 0;
  927. }
  928. INI_SetTag(file, YSI_g_sINIWriteTag[_:file][tmptag][E_INI_TAGS_NAME]);
  929. pos = 0;
  930. }
  931. new
  932. curtag = YSI_g_sINICurrentTag[_:file];
  933. if (YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_START] == -1)
  934. {
  935. YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_START] = pos;
  936. }
  937. else
  938. {
  939. YSI_g_sINIWriteBuffer[_:file][YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_LAST]][E_INI_WRITE_NEXT] = pos;
  940. }
  941. strcpy(YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_NAME], name, MAX_INI_ENTRY_NAME);
  942. //strpack(YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_NAME], name, MAX_INI_ENTRY_NAME);
  943. strcpy(YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_TEXT], data, MAX_INI_ENTRY_TEXT);
  944. YSI_g_sINIWriteBuffer[_:file][pos][E_INI_WRITE_NEXT] = -1;
  945. YSI_g_sINIWriteTag[_:file][curtag][E_INI_TAGS_LAST] = pos;
  946. YSI_g_sINIWritePos[_:file]++;
  947. return 1;
  948. }
  949. /*----------------------------------------------------------------------------*\
  950. Function:
  951. INI_RemoveEntry
  952. Params:
  953. INI:file - File to write to.
  954. name[] - Item to remove.
  955. Return:
  956. -
  957. Notes:
  958. Wrapper for INI_AddToBuffer for removing data.
  959. \*----------------------------------------------------------------------------*/
  960. stock INI_RemoveEntry(INI:file, name[])
  961. {
  962. P:3("INI_RemoveEntry called: %i, \"%s\"", _:file, name);
  963. static
  964. sData[2] = {-1, 0};
  965. INI_AddToBuffer(file, name, sData);
  966. }
  967. /*----------------------------------------------------------------------------*\
  968. Function:
  969. INI_WriteString
  970. Params:
  971. INI:file - File to write to.
  972. name[] - Data name.
  973. data[] - Data.
  974. Return:
  975. -
  976. Notes:
  977. Wrapper for INI_AddToBuffer for strings.
  978. \*----------------------------------------------------------------------------*/
  979. stock INI_WriteString(INI:file, name[], data[])
  980. {
  981. P:3("INI_WriteString called: %i, \"%s\", \"%s\"", _:file, name, data);
  982. INI_AddToBuffer(file, name, data);
  983. }
  984. /*----------------------------------------------------------------------------*\
  985. Function:
  986. INI_WriteInt
  987. Params:
  988. INI:file - File to write to.
  989. name[] - Data name.
  990. data - Integer data.
  991. Return:
  992. -
  993. Notes:
  994. Wrapper for INI_AddToBuffer for integers.
  995. \*----------------------------------------------------------------------------*/
  996. stock INI_WriteInt(INI:file, name[], data)
  997. {
  998. P:3("INI_WriteInt called: %i, \"%s\", %i", _:file, name, data);
  999. new
  1000. str[12];
  1001. valstr(str, data);
  1002. INI_AddToBuffer(file, name, str);
  1003. }
  1004. /*----------------------------------------------------------------------------*\
  1005. Function:
  1006. INI_WriteHex
  1007. Params:
  1008. INI:file - File to write to.
  1009. name[] - Data name.
  1010. data - Hex data.
  1011. Return:
  1012. -
  1013. Notes:
  1014. Wrapper for INI_AddToBuffer for integers to be written as hex values.
  1015. \*----------------------------------------------------------------------------*/
  1016. stock INI_WriteHex(INI:file, name[], data)
  1017. {
  1018. P:3("INI_WriteHex called: %i, \"%s\", %i", _:file, name, data);
  1019. static const
  1020. sc_values[] = !"0123456789ABCDEF";
  1021. new
  1022. str[11],
  1023. i = 9;
  1024. do
  1025. {
  1026. str[i--] = sc_values{data & 0x0F};
  1027. data >>>= 4;
  1028. }
  1029. while (data);
  1030. str[i--] = 'x';
  1031. str[i] = '0';
  1032. INI_AddToBuffer(file, name, str[i]);
  1033. }
  1034. /*----------------------------------------------------------------------------*\
  1035. Function:
  1036. INI_WriteBin
  1037. Params:
  1038. INI:file - File to write to.
  1039. name[] - Data name.
  1040. data - Binary data.
  1041. Return:
  1042. -
  1043. Notes:
  1044. Wrapper for INI_AddToBuffer for integers to be written as binary values.
  1045. \*----------------------------------------------------------------------------*/
  1046. stock INI_WriteBin(INI:file, name[], data)
  1047. {
  1048. P:3("INI_WriteBin called: %i, \"%s\", %i", _:file, name, data);
  1049. // Do four values at once for speed. This uses a packed string and unpacks
  1050. // it so that we can quickly write four values at once.
  1051. static const
  1052. sc_values[] = !"0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" "1010" "1011" "1100" "1101" "1110" "1111";
  1053. new
  1054. str[35],
  1055. i = 10;
  1056. do
  1057. {
  1058. str[--i] = sc_values[data & 0x0F];
  1059. data >>>= 4;
  1060. }
  1061. while (data);
  1062. // Convert the coalesced values to individual values.
  1063. strunpack(str[i], str[i], 33);
  1064. str[--i] = 'b';
  1065. str[--i] = '0';
  1066. INI_AddToBuffer(file, name, str[i]);
  1067. }
  1068. /*----------------------------------------------------------------------------*\
  1069. Function:
  1070. INI_WriteBool
  1071. Params:
  1072. INI:file - File to write to.
  1073. name[] - Data name.
  1074. data - Boolean data.
  1075. Return:
  1076. -
  1077. Notes:
  1078. Wrapper for INI_AddToBuffer for booleans.
  1079. \*----------------------------------------------------------------------------*/
  1080. stock INI_WriteBool(INI:file, name[], bool:data)
  1081. {
  1082. P:3("INI_WriteBool called: %i, \"%s\", %i", _:file, name, _:data);
  1083. if (data)
  1084. {
  1085. INI_AddToBuffer(file, name, "true");
  1086. }
  1087. else
  1088. {
  1089. INI_AddToBuffer(file, name, "false");
  1090. }
  1091. }
  1092. /*----------------------------------------------------------------------------*\
  1093. Function:
  1094. INI_WriteFloat
  1095. Params:
  1096. INI:file - File to write to.
  1097. name[] - Data name.
  1098. Float:data - Float data.
  1099. accuracy - number of decimal places to write.
  1100. Return:
  1101. -
  1102. Notes:
  1103. Wrapper for INI_AddToBuffer for floats. Uses custom code instead of
  1104. format() as it's actually faster for something simple like this.
  1105. \*----------------------------------------------------------------------------*/
  1106. stock INI_WriteFloat(INI:file, name[], Float:data, accuracy = 6)
  1107. {
  1108. P:3("INI_WriteFloat called: %i, \"%s\", %f, %i", _:file, name, data, accuracy);
  1109. new
  1110. str[32];
  1111. format(str, sizeof (str), "%.*f", accuracy, data);
  1112. INI_AddToBuffer(file, name, str);
  1113. }
  1114. /*----------------------------------------------------------------------------*\
  1115. Function:
  1116. INI_WriteBuffer
  1117. Params:
  1118. INI:file - INI stream to write to file.
  1119. Return:
  1120. Success/fail.
  1121. Notes:
  1122. Opens the required file for reading and a temp file for read/writing. Goes
  1123. through the entire file reading all contained data. If it reaches a tag
  1124. line ([tag_name]) it dumps any unwritten data from the last tag (if there
  1125. was one) and starts processing the new tag. While a tag is being processed
  1126. every line is compared against the UNWRITTEN new data for that tag in the
  1127. buffer, if they're the same it writes the new data instead (it also writes
  1128. any comments which were after the data in the original line back), else it
  1129. writes the original line back.
  1130. Once all the new data is written to the temp file any tags which haven't
  1131. been processed at all (i.e. were not found in the original file) are
  1132. written to the temp file along with all their data. The original file is
  1133. then destroyed and reopend and all the data copied out from the temp file
  1134. to the newly opened original file, closed and saved.
  1135. \*----------------------------------------------------------------------------*/
  1136. static stock INI_WriteBuffer(INI:file)
  1137. {
  1138. P:4("INI_WriteBuffer called: %i", _:file);
  1139. if (_:file < 0 || _:file >= INI_MAX_WRITES) return 0;
  1140. new
  1141. //File:buffer = fopen("_temp_ysi_user_file_.ysi", io_write),
  1142. File:buffer = ftemp(),
  1143. File:source = fopen(YSI_g_sINIWriteFile[_:file], io_read);
  1144. if (buffer)
  1145. {
  1146. new
  1147. line[MAX_INI_LINE],
  1148. BitArray:read<INI_BUFFER_SIZE>, //:read[INI_BUFFER_BITS],
  1149. writing = 0,
  1150. skipTag = 0,
  1151. //Bit:tagswritten,
  1152. BitArray:tagswritten<MAX_INI_TAGS>,
  1153. tagpos = YSI_g_sINITagPos[_:file],
  1154. start = -1,
  1155. blank;
  1156. if (source)
  1157. {
  1158. // Write tagless data.
  1159. if (!YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_NAME][0])
  1160. {
  1161. start = YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_START];
  1162. // Will be -1 if there is no tagless data.
  1163. if (start != -1)
  1164. {
  1165. if (start == cellmax)
  1166. {
  1167. skipTag = 1;
  1168. start = -1;
  1169. }
  1170. else
  1171. {
  1172. writing = 1;
  1173. }
  1174. Bit_Set(tagswritten, 0, true);
  1175. }
  1176. }
  1177. while (fread(source, line))
  1178. {
  1179. new
  1180. pos = 1;
  1181. for (new i = 0; line[i]; i++)
  1182. {
  1183. if (line[i] == ';')
  1184. {
  1185. goto INI_WriteBuffer_cont1;
  1186. }
  1187. else if (line[i] > ' ')
  1188. {
  1189. // Determine that the line is not blank.
  1190. pos = 0;
  1191. break;
  1192. }
  1193. }
  1194. if (pos)
  1195. {
  1196. blank++;
  1197. continue;
  1198. }
  1199. if (line[0] == '[' && (pos = chrfind(']', line)) != -1 && endofline(line, pos + 1))
  1200. {
  1201. //pos--;
  1202. writing = 0;
  1203. new
  1204. form[MAX_INI_LINE];
  1205. // Reached a new tag - flush the rest of the last tag.
  1206. while (start != -1)
  1207. {
  1208. if (!Bit_GetBit(read, start))
  1209. {
  1210. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] != -1)
  1211. {
  1212. 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]);
  1213. fwrite(buffer, form);
  1214. }
  1215. Bit_Set(read, start, true);
  1216. }
  1217. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  1218. }
  1219. if (!skipTag)
  1220. {
  1221. while (blank--) fwrite(buffer, INI_NEW_LINE);
  1222. }
  1223. blank = 0;
  1224. skipTag = 0;
  1225. // Note the start of the new tag.
  1226. line[pos] = '\0';
  1227. for (new j = 0; j < tagpos; j++)
  1228. {
  1229. //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))
  1230. //printf("compare %d, %s, %s", Bit_Get(tagswritten, j), unpack(YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME]), line[1]);
  1231. if (!Bit_Get(tagswritten, j) && !strcmp(YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME], line[1])) //, true, pos))
  1232. {
  1233. /*if (YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME])
  1234. {
  1235. continue;
  1236. }*/
  1237. start = YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_START];
  1238. P:7("INI_WriteBuffer: Next tag: %s, %d", line[1], start);
  1239. if (start == cellmax)
  1240. {
  1241. skipTag = 1;
  1242. start = -1;
  1243. writing = 0;
  1244. }
  1245. else
  1246. {
  1247. writing = 1;
  1248. }
  1249. //tagswritten |= Bit:(1 << j);
  1250. Bit_Set(tagswritten, j, true);
  1251. break;
  1252. }
  1253. }
  1254. line[pos--] = ']';
  1255. }
  1256. else if (writing)
  1257. {
  1258. new
  1259. name[MAX_INI_ENTRY_NAME],
  1260. temp,
  1261. liststart = start;
  1262. INI_GetEntryName(line, name, temp);
  1263. pos = chrfind(';', line, temp);
  1264. while (blank--) fwrite(buffer, INI_NEW_LINE);
  1265. blank = 0;
  1266. while (start != -1)
  1267. {
  1268. if (!Bit_GetBit(read, start) && !strcmp(name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NAME]))
  1269. {
  1270. Bit_Set(read, start, true);//, INI_BUFFER_SIZE);
  1271. // Delete the current entry from the file, as per
  1272. // our request.
  1273. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] == -1)
  1274. {
  1275. // Can't use "continue" as we're in an inner
  1276. // loop.
  1277. goto INI_WriteBuffer_cont2;
  1278. }
  1279. if (pos != -1)
  1280. {
  1281. format(line, sizeof (line), "%s = %s %s", name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT], line[pos]);
  1282. }
  1283. else
  1284. {
  1285. format(line, sizeof (line), "%s = %s" INI_NEW_LINE, name, YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT]);
  1286. }
  1287. //start = -1;
  1288. break;
  1289. }
  1290. else
  1291. {
  1292. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  1293. }
  1294. }
  1295. /*if (start == -1)
  1296. {
  1297. // Wasn't found.
  1298. start = strlen(line);
  1299. if (line[start - 1] > ' ')
  1300. {
  1301. strcat(line, INI_NEW_LINE);
  1302. }
  1303. }*/
  1304. start = liststart;
  1305. }
  1306. INI_WriteBuffer_cont1:
  1307. if (!skipTag)
  1308. {
  1309. fwrite(buffer, line);
  1310. }
  1311. INI_WriteBuffer_cont2:
  1312. }
  1313. // Write any data from this tag group not in the original file.
  1314. // I.e. flush the final read tag.
  1315. while (start != -1)
  1316. {
  1317. if (!Bit_GetBit(read, start))
  1318. {
  1319. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] != -1)
  1320. {
  1321. 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]);
  1322. fwrite(buffer, line);
  1323. }
  1324. }
  1325. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  1326. }
  1327. fclose(source);
  1328. }
  1329. // Write any tag groups not found in the original file.
  1330. //printf("write some tag");
  1331. for (new j = 0; j < tagpos; j++)
  1332. {
  1333. //if (!(tagswritten & Bit:(1 << j)))
  1334. //printf("write this tag: %s", unpack(YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME]));
  1335. if (!Bit_Get(tagswritten, j))
  1336. {
  1337. start = YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_START];
  1338. if (start == cellmax)
  1339. {
  1340. continue;
  1341. }
  1342. if (j || YSI_g_sINIWriteTag[_:file][0][E_INI_TAGS_NAME][0])
  1343. {
  1344. strunpack(line, YSI_g_sINIWriteTag[_:file][j][E_INI_TAGS_NAME]);
  1345. format(line, sizeof (line), "[%s]" INI_NEW_LINE, line);
  1346. fwrite(buffer, line);
  1347. }
  1348. while (start != -1)
  1349. {
  1350. if (YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_TEXT][0] != -1)
  1351. {
  1352. 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]);
  1353. fwrite(buffer, line);
  1354. }
  1355. start = YSI_g_sINIWriteBuffer[_:file][start][E_INI_WRITE_NEXT];
  1356. }
  1357. }
  1358. }
  1359. //fclose(buffer);
  1360. // This renames the file back (by copying all the data).
  1361. fremove(YSI_g_sINIWriteFile[_:file]);
  1362. #if defined _inc_TODO_COPY_INC
  1363. #else
  1364. #if defined _int_TODO_OTHER_COPY_INC
  1365. #else
  1366. source = fopen(YSI_g_sINIWriteFile[_:file], io_write);
  1367. //buffer = fopen("_temp_ysi_user_file_.ysi", io_read);
  1368. fseek(buffer);
  1369. if (source) // && buffer)
  1370. {
  1371. while (fread(buffer, line)) fwrite(source, line);
  1372. /*new
  1373. val;
  1374. printf("start: %d", _:E_INI_WRITE * INI_BUFFER_SIZE);
  1375. while ((val = fblockread(buffer, YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME], _:E_INI_WRITE * INI_BUFFER_SIZE)))
  1376. {
  1377. printf("%d %d", val, strlen(YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME]));
  1378. printf("%s", YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME]);
  1379. fblockwrite(source, YSI_g_sINIWriteBuffer[_:file][0][E_INI_WRITE_NAME], val);
  1380. }*/
  1381. fclose(buffer);
  1382. fclose(source);
  1383. }
  1384. //fremove("_temp_ysi_user_file_.ysi");
  1385. #endif
  1386. #endif
  1387. // Reset the write buffer.
  1388. YSI_g_sINITagPos[_:file] = 0;
  1389. YSI_g_sINIWritePos[_:file] = 0;
  1390. YSI_g_sINICurrentTag[_:file] = 0;
  1391. return 1;
  1392. }
  1393. return 0;
  1394. }