list.c 23 KB

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