bag_t *generate_index(FILE *input, int min_word_len) { bag_t *index = bag_create(entry_cmp); if (index) { char word[LINE_LENGTH] = ""; entry_t new_word, *existing_entry; bag_elem_t new_entry; unsigned page = 0; while (get_word(input, word, &page)) { new_word.entry_word = word; // check if the length of the word is long enough if(strlen(word) >= min_word_len) { existing_entry = bag_contains(index, &new_word); if(existing_entry != NULL) // if the word is already in index { entry_add(existing_entry, page); // add the location to the list of locations for that word } else // if the word isn't in the index { new_entry = entry_create(word, page); // create the entry bag_insert(index, new_entry); // add the location } } } } return index; }
//adds a new entry to the list with the values user inputs entry* set(char *ptr, char key[MAX_KEY], entry *entryHead) { //list is empty, initialize a new list if (entryHead == NULL) { entryHead = (entry *) entrylist_init(key); } //find entry with the specified key entry *chosenEntry = get_entry(entryHead, key); //entry with such a key does not exist. add new entry to the list if (chosenEntry == NULL) { chosenEntry = entry_add(entryHead, key); entryHead = chosenEntry; } //if the entry already has values, free the memory that is allocated for those values if (chosenEntry->values != NULL) { free(chosenEntry->values); } size_t length = 0; chosenEntry->values = read_values(ptr, &length); chosenEntry->length = length; printf("ok\n"); return entryHead; //updated head of the list }
static int object_entry_handler(const char *name, const struct json_value *val, void *arg) { struct odict *o = arg; return entry_add(o, name, val); }
int main(int argc, char **argv) { entry *l = NULL, *e; entry e1, e2, e3; int i = 0, v[3]; e1.i = 1; e2.i = 2; e3.i = 3; entry_add(&l, &e1); assert(l == &e1); assert(l->next == l); assert(l->prev == l); entry_add(&l, &e2); assert(l == &e1); assert(l->next == &e2); assert(l->prev == &e2); entry_add(&l, &e3); assert(l == &e1); assert(l->next == &e2); assert(l->prev == &e3); entry_rem(&l, l); assert(l == &e2); assert(l->next == &e3); assert(l->prev == &e3); assert(entry_next(&l, l) == &e3); assert(entry_prev(&l, l) == NULL); assert(entry_next(&l, &e3) == NULL); assert(entry_prev(&l, &e3) == &e2); for (e = l; e != NULL; e = entry_next(&l, e)) v[i++] = e->i; assert(v[0] == 2); assert(v[1] == 3); entry_rem(&l, &e3); assert(l == &e2); entry_rem(&l, l); assert(l == NULL); }
static int array_entry_handler(unsigned idx, const struct json_value *val, void *arg) { struct odict *o = arg; char index[64]; if (re_snprintf(index, sizeof(index), "%u", idx) < 0) return ENOMEM; return entry_add(o, index, val); }
static void process_dents(lx_s *body, struct dirent *dentp, int dbytes) { while (dbytes) { if (dentp->d_reclen > dbytes) { die_html(HTERR_SERVERR, 0, "dir record is larger than available entry size (fs error?)", err_dir_index); } if (dentp->d_fileno) entry_add(body, dentp); dbytes -= dentp->d_reclen; dentp = (struct dirent *)((char *)dentp + dentp->d_reclen); } }
//returns a reversed copy of all the entries that the given entrylist stores entry* get_reversed_entrylist_copy(entry *source) { if (source == NULL) { return NULL; } //initialize a new entrylist and copy the first entry (which is head of the list) entry *destination = entrylist_init(source->key); destination->values = get_values_copy(source->values, source->length); destination->length = source->length; //copy the rest of the entries from source entry list to destination entrylist entry *currentSourceEntry = source->next; while (currentSourceEntry != NULL) { destination = entry_add(destination, currentSourceEntry->key); destination->values = get_values_copy(currentSourceEntry->values, currentSourceEntry->length); destination->length = currentSourceEntry->length; currentSourceEntry = currentSourceEntry->next; } return destination; }
int define(buffer_t *buffer, entry_t **list, char *args) { polynomial_t *p = NULL; complex_t *z = NULL; char **table = NULL, *output = NULL, *input = NULL, *name = NULL; char run = 1; size_t size = 0, i = 0; if (buffer == NULL) return EXIT_FAILURE; table = split(args, ' ', &size); if (table == NULL) return EXIT_FAILURE; if (size == 1) { buffer_read(buffer, 0, "Entrez le nom de votre polynome : "); name = buffer_get(buffer); free(*table); free(table); } else { name = (char*) malloc(sizeof(table[1]) * sizeof(char)); strcpy(name, table[1]); for (i = 0; i < size; i++) free(table[i]); free(table); } p = polynomial_init(name); if (p == NULL) return EXIT_FAILURE; while (run) { if (p->size == 0) fprintf(stdout, "%s(X) = ", p->name); else { polynomial_display(p); fprintf(stdout, " + "); } buffer_read(buffer, 0, NULL); input = buffer_get(buffer); z = complex_fromString(input); if (z != NULL) { output = complex_toString(z, 0); if (p->size == 0) { if (z->re != 0.0 && z->im != 0.0) fprintf(stdout, "%s(X) = (%s) X ^ ", p->name, output); else fprintf(stdout, "%s(X) = %s X ^ ", p->name, output); } else { polynomial_display(p); if (z->re != 0.0 && z->im != 0.0) fprintf(stdout, " + (%s) X ^ ", output); else fprintf(stdout, " + (%s) X ^ ", output); } free(output); buffer_read(buffer, 0, NULL); input = buffer_get(buffer); polynomial_append(p, z, strtoul(input, NULL, 0)); free(input); buffer_read(buffer, 0, "Voulez-vous continuer (oui/non) ? "); input = buffer_get(buffer); if (!strcmp(input, "non") || !strcmp(input, "n") || !strcmp(input, "N")) run = 0; } } entry_add(list, (void*) p, POLYNOMIAL); return EXIT_SUCCESS; }
int integrate(entry_t **list, char *args) { complex_t *z = NULL; entry_t *e = NULL; polynomial_t *q = NULL; size_t size = 0, i = 0; char **table = NULL, *c = NULL, count = 0; if (list == NULL || args == NULL) return EXIT_FAILURE; table = split(args, ' ', &size); if (table == NULL || size == 1) { if (table != NULL) { free(*table); free(table); } return EXIT_FAILURE; } e = entry_get(*list, table[1]); if (e == NULL || e->type != POLYNOMIAL) { for (i = 0; i < size; i++) free(table[i]); free(table); return EXIT_FAILURE; } if (size > 2) { c = args; while (count < 2) { if (*c == ' ') count++; c++; } z = complex_fromString(c); if (z == NULL) { for (i = 0; i < size; i++) free(table[i]); free(table); return EXIT_FAILURE; } q = polynomial_integrate((polynomial_t*) e->polynomial, z); } else q = polynomial_integrate((polynomial_t*) e->polynomial, NULL); if (q == NULL) { if (z != NULL) complex_free(z); for (i = 0; i < size; i++) free(table[i]); free(table); return EXIT_FAILURE; } polynomial_display(q); fprintf(stdout, "\n"); entry_add(list, (void*) q, POLYNOMIAL); for (i = 0; i < size; i++) free(table[i]); free(table); return EXIT_SUCCESS; }