Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool load(const char* dictionary)
- {
- // TODO
- // do some prep work
- struct stat buf;
- if (stat(dictionary,&buf))
- return false;
- // map file into memory
- int fd = open(dictionary, O_RDONLY);
- if (fd < 0)
- return false;
- char *map = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (map == MAP_FAILED)
- {
- close(fd);
- return false;
- }
- // start = &map[0];
- // this is a pointer to point at the first character in the string
- char* start = map;
- // end = start;
- // this is a pointer to mark the end of the string \n
- char* end = start;
- // loop to advance "end" until it points to a newline
- for (char* i = end; *i < sizeof(buf.st_size); i++)
- {
- // check if i is \n
- if ( *i == '\n')
- {
- //start is now a pointer to a null terminated string!
- // place that pointer into the node
- node* new_node = malloc(sizeof(node));
- new_node->next = NULL;
- new_node->word = start;
- //place the node into the hashtable
- int x = hash_d(new_node->word);
- new_node->next = hashtable[x];
- hashtable[x] = new_node;
- // set start to point one character after end, so it points to the start of the next string
- start = &end[*i + 1];
- }
- // repeat until you have processed all the strings
- }
- // do some cleanup work
- close(fd);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement