sokol_args.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. #ifndef SOKOL_ARGS_INCLUDED
  2. /*
  3. sokol_args.h -- cross-platform key/value arg-parsing for web and native
  4. Project URL: https://github.com/floooh/sokol
  5. Do this:
  6. #define SOKOL_IMPL
  7. before you include this file in *one* C or C++ file to create the
  8. implementation.
  9. Optionally provide the following defines with your own implementations:
  10. SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
  11. SOKOL_LOG(msg) - your own logging functions (default: puts(msg))
  12. SOKOL_CALLOC(n,s) - your own calloc() implementation (default: calloc(n,s))
  13. SOKOL_FREE(p) - your own free() implementation (default: free(p))
  14. SOKOL_API_DECL - public function declaration prefix (default: extern)
  15. SOKOL_API_IMPL - public function implementation prefix (default: -)
  16. If sokol_args.h is compiled as a DLL, define the following before
  17. including the declaration or implementation:
  18. SOKOL_DLL
  19. On Windows, SOKOL_DLL will define SOKOL_API_DECL as __declspec(dllexport)
  20. or __declspec(dllimport) as needed.
  21. OVERVIEW
  22. ========
  23. sokol_args.h provides a simple unified argument parsing API for WebAssembly and
  24. native apps.
  25. When running as WebAssembly app, arguments are taken from the page URL:
  26. https://floooh.github.io/tiny8bit/kc85.html?type=kc85_3&mod=m022&snapshot=kc85/jungle.kcc
  27. The same arguments provided to a command line app:
  28. kc85 type=kc85_3 mod=m022 snapshot=kc85/jungle.kcc
  29. ARGUMENT FORMATTING
  30. ===================
  31. On the web platform, arguments must be formatted as a valid URL query string
  32. with 'percent encoding' used for special characters.
  33. Strings are expected to be UTF-8 encoded (although sokol_args.h doesn't
  34. contain any special UTF-8 handling). See below on how to obtain
  35. UTF-8 encoded argc/argv values on Windows when using WinMain() as
  36. entry point.
  37. On native platforms the following rules must be followed:
  38. Arguments have the general form
  39. key=value
  40. Key/value pairs are separated by 'whitespace', valid whitespace
  41. characters are space and tab.
  42. Whitespace characters in front and after the separating '=' character
  43. are ignored:
  44. key = value
  45. ...is the same as
  46. key=value
  47. The 'key' string must be a simple string without escape sequences or whitespace.
  48. Currently 'single keys' without values are not allowed, but may be
  49. in the future.
  50. The 'value' string can be quoted, and quoted value strings can contain
  51. whitespace:
  52. key = 'single-quoted value'
  53. key = "double-quoted value"
  54. Single-quoted value strings can contain double quotes, and vice-versa:
  55. key = 'single-quoted value "can contain double-quotes"'
  56. key = "double-quoted value 'can contain single-quotes'"
  57. Note that correct quoting can be tricky on some shells, since command
  58. shells may remove quotes, unless they're escaped.
  59. Value strings can contain a small selection of escape sequences:
  60. \n - newline
  61. \r - carriage return
  62. \t - tab
  63. \\ - escaped backslash
  64. (more escape codes may be added in the future).
  65. CODE EXAMPLE
  66. ============
  67. int main(int argc, char* argv[]) {
  68. // initialize sokol_args with default parameters
  69. sargs_setup(&(sargs_desc){
  70. .argc = argc,
  71. .argv = argv
  72. });
  73. // check if a key exists...
  74. if (sargs_exists("bla")) {
  75. ...
  76. }
  77. // get value string for key, if not found, return empty string ""
  78. const char* val0 = sargs_value("bla");
  79. // get value string for key, or default string if key not found
  80. const char* val1 = sargs_value_def("bla", "default_value");
  81. // check if a key matches expected value
  82. if (sargs_equals("type", "kc85_4")) {
  83. ...
  84. }
  85. // check if a key's value is "true", "yes" or "on"
  86. if (sargs_boolean("joystick_enabled")) {
  87. ...
  88. }
  89. // iterate over keys and values
  90. for (int i = 0; i < sargs_num_args(); i++) {
  91. printf("key: %s, value: %s\n", sargs_key_at(i), sargs_value_at(i));
  92. }
  93. // lookup argument index by key string, will return -1 if key
  94. // is not found, sargs_key_at() and sargs_value_at() will return
  95. // an empty string for invalid indices
  96. int index = sargs_find("bla");
  97. printf("key: %s, value: %s\n", sargs_key_at(index), sargs_value_at(index));
  98. // shutdown sokol-args
  99. sargs_shutdown();
  100. }
  101. WINMAIN AND ARGC / ARGV
  102. =======================
  103. On Windows with WinMain() based apps, use the __argc and __argv global
  104. variables provided by Windows. These are compatible with main(argc, argv)
  105. and have already been converted to UTF-8 by Windows:
  106. int WINAPI WinMain(...) {
  107. sargs_setup(&(sargs_desc){
  108. .argc = __argc,
  109. .argv = __argv
  110. });
  111. }
  112. (this is also what sokol_app.h uses btw)
  113. API DOCUMENTATION
  114. =================
  115. void sargs_setup(const sargs_desc* desc)
  116. Initialize sokol_args, desc contains the following configuration
  117. parameters:
  118. int argc - the main function's argc parameter
  119. char** argv - the main function's argv parameter
  120. int max_args - max number of key/value pairs, default is 16
  121. int buf_size - size of the internal string buffer, default is 16384
  122. Note that on the web, argc and argv will be ignored and the arguments
  123. will be taken from the page URL instead.
  124. sargs_setup() will allocate 2 memory chunks: one for keeping track
  125. of the key/value args of size 'max_args*8', and a string buffer
  126. of size 'buf_size'.
  127. void sargs_shutdown(void)
  128. Shutdown sokol-args and free any allocated memory.
  129. bool sargs_isvalid(void)
  130. Return true between sargs_setup() and sargs_shutdown()
  131. bool sargs_exists(const char* key)
  132. Test if a key arg exists.
  133. const char* sargs_value(const char* key)
  134. Return value associated with key. Returns an empty
  135. string ("") if the key doesn't exist.
  136. const char* sargs_value_def(const char* key, const char* default)
  137. Return value associated with key, or the provided default
  138. value if the value doesn't exist.
  139. bool sargs_equals(const char* key, const char* val);
  140. Return true if the value associated with key matches
  141. the 'val' argument.
  142. bool sargs_boolean(const char* key)
  143. Return true if the value string of 'key' is one
  144. of 'true', 'yes', 'on'.
  145. int sargs_find(const char* key)
  146. Find argument by key name and return its index, or -1 if not found.
  147. int sargs_num_args(void)
  148. Return number of key/value pairs.
  149. const char* sargs_key_at(int index)
  150. Return the key name of argument at index. Returns empty string if
  151. is index is outside range.
  152. const char* sargs_value_at(int index)
  153. Return the value of argument at index. Returns empty string
  154. if index is outside range.
  155. TODO
  156. ====
  157. - parsing errors?
  158. LICENSE
  159. =======
  160. zlib/libpng license
  161. Copyright (c) 2018 Andre Weissflog
  162. This software is provided 'as-is', without any express or implied warranty.
  163. In no event will the authors be held liable for any damages arising from the
  164. use of this software.
  165. Permission is granted to anyone to use this software for any purpose,
  166. including commercial applications, and to alter it and redistribute it
  167. freely, subject to the following restrictions:
  168. 1. The origin of this software must not be misrepresented; you must not
  169. claim that you wrote the original software. If you use this software in a
  170. product, an acknowledgment in the product documentation would be
  171. appreciated but is not required.
  172. 2. Altered source versions must be plainly marked as such, and must not
  173. be misrepresented as being the original software.
  174. 3. This notice may not be removed or altered from any source
  175. distribution.
  176. */
  177. #define SOKOL_ARGS_INCLUDED (1)
  178. #include <stdint.h>
  179. #include <stdbool.h>
  180. #ifndef SOKOL_API_DECL
  181. #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_IMPL)
  182. #define SOKOL_API_DECL __declspec(dllexport)
  183. #elif defined(_WIN32) && defined(SOKOL_DLL)
  184. #define SOKOL_API_DECL __declspec(dllimport)
  185. #else
  186. #define SOKOL_API_DECL extern
  187. #endif
  188. #endif
  189. #ifdef __cplusplus
  190. extern "C" {
  191. #endif
  192. typedef struct sargs_desc {
  193. int argc;
  194. char** argv;
  195. int max_args;
  196. int buf_size;
  197. } sargs_desc;
  198. /* setup sokol-args */
  199. SOKOL_API_DECL void sargs_setup(const sargs_desc* desc);
  200. /* shutdown sokol-args */
  201. SOKOL_API_DECL void sargs_shutdown(void);
  202. /* true between sargs_setup() and sargs_shutdown() */
  203. SOKOL_API_DECL bool sargs_isvalid(void);
  204. /* test if an argument exists by key name */
  205. SOKOL_API_DECL bool sargs_exists(const char* key);
  206. /* get value by key name, return empty string if key doesn't exist */
  207. SOKOL_API_DECL const char* sargs_value(const char* key);
  208. /* get value by key name, return provided default if key doesn't exist */
  209. SOKOL_API_DECL const char* sargs_value_def(const char* key, const char* def);
  210. /* return true if val arg matches the value associated with key */
  211. SOKOL_API_DECL bool sargs_equals(const char* key, const char* val);
  212. /* return true if key's value is "true", "yes" or "on" */
  213. SOKOL_API_DECL bool sargs_boolean(const char* key);
  214. /* get index of arg by key name, return -1 if not exists */
  215. SOKOL_API_DECL int sargs_find(const char* key);
  216. /* get number of parsed arguments */
  217. SOKOL_API_DECL int sargs_num_args(void);
  218. /* get key name of argument at index, or empty string */
  219. SOKOL_API_DECL const char* sargs_key_at(int index);
  220. /* get value string of argument at index, or empty string */
  221. SOKOL_API_DECL const char* sargs_value_at(int index);
  222. #ifdef __cplusplus
  223. } /* extern "C" */
  224. #endif
  225. #endif // SOKOL_ARGS_INCLUDED
  226. /*--- IMPLEMENTATION ---------------------------------------------------------*/
  227. #ifdef SOKOL_IMPL
  228. #define SOKOL_ARGS_IMPL_INCLUDED (1)
  229. #include <string.h> /* memset, strcmp */
  230. #if defined(__EMSCRIPTEN__)
  231. #include <emscripten/emscripten.h>
  232. #endif
  233. #ifndef SOKOL_API_IMPL
  234. #define SOKOL_API_IMPL
  235. #endif
  236. #ifndef SOKOL_DEBUG
  237. #ifndef NDEBUG
  238. #define SOKOL_DEBUG (1)
  239. #endif
  240. #endif
  241. #ifndef SOKOL_ASSERT
  242. #include <assert.h>
  243. #define SOKOL_ASSERT(c) assert(c)
  244. #endif
  245. #if !defined(SOKOL_CALLOC) && !defined(SOKOL_FREE)
  246. #include <stdlib.h>
  247. #endif
  248. #if !defined(SOKOL_CALLOC)
  249. #define SOKOL_CALLOC(n,s) calloc(n,s)
  250. #endif
  251. #if !defined(SOKOL_FREE)
  252. #define SOKOL_FREE(p) free(p)
  253. #endif
  254. #ifndef SOKOL_LOG
  255. #ifdef SOKOL_DEBUG
  256. #include <stdio.h>
  257. #define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); }
  258. #else
  259. #define SOKOL_LOG(s)
  260. #endif
  261. #endif
  262. #ifndef _SOKOL_PRIVATE
  263. #if defined(__GNUC__)
  264. #define _SOKOL_PRIVATE __attribute__((unused)) static
  265. #else
  266. #define _SOKOL_PRIVATE static
  267. #endif
  268. #endif
  269. #define _sargs_def(val, def) (((val) == 0) ? (def) : (val))
  270. #define _SARGS_MAX_ARGS_DEF (16)
  271. #define _SARGS_BUF_SIZE_DEF (16*1024)
  272. /* parser state (no parser needed on emscripten) */
  273. #if !defined(__EMSCRIPTEN__)
  274. #define _SARGS_EXPECT_KEY (1<<0)
  275. #define _SARGS_EXPECT_SEP (1<<1)
  276. #define _SARGS_EXPECT_VAL (1<<2)
  277. #define _SARGS_PARSING_KEY (1<<3)
  278. #define _SARGS_PARSING_VAL (1<<4)
  279. #define _SARGS_ERROR (1<<5)
  280. #endif
  281. /* a key/value pair struct */
  282. typedef struct {
  283. int key; /* index to start of key string in buf */
  284. int val; /* index to start of value string in buf */
  285. } _sargs_kvp_t;
  286. /* sokol-args state */
  287. typedef struct {
  288. int max_args; /* number of key/value pairs in args array */
  289. int num_args; /* number of valid items in args array */
  290. _sargs_kvp_t* args; /* key/value pair array */
  291. int buf_size; /* size of buffer in bytes */
  292. int buf_pos; /* current buffer position */
  293. char* buf; /* character buffer, first char is reserved and zero for 'empty string' */
  294. bool valid;
  295. /* arg parsing isn't needed on emscripten */
  296. #if !defined(__EMSCRIPTEN__)
  297. uint32_t parse_state;
  298. char quote; /* current quote char, 0 if not in a quote */
  299. bool in_escape; /* currently in an escape sequence */
  300. #endif
  301. } _sargs_state_t;
  302. static _sargs_state_t _sargs;
  303. /*== PRIVATE IMPLEMENTATION FUNCTIONS ========================================*/
  304. _SOKOL_PRIVATE void _sargs_putc(char c) {
  305. if ((_sargs.buf_pos+2) < _sargs.buf_size) {
  306. _sargs.buf[_sargs.buf_pos++] = c;
  307. }
  308. }
  309. _SOKOL_PRIVATE const char* _sargs_str(int index) {
  310. SOKOL_ASSERT((index >= 0) && (index < _sargs.buf_size));
  311. return &_sargs.buf[index];
  312. }
  313. /*-- argument parser functions (not required on emscripten) ------------------*/
  314. #if !defined(__EMSCRIPTEN__)
  315. _SOKOL_PRIVATE void _sargs_expect_key(void) {
  316. _sargs.parse_state = _SARGS_EXPECT_KEY;
  317. }
  318. _SOKOL_PRIVATE bool _sargs_key_expected(void) {
  319. return 0 != (_sargs.parse_state & _SARGS_EXPECT_KEY);
  320. }
  321. _SOKOL_PRIVATE void _sargs_expect_val(void) {
  322. _sargs.parse_state = _SARGS_EXPECT_VAL;
  323. }
  324. _SOKOL_PRIVATE bool _sargs_val_expected(void) {
  325. return 0 != (_sargs.parse_state & _SARGS_EXPECT_VAL);
  326. }
  327. _SOKOL_PRIVATE void _sargs_expect_sep(void) {
  328. _sargs.parse_state = _SARGS_EXPECT_SEP;
  329. }
  330. _SOKOL_PRIVATE bool _sargs_any_expected(void) {
  331. return 0 != (_sargs.parse_state & (_SARGS_EXPECT_KEY | _SARGS_EXPECT_VAL | _SARGS_EXPECT_SEP));
  332. }
  333. _SOKOL_PRIVATE bool _sargs_is_separator(char c) {
  334. return c == '=';
  335. }
  336. _SOKOL_PRIVATE bool _sargs_is_quote(char c) {
  337. if (0 == _sargs.quote) {
  338. return (c == '\'') || (c == '"');
  339. }
  340. else {
  341. return c == _sargs.quote;
  342. }
  343. }
  344. _SOKOL_PRIVATE void _sargs_begin_quote(char c) {
  345. _sargs.quote = c;
  346. }
  347. _SOKOL_PRIVATE void _sargs_end_quote(void) {
  348. _sargs.quote = 0;
  349. }
  350. _SOKOL_PRIVATE bool _sargs_in_quotes(void) {
  351. return 0 != _sargs.quote;
  352. }
  353. _SOKOL_PRIVATE bool _sargs_is_whitespace(char c) {
  354. return !_sargs_in_quotes() && ((c == ' ') || (c == '\t'));
  355. }
  356. _SOKOL_PRIVATE void _sargs_start_key(void) {
  357. SOKOL_ASSERT(_sargs.num_args < _sargs.max_args);
  358. _sargs.parse_state = _SARGS_PARSING_KEY;
  359. _sargs.args[_sargs.num_args].key = _sargs.buf_pos;
  360. }
  361. _SOKOL_PRIVATE void _sargs_end_key(void) {
  362. SOKOL_ASSERT(_sargs.num_args < _sargs.max_args);
  363. _sargs_putc(0);
  364. _sargs.parse_state = 0;
  365. }
  366. _SOKOL_PRIVATE bool _sargs_parsing_key(void) {
  367. return 0 != (_sargs.parse_state & _SARGS_PARSING_KEY);
  368. }
  369. _SOKOL_PRIVATE void _sargs_start_val(void) {
  370. SOKOL_ASSERT(_sargs.num_args < _sargs.max_args);
  371. _sargs.parse_state = _SARGS_PARSING_VAL;
  372. _sargs.args[_sargs.num_args].val = _sargs.buf_pos;
  373. }
  374. _SOKOL_PRIVATE void _sargs_end_val(void) {
  375. SOKOL_ASSERT(_sargs.num_args < _sargs.max_args);
  376. _sargs_putc(0);
  377. _sargs.num_args++;
  378. _sargs.parse_state = 0;
  379. }
  380. _SOKOL_PRIVATE bool _sargs_is_escape(char c) {
  381. return '\\' == c;
  382. }
  383. _SOKOL_PRIVATE void _sargs_start_escape(void) {
  384. _sargs.in_escape = true;
  385. }
  386. _SOKOL_PRIVATE bool _sargs_in_escape(void) {
  387. return _sargs.in_escape;
  388. }
  389. _SOKOL_PRIVATE char _sargs_escape(char c) {
  390. switch (c) {
  391. case 'n': return '\n';
  392. case 't': return '\t';
  393. case 'r': return '\r';
  394. case '\\': return '\\';
  395. default: return c;
  396. }
  397. }
  398. _SOKOL_PRIVATE void _sargs_end_escape(void) {
  399. _sargs.in_escape = false;
  400. }
  401. _SOKOL_PRIVATE bool _sargs_parsing_val(void) {
  402. return 0 != (_sargs.parse_state & _SARGS_PARSING_VAL);
  403. }
  404. _SOKOL_PRIVATE bool _sargs_parse_carg(const char* src) {
  405. char c;
  406. while (0 != (c = *src++)) {
  407. if (_sargs_in_escape()) {
  408. c = _sargs_escape(c);
  409. _sargs_end_escape();
  410. }
  411. else if (_sargs_is_escape(c)) {
  412. _sargs_start_escape();
  413. continue;
  414. }
  415. if (_sargs_any_expected()) {
  416. if (!_sargs_is_whitespace(c)) {
  417. /* start of key, value or separator */
  418. if (_sargs_key_expected()) {
  419. /* start of new key */
  420. _sargs_start_key();
  421. }
  422. else if (_sargs_val_expected()) {
  423. /* start of value */
  424. if (_sargs_is_quote(c)) {
  425. _sargs_begin_quote(c);
  426. continue;
  427. }
  428. _sargs_start_val();
  429. }
  430. else {
  431. /* separator */
  432. if (_sargs_is_separator(c)) {
  433. _sargs_expect_val();
  434. continue;
  435. }
  436. }
  437. }
  438. else {
  439. /* skip white space */
  440. continue;
  441. }
  442. }
  443. else if (_sargs_parsing_key()) {
  444. if (_sargs_is_whitespace(c) || _sargs_is_separator(c)) {
  445. /* end of key string */
  446. _sargs_end_key();
  447. if (_sargs_is_separator(c)) {
  448. _sargs_expect_val();
  449. }
  450. else {
  451. _sargs_expect_sep();
  452. }
  453. continue;
  454. }
  455. }
  456. else if (_sargs_parsing_val()) {
  457. if (_sargs_in_quotes()) {
  458. /* when in quotes, whitespace is a normal character
  459. and a matching quote ends the value string
  460. */
  461. if (_sargs_is_quote(c)) {
  462. _sargs_end_quote();
  463. _sargs_end_val();
  464. _sargs_expect_key();
  465. continue;
  466. }
  467. }
  468. else if (_sargs_is_whitespace(c)) {
  469. /* end of value string (no quotes) */
  470. _sargs_end_val();
  471. _sargs_expect_key();
  472. continue;
  473. }
  474. }
  475. _sargs_putc(c);
  476. }
  477. if (_sargs_parsing_key()) {
  478. _sargs_end_key();
  479. _sargs_expect_sep();
  480. }
  481. else if (_sargs_parsing_val() && !_sargs_in_quotes()) {
  482. _sargs_end_val();
  483. _sargs_expect_key();
  484. }
  485. return true;
  486. }
  487. _SOKOL_PRIVATE bool _sargs_parse_cargs(int argc, const char** argv) {
  488. _sargs_expect_key();
  489. bool retval = true;
  490. for (int i = 1; i < argc; i++) {
  491. retval &= _sargs_parse_carg(argv[i]);
  492. }
  493. _sargs.parse_state = 0;
  494. return retval;
  495. }
  496. #endif /* __EMSCRIPTEN__ */
  497. /*-- EMSCRIPTEN IMPLEMENTATION -----------------------------------------------*/
  498. #if defined(__EMSCRIPTEN__)
  499. #ifdef __cplusplus
  500. extern "C" {
  501. #endif
  502. EMSCRIPTEN_KEEPALIVE void _sargs_add_kvp(const char* key, const char* val) {
  503. SOKOL_ASSERT(_sargs.valid && key && val);
  504. if (_sargs.num_args >= _sargs.max_args) {
  505. return;
  506. }
  507. /* copy key string */
  508. char c;
  509. _sargs.args[_sargs.num_args].key = _sargs.buf_pos;
  510. const char* ptr = key;
  511. while (0 != (c = *ptr++)) {
  512. _sargs_putc(c);
  513. }
  514. _sargs_putc(0);
  515. /* copy value string */
  516. _sargs.args[_sargs.num_args].val = _sargs.buf_pos;
  517. ptr = val;
  518. while (0 != (c = *ptr++)) {
  519. _sargs_putc(c);
  520. }
  521. _sargs_putc(0);
  522. _sargs.num_args++;
  523. }
  524. #ifdef __cplusplus
  525. } /* extern "C" */
  526. #endif
  527. /* JS function to extract arguments from the page URL */
  528. EM_JS(void, sargs_js_parse_url, (void), {
  529. var params = new URLSearchParams(window.location.search).entries();
  530. for (var p = params.next(); !p.done; p = params.next()) {
  531. var key = p.value[0];
  532. var val = p.value[1];
  533. var res = ccall('_sargs_add_kvp', 'void', ['string','string'], [key,val]);
  534. }
  535. });
  536. #endif /* EMSCRIPTEN */
  537. /*== PUBLIC IMPLEMENTATION FUNCTIONS =========================================*/
  538. SOKOL_API_IMPL void sargs_setup(const sargs_desc* desc) {
  539. SOKOL_ASSERT(desc);
  540. memset(&_sargs, 0, sizeof(_sargs));
  541. _sargs.max_args = _sargs_def(desc->max_args, _SARGS_MAX_ARGS_DEF);
  542. _sargs.buf_size = _sargs_def(desc->buf_size, _SARGS_BUF_SIZE_DEF);
  543. SOKOL_ASSERT(_sargs.buf_size > 8);
  544. _sargs.args = (_sargs_kvp_t*) SOKOL_CALLOC(_sargs.max_args, sizeof(_sargs_kvp_t));
  545. _sargs.buf = (char*) SOKOL_CALLOC(_sargs.buf_size, sizeof(char));
  546. /* the first character in buf is reserved and always zero, this is the 'empty string' */
  547. _sargs.buf_pos = 1;
  548. _sargs.valid = true;
  549. #if defined(__EMSCRIPTEN__)
  550. /* on emscripten, ignore argc/argv, and parse the page URL instead */
  551. sargs_js_parse_url();
  552. #else
  553. /* on native platform, parse argc/argv */
  554. _sargs_parse_cargs(desc->argc, (const char**) desc->argv);
  555. #endif
  556. }
  557. SOKOL_API_IMPL void sargs_shutdown(void) {
  558. SOKOL_ASSERT(_sargs.valid);
  559. if (_sargs.args) {
  560. SOKOL_FREE(_sargs.args);
  561. _sargs.args = 0;
  562. }
  563. if (_sargs.buf) {
  564. SOKOL_FREE(_sargs.buf);
  565. _sargs.buf = 0;
  566. }
  567. _sargs.valid = false;
  568. }
  569. SOKOL_API_IMPL bool sargs_isvalid(void) {
  570. return _sargs.valid;
  571. }
  572. SOKOL_API_IMPL int sargs_find(const char* key) {
  573. SOKOL_ASSERT(_sargs.valid && key);
  574. for (int i = 0; i < _sargs.num_args; i++) {
  575. if (0 == strcmp(_sargs_str(_sargs.args[i].key), key)) {
  576. return i;
  577. }
  578. }
  579. return -1;
  580. }
  581. SOKOL_API_IMPL int sargs_num_args(void) {
  582. SOKOL_ASSERT(_sargs.valid);
  583. return _sargs.num_args;
  584. }
  585. SOKOL_API_IMPL const char* sargs_key_at(int index) {
  586. SOKOL_ASSERT(_sargs.valid);
  587. if ((index >= 0) && (index < _sargs.num_args)) {
  588. return _sargs_str(_sargs.args[index].key);
  589. }
  590. else {
  591. /* index 0 is always the empty string */
  592. return _sargs_str(0);
  593. }
  594. }
  595. SOKOL_API_IMPL const char* sargs_value_at(int index) {
  596. SOKOL_ASSERT(_sargs.valid);
  597. if ((index >= 0) && (index < _sargs.num_args)) {
  598. return _sargs_str(_sargs.args[index].val);
  599. }
  600. else {
  601. /* index 0 is always the empty string */
  602. return _sargs_str(0);
  603. }
  604. }
  605. SOKOL_API_IMPL bool sargs_exists(const char* key) {
  606. SOKOL_ASSERT(_sargs.valid && key);
  607. return -1 != sargs_find(key);
  608. }
  609. SOKOL_API_IMPL const char* sargs_value(const char* key) {
  610. SOKOL_ASSERT(_sargs.valid && key);
  611. return sargs_value_at(sargs_find(key));
  612. }
  613. SOKOL_API_IMPL const char* sargs_value_def(const char* key, const char* def) {
  614. SOKOL_ASSERT(_sargs.valid && key && def);
  615. int arg_index = sargs_find(key);
  616. if (-1 != arg_index) {
  617. return sargs_value_at(arg_index);
  618. }
  619. else {
  620. return def;
  621. }
  622. }
  623. SOKOL_API_IMPL bool sargs_equals(const char* key, const char* val) {
  624. SOKOL_ASSERT(_sargs.valid && key && val);
  625. return 0 == strcmp(sargs_value(key), val);
  626. }
  627. SOKOL_API_IMPL bool sargs_boolean(const char* key) {
  628. const char* val = sargs_value(key);
  629. return (0 == strcmp("true", val)) ||
  630. (0 == strcmp("yes", val)) ||
  631. (0 == strcmp("on", val));
  632. }
  633. #endif /* SOKOL_IMPL */