#include "parse.h" #include "indextext.h" #include #include #include #pragma alloca static wordlist* currentwords; static totalwords; void indexword(const char* word, int wordlength, int lineNumber); void indexbodytext(const char* bodytext, int lineNumber) { int charindex; int wordlength; wordlength = 0; for (charindex = 0; charindex < strlen(bodytext); charindex++) { if (isalnum(bodytext[charindex]) || bodytext[charindex] == '_' || bodytext[charindex] == '~') { wordlength++; } else { if (wordlength > 2) { indexword(bodytext + (charindex - wordlength), wordlength, lineNumber); } wordlength = 0; } } if (wordlength > 2) { indexword(bodytext + (charindex - wordlength), wordlength, lineNumber); } } int simplify_tag(char* original_text) { char* tag = original_text; char* index; char* end_original = original_text + strlen(original_text); int result_length = 0; while ((isalnum(*tag) || (strspn(tag, "_./")))) { *tag = tolower(*tag); tag++; } index = tag + strspn(tag, " \t\n\r"); if (*index == '=') { *tag++ = *index++; index += strspn(index, " \t\n\r"); if (*index == '"') { while (++index < end_original && *index != '"') { *tag++ = tolower(*index); } } else { while ((index < end_original) && ((isalnum(*index) || strspn(index, "_./")))) { *tag++ = tolower(*index++); } } *tag = '\0'; result_length = index - original_text; } return result_length; } void indexcomment(const char* commenttext) { int tag_index = 0; int score = 0; int comment_length = strlen(commenttext); /* char* tag_result = (char*)malloc(comment_length); */ char* tag_result = (char*)alloca(comment_length); strcpy(tag_result, commenttext + 1 + strspn(commenttext + 1, " \t\n\r")); tag_index = simplify_tag(tag_result) + 2; if (tag_index > 0) { if (tag_index < comment_length) { const char* score_pointer = commenttext + tag_index + 1; score_pointer += strspn(score_pointer, " \t\n\r"); if (score_pointer < commenttext + comment_length && isdigit(*score_pointer)) { sscanf(score_pointer, "%i\n", &score); } } indexword(tag_result, strlen(tag_result), score); } /* free(tag_result); */ } void indexword(const char* word, int wordlength, int lineNumber) { wordlist** listpointer; for (listpointer = ¤twords; *listpointer != NULL; listpointer = &((*listpointer)->next)) { if (wordlength == strlen((*listpointer)->word)) { if (strncasecmp((*listpointer)->word, word, wordlength) == 0) { (*listpointer)->density++; break; } } } if ((*listpointer) == NULL) { int charIndex; *listpointer = (wordlist*)malloc(sizeof(wordlist)); (*listpointer)->word = (char*)malloc(wordlength + 1); for (charIndex = 0; charIndex < wordlength; charIndex++) { (*listpointer)->word[charIndex] = tolower(word[charIndex]); } (*listpointer)->word[charIndex] = '\0'; (*listpointer)->firstline = lineNumber; (*listpointer)->density = 1; (*listpointer)->next = NULL; } totalwords++; } void initializeindex(){ currentwords = NULL; totalwords = 0; } wordlist* orphanindex(){ wordlist* templist; for (templist = currentwords; templist!= NULL; templist = templist->next) { templist->density = (2<<16) * (((float)templist->density) / totalwords); } templist = currentwords; currentwords = NULL; return templist; }