void read_input_file( char *filename ) { FILE *input = open_file_read( filename ); int r, t_len, c_len; double count; t_dict = g_hash_table_new_full( &g_int_hash, &g_int_equal, NULL, destroy_word_count ); c_dict = g_hash_table_new_full( &g_int_hash, &g_int_equal, NULL, destroy_word_count ); symbols_dict = g_hash_table_new_full( &g_str_hash, &g_str_equal, free, free ); inv_symbols_dict = g_hash_table_new_full( &g_int_hash, &g_int_equal, NULL, NULL ); while( !feof( input ) ) { char *target_buff = NULL; char *context_buff = NULL; // Read the strings and count from the file r = fscanf( input, "%ms %ms %lf", &target_buff, &context_buff, &count ); // Reads potentially large strings from file // Shrink the strings to minimal memory, reject too large words if( r == 3 ) { t_len = strlen( target_buff ); c_len = strlen( context_buff ); if( t_len <= MAX_S && c_len <= MAX_S ) { //perra( "Target:\"%s\" Context:\"%s\"\n",target,context ); insert_into_index( t_dict, symbols_dict, inv_symbols_dict, target_buff, context_buff, t_len, c_len, count, &idc_t ); insert_into_index( c_dict, symbols_dict, inv_symbols_dict, context_buff, target_buff, c_len, t_len, count, &idc_c ); n_pairs += count; } else { perra( "Warning: Ignore (\"%s\",\"%s\")\n", target_buff, context_buff ); perra( "Word length (%d,%d) => max is %d\n", t_len, c_len, MAX_S ); } } free( target_buff ); free( context_buff ); target_buff = NULL; context_buff = NULL; } fclose( input ); }
void removeFromBufferAndIndex(int thread_number){ char * file_name; FILE * file; char buffer[512]; int line_number = 1; // Waiting to remove from buffer //printf("STOP REMOVE!\n"); pthread_mutex_lock(&mutex_one); //printf("IN REMOVE LOCK\n"); while(buffer_amount == 0){ if(scanning_done){ pthread_mutex_unlock(&mutex_one); return; } pthread_cond_wait(&fill_cond, &mutex_one); //printf("STUCK IN REMOVE\n"); } file_name = scan_buffer[buffer_amount - 1]; --buffer_amount; //printf("--BUFFER_AMOUNT -> in removeFromBufferAndIndex\n"); pthread_cond_signal(&empty_cond); pthread_mutex_unlock(&mutex_one); // Has removed from buffer and signal to others who are waiting //printf("Thread: %d File to be indexed: %s\n", thread_number, file_name); if ((file = fopen(file_name, "r")) == NULL) { //printf("Wasn't able to open: %s\n", file_name); return; } while (!feof(file)) { char * word; char * saveptr; fgets(buffer, 512, file); word = strtok_r(buffer, " \n\t-_!@#$%^&*()_+=,./<>?", &saveptr); while (word != NULL) { //printf("Word inserted: %s\n",word); //TESTING PURPOSES ONLY (using old index.c) pthread_mutex_lock(&mutex_test); insert_into_index(word, file_name, line_number); //TESTING PURPOSES ONLY (using old index.c) pthread_mutex_unlock(&mutex_test); word = strtok_r(NULL, " \n\t-_!@#$%^&*()_+=,./<>?",&saveptr); } line_number = line_number+1; } fclose(file); }
int main(int argc, char * argv[]) { index_search_results_t * results; init_index(); insert_into_index("hello", "test.c", 10); insert_into_index("hello", "test.c", 20); insert_into_index("hello", "foo.c", 30); insert_into_index("goodbye", "test.c", 10); results = find_in_index("hello"); if (results) { int i; for (i = 0; i < results->num_results; i++) { printf("%s: %d\n", results->results[i].file_name, results->results[i].line_number); } free(results); } find_in_index("goodbye"); return(0); }