Created
October 25, 2025 03:04
-
-
Save NattyNarwhal/ef3f5da7bd0269180da764ebeca8f99e to your computer and use it in GitHub Desktop.
aspell prefix zip (prezip) reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef struct PZFile { | |
| FILE *f; | |
| int indices[26]; | |
| } PZFile; | |
| bool fpzopen(PZFile *f, const char *filename) | |
| { | |
| FILE *fp; | |
| if (!f) { | |
| return false; | |
| } | |
| fp = fopen(filename, "rb"); | |
| if (!fp) { | |
| return false; | |
| } | |
| if (fgetc(fp) != 0x02) { | |
| fclose(fp); | |
| return false; | |
| } | |
| f->f = fp; | |
| memset(f->indices, 0, sizeof(int) * 26); | |
| return true; | |
| } | |
| void fpzclose(PZFile *f) | |
| { | |
| fclose(f->f); | |
| } | |
| typedef bool (*PZCallback)(const char*, const char*); | |
| bool fpzread(PZFile *f, PZCallback cb, const char *data) | |
| { | |
| char word[32], *word_ptr; | |
| int c; | |
| word_ptr = word; | |
| if (data && *data >= 'a' && *data <= 'z' && f->indices[*data - 'a']) { | |
| fseek(f->f, f->indices[*data - 'a'], SEEK_SET); | |
| } else { | |
| fseek(f->f, 1, SEEK_SET); | |
| } | |
| while ((c = fgetc(f->f)) != EOF) { | |
| if (c < 0x1F) { /* string starts at index n of old string */ | |
| *word_ptr = '\0'; | |
| word_ptr = word + c; | |
| /* cache */ | |
| if (c == 0) { | |
| long pos; | |
| pos = ftell(f->f); | |
| c = fgetc(f->f); | |
| if (f->indices[c - 'a']) { | |
| /* we got past our ngram */ | |
| break; | |
| } | |
| if (c >= 'a' && c <= 'z') { | |
| f->indices[c - 'a'] = pos; | |
| } | |
| ungetc(c, f->f); | |
| } | |
| /* new prefix, submit current word */ | |
| if (*word) { | |
| if (cb(word, data)) { | |
| return true; | |
| } | |
| } | |
| } else if (c == 0x1F && fgetc(f->f) == 0xFF) { | |
| break; | |
| } else { | |
| *word_ptr++ = c; | |
| } | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment