static error test_add(atom_set_t *d) { error err; int i; printf("test: add\n"); for (i = 0; i < NELEMS(data); i++) { atom_t idx; printf("adding '%s'... ", data[i]); err = atom_new(d, (const unsigned char *) data[i], strlen(data[i]) + 1, &idx); if (err && err != error_ATOM_NAME_EXISTS) return err; if (err == error_ATOM_NAME_EXISTS) printf("already exists "); printf("as %d\n", idx); } return error_OK; }
const char* atom_int(long n) { char str[43]; char *str1 = str + sizeof(str); unsigned long m; if(n == LONG_MIN) { m = LONG_MAX + 1UL; } else if(n < 0) { m = -n; } else { m = n; } do { *(--str1) = m%10 + '0'; m = m/10; }while(m); if(n < 0) { *(--str1) = '-'; } return atom_new(str1,str + sizeof(str) - str1); }
error filenamedb_add(filenamedb_t *db, const char *id, const char *filename) { error err; int kindex; atom_t vindex; const unsigned char *key; const char *value; err = digestdb_add((const unsigned char *) id, &kindex); if (err) return err; key = digestdb_get(kindex); err = atom_new(db->filenames, (const unsigned char *) filename, strlen(filename) + 1, &vindex); if (err == error_ATOM_NAME_EXISTS) err = error_OK; else if (err) return err; value = (const char *) atom_get(db->filenames, vindex, NULL); /* this will update the value if the key is already present */ hash_insert(db->hash, (unsigned char *) key, (char *) value); return error_OK; }
static error test_random(atom_set_t *d) { error err; int i; printf("test: random\n"); for (i = 0; i < 100; i++) { const char *name; atom_t idx; name = randomname(); printf("adding '%s'... ", name); err = atom_new(d, (const unsigned char *) name, strlen(name) + 1, &idx); if (err && err != error_ATOM_NAME_EXISTS) return err; if (err == error_ATOM_NAME_EXISTS) printf("already exists "); printf("as %d\n", idx); } return error_OK; }
result_t digestdb_add(const unsigned char *digest, int *index) { result_t err; err = atom_new(LOCALS.digests, digest, digestdb_DIGESTSZ, (atom_t *) index); if (err == result_ATOM_NAME_EXISTS) err = result_OK; return err; }
const char *atom_int(long n){ char str[MAX_NUM_LEN]; char *s = str + sizeof(str); unsigned long m; if(n==LONG_MIN) m = LONG_MAX + 1UL; else if (n<0) m = -n; else m = n; do *--s = m%10+'0'; while((m/=10)>0); if(n<0) *--s = '-'; return atom_new(s, str + sizeof(str) - s); }
error atom_set(atom_set_t *s, atom_t a, const unsigned char *block, size_t length) { error err; atom_t newa; loc *p, *q; loc t; assert(s); assert(a != atom_NOT_FOUND); assert(block); assert(length > 0); if (!s->locpools) return error_ATOM_SET_EMPTY; assert(s->l_used >= 1); if (!ATOMVALID(a)) return error_ATOM_OUT_OF_RANGE; err = atom_new(s, block, length, &newa); if (err == error_ATOM_NAME_EXISTS && newa == a) /* setting an atom to its existing data is ignored */ return error_OK; else if (err) return err; atom_delete(s, a); /* now transpose old and new atoms */ p = &ATOMLOC(a); q = &ATOMLOC(newa); t = *p; *p = *q; *q = t; return error_OK; }
static error test_rename(atom_set_t *d) { error err; int i; printf("test: rename\n"); for (i = 0; i < NELEMS(newnames); i++) { atom_t idx; printf("adding '%s'... ", data[i]); err = atom_new(d, (const unsigned char *) data[i], strlen(data[i]) + 1, &idx); if (err && err != error_ATOM_NAME_EXISTS) return err; if (err == error_ATOM_NAME_EXISTS) printf("already exists "); printf("as %d\n", idx); printf("renaming index %d to '%s'... ", idx, newnames[i]); err = atom_set(d, idx, (const unsigned char *) newnames[i], strlen(newnames[i]) + 1); if (err == error_ATOM_NAME_EXISTS) printf("already exists!"); else if (err) return err; else printf("ok"); printf("\n"); } return error_OK; }
static Atom* parse_line(const gchar* ln) { static GRegex* re = NULL; GMatchInfo* mi = NULL; Atom* rv = NULL; if ( !re ) { re = g_regex_new("(-*)([A-Z]+)(?:\\((.+)*\\))?", G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL); } if ( g_regex_match(re, ln, 0, &mi) ) { gchar* dashes = g_match_info_fetch(mi, 1); rv = atom_new( identify(g_match_info_fetch(mi, 2)), dashes ? strlen(dashes) : 0, build_args(g_match_info_fetch(mi, 3))); g_match_info_free(mi); } return rv; }
int tokenize(char *e) { int position = 0; int i; regmatch_t pmatch; nr_token = 0; while(e[position] != '\0') { /* Try all rules one by one. */ for(i = 0; i < NR_REGEX; i++) { if(regexec(&re[i], e + position, 1, &pmatch, 0) == 0 && pmatch.rm_so == 0) { char *substr_start = e + position; int substr_len = pmatch.rm_eo; position += substr_len; int this_type = rules[i].token_type; if (this_type != NOTYPE) { tokens[nr_token].type = this_type; tokens[nr_token].ap = atom_new(substr_start, this_type, substr_len); atom *ap = tokens[nr_token].ap; Log("token[%d], %s \"%s\", size %d", nr_token, type_repr(ap->type), atom_repr(ap), ap->len); ++nr_token; } break; /* jump out of for loop */ } } if (i == NR_REGEX) { test(0, "no match at position %d\n%s\n%*.s^\n", position, e, position, ""); } } tokens_begin = 0; tokens_end = nr_token; return nr_token; }
const char *atom_int(long n) { char str[43]; char *s = str + sizeof(str); unsigned long m; if (n == LONG_MIN) { m = LONG_MAX + 1UL; } else if (n < 0) { m = -n; } else { m = n; } do { *--s = m % 10 + '0'; } while ((m /= 10) > 0); if (n < 0) { *--s = '-'; } return atom_new(s, sizeof(str) - (s-str)); }
static error unformat_value(const char *buf, size_t len, void **value, void *opaque) { error err; filenamedb_t *db = opaque; atom_t vindex; NOT_USED(len); err = atom_new(db->filenames, (const unsigned char *) buf, strlen(buf) + 1, // use 'len'? &vindex); if (err == error_ATOM_NAME_EXISTS) err = error_OK; else if (err) return err; *value = (void *) atom_get(db->filenames, vindex, NULL); // casting away const return error_OK; }
/* token name will be duplicated */ Atom* atom_new_token(const char* token_name) { Atom* a = atom_new(); atom_set_token(a, token_name); return a; }
Atom* atom_reference(const Atom* ref) { Atom* a = atom_new(); atom_set_reference(a, ref); return a; }
const char* atom_string(const char *str) { assert(str); return atom_new(str,strlen(str)); }
Atom* atom_new_unknown(void) { return atom_new(); }
Atom* atom_new_int(long value) { Atom* a = atom_new(); atom_set_int(a, value); return a; }
Atom* atom_new_uint(unsigned long value) { Atom* a = atom_new(); atom_set_uint(a, value); return a; }
/* str will be duplicate */ Atom* atom_new_string(const char* str) { Atom* a = atom_new(); atom_set_string(a, str); return a; }
Atom* atom_new_boolean(char boolean) { Atom* a = atom_new(); atom_set_boolean(a, boolean); return a; }
/* func will be duplicated */ Atom* atom_new_function(const Function* func) { Atom* a = atom_new(); atom_set_function(a, func); return a; }
/* list will be duplicated */ Atom* atom_new_list(const List* list) { Atom* a = atom_new(); atom_set_list(a, list); return a; }