static void gen_tf (const struct parse_tf *tf, FILE *out, FILE *f_strs, const array_t *arrs, int narrs) { char *buf_deps, *buf_ports; size_t sz_deps, sz_ports; FILE *f_ports = open_memstream (&buf_ports, &sz_ports); FILE *f_deps = open_memstream (&buf_deps, &sz_deps); int start = ftell (out); struct tf hdr = {ftell (f_strs) + VALID_OFS, tf->nrules}; if (tf->prefix) fwrite (tf->prefix, 1, strlen (tf->prefix) + 1, f_strs); else hdr.prefix = 0; fwrite (&hdr, sizeof hdr, 1, out); /* TODO: Alignment? */ struct rule rules[hdr.nrules]; memset (rules, 0, sizeof rules); int i = 0; for (struct parse_rule *r = tf->rules.head; r; r = r->next, i++) { struct rule *tmp = &rules[i]; tmp->idx = r->idx; tmp->in = gen_ports (ARR (r->in), r->in.n, f_ports); tmp->out = gen_ports (ARR (r->out), r->out.n, f_ports); tmp->match = arr_find (r->match, arrs, narrs); tmp->mask = arr_find (r->mask, arrs, narrs); tmp->rewrite = arr_find (r->rewrite, arrs, narrs); if (r->deps.head) tmp->deps = gen_deps (&r->deps, f_deps, f_ports, arrs, narrs); //tmp->desc = barfoo; } fclose (f_ports); fclose (f_deps); qsort (rules, hdr.nrules, sizeof *rules, rule_cmp); fwrite (rules, hdr.nrules, sizeof *rules, out); hdr.map_ofs = ftell (out) - start; gen_map (out, &tf->in_map, rules, ARR_LEN (rules)); hdr.ports_ofs = ftell (out) - start; fwrite (buf_ports, 1, sz_ports, out); free (buf_ports); hdr.deps_ofs = ftell (out) - start; fwrite (buf_deps, 1, sz_deps, out); free (buf_deps); int end = ftell (out); fseek (out, start, SEEK_SET); fwrite (&hdr, sizeof hdr, 1, out); fseek (out, end, SEEK_SET); }
static void new_void_arr_find(void**state) { arr_t * arr = arr_new(5); int mas[5] = {1 , 3 , 3 ,3 ,5}; for(int i = 0 ; i<5 ; i++) arr_add(arr, i, mas[i]); assert_int_equal(arr_find(arr) , 3); arr_remove(&arr); }
static uint32_t gen_deps (struct list_parse_dep *deps, FILE *f_deps, FILE *f_ports, const array_t *arrs, int narrs) { uint32_t n = deps->n; uint32_t ret = VALID_OFS + ftell (f_deps); fwrite (&n, sizeof n, 1, f_deps); for (struct parse_dep *dep = deps->head; dep; dep = dep->next) { struct dep tmp = {dep->rule}; tmp.match = arr_find (dep->match, arrs, narrs); tmp.port = gen_ports (dep->ports, dep->nports, f_ports); fwrite (&tmp, sizeof tmp, 1, f_deps); } return ret; }