/* * BOOLEAN *find_synonym(char *word, FILE *p_dictionary, long *letters, char *buff) * * Finds Identical word in the dictionary file. * Returns a true if found a synonym(that will be saved to buff) or false * if failed. * * params: char *word - the word that needs to be replaced. * FILE *p_dictionary - pointer to the dictionary text file. * long *letters - Array that the function set_letters_pos returned. * char *buff - the string that the synonym will be saved to. * */ BOOLEAN find_synonym(char *word, FILE *p_dictionary, long *letters, char *buff) { long char_pos; char first_letter = char_to_upper(*word); /* set file pointer position to that letter if it exists */ if((char_pos = *(letters + first_letter - CAP_A)) == -1) { if(PRINT_ERROR) { fprintf(stderr, "%c wasn't found\n", first_letter); PRINT_FILE_LINE(); } return false; } fseek(p_dictionary, char_pos, SEEK_SET); /* set file pointer to next line and start comparing */ next_line(p_dictionary); if(cmp_words(word, p_dictionary) == false) { return false; } return get_synonym(p_dictionary, buff); }
/* * BOOLEAN get_synonym(FILE *p_dictionary, char *buff) * * Given a file pointer that points to the first character * of the synonym region * * */ static BOOLEAN get_synonym(FILE *p_dictionary, char *buff) { BOOLEAN flag; char s_num_of_synonyms[3]; /* get number of synonyms this word has */ flag = get_next_word(p_dictionary, s_num_of_synonyms); if(flag == false) { if (PRINT_ERROR) { fprintf(stderr, "get_next_word returned false\n"); PRINT_FILE_LINE(); } return false; } int num_of_synonyms = 0; /* convert string to int */ int i; for(i = 0; *(s_num_of_synonyms + i) != NTS; i++) { num_of_synonyms *= 10; num_of_synonyms += (*(s_num_of_synonyms + i) - '0'); } /* generate random number to pick a synonym */ srand(time(NULL)); i = rand() % num_of_synonyms; for(; i > 0; i--) { flag = get_next_word(p_dictionary, buff); if(flag == false) { if (PRINT_ERROR) { fprintf(stderr, "get_next_word returned false\n"); PRINT_FILE_LINE(); } return false; } } flag = get_next_word(p_dictionary, buff); return flag; }
ssize_t ssl_socket_size(struct ssl_socket_t *self_p) { PRINT_FILE_LINE(); BTASSERT(self_p != NULL); ssl_size_counter++; return (0); }
/* * BOOLEAN get_next_word(FILE *p_dictionary, char *buff) * * given a file pointer with SEEK_CUR in the beginning * of a line, returns true and assigns the word to buff * if succeeded in finding the next word or false if get_word_length() failed. * */ static BOOLEAN get_next_word(FILE *p_dictionary, char *buff) { /* Note that an external buffer is necessary as returning * a pointer created in a local scope is very unsafe because it will * be released as soon as the function is exited. * Furthermore dynamically allocating an array within the function * will force the caller function to free it even though it * didn't create it. */ int i; /* might be better to use realloc instead of finding length */ int length = get_word_length(p_dictionary); /* check if the function failed to find anything */ if(length == 0) { return false; } for(i = 0; i < length; ++i) { if(i == WORD_MAX_LENGTH) { if (PRINT_ERROR) { fprintf(stderr, "word is too long\n"); PRINT_FILE_LINE(); } return false; } *(buff+i) = getc(p_dictionary); } *(buff + i) = NTS; getc(p_dictionary); return true; }