list.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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 occurrence 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. * Synopsis:
  80. * list::remove(value)
  81. * Description:
  82. * Removes the first occurrence of value in the list, which must be in the list.
  83. *
  84. * Synopsis:
  85. * list::set(list l1, ..., list lN)
  86. * Description:
  87. * Replaces the list with the concatenation of given lists.
  88. */
  89. #include <stdlib.h>
  90. #include <string.h>
  91. #include <stdio.h>
  92. #include <inttypes.h>
  93. #include <misc/offset.h>
  94. #include <misc/parse_number.h>
  95. #include <structure/IndexedList.h>
  96. #include <ncd/NCDModule.h>
  97. #include <ncd/value_utils.h>
  98. #include <generated/blog_channel_ncd_list.h>
  99. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  100. struct elem {
  101. IndexedListNode il_node;
  102. NCDValMem mem;
  103. NCDValRef val;
  104. };
  105. struct instance {
  106. NCDModuleInst *i;
  107. IndexedList il;
  108. };
  109. struct length_instance {
  110. NCDModuleInst *i;
  111. uint64_t length;
  112. };
  113. struct get_instance {
  114. NCDModuleInst *i;
  115. NCDValMem mem;
  116. NCDValRef val;
  117. };
  118. struct contains_instance {
  119. NCDModuleInst *i;
  120. int contains;
  121. };
  122. struct find_instance {
  123. NCDModuleInst *i;
  124. int is_found;
  125. uint64_t found_pos;
  126. };
  127. static uint64_t list_count (struct instance *o)
  128. {
  129. return IndexedList_Count(&o->il);
  130. }
  131. static struct elem * insert_value (NCDModuleInst *i, struct instance *o, NCDValRef val, uint64_t idx)
  132. {
  133. ASSERT(idx <= list_count(o))
  134. ASSERT(!NCDVal_IsInvalid(val))
  135. struct elem *e = malloc(sizeof(*e));
  136. if (!e) {
  137. ModuleLog(i, BLOG_ERROR, "malloc failed");
  138. goto fail0;
  139. }
  140. NCDValMem_Init(&e->mem);
  141. e->val = NCDVal_NewCopy(&e->mem, val);
  142. if (NCDVal_IsInvalid(e->val)) {
  143. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  144. goto fail1;
  145. }
  146. IndexedList_InsertAt(&o->il, &e->il_node, idx);
  147. return e;
  148. fail1:
  149. NCDValMem_Free(&e->mem);
  150. free(e);
  151. fail0:
  152. return NULL;
  153. }
  154. static void remove_elem (struct instance *o, struct elem *e)
  155. {
  156. IndexedList_Remove(&o->il, &e->il_node);
  157. NCDValMem_Free(&e->mem);
  158. free(e);
  159. }
  160. static struct elem * get_elem_at (struct instance *o, uint64_t idx)
  161. {
  162. ASSERT(idx < list_count(o))
  163. IndexedListNode *iln = IndexedList_GetAt(&o->il, idx);
  164. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  165. return e;
  166. }
  167. static struct elem * get_first_elem (struct instance *o)
  168. {
  169. ASSERT(list_count(o) > 0)
  170. IndexedListNode *iln = IndexedList_GetFirst(&o->il);
  171. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  172. return e;
  173. }
  174. static struct elem * get_last_elem (struct instance *o)
  175. {
  176. ASSERT(list_count(o) > 0)
  177. IndexedListNode *iln = IndexedList_GetLast(&o->il);
  178. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  179. return e;
  180. }
  181. static void cut_list_front (struct instance *o, uint64_t count)
  182. {
  183. while (list_count(o) > count) {
  184. remove_elem(o, get_first_elem(o));
  185. }
  186. }
  187. static void cut_list_back (struct instance *o, uint64_t count)
  188. {
  189. while (list_count(o) > count) {
  190. remove_elem(o, get_last_elem(o));
  191. }
  192. }
  193. static int append_list_contents (NCDModuleInst *i, struct instance *o, NCDValRef args)
  194. {
  195. ASSERT(NCDVal_IsList(args))
  196. uint64_t orig_count = list_count(o);
  197. size_t append_count = NCDVal_ListCount(args);
  198. for (size_t j = 0; j < append_count; j++) {
  199. NCDValRef elem = NCDVal_ListGet(args, j);
  200. if (!insert_value(i, o, elem, list_count(o))) {
  201. goto fail;
  202. }
  203. }
  204. return 1;
  205. fail:
  206. cut_list_back(o, orig_count);
  207. return 0;
  208. }
  209. static int append_list_contents_contents (NCDModuleInst *i, struct instance *o, NCDValRef args)
  210. {
  211. ASSERT(NCDVal_IsList(args))
  212. uint64_t orig_count = list_count(o);
  213. size_t append_count = NCDVal_ListCount(args);
  214. for (size_t j = 0; j < append_count; j++) {
  215. NCDValRef elem = NCDVal_ListGet(args, j);
  216. if (!NCDVal_IsList(elem)) {
  217. ModuleLog(i, BLOG_ERROR, "wrong type");
  218. goto fail;
  219. }
  220. if (!append_list_contents(i, o, elem)) {
  221. goto fail;
  222. }
  223. }
  224. return 1;
  225. fail:
  226. cut_list_back(o, orig_count);
  227. return 0;
  228. }
  229. static struct elem * find_elem (struct instance *o, NCDValRef val, uint64_t start_idx, uint64_t *out_idx)
  230. {
  231. if (start_idx >= list_count(o)) {
  232. return NULL;
  233. }
  234. for (IndexedListNode *iln = IndexedList_GetAt(&o->il, start_idx); iln; iln = IndexedList_GetNext(&o->il, iln)) {
  235. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  236. if (NCDVal_Compare(e->val, val) == 0) {
  237. if (out_idx) {
  238. *out_idx = start_idx;
  239. }
  240. return e;
  241. }
  242. start_idx++;
  243. }
  244. return NULL;
  245. }
  246. static int list_to_value (NCDModuleInst *i, struct instance *o, NCDValMem *mem, NCDValRef *out_val)
  247. {
  248. *out_val = NCDVal_NewList(mem, IndexedList_Count(&o->il));
  249. if (NCDVal_IsInvalid(*out_val)) {
  250. ModuleLog(i, BLOG_ERROR, "NCDVal_NewList failed");
  251. goto fail;
  252. }
  253. for (IndexedListNode *iln = IndexedList_GetFirst(&o->il); iln; iln = IndexedList_GetNext(&o->il, iln)) {
  254. struct elem *e = UPPER_OBJECT(iln, struct elem, il_node);
  255. NCDValRef copy = NCDVal_NewCopy(mem, e->val);
  256. if (NCDVal_IsInvalid(copy)) {
  257. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  258. goto fail;
  259. }
  260. NCDVal_ListAppend(*out_val, copy);
  261. }
  262. return 1;
  263. fail:
  264. return 0;
  265. }
  266. static void func_new_list (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  267. {
  268. struct instance *o = vo;
  269. o->i = i;
  270. // init list
  271. IndexedList_Init(&o->il);
  272. // append contents
  273. if (!append_list_contents(i, o, params->args)) {
  274. goto fail1;
  275. }
  276. // signal up
  277. NCDModuleInst_Backend_Up(o->i);
  278. return;
  279. fail1:
  280. cut_list_front(o, 0);
  281. NCDModuleInst_Backend_SetError(i);
  282. NCDModuleInst_Backend_Dead(i);
  283. }
  284. static void func_new_listfrom (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  285. {
  286. struct instance *o = vo;
  287. o->i = i;
  288. // init list
  289. IndexedList_Init(&o->il);
  290. // append contents contents
  291. if (!append_list_contents_contents(i, o, params->args)) {
  292. goto fail1;
  293. }
  294. // signal up
  295. NCDModuleInst_Backend_Up(o->i);
  296. return;
  297. fail1:
  298. cut_list_front(o, 0);
  299. NCDModuleInst_Backend_SetError(i);
  300. NCDModuleInst_Backend_Dead(i);
  301. }
  302. static void func_die (void *vo)
  303. {
  304. struct instance *o = vo;
  305. // free list elements
  306. cut_list_front(o, 0);
  307. NCDModuleInst_Backend_Dead(o->i);
  308. }
  309. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  310. {
  311. struct instance *o = vo;
  312. if (!strcmp(name, "")) {
  313. if (!list_to_value(o->i, o, mem, out)) {
  314. return 0;
  315. }
  316. return 1;
  317. }
  318. if (!strcmp(name, "length")) {
  319. *out = ncd_make_uintmax(mem, list_count(o));
  320. if (NCDVal_IsInvalid(*out)) {
  321. ModuleLog(o->i, BLOG_ERROR, "ncd_make_uintmax failed");
  322. }
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. static void append_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  328. {
  329. // check arguments
  330. NCDValRef arg;
  331. if (!NCDVal_ListRead(params->args, 1, &arg)) {
  332. ModuleLog(i, BLOG_ERROR, "wrong arity");
  333. goto fail0;
  334. }
  335. // get method object
  336. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  337. // append
  338. if (!insert_value(i, mo, arg, list_count(mo))) {
  339. goto fail0;
  340. }
  341. // signal up
  342. NCDModuleInst_Backend_Up(i);
  343. return;
  344. fail0:
  345. NCDModuleInst_Backend_SetError(i);
  346. NCDModuleInst_Backend_Dead(i);
  347. }
  348. static void appendv_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  349. {
  350. // check arguments
  351. NCDValRef arg;
  352. if (!NCDVal_ListRead(params->args, 1, &arg)) {
  353. ModuleLog(i, BLOG_ERROR, "wrong arity");
  354. goto fail0;
  355. }
  356. if (!NCDVal_IsList(arg)) {
  357. ModuleLog(i, BLOG_ERROR, "wrong type");
  358. goto fail0;
  359. }
  360. // get method object
  361. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  362. // append
  363. if (!append_list_contents(i, mo, arg)) {
  364. goto fail0;
  365. }
  366. // signal up
  367. NCDModuleInst_Backend_Up(i);
  368. return;
  369. fail0:
  370. NCDModuleInst_Backend_SetError(i);
  371. NCDModuleInst_Backend_Dead(i);
  372. }
  373. static void length_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  374. {
  375. struct length_instance *o = vo;
  376. o->i = i;
  377. // check arguments
  378. if (!NCDVal_ListRead(params->args, 0)) {
  379. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  380. goto fail0;
  381. }
  382. // get method object
  383. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  384. // remember length
  385. o->length = list_count(mo);
  386. // signal up
  387. NCDModuleInst_Backend_Up(o->i);
  388. return;
  389. fail0:
  390. NCDModuleInst_Backend_SetError(i);
  391. NCDModuleInst_Backend_Dead(i);
  392. }
  393. static void length_func_die (void *vo)
  394. {
  395. struct length_instance *o = vo;
  396. NCDModuleInst_Backend_Dead(o->i);
  397. }
  398. static int length_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  399. {
  400. struct length_instance *o = vo;
  401. if (!strcmp(name, "")) {
  402. *out = ncd_make_uintmax(mem, o->length);
  403. if (NCDVal_IsInvalid(*out)) {
  404. ModuleLog(o->i, BLOG_ERROR, "ncd_make_uintmax failed");
  405. }
  406. return 1;
  407. }
  408. return 0;
  409. }
  410. static void get_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  411. {
  412. struct get_instance *o = vo;
  413. o->i = i;
  414. // check arguments
  415. NCDValRef index_arg;
  416. if (!NCDVal_ListRead(params->args, 1, &index_arg)) {
  417. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  418. goto fail0;
  419. }
  420. if (!NCDVal_IsString(index_arg)) {
  421. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  422. goto fail0;
  423. }
  424. uintmax_t index;
  425. if (!ncd_read_uintmax(index_arg, &index)) {
  426. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  427. goto fail0;
  428. }
  429. // get method object
  430. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  431. // check index
  432. if (index >= list_count(mo)) {
  433. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  434. goto fail0;
  435. }
  436. // get element
  437. struct elem *e = get_elem_at(mo, index);
  438. // init mem
  439. NCDValMem_Init(&o->mem);
  440. // copy value
  441. o->val = NCDVal_NewCopy(&o->mem, e->val);
  442. if (NCDVal_IsInvalid(o->val)) {
  443. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  444. goto fail1;
  445. }
  446. // signal up
  447. NCDModuleInst_Backend_Up(o->i);
  448. return;
  449. fail1:
  450. NCDValMem_Free(&o->mem);
  451. fail0:
  452. NCDModuleInst_Backend_SetError(i);
  453. NCDModuleInst_Backend_Dead(i);
  454. }
  455. static void get_func_die (void *vo)
  456. {
  457. struct get_instance *o = vo;
  458. // free mem
  459. NCDValMem_Free(&o->mem);
  460. NCDModuleInst_Backend_Dead(o->i);
  461. }
  462. static int get_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  463. {
  464. struct get_instance *o = vo;
  465. if (!strcmp(name, "")) {
  466. *out = NCDVal_NewCopy(mem, o->val);
  467. if (NCDVal_IsInvalid(*out)) {
  468. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  469. }
  470. return 1;
  471. }
  472. return 0;
  473. }
  474. static void shift_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  475. {
  476. // check arguments
  477. if (!NCDVal_ListRead(params->args, 0)) {
  478. ModuleLog(i, BLOG_ERROR, "wrong arity");
  479. goto fail0;
  480. }
  481. // get method object
  482. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  483. // check first
  484. if (list_count(mo) == 0) {
  485. ModuleLog(i, BLOG_ERROR, "list has no elements");
  486. goto fail0;
  487. }
  488. // remove first
  489. remove_elem(mo, get_first_elem(mo));
  490. // signal up
  491. NCDModuleInst_Backend_Up(i);
  492. return;
  493. fail0:
  494. NCDModuleInst_Backend_SetError(i);
  495. NCDModuleInst_Backend_Dead(i);
  496. }
  497. static void contains_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  498. {
  499. struct contains_instance *o = vo;
  500. o->i = i;
  501. // read arguments
  502. NCDValRef value_arg;
  503. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  504. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  505. goto fail0;
  506. }
  507. // get method object
  508. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  509. // search
  510. o->contains = !!find_elem(mo, value_arg, 0, NULL);
  511. // signal up
  512. NCDModuleInst_Backend_Up(o->i);
  513. return;
  514. fail0:
  515. NCDModuleInst_Backend_SetError(i);
  516. NCDModuleInst_Backend_Dead(i);
  517. }
  518. static void contains_func_die (void *vo)
  519. {
  520. struct contains_instance *o = vo;
  521. NCDModuleInst_Backend_Dead(o->i);
  522. }
  523. static int contains_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  524. {
  525. struct contains_instance *o = vo;
  526. if (!strcmp(name, "")) {
  527. const char *value = (o->contains ? "true" : "false");
  528. *out = NCDVal_NewString(mem, value);
  529. if (NCDVal_IsInvalid(*out)) {
  530. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  531. }
  532. return 1;
  533. }
  534. return 0;
  535. }
  536. static void find_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  537. {
  538. struct find_instance *o = vo;
  539. o->i = i;
  540. // read arguments
  541. NCDValRef start_pos_arg;
  542. NCDValRef value_arg;
  543. if (!NCDVal_ListRead(params->args, 2, &start_pos_arg, &value_arg)) {
  544. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  545. goto fail0;
  546. }
  547. if (!NCDVal_IsString(start_pos_arg)) {
  548. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  549. goto fail0;
  550. }
  551. // read start position
  552. uintmax_t start_pos;
  553. if (!ncd_read_uintmax(start_pos_arg, &start_pos) || start_pos > UINT64_MAX) {
  554. ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
  555. goto fail0;
  556. }
  557. // get method object
  558. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  559. // find
  560. o->is_found = !!find_elem(mo, value_arg, start_pos, &o->found_pos);
  561. // signal up
  562. NCDModuleInst_Backend_Up(o->i);
  563. return;
  564. fail0:
  565. NCDModuleInst_Backend_SetError(i);
  566. NCDModuleInst_Backend_Dead(i);
  567. }
  568. static void find_func_die (void *vo)
  569. {
  570. struct find_instance *o = vo;
  571. NCDModuleInst_Backend_Dead(o->i);
  572. }
  573. static int find_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  574. {
  575. struct find_instance *o = vo;
  576. if (!strcmp(name, "pos")) {
  577. char value[64] = "none";
  578. if (o->is_found) {
  579. generate_decimal_repr_string(o->found_pos, value);
  580. }
  581. *out = NCDVal_NewString(mem, value);
  582. if (NCDVal_IsInvalid(*out)) {
  583. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  584. }
  585. return 1;
  586. }
  587. if (!strcmp(name, "found")) {
  588. const char *value = (o->is_found ? "true" : "false");
  589. *out = NCDVal_NewString(mem, value);
  590. if (NCDVal_IsInvalid(*out)) {
  591. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  592. }
  593. return 1;
  594. }
  595. return 0;
  596. }
  597. static void removeat_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  598. {
  599. // read arguments
  600. NCDValRef remove_pos_arg;
  601. if (!NCDVal_ListRead(params->args, 1, &remove_pos_arg)) {
  602. ModuleLog(i, BLOG_ERROR, "wrong arity");
  603. goto fail0;
  604. }
  605. if (!NCDVal_IsString(remove_pos_arg)) {
  606. ModuleLog(i, BLOG_ERROR, "wrong type");
  607. goto fail0;
  608. }
  609. // read position
  610. uintmax_t remove_pos;
  611. if (!ncd_read_uintmax(remove_pos_arg, &remove_pos)) {
  612. ModuleLog(i, BLOG_ERROR, "wrong pos");
  613. goto fail0;
  614. }
  615. // get method object
  616. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  617. // check position
  618. if (remove_pos >= list_count(mo)) {
  619. ModuleLog(i, BLOG_ERROR, "pos out of range");
  620. goto fail0;
  621. }
  622. // remove
  623. remove_elem(mo, get_elem_at(mo, remove_pos));
  624. // signal up
  625. NCDModuleInst_Backend_Up(i);
  626. return;
  627. fail0:
  628. NCDModuleInst_Backend_SetError(i);
  629. NCDModuleInst_Backend_Dead(i);
  630. }
  631. static void remove_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  632. {
  633. // read arguments
  634. NCDValRef value_arg;
  635. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  636. ModuleLog(i, BLOG_ERROR, "wrong arity");
  637. goto fail0;
  638. }
  639. // get method object
  640. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  641. // find element
  642. struct elem *e = find_elem(mo, value_arg, 0, NULL);
  643. if (!e) {
  644. ModuleLog(i, BLOG_ERROR, "value does not exist");
  645. goto fail0;
  646. }
  647. // remove element
  648. remove_elem(mo, e);
  649. // signal up
  650. NCDModuleInst_Backend_Up(i);
  651. return;
  652. fail0:
  653. NCDModuleInst_Backend_SetError(i);
  654. NCDModuleInst_Backend_Dead(i);
  655. }
  656. static void set_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  657. {
  658. // get method object
  659. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  660. // remember old count
  661. uint64_t old_count = list_count(mo);
  662. // append contents of our lists
  663. if (!append_list_contents_contents(i, mo, params->args)) {
  664. goto fail0;
  665. }
  666. // remove old elements
  667. cut_list_front(mo, list_count(mo) - old_count);
  668. // signal up
  669. NCDModuleInst_Backend_Up(i);
  670. return;
  671. fail0:
  672. NCDModuleInst_Backend_SetError(i);
  673. NCDModuleInst_Backend_Dead(i);
  674. }
  675. static struct NCDModule modules[] = {
  676. {
  677. .type = "list",
  678. .func_new2 = func_new_list,
  679. .func_die = func_die,
  680. .func_getvar = func_getvar,
  681. .alloc_size = sizeof(struct instance)
  682. }, {
  683. .type = "listfrom",
  684. .base_type = "list",
  685. .func_new2 = func_new_listfrom,
  686. .func_die = func_die,
  687. .func_getvar = func_getvar,
  688. .alloc_size = sizeof(struct instance)
  689. }, {
  690. .type = "concatlist", // alias for listfrom
  691. .base_type = "list",
  692. .func_new2 = func_new_listfrom,
  693. .func_die = func_die,
  694. .func_getvar = func_getvar,
  695. .alloc_size = sizeof(struct instance)
  696. }, {
  697. .type = "list::append",
  698. .func_new2 = append_func_new
  699. }, {
  700. .type = "list::appendv",
  701. .func_new2 = appendv_func_new
  702. }, {
  703. .type = "list::length",
  704. .func_new2 = length_func_new,
  705. .func_die = length_func_die,
  706. .func_getvar = length_func_getvar,
  707. .alloc_size = sizeof(struct length_instance)
  708. }, {
  709. .type = "list::get",
  710. .func_new2 = get_func_new,
  711. .func_die = get_func_die,
  712. .func_getvar = get_func_getvar,
  713. .alloc_size = sizeof(struct get_instance)
  714. }, {
  715. .type = "list::shift",
  716. .func_new2 = shift_func_new
  717. }, {
  718. .type = "list::contains",
  719. .func_new2 = contains_func_new,
  720. .func_die = contains_func_die,
  721. .func_getvar = contains_func_getvar,
  722. .alloc_size = sizeof(struct contains_instance)
  723. }, {
  724. .type = "list::find",
  725. .func_new2 = find_func_new,
  726. .func_die = find_func_die,
  727. .func_getvar = find_func_getvar,
  728. .alloc_size = sizeof(struct find_instance)
  729. }, {
  730. .type = "list::remove_at",
  731. .func_new2 = removeat_func_new
  732. }, {
  733. .type = "list::remove",
  734. .func_new2 = remove_func_new
  735. }, {
  736. .type = "list::set",
  737. .func_new2 = set_func_new
  738. }, {
  739. .type = NULL
  740. }
  741. };
  742. const struct NCDModuleGroup ncdmodule_list = {
  743. .modules = modules
  744. };