StringArray *load_topwords(const char *lang) { int i = 0; char *path = topwords_file(lang); char *content = read_file(path); int words_count = char_count(content, ' ') + 1; char *pch; StringArray *sarray = create_string_array(words_count); sarray->items = (char**) safe_malloc(sizeof(char*) * words_count); for (pch = strtok(content, " "); pch != NULL; pch = strtok(NULL, " ")) { sarray->items[i++] = pch; } free(path); // 'content' cannot be freed, because of strtok return sarray; }
int iterate_stack(FILE *fpb, char *key, char *targetKey, int k) { char Ch; char **global_arr = create_string_array(k); int global_ptr = 0; POSTINGSPTR PostOffset; PAGENO PgNum, PtrToNxtLfPg, PtrToFinalRtgPg; NUMKEYS NumKeys; KEYLEN KeyLen; NUMBYTES NumBytes; int number_of_predecessors = 0; PAGENO cal[k]; int calCounter = 0; while (!empty() && k > 0) { PAGENO curr = pop(); cal[calCounter++] = curr; if (curr < 1) { printf("ERROR page numbers start from 1 and on\n"); return -1; } if (fpb == NULL) { printf("ERROR fbp is NULL\n"); return -1; } if (ffsize(fpb) <= (curr - 1) * PAGESIZE) { /* illegal page number */ printf("ERROR page numbers start from 1 and not exceed the last one \n"); return -1; } fseek(fpb, (long) (curr - 1) * PAGESIZE, 0); fread(&Ch, sizeof(Ch), 1, fpb); if (feof(fpbtree) != 0) exit(0); fread(&PgNum, sizeof(PgNum), 1, fpb); if (Ch == LeafSymbol) { fread(&PtrToNxtLfPg, sizeof(PtrToNxtLfPg), 1, fpb); } fread(&NumBytes, sizeof(NumBytes), 1, fpb); fread(&NumKeys, sizeof(NumKeys), 1, fpb); if (Ch == NonLeafSymbol) { fread(&PtrToFinalRtgPg, sizeof(PtrToFinalRtgPg), 1, fpb); } char values[NumKeys][MAXWORDSIZE]; int j = 0, hits = 0; for (; j < NumKeys; j++) { if (Ch == NonLeafSymbol) { fread(&PgNum, sizeof(PgNum), 1, fpb); fread(&KeyLen, sizeof(KeyLen), 1, fpb); fread(key, sizeof(char), KeyLen, fpb); (*(key + KeyLen)) = '\0'; int Result = CompareKeys(key, targetKey); if (Result == 1) { push(PgNum); } } if (Ch == LeafSymbol) { fread(&KeyLen, sizeof(KeyLen), 1, fpb); fflush(stdout); fread(key, sizeof(char), KeyLen, fpb); (*(key + KeyLen)) = '\0'; if (CompareKeys(key, targetKey) == 1) { strcpy(values[j], key); hits++; } fread(&PostOffset, sizeof(PostOffset), 1, fpb); } } if (Ch == NonLeafSymbol) { if (!checkProcessedBefore(PtrToFinalRtgPg, cal, calCounter)) { push(PtrToFinalRtgPg); } } int p = hits - 1; for (; p >= 0 && k > 0; p--) { number_of_predecessors++; strcpy(global_arr[global_ptr], values[p]); global_ptr++; --k; } } int g = global_ptr - 1; printf("found %d predecessors:\n", number_of_predecessors); for (; g >= 0; g--) { printf("%s\n", global_arr[g]); } fflush(stdout); return number_of_predecessors; }