Skip to content

Commit

Permalink
Fixed issue 1303
Browse files Browse the repository at this point in the history
  • Loading branch information
theraysmith committed Oct 7, 2014
1 parent 4904afe commit bfd2cb8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
52 changes: 33 additions & 19 deletions ccutil/tessdatamanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,30 @@ void TessdataManager::CopyFile(FILE *input_file, FILE *output_file,
delete[] chunk;
}

void TessdataManager::WriteMetadata(inT64 *offset_table,
bool TessdataManager::WriteMetadata(inT64 *offset_table,
const char * language_data_path_prefix,
FILE *output_file) {
fseek(output_file, 0, SEEK_SET);
inT32 num_entries = TESSDATA_NUM_ENTRIES;
fwrite(&num_entries, sizeof(inT32), 1, output_file);
fwrite(offset_table, sizeof(inT64), TESSDATA_NUM_ENTRIES, output_file);
fclose(output_file);

tprintf("TessdataManager combined tesseract data files.\n");
for (int i = 0; i < TESSDATA_NUM_ENTRIES; ++i) {
tprintf("Offset for type %2d (%s%-22s) is %lld\n", i,
language_data_path_prefix, kTessdataFileSuffixes[i],
offset_table[i]);
bool result = true;
if (fseek(output_file, 0, SEEK_SET) != 0 ||
fwrite(&num_entries, sizeof(inT32), 1, output_file) != 1 ||
fwrite(offset_table, sizeof(inT64), TESSDATA_NUM_ENTRIES,
output_file) != TESSDATA_NUM_ENTRIES) {
fclose(output_file);
result = false;
tprintf("WriteMetadata failed in TessdataManager!\n");
} else if (fclose(output_file)) {
result = false;
tprintf("WriteMetadata failed to close file!\n");
} else {
tprintf("TessdataManager combined tesseract data files.\n");
for (int i = 0; i < TESSDATA_NUM_ENTRIES; ++i) {
tprintf("Offset for type %2d (%s%-22s) is %lld\n", i,
language_data_path_prefix, kTessdataFileSuffixes[i],
offset_table[i]);
}
}
return result;
}

bool TessdataManager::CombineDataFiles(
Expand All @@ -124,8 +133,11 @@ bool TessdataManager::CombineDataFiles(
return false;
}
// Leave some space for recording the offset_table.
fseek(output_file,
sizeof(inT32) + sizeof(inT64) * TESSDATA_NUM_ENTRIES, SEEK_SET);
if (fseek(output_file,
sizeof(inT32) + sizeof(inT64) * TESSDATA_NUM_ENTRIES, SEEK_SET)) {
tprintf("Error seeking %s\n", output_filename);
return false;
}

TessdataType type = TESSDATA_NUM_ENTRIES;
bool text_file = false;
Expand Down Expand Up @@ -161,8 +173,7 @@ bool TessdataManager::CombineDataFiles(
return false;
}

WriteMetadata(offset_table, language_data_path_prefix, output_file);
return true;
return WriteMetadata(offset_table, language_data_path_prefix, output_file);
}

bool TessdataManager::OverwriteComponents(
Expand All @@ -185,8 +196,12 @@ bool TessdataManager::OverwriteComponents(
}

// Leave some space for recording the offset_table.
fseek(output_file,
sizeof(inT32) + sizeof(inT64) * TESSDATA_NUM_ENTRIES, SEEK_SET);
if (fseek(output_file,
sizeof(inT32) + sizeof(inT64) * TESSDATA_NUM_ENTRIES, SEEK_SET)) {
fclose(output_file);
tprintf("Error seeking %s\n", new_traineddata_filename);
return false;
}

// Open the files with the new components.
for (i = 0; i < num_new_components; ++i) {
Expand All @@ -212,8 +227,7 @@ bool TessdataManager::OverwriteComponents(
}
}
const char *language_data_path_prefix = strchr(new_traineddata_filename, '.');
WriteMetadata(offset_table, language_data_path_prefix, output_file);
return true;
return WriteMetadata(offset_table, language_data_path_prefix, output_file);
}

bool TessdataManager::TessdataTypeFromFileSuffix(
Expand Down
6 changes: 4 additions & 2 deletions ccutil/tessdatamanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ class TessdataManager {
return swap_;
}

/** Writes the number of entries and the given offset table to output_file. */
static void WriteMetadata(inT64 *offset_table,
/** Writes the number of entries and the given offset table to output_file.
* Returns false on error.
*/
static bool WriteMetadata(inT64 *offset_table,
const char *language_data_path_prefix,
FILE *output_file);

Expand Down

0 comments on commit bfd2cb8

Please sign in to comment.