Skip to content

Commit f4e982e

Browse files
committed
combine_tessdata: Handle failures when extracting
Report an error and terminate if that fails. Use also EXIT_SUCCESS and EXIT_FAILURE for the return values of main() and add missing return at end of main(). Signed-off-by: Stefan Weil <[email protected]>
1 parent 7434590 commit f4e982e

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/training/combine_tessdata.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main(int argc, char **argv) {
7272
tesseract::TessdataManager tm;
7373
if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
7474
printf("%s\n", tesseract::TessBaseAPI::Version());
75-
return 0;
75+
return EXIT_SUCCESS;
7676
} else if (argc == 2) {
7777
printf("Combining tessdata files\n");
7878
STRING lang = argv[1];
@@ -92,16 +92,22 @@ int main(int argc, char **argv) {
9292
// Initialize TessdataManager with the data in the given traineddata file.
9393
if (!tm.Init(argv[2])) {
9494
tprintf("Failed to read %s\n", argv[2]);
95-
exit(1);
95+
return EXIT_FAILURE;
9696
}
9797
printf("Extracting tessdata components from %s\n", argv[2]);
9898
if (strcmp(argv[1], "-e") == 0) {
9999
for (i = 3; i < argc; ++i) {
100+
errno = 0;
100101
if (tm.ExtractToFile(argv[i])) {
101102
printf("Wrote %s\n", argv[i]);
102-
} else {
103+
} else if (errno == 0) {
103104
printf("Not extracting %s, since this component"
104105
" is not present\n", argv[i]);
106+
return EXIT_FAILURE;
107+
} else {
108+
printf("Error, could not extract %s: %s\n",
109+
argv[i], strerror(errno));
110+
return EXIT_FAILURE;
105111
}
106112
}
107113
} else { // extract all the components
@@ -111,8 +117,13 @@ int main(int argc, char **argv) {
111117
if (*last != '.')
112118
filename += '.';
113119
filename += tesseract::kTessdataFileSuffixes[i];
120+
errno = 0;
114121
if (tm.ExtractToFile(filename.string())) {
115122
printf("Wrote %s\n", filename.string());
123+
} else if (errno != 0) {
124+
printf("Error, could not extract %s: %s\n",
125+
filename.string(), strerror(errno));
126+
return EXIT_FAILURE;
116127
}
117128
}
118129
}
@@ -124,7 +135,7 @@ int main(int argc, char **argv) {
124135
if (rename(new_traineddata_filename, traineddata_filename.string()) != 0) {
125136
tprintf("Failed to create a temporary file %s\n",
126137
traineddata_filename.string());
127-
exit(1);
138+
return EXIT_FAILURE;
128139
}
129140

130141
// Initialize TessdataManager with the data in the given traineddata file.
@@ -135,17 +146,17 @@ int main(int argc, char **argv) {
135146
} else if (argc == 3 && strcmp(argv[1], "-c") == 0) {
136147
if (!tm.Init(argv[2])) {
137148
tprintf("Failed to read %s\n", argv[2]);
138-
exit(1);
149+
return EXIT_FAILURE;
139150
}
140151
tesseract::TFile fp;
141152
if (!tm.GetComponent(tesseract::TESSDATA_LSTM, &fp)) {
142153
tprintf("No LSTM Component found in %s!\n", argv[2]);
143-
exit(1);
154+
return EXIT_FAILURE;
144155
}
145156
tesseract::LSTMRecognizer recognizer;
146157
if (!recognizer.DeSerialize(&tm, &fp)) {
147158
tprintf("Failed to deserialize LSTM in %s!\n", argv[2]);
148-
exit(1);
159+
return EXIT_FAILURE;
149160
}
150161
recognizer.ConvertToInt();
151162
GenericVector<char> lstm_data;
@@ -155,7 +166,7 @@ int main(int argc, char **argv) {
155166
lstm_data.size());
156167
if (!tm.SaveFile(argv[2], nullptr)) {
157168
tprintf("Failed to write modified traineddata:%s!\n", argv[2]);
158-
exit(1);
169+
return EXIT_FAILURE;
159170
}
160171
} else if (argc == 3 && strcmp(argv[1], "-d") == 0) {
161172
// Initialize TessdataManager with the data in the given traineddata file.
@@ -186,4 +197,5 @@ int main(int argc, char **argv) {
186197
return 1;
187198
}
188199
tm.Directory();
200+
return EXIT_SUCCESS;
189201
}

0 commit comments

Comments
 (0)