int main(int args, char *argv[]) { FILE *input = fopen(argv[1],"r"); int num; char command; while(fscanf(input,"%c %d\n",&command,&num) != EOF) { if(command == 'a') dl_insert(front,num); else if(command == 'b') dl_del(front,num); else if(command == 'f') { valueT val = dl_get_front(front); printf("front value:%d\n",val); } else if(command == 'g') { valueT val = dl_get_back(front); printf("Back value:%d\n",val); } else if(command == 'd') dl_pop_front(front); else if(command == 'e') dl_pop_back(front); else if(command == 'h') { valueT val = dl_check(front, num); printf("val exists:%d\n",val); } else if(command == 'j') { dl_insert_front(front,num); } else if(command == 'p') { printList(); } } fclose(input); return 0; }
bool print_words_to_file_and_screen(list_t *list, int number, char *output){ check(list != NULL, "list was NULL."); check(output != NULL, "output was NULL."); int total_node_count = dl_count(list); int i = 0; char *temp = NULL; int word_count = 0; FILE *output_file = NULL; if(total_node_count != 0){ check(dl_sort(list) == 1, "sort did not complete properly."); } output_file = fopen(output, "w"); check(output_file != NULL, "Could not create file for writing."); for(i = 0; i < total_node_count; i++){ temp = dl_get_back(list); word_count = dl_get_wordcount(list, temp); check(temp != NULL && word_count != 0, "Invalid word or word count. " "temp: '%s', word_count: %d", temp == NULL ? "NULL" : temp, word_count); if(i < number){ printf("%d\t%s\n", word_count, temp); } check(fprintf(output_file, "%d\t%s\n", word_count, temp) > 0, "An error occurred while writing to file."); check(dl_pop_back(list) == 1, "Did not remove back properly."); } fclose(output_file); return 1; error: if(output_file != NULL){ fclose(output_file); } return 0; }
void *execute_read(void *ptr){ readargs *args = (readargs*)ptr; list_t *temp_list = NULL; temp_list = dl_init(); read_file_word_by_word(args->fp, temp_list, args->actual_offset, args->child_read_size, args->size); int node_count = dl_count(temp_list); int wordcount = 0; char *temp_word = NULL; pthread_mutex_lock(&mutex); int i = 0; for(i = 0; i < node_count; i++){ temp_word = dl_get_back(temp_list); wordcount = dl_get_wordcount(temp_list, temp_word); dl_insert_merge(args->list, temp_word, wordcount); dl_pop_back(temp_list); } pthread_mutex_unlock(&mutex); dl_clear_destroy(temp_list); return (NULL); }
bool get_words_from_files(list_t *wordlist, char *input) { list_t *filelist = NULL; filelist = dl_init(); check(get_all_files(input, filelist) == 1, "Could not compile full list."); int file_count = dl_count(filelist); int i = 0; char *filename = NULL; for(i = 0; i < file_count; i++) { filename = dl_get_back(filelist); check(filename != NULL, "filename was NULL."); read_file_word_by_word(filename, wordlist); check(dl_pop_back(filelist) == 1, "Did not pop back properly."); } dl_clear_destroy(filelist); return 1; error: if(filelist != NULL) { dl_clear_destroy(filelist); } return 0; }