| 143 | } |
| 144 | |
| 145 | MarisaDictPtr MarisaDict::NewFromFile(FILE* fp) { |
| 146 | // Verify file header |
| 147 | size_t headerLen = strlen(OCD2_HEADER); |
| 148 | void* buffer = malloc(sizeof(char) * headerLen); |
| 149 | size_t bytesRead = fread(buffer, sizeof(char), headerLen, fp); |
| 150 | if (bytesRead != headerLen || memcmp(buffer, OCD2_HEADER, headerLen) != 0) { |
| 151 | throw InvalidFormat("Invalid OpenCC dictionary header"); |
| 152 | } |
| 153 | free(buffer); |
| 154 | long trieOffset = ftell(fp); |
| 155 | fseek(fp, 0L, SEEK_END); |
| 156 | long fileEnd = ftell(fp); |
| 157 | fseek(fp, trieOffset, SEEK_SET); |
| 158 | size_t remainingSize = |
| 159 | (fileEnd > trieOffset) ? static_cast<size_t>(fileEnd - trieOffset) : 0; |
| 160 | |
| 161 | MarisaDictPtr dict(new MarisaDict()); |
| 162 | if (remainingSize > 0) { |
| 163 | dict->internal->mappedBuffer.resize(remainingSize); |
| 164 | bytesRead = fread(&dict->internal->mappedBuffer[0], |
| 165 | sizeof(char), remainingSize, fp); |
| 166 | if (bytesRead != remainingSize) { |
| 167 | throw InvalidFormat("Invalid OpenCC Marisa dictionary."); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | dict->LoadFromMappedBuffer(); |
| 172 | return dict; |
| 173 | } |
| 174 | |
| 175 | MarisaDictPtr MarisaDict::NewFromBuffer(const char* data, size_t size) { |
| 176 | // Verify file header |
nothing calls this directly
no test coverage detected