int readDictionary(Dictionary *dict) { int row, col; //---------------CHANGE DICTIONARY HERE!------// FILE *ifp = fopen("dictionary.txt", "r"); //---------------CHANGE DICTIONARY HERE!------// //use temp_array to figure out strlen() char array[MAX_WORD_LENGTH + 1]; fscanf(ifp, "%d", &dict->size); //malloc() first dimension of words[][] dict->words = malloc(sizeof(char *) * (dict->size)); //return 0 if pointer remains NULL after calling malloc() if (dict->words == NULL) { return 0; } //malloc() second dimension of words[][] for (row = 0; row < dict->size; row++) { //set array equal to current line of dictionary.txt fscanf(ifp, "%s", array); //malloc() just enough space on current row of dict->words[][] dict->words[row] = malloc(sizeof(char) * (strlen(array) + 1)); //return 0 if pointer remains NULL after calling malloc() if (dict->words[row] == NULL) { destroyDictionary(dict); return 0; } //read in words from array into dict->words strcpy(dict->words[row], array); } //malloc() counts[] array dict->counts = malloc(sizeof(int) * dict->size); //clear memory and return 0 if malloc() fails if (dict->counts == NULL) { destroyDictionary(dict); return 0; } //print dictionary if (DEBUG) { printf("___________DICTIONARY____________\n"); for (row = 0; row < dict->size; row++) { printf("%s\n", dict->words[row]); } printf("_________________________________\n\n"); } fclose(ifp); return 1; }
void test_LZ78_Compression_Variable_mode_given_input_file_sampleText_forCompresssion_2() { InStream *in = initInStream(); OutStream *out = initOutStream(); Dictionary *dictionary = initDictionary(100); in = openInStream("test/support/Source/sampleText_forCompresssion_2.txt", "rb" , in); out = openOutStream("test/support/Compressed/Variable/sampleText_forCompresssion_Compressed_Variable_2.txt", "wb" , out); LZ78_Compressor(dictionary,in,out,Variable); TEST_ASSERT_EQUAL_STRING("A",dictionary->Entry[0].data); TEST_ASSERT_EQUAL_STRING("AA",dictionary->Entry[1].data); TEST_ASSERT_EQUAL_STRING("AAA",dictionary->Entry[2].data); TEST_ASSERT_EQUAL_STRING("AAA ",dictionary->Entry[3].data); TEST_ASSERT_EQUAL_STRING("B",dictionary->Entry[4].data); TEST_ASSERT_EQUAL_STRING("BB",dictionary->Entry[5].data); TEST_ASSERT_EQUAL_STRING("BBB",dictionary->Entry[6].data); TEST_ASSERT_EQUAL_STRING("BBBB",dictionary->Entry[7].data); TEST_ASSERT_EQUAL_STRING("C",dictionary->Entry[8].data); TEST_ASSERT_EQUAL_STRING("",dictionary->Entry[9].data); closeInStream(in); closeOutStream(out); freeInStream(in); freeOutStream(out); destroyDictionary(dictionary,100); }
void resetGlobalRegionDictionary() { destroyDictionary(); if ( _registeredObject != NULL ) { _object = NEW GlobalRegionDictionary( *_registeredObject ); _object->setRegisteredObject( _registeredObject ); NewNewDirectoryEntryData *entry = getDirectoryEntry( *_object, 1 ); if ( entry == NULL ) { entry = NEW NewNewDirectoryEntryData(); _object->setRegionData( 1, entry ); //resetGlobalRegionDictionary } } }
void test_LZ78_Compression_Variable_mode_given_input_file_libjansson_4_dll_() { InStream *in = initInStream(); OutStream *out = initOutStream(); Dictionary *dictionary = initDictionary(4096); in = openInStream("test/support/Source/libjansson-4.dll", "rb" , in); out = openOutStream("test/support/Compressed/Variable/libjansson-4_Compressed_Variable.dll", "wb" , out); LZ78_Compressor(dictionary,in,out,Variable); closeInStream(in); closeOutStream(out); freeInStream(in); freeOutStream(out); destroyDictionary(dictionary,4096); }
int main (void) { int row, col, i, max; char *str = malloc(sizeof(char) * (MAX_WORD_LENGTH + 1)); char temp[2]; Dictionary *dict = malloc(sizeof(Dictionary)); WordSearchPuzzle *puzzle = malloc(sizeof(WordSearchPuzzle)); readDictionary(dict); readPuzzle(puzzle); //set bigger dimension of puzzle, use for determining //length of diagonal for() loops if (puzzle->height > puzzle->width) { max = puzzle->height; } else { max = puzzle->width; } temp[1] = '\0'; for (row = 0; row < puzzle->height; row++) { for (col = 0; col < puzzle->width; col++) { //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //single char strings *str = puzzle->grid[row][col]; checkString(dict, str); //build strings horizontally-right for (i = 1; i < puzzle->width; i++) { //make sure not to go too far! if (col + i < puzzle->width) { temp[0] = puzzle->grid[row][col + i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build strings horizontally-left for (i = 1; i < puzzle->width; i++) { //make sure not to go too far! if (col - i > 0) { temp[0] = puzzle->grid[row][col - i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build strings vertically-down for (i = 1; i < puzzle->height; i++) { if (row + i < puzzle->height) { temp[0] = puzzle->grid[row + i][col]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build strings vertically-up for (i = 1; i < puzzle->height; i++) { //why is this >=? top row of puzzle (height/width ints) hindering correct reading? if (row - i >= 0) { temp[0] = puzzle->grid[row - i][col]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally up-left for (i = 1; i <= max; i++) { if (row - i >= 0 && col - i >= 0) { temp[0] = puzzle->grid[row - i][col - i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally up-right for (i = 1; i < max; i++) { if (row - i >= 0 && col + i < puzzle->width) { temp[0] = puzzle->grid[row - i][col + i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally down-left for (i = 1; i < max; i++) { if (row + i < puzzle->height && col - i >= 0) { temp[0] = puzzle->grid[row + i][col - i]; str = strcat(str, temp); checkString(dict, str); } } //clear array, except for char at index 0 for (i = 1; i < MAX_WORD_LENGTH + 1; i++) { str[i] = '\0'; } //build string diagonally down-right for (i = 1; i < max; i++) { if (row + i < puzzle->height && col + i < puzzle->width) { temp[0] = puzzle->grid[row + i][col + i]; str = strcat(str, temp); checkString(dict, str); } } }//end [col] for loop }//end [row] for loop for (i = 0; i < dict->size; i++) { if (dict->counts[i] > 0) { printf("%s (%d)\n", dict->words[i], dict->counts[i]); } } destroyDictionary(dict); destroyPuzzle(puzzle); free(str); str = NULL; return 0; }
~Object() { destroyDictionary(); delete _registeredObject; }