HashTable *readFile(char *filename) { HashTable *index = initializeHashTable(); FILE *file = fopen(filename, "rb"); char *word = malloc(31 * sizeof(char)); //longest english word is 30 characters, so allocate 31 chars int docs; //read in each word and number of docs while(fscanf(file, "%s %d", word, &docs) > 0 && !feof(file)){ //for each document add the word with the id for appropriate frequency for(int i = 0; i < docs; i++){ int doc_id, freq; if (fscanf(file, "%d %d", &doc_id, &freq) > 0) { for(int j = 0; j < freq; j++) { addToHashTable(index, word, doc_id); } } else { break; } } free(word); word = malloc(31 * sizeof(char)); } free(word); fclose(file); return index; }
void test__insert_into_hash_table_large_num_keys(void **state) { initializeHashTable(10); Oid o = 139753; int result = 0; int i = 0; for(; i < 20; i++) { result = insertIntoHashTable(o + i, 'x'); assert_true(result == 0); } for(i = 0; i < 20; i++) { result = removeNode(o + i); assert_true(result == 0); } for(i = 0; i < 20; i++) { char typstorage = getTypstorage(o + i); assert_true(typstorage == EMPTY_TYPSTORAGE); } cleanUpTable(); }
void test__remove_node_not_present(void **state) { initializeHashTable(100); int result = removeNode(101); assert_true(result == -1); cleanUpTable(); }
void test__get_typstorage_invalid_key(void **state) { initializeHashTable(100); char typstorage = getTypstorage(100); assert_true(typstorage == EMPTY_TYPSTORAGE); cleanUpTable(); }
int main() { int choice; char ans = 'y'; char fname[SIZE]; struct linknode *hashTable[TABLESIZE]; initializeHashTable(hashTable); while(ans == 'y') { printf("\n1. Create file\n"); printf("2. Delete File\n"); printf("3. Search File\n"); printf("4. Display Table\n"); printf("5. Exit\n"); printf("6. Write to disk\n"); printf("7. Read from disk\n"); printf("\nEnter choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter file name: "); scanf("%s", fname); insertFile(fname, hashTable); break; case 2: printf("Enter file name: "); scanf("%s", fname); deleteFile(fname, hashTable); break; case 3: printf("Enter file name: "); scanf("%s", fname); searchFile(fname, hashTable); break; case 4: displayTable(hashTable); break; case 5: exit(0); case 6: writeToDisk(hashTable); break; case 7: readFromDisk(hashTable); break; } } return (0); }
void test__initialize_hash_table(void **state) { int a = 90; int result = initializeHashTable(a); assert_true(result == 0); assert_true(hash_table != NULL); assert_true(HASH_TABLE_SIZE == 90); cleanUpTable(); }
void test__get_typstorage(void **state) { initializeHashTable(100); Oid o = 139753; int t = 'x'; int result = insertIntoHashTable(o, t); assert_true(result == 0); char typstorage = getTypstorage(o); assert_true(typstorage == t); cleanUpTable(); }
void test__insert_into_hash_table_duplicate(void **state) { initializeHashTable(100); Oid o = 139753; int t = 'x'; int result = insertIntoHashTable(o, t); assert_true(result == 0); result = insertIntoHashTable(o, t); assert_true(result == -1); cleanUpTable(); }
void test__insert_into_hash_table(void **state) { initializeHashTable(90); Oid o = 139753; int t = 'x'; int result = insertIntoHashTable(o, t); assert_true(result == 0); result = getTypstorage(o); assert_true(result == 'x'); cleanUpTable(); }
void test__remove_node(void **state) { initializeHashTable(100); Oid o = 139753; int t = 'x'; int result = insertIntoHashTable(o, t); assert_true(result == 0); result = removeNode(o); assert_true(result == 0); char typstorage = getTypstorage(o); assert_true(typstorage == EMPTY_TYPSTORAGE); cleanUpTable(); }
void test__clean_up_table(void **state) { initializeHashTable(100); Oid o = 123456; int i = 0; int result = 0; for(; i < 20; i++) { result = insertIntoHashTable(o + i, 'x'); assert_true(result == 0); } cleanUpTable(); assert_true(hash_table == NULL); assert_true(HASH_TABLE_SIZE == 0); }
void test__remove_node_not_present_in_list(void **state) { initializeHashTable(10); Oid o = 139753; int i = 0; int result = 0; for(; i < 20; i++) { result = insertIntoHashTable(o + i, 'x'); assert_true(result == 0); } result = removeNode(o + 21); assert_true(result == -1); cleanUpTable(); }
int main(int argc, char *argv[]){ /* argument checking */ if (argc != 3){ printf("The query engine required 2 arguments.\n"); exit(EXIT_FAILURE); } if ( !IsFile(argv[1]) ){ printf("%s is not a valid file.", argv[1]); exit(EXIT_FAILURE); } if ( !IsDir(argv[2]) ){ printf("%s is not a valid directory", argv[2]); exit(EXIT_FAILURE); } /* HashTable to store data from the file provided */ HashTable *ht = malloc(sizeof(HashTable)); initializeHashTable(ht); printf("Reading in the file...\n"); ReadFile(argv[1], ht); signal(SIGINT, intHandler); while(running){ DocumentNode *final = NULL; char input[MAX_INPUT + 1]; // character array to hold user input printf("Enter your words: "); /* make sure everything went well on the user's end */ int ret = GetUserInput(input); if( ret == 0){ continue; } else if(ret == 2){ break; } /* handle the input */ final = CreateUnsortedDocList(input, ht); SortDocList(&final); ReportInfo(final, argv[2]); FreeDocList(final); }
int main(void) { Error error; HashTable *table; Data *data = NULL; error.error = 0; strcpy((char *)(error.str),"\0"); table = initializeHashTable(2,&error); setDataInTableByString(table,"abc",10,&error); puts("========="); setDataInTableByString(table,"aaaaaaaaaaaaaa",5,&error); puts("========="); setDataInTableByString(table,"abd",1,&error); puts("========="); setDataInTableByString(table,"abc",7,&error); puts("========="); printHashTable(table);puts("===="); puts("========="); data = getDataFromTableByString(table,"abc"); puts("========="); puts("===="); if (NULL != data) { printf("Data(%p):Key='%s',val=(%"PRIu64")\n",data,data->key,data->value); } else { printf("Not fuond!\n"); } deleteDataFromTableByString(table,"abc"); puts("========="); setDataInTableByString(table,"abc",7,&error); puts("========="); setDataInTableByString(table,"olololololololololololol",1000000,&error); puts("========="); setDataInTableByString(table,"AhTiZEbanyTiNahui",70000000000,&error); puts("========="); data = getDataFromTableByString(table,"AhTiZEbanyTiNahui"); puts("========="); puts("===="); if (NULL != data) { printf("Data(%p):Key='%s',val=(%"PRIu64")\n",data,data->key,data->value); } else { printf("Not fuond!\n"); } return 0; }
/* ========================================================================== */ int main(int argc, char* argv[]) { int filenum=1; int initdepth=1; // check command line arguments if (argcheck(argc,argv) == 1){ exit(1); } char *starturl = argv[1]; char *targetdir = argv[2]; int depth = atoi(argv[3]); //initialize our hashtables and url list HashTable *myhashtable; List *mylist; myhashtable=initializeHashTable(); mylist=initializelist(); // init curl curl_global_init(CURL_GLOBAL_ALL); // setup seed page WebPage *startpage = createWebPage(starturl,0); // get seed webpage. If the url is invalid, quit and send an error message. if ( GetWebPage(startpage) == 0 ){ printf("The url that you entered was invalid. Please try again."); free(startpage->html); free(startpage); exit(1); } // write seed file createfile(startpage,targetdir,filenum); filenum++; // add seed page to hashtable insertHashTable(myhashtable,startpage->url); // extract urls from seed page if( depth > 0 ){ parseHTML(startpage,mylist,myhashtable,initdepth); } // while there are urls to crawl while ( mylist->head != NULL ){ // get next url from list WebPage *nextpage = listpop(mylist); int currdepth = nextpage->depth; // get webpage for url // If the url is invalid, quit and free the memory if (GetWebPage(nextpage) != 0 ){ createfile(nextpage,targetdir,filenum); filenum++; // extract urls from webpage if ( currdepth < depth ){ parseHTML(nextpage, mylist, myhashtable, currdepth+1); } } free(nextpage->html); free(nextpage->url); free(nextpage); sleep(SLEEPTIME); } // cleanup curl free(startpage->html); free(startpage); freeHashTable(myhashtable); freelist(mylist); curl_global_cleanup(); return 0; }
int main(int argc, char *argv[]) { //Check validity of parameters if (argc != 3 && argc != 4) { fprintf(stderr, "Usage Error; This program has two methods of use as follows:\n"); fprintf(stderr, "./indexer [DIRECTORY] [FILENAME]\n"); fprintf(stderr, "./indexer [DIRECTORY] [FILENAME 1] [FILENAME 2]\n"); return -1; } //Check that the directory exists and is readable DIR *directory = opendir(argv[1]); if(directory == NULL) { if(errno == EACCES) fprintf(stderr, "Error: Directory has incompatible permissions for this program\n"); else if(errno == ENOENT) fprintf(stderr, "Error: Directory does not exist\n"); else if (errno == ENOTDIR) fprintf(stderr, "Error: Given directory is not a directory\n"); else fprintf(stderr, "Error: Incompatible directory.\n"); closedir(directory); return -1; } closedir(directory); logfile = fopen("IndexerLogFile", "w"); HashTable *inverted_index = initializeHashTable(); fprintf(logfile, "Building the Index\n"); char *dir_name = malloc(strlen(argv[1]) + 1); strcpy(dir_name, argv[1]); if (buildIndexFromDirectory(dir_name, inverted_index) != 1) { fprintf(logfile, "Error: Unable to build index\n"); fprintf(stderr, "Error: Unable to build index\n"); free(dir_name); fclose(logfile); return -1; } fprintf(logfile, "Index Built!\n"); if (saveFile(argv[2], inverted_index) != 1){ fprintf(stderr, "Unable to save the index into %s\n", argv[2]); cleanupHashTable(inverted_index); free(dir_name); fclose(logfile); return -1; } fprintf(logfile, "Index written to %s\n", argv[2]); cleanupHashTable(inverted_index); free(dir_name); //if in testing mode if (argc == 4) { fprintf(logfile, "Testing the index\n"); HashTable *reindex = readFile(argv[2]); saveFile(argv[3], reindex); fprintf(logfile, "Finished Testing\n"); cleanupHashTable(reindex); } fclose(logfile); return 0; }