2
0

sokol_args.h 26 KB

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