list.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /**
  2. * @file list.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * List construction module.
  32. *
  33. * Synopsis:
  34. * list(elem1, ..., elemN)
  35. * list listfrom(list l1, ..., list lN)
  36. *
  37. * Description:
  38. * The first form creates a list with the given elements.
  39. * The second form creates a list by concatenating the given
  40. * lists.
  41. *
  42. * Variables:
  43. * (empty) - list containing elem1, ..., elemN
  44. * length - number of elements in list
  45. *
  46. * Synopsis: list::append(arg)
  47. *
  48. * Synopsis: list::appendv(list arg)
  49. * Description: Appends the elements of arg to the list.
  50. *
  51. * Synopsis: list::length()
  52. * Variables:
  53. * (empty) - number of elements in list at the time of initialization
  54. * of this method
  55. *
  56. * Synopsis: list::get(string index)
  57. * Variables:
  58. * (empty) - element of list at position index (starting from zero) at the time of initialization
  59. *
  60. * Synopsis: list::shift()
  61. *
  62. * Synopsis: list::contains(value)
  63. * Variables:
  64. * (empty) - "true" if list contains value, "false" if not
  65. *
  66. * Synopsis:
  67. * list::find(start_pos, value)
  68. * Description:
  69. * finds the first occurance of 'value' in the list at position >='start_pos'.
  70. * Variables:
  71. * pos - position of element, or "none" if not found
  72. * found - "true" if found, "false" if not
  73. *
  74. * Sysnopsis:
  75. * list::remove_at(remove_pos)
  76. * Description:
  77. * Removes the element at position 'remove_pos', which must refer to an existing element.
  78. */
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include <stdio.h>
  82. #include <inttypes.h>
  83. #include <misc/parse_number.h>
  84. #include <ncd/NCDModule.h>
  85. #include <generated/blog_channel_ncd_list.h>
  86. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  87. struct instance {
  88. NCDModuleInst *i;
  89. NCDValue list;
  90. };
  91. struct append_instance {
  92. NCDModuleInst *i;
  93. };
  94. struct appendv_instance {
  95. NCDModuleInst *i;
  96. };
  97. struct length_instance {
  98. NCDModuleInst *i;
  99. size_t length;
  100. };
  101. struct get_instance {
  102. NCDModuleInst *i;
  103. NCDValue value;
  104. };
  105. struct shift_instance {
  106. NCDModuleInst *i;
  107. };
  108. struct contains_instance {
  109. NCDModuleInst *i;
  110. int contains;
  111. };
  112. struct find_instance {
  113. NCDModuleInst *i;
  114. int is_found;
  115. size_t found_pos;
  116. };
  117. struct removeat_instance {
  118. NCDModuleInst *i;
  119. };
  120. static void func_new_list (NCDModuleInst *i)
  121. {
  122. // allocate instance
  123. struct instance *o = malloc(sizeof(*o));
  124. if (!o) {
  125. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  126. goto fail0;
  127. }
  128. NCDModuleInst_Backend_SetUser(i, o);
  129. // init arguments
  130. o->i = i;
  131. // copy list
  132. if (!NCDValue_InitCopy(&o->list, i->args)) {
  133. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  134. goto fail1;
  135. }
  136. // signal up
  137. NCDModuleInst_Backend_Up(o->i);
  138. return;
  139. fail1:
  140. free(o);
  141. fail0:
  142. NCDModuleInst_Backend_SetError(i);
  143. NCDModuleInst_Backend_Dead(i);
  144. }
  145. static void func_new_listfrom (NCDModuleInst *i)
  146. {
  147. // allocate instance
  148. struct instance *o = malloc(sizeof(*o));
  149. if (!o) {
  150. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  151. goto fail0;
  152. }
  153. NCDModuleInst_Backend_SetUser(i, o);
  154. // init arguments
  155. o->i = i;
  156. // init list
  157. NCDValue_InitList(&o->list);
  158. // append contents of list arguments
  159. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  160. // check type
  161. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  162. ModuleLog(i, BLOG_ERROR, "wrong type");
  163. goto fail2;
  164. }
  165. // copy list
  166. NCDValue copy;
  167. if (!NCDValue_InitCopy(&copy, arg)) {
  168. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  169. goto fail2;
  170. }
  171. // append
  172. if (!NCDValue_ListAppendList(&o->list, copy)) {
  173. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  174. NCDValue_Free(&copy);
  175. goto fail2;
  176. }
  177. }
  178. // signal up
  179. NCDModuleInst_Backend_Up(o->i);
  180. return;
  181. fail2:
  182. NCDValue_Free(&o->list);
  183. fail1:
  184. free(o);
  185. fail0:
  186. NCDModuleInst_Backend_SetError(i);
  187. NCDModuleInst_Backend_Dead(i);
  188. }
  189. static void func_die (void *vo)
  190. {
  191. struct instance *o = vo;
  192. NCDModuleInst *i = o->i;
  193. // free list
  194. NCDValue_Free(&o->list);
  195. // free instance
  196. free(o);
  197. NCDModuleInst_Backend_Dead(i);
  198. }
  199. static int func_getvar (void *vo, const char *name, NCDValue *out)
  200. {
  201. struct instance *o = vo;
  202. if (!strcmp(name, "")) {
  203. if (!NCDValue_InitCopy(out, &o->list)) {
  204. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  205. return 0;
  206. }
  207. return 1;
  208. }
  209. if (!strcmp(name, "length")) {
  210. char str[64];
  211. snprintf(str, sizeof(str), "%zu", NCDValue_ListCount(&o->list));
  212. if (!NCDValue_InitString(out, str)) {
  213. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  214. return 0;
  215. }
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. static void append_func_new (NCDModuleInst *i)
  221. {
  222. // allocate instance
  223. struct append_instance *o = malloc(sizeof(*o));
  224. if (!o) {
  225. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  226. goto fail0;
  227. }
  228. NCDModuleInst_Backend_SetUser(i, o);
  229. // init arguments
  230. o->i = i;
  231. // check arguments
  232. NCDValue *arg;
  233. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  234. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  235. goto fail1;
  236. }
  237. // get method object
  238. struct instance *mo = i->method_object->inst_user;
  239. // append
  240. NCDValue v;
  241. if (!NCDValue_InitCopy(&v, arg)) {
  242. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  243. goto fail1;
  244. }
  245. if (!NCDValue_ListAppend(&mo->list, v)) {
  246. NCDValue_Free(&v);
  247. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  248. goto fail1;
  249. }
  250. // signal up
  251. NCDModuleInst_Backend_Up(o->i);
  252. return;
  253. fail1:
  254. free(o);
  255. fail0:
  256. NCDModuleInst_Backend_SetError(i);
  257. NCDModuleInst_Backend_Dead(i);
  258. }
  259. static void append_func_die (void *vo)
  260. {
  261. struct append_instance *o = vo;
  262. NCDModuleInst *i = o->i;
  263. // free instance
  264. free(o);
  265. NCDModuleInst_Backend_Dead(i);
  266. }
  267. static void appendv_func_new (NCDModuleInst *i)
  268. {
  269. // allocate instance
  270. struct appendv_instance *o = malloc(sizeof(*o));
  271. if (!o) {
  272. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  273. goto fail0;
  274. }
  275. NCDModuleInst_Backend_SetUser(i, o);
  276. // init arguments
  277. o->i = i;
  278. // check arguments
  279. NCDValue *arg;
  280. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  281. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  282. goto fail1;
  283. }
  284. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  285. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  286. goto fail1;
  287. }
  288. // get method object
  289. struct instance *mo = i->method_object->inst_user;
  290. // append
  291. NCDValue l;
  292. if (!NCDValue_InitCopy(&l, arg)) {
  293. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  294. goto fail1;
  295. }
  296. if (!NCDValue_ListAppendList(&mo->list, l)) {
  297. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  298. NCDValue_Free(&l);
  299. goto fail1;
  300. }
  301. // signal up
  302. NCDModuleInst_Backend_Up(o->i);
  303. return;
  304. fail1:
  305. free(o);
  306. fail0:
  307. NCDModuleInst_Backend_SetError(i);
  308. NCDModuleInst_Backend_Dead(i);
  309. }
  310. static void appendv_func_die (void *vo)
  311. {
  312. struct appendv_instance *o = vo;
  313. NCDModuleInst *i = o->i;
  314. // free instance
  315. free(o);
  316. NCDModuleInst_Backend_Dead(i);
  317. }
  318. static void length_func_new (NCDModuleInst *i)
  319. {
  320. // allocate instance
  321. struct length_instance *o = malloc(sizeof(*o));
  322. if (!o) {
  323. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  324. goto fail0;
  325. }
  326. NCDModuleInst_Backend_SetUser(i, o);
  327. // init arguments
  328. o->i = i;
  329. // check arguments
  330. if (!NCDValue_ListRead(o->i->args, 0)) {
  331. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  332. goto fail1;
  333. }
  334. // get method object
  335. struct instance *mo = i->method_object->inst_user;
  336. // remember length
  337. o->length = NCDValue_ListCount(&mo->list);
  338. // signal up
  339. NCDModuleInst_Backend_Up(o->i);
  340. return;
  341. fail1:
  342. free(o);
  343. fail0:
  344. NCDModuleInst_Backend_SetError(i);
  345. NCDModuleInst_Backend_Dead(i);
  346. }
  347. static void length_func_die (void *vo)
  348. {
  349. struct length_instance *o = vo;
  350. NCDModuleInst *i = o->i;
  351. // free instance
  352. free(o);
  353. NCDModuleInst_Backend_Dead(i);
  354. }
  355. static int length_func_getvar (void *vo, const char *name, NCDValue *out)
  356. {
  357. struct length_instance *o = vo;
  358. if (!strcmp(name, "")) {
  359. char str[64];
  360. snprintf(str, sizeof(str), "%zu", o->length);
  361. if (!NCDValue_InitString(out, str)) {
  362. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  363. return 0;
  364. }
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. static void get_func_new (NCDModuleInst *i)
  370. {
  371. // allocate instance
  372. struct get_instance *o = malloc(sizeof(*o));
  373. if (!o) {
  374. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  375. goto fail0;
  376. }
  377. NCDModuleInst_Backend_SetUser(i, o);
  378. // init arguments
  379. o->i = i;
  380. // check arguments
  381. NCDValue *index_arg;
  382. if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
  383. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  384. goto fail1;
  385. }
  386. if (NCDValue_Type(index_arg) != NCDVALUE_STRING) {
  387. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  388. goto fail1;
  389. }
  390. uintmax_t index;
  391. if (!parse_unsigned_integer(NCDValue_StringValue(index_arg), &index)) {
  392. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  393. goto fail1;
  394. }
  395. // get method object
  396. struct instance *mo = i->method_object->inst_user;
  397. // check index
  398. if (index >= NCDValue_ListCount(&mo->list)) {
  399. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  400. goto fail1;
  401. }
  402. // copy value
  403. if (!NCDValue_InitCopy(&o->value, NCDValue_ListGet(&mo->list, index))) {
  404. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  405. goto fail1;
  406. }
  407. // signal up
  408. NCDModuleInst_Backend_Up(o->i);
  409. return;
  410. fail1:
  411. free(o);
  412. fail0:
  413. NCDModuleInst_Backend_SetError(i);
  414. NCDModuleInst_Backend_Dead(i);
  415. }
  416. static void get_func_die (void *vo)
  417. {
  418. struct get_instance *o = vo;
  419. NCDModuleInst *i = o->i;
  420. // free value
  421. NCDValue_Free(&o->value);
  422. // free instance
  423. free(o);
  424. NCDModuleInst_Backend_Dead(i);
  425. }
  426. static int get_func_getvar (void *vo, const char *name, NCDValue *out)
  427. {
  428. struct get_instance *o = vo;
  429. if (!strcmp(name, "")) {
  430. if (!NCDValue_InitCopy(out, &o->value)) {
  431. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  432. return 0;
  433. }
  434. return 1;
  435. }
  436. return 0;
  437. }
  438. static void shift_func_new (NCDModuleInst *i)
  439. {
  440. // allocate instance
  441. struct shift_instance *o = malloc(sizeof(*o));
  442. if (!o) {
  443. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  444. goto fail0;
  445. }
  446. NCDModuleInst_Backend_SetUser(i, o);
  447. // init arguments
  448. o->i = i;
  449. // check arguments
  450. if (!NCDValue_ListRead(o->i->args, 0)) {
  451. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  452. goto fail1;
  453. }
  454. // get method object
  455. struct instance *mo = i->method_object->inst_user;
  456. // shift
  457. if (!NCDValue_ListFirst(&mo->list)) {
  458. ModuleLog(o->i, BLOG_ERROR, "list has no elements");
  459. goto fail1;
  460. }
  461. NCDValue v = NCDValue_ListShift(&mo->list);
  462. NCDValue_Free(&v);
  463. // signal up
  464. NCDModuleInst_Backend_Up(o->i);
  465. return;
  466. fail1:
  467. free(o);
  468. fail0:
  469. NCDModuleInst_Backend_SetError(i);
  470. NCDModuleInst_Backend_Dead(i);
  471. }
  472. static void shift_func_die (void *vo)
  473. {
  474. struct shift_instance *o = vo;
  475. NCDModuleInst *i = o->i;
  476. // free instance
  477. free(o);
  478. NCDModuleInst_Backend_Dead(i);
  479. }
  480. static void contains_func_new (NCDModuleInst *i)
  481. {
  482. // allocate instance
  483. struct contains_instance *o = malloc(sizeof(*o));
  484. if (!o) {
  485. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  486. goto fail0;
  487. }
  488. NCDModuleInst_Backend_SetUser(i, o);
  489. // init arguments
  490. o->i = i;
  491. // read arguments
  492. NCDValue *value_arg;
  493. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  494. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  495. goto fail1;
  496. }
  497. // get method object
  498. struct instance *mo = i->method_object->inst_user;
  499. // search
  500. o->contains = 0;
  501. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  502. if (NCDValue_Compare(v, value_arg) == 0) {
  503. o->contains = 1;
  504. break;
  505. }
  506. }
  507. // signal up
  508. NCDModuleInst_Backend_Up(o->i);
  509. return;
  510. fail1:
  511. free(o);
  512. fail0:
  513. NCDModuleInst_Backend_SetError(i);
  514. NCDModuleInst_Backend_Dead(i);
  515. }
  516. static void contains_func_die (void *vo)
  517. {
  518. struct contains_instance *o = vo;
  519. NCDModuleInst *i = o->i;
  520. // free instance
  521. free(o);
  522. NCDModuleInst_Backend_Dead(i);
  523. }
  524. static int contains_func_getvar (void *vo, const char *name, NCDValue *out)
  525. {
  526. struct contains_instance *o = vo;
  527. if (!strcmp(name, "")) {
  528. const char *value = (o->contains ? "true" : "false");
  529. if (!NCDValue_InitString(out, value)) {
  530. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  531. return 0;
  532. }
  533. return 1;
  534. }
  535. return 0;
  536. }
  537. static void find_func_new (NCDModuleInst *i)
  538. {
  539. // allocate instance
  540. struct find_instance *o = malloc(sizeof(*o));
  541. if (!o) {
  542. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  543. goto fail0;
  544. }
  545. NCDModuleInst_Backend_SetUser(i, o);
  546. // init arguments
  547. o->i = i;
  548. // read arguments
  549. NCDValue *start_pos_arg;
  550. NCDValue *value_arg;
  551. if (!NCDValue_ListRead(i->args, 2, &start_pos_arg, &value_arg)) {
  552. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  553. goto fail1;
  554. }
  555. if (NCDValue_Type(start_pos_arg) != NCDVALUE_STRING) {
  556. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  557. goto fail1;
  558. }
  559. // read start position
  560. uintmax_t start_pos;
  561. if (!parse_unsigned_integer(NCDValue_StringValue(start_pos_arg), &start_pos)) {
  562. ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
  563. goto fail1;
  564. }
  565. // get method object
  566. struct instance *mo = i->method_object->inst_user;
  567. // search
  568. o->is_found = 0;
  569. size_t pos = 0;
  570. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  571. if (pos >= start_pos && NCDValue_Compare(v, value_arg) == 0) {
  572. o->is_found = 1;
  573. o->found_pos = pos;
  574. break;
  575. }
  576. pos++;
  577. }
  578. // signal up
  579. NCDModuleInst_Backend_Up(o->i);
  580. return;
  581. fail1:
  582. free(o);
  583. fail0:
  584. NCDModuleInst_Backend_SetError(i);
  585. NCDModuleInst_Backend_Dead(i);
  586. }
  587. static void find_func_die (void *vo)
  588. {
  589. struct find_instance *o = vo;
  590. NCDModuleInst *i = o->i;
  591. // free instance
  592. free(o);
  593. NCDModuleInst_Backend_Dead(i);
  594. }
  595. static int find_func_getvar (void *vo, const char *name, NCDValue *out)
  596. {
  597. struct find_instance *o = vo;
  598. if (!strcmp(name, "pos")) {
  599. char value[64];
  600. if (o->is_found) {
  601. snprintf(value, sizeof(value), "%zu", o->found_pos);
  602. } else {
  603. snprintf(value, sizeof(value), "none");
  604. }
  605. if (!NCDValue_InitString(out, value)) {
  606. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  607. return 0;
  608. }
  609. return 1;
  610. }
  611. if (!strcmp(name, "found")) {
  612. const char *value = (o->is_found ? "true" : "false");
  613. if (!NCDValue_InitString(out, value)) {
  614. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  615. return 0;
  616. }
  617. return 1;
  618. }
  619. return 0;
  620. }
  621. static void removeat_func_new (NCDModuleInst *i)
  622. {
  623. // allocate instance
  624. struct removeat_instance *o = malloc(sizeof(*o));
  625. if (!o) {
  626. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  627. goto fail0;
  628. }
  629. NCDModuleInst_Backend_SetUser(i, o);
  630. // init arguments
  631. o->i = i;
  632. // read arguments
  633. NCDValue *remove_pos_arg;
  634. if (!NCDValue_ListRead(i->args, 1, &remove_pos_arg)) {
  635. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  636. goto fail1;
  637. }
  638. if (NCDValue_Type(remove_pos_arg) != NCDVALUE_STRING) {
  639. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  640. goto fail1;
  641. }
  642. // read position
  643. uintmax_t remove_pos;
  644. if (!parse_unsigned_integer(NCDValue_StringValue(remove_pos_arg), &remove_pos)) {
  645. ModuleLog(o->i, BLOG_ERROR, "wrong pos");
  646. goto fail1;
  647. }
  648. // get method object
  649. struct instance *mo = i->method_object->inst_user;
  650. // check position
  651. if (remove_pos >= NCDValue_ListCount(&mo->list)) {
  652. ModuleLog(o->i, BLOG_ERROR, "pos out of range");
  653. goto fail1;
  654. }
  655. // find and remove
  656. size_t pos = 0;
  657. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  658. if (pos == remove_pos) {
  659. NCDValue removed_v = NCDValue_ListRemove(&mo->list, v);
  660. NCDValue_Free(&removed_v);
  661. break;
  662. }
  663. pos++;
  664. }
  665. // signal up
  666. NCDModuleInst_Backend_Up(o->i);
  667. return;
  668. fail1:
  669. free(o);
  670. fail0:
  671. NCDModuleInst_Backend_SetError(i);
  672. NCDModuleInst_Backend_Dead(i);
  673. }
  674. static void removeat_func_die (void *vo)
  675. {
  676. struct removeat_instance *o = vo;
  677. NCDModuleInst *i = o->i;
  678. // free instance
  679. free(o);
  680. NCDModuleInst_Backend_Dead(i);
  681. }
  682. static const struct NCDModule modules[] = {
  683. {
  684. .type = "list",
  685. .func_new = func_new_list,
  686. .func_die = func_die,
  687. .func_getvar = func_getvar
  688. }, {
  689. .type = "listfrom",
  690. .base_type = "list",
  691. .func_new = func_new_listfrom,
  692. .func_die = func_die,
  693. .func_getvar = func_getvar
  694. }, {
  695. .type = "concatlist", // alias for listfrom
  696. .base_type = "list",
  697. .func_new = func_new_listfrom,
  698. .func_die = func_die,
  699. .func_getvar = func_getvar
  700. }, {
  701. .type = "list::append",
  702. .func_new = append_func_new,
  703. .func_die = append_func_die
  704. }, {
  705. .type = "list::appendv",
  706. .func_new = appendv_func_new,
  707. .func_die = appendv_func_die
  708. }, {
  709. .type = "list::length",
  710. .func_new = length_func_new,
  711. .func_die = length_func_die,
  712. .func_getvar = length_func_getvar
  713. }, {
  714. .type = "list::get",
  715. .func_new = get_func_new,
  716. .func_die = get_func_die,
  717. .func_getvar = get_func_getvar
  718. }, {
  719. .type = "list::shift",
  720. .func_new = shift_func_new,
  721. .func_die = shift_func_die
  722. }, {
  723. .type = "list::contains",
  724. .func_new = contains_func_new,
  725. .func_die = contains_func_die,
  726. .func_getvar = contains_func_getvar
  727. }, {
  728. .type = "list::find",
  729. .func_new = find_func_new,
  730. .func_die = find_func_die,
  731. .func_getvar = find_func_getvar
  732. }, {
  733. .type = "list::remove_at",
  734. .func_new = removeat_func_new,
  735. .func_die = removeat_func_die
  736. }, {
  737. .type = NULL
  738. }
  739. };
  740. const struct NCDModuleGroup ncdmodule_list = {
  741. .modules = modules
  742. };