Skip to content

Commit 1e3eaf8

Browse files
committed
Cleaned archive code
Signed-off-by: Stefan Weil <[email protected]>
1 parent cb1b4b9 commit 1e3eaf8

File tree

1 file changed

+15
-117
lines changed

1 file changed

+15
-117
lines changed

src/ccutil/tessdatamanager.cpp

Lines changed: 15 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -75,51 +75,29 @@ void TessdataManager::LoadFileLater(const char *data_file_name) {
7575
#if defined(HAVE_LIBARCHIVE)
7676
bool TessdataManager::LoadArchiveFile(const char *filename) {
7777
bool result = false;
78-
#ifndef NDEBUG
79-
tprintf("TessdataManager::%s(%s)\n", __func__, filename);
80-
#endif
8178
archive *a = archive_read_new();
8279
if (a != nullptr) {
83-
archive_read_support_filter_all(a); // new
80+
archive_read_support_filter_all(a);
8481
archive_read_support_format_all(a);
85-
std::string zipfile(filename);
86-
bool found = false;
87-
if (archive_read_open_filename(a, zipfile.c_str(), 4096) == ARCHIVE_OK) {
88-
found = true;
89-
} else {
90-
archive_read_free(a);
91-
zipfile += ".zip";
92-
a = archive_read_new();
93-
archive_read_support_filter_all(a);
94-
archive_read_support_format_all(a);
95-
if (archive_read_open_filename(a, zipfile.c_str(), 4096) == ARCHIVE_OK) {
96-
found = true;
97-
}
98-
}
99-
if (found) {
100-
archive_entry *ae;
101-
while (archive_read_next_header(a, &ae) == ARCHIVE_OK) {
102-
const char *fileName = archive_entry_pathname(ae);
82+
if (archive_read_open_filename(a, filename, 4096) == ARCHIVE_OK) {
83+
archive_entry *ae;
84+
while (archive_read_next_header(a, &ae) == ARCHIVE_OK) {
85+
const char *component = archive_entry_pathname(ae);
86+
if (component != nullptr) {
10387
TessdataType type;
104-
if (fileName != nullptr &&
105-
TessdataTypeFromFileName(fileName, &type)) {
106-
#if !defined(NDEBUG)
107-
tprintf("TessdataTypeFromFileName(%s, ...) passed, type %d\n",
108-
fileName, type);
109-
#endif
88+
if (TessdataTypeFromFileName(component, &type)) {
11089
int64_t size = archive_entry_size(ae);
11190
if (size > 0) {
11291
entries_[type].resize_no_init(size);
11392
if (archive_read_data(a, &entries_[type][0], size) == size) {
93+
is_loaded_ = true;
11494
}
11595
}
11696
}
11797
}
118-
is_loaded_ = true;
119-
result = true;
12098
}
121-
archive_read_close(a);
122-
//~ }
99+
result = is_loaded_;
100+
}
123101
archive_read_free(a);
124102
}
125103
return result;
@@ -129,46 +107,24 @@ bool TessdataManager::LoadArchiveFile(const char *filename) {
129107
#if defined(HAVE_LIBZIP)
130108
bool TessdataManager::LoadZipFile(const char *filename) {
131109
bool result = false;
132-
#ifndef NDEBUG
133-
tprintf("TessdataManager::%s(%s)\n", __func__, filename);
134-
#endif
135-
std::string zipfile(filename);
136110
int err;
137-
zip_t *uf = zip_open(zipfile.c_str(), ZIP_RDONLY, &err);
138-
if (uf == nullptr) {
139-
zipfile += ".zip";
140-
uf = zip_open(zipfile.c_str(), ZIP_RDONLY, &err);
141-
}
111+
zip_t *uf = zip_open(filename, ZIP_RDONLY, &err);
142112
if (uf != nullptr) {
143-
#ifndef NDEBUG
144-
tprintf("zip_open(%s) passed\n", zipfile.c_str());
145-
#endif
146113
int64_t nEntries = zip_get_num_entries(uf, ZIP_FL_UNCHANGED);
147114
for (int i = 0; i < nEntries; i++) {
148115
zip_stat_t zipStat;
149116
if (zip_stat_index(uf, i, ZIP_FL_UNCHANGED, &zipStat) == 0 &&
150117
(zipStat.valid & ZIP_STAT_NAME) && (zipStat.valid & ZIP_STAT_SIZE)) {
151-
#ifndef NDEBUG
152-
tprintf("zip_get_name(...) passed, file %s\n", zipStat.name);
153-
#endif
154118
TessdataType type;
155119
if (TessdataTypeFromFileName(zipStat.name, &type)) {
156-
#ifndef NDEBUG
157-
tprintf("TessdataTypeFromFileName(%s, ...) passed, type %d\n",
158-
zipStat.name, type);
159-
#endif
160120
zip_file_t *zipFile = zip_fopen_index(uf, i, ZIP_FL_UNCHANGED);
161121
if (zipFile == nullptr) {
162-
#ifndef NDEBUG
163122
tprintf("zip_fopen_index(...) failed\n");
164-
#endif
165123
} else {
166124
entries_[type].resize_no_init(zipStat.size);
167125
if (zip_fread(zipFile, &entries_[type][0], zipStat.size) !=
168126
static_cast<int64_t>(zipStat.size)) {
169-
#ifndef NDEBUG
170127
tprintf("zip_fread(...) failed\n");
171-
#endif
172128
}
173129
zip_fclose(zipFile);
174130
}
@@ -178,15 +134,9 @@ bool TessdataManager::LoadZipFile(const char *filename) {
178134
is_loaded_ = true;
179135
err = zip_close(uf);
180136
if (err != 0) {
181-
#ifndef NDEBUG
182137
tprintf("zip_close(...) failed\n");
183-
#endif
184138
}
185139
result = true;
186-
} else {
187-
#ifndef NDEBUG
188-
tprintf("zip_open(%s) failed\n", zipfile.c_str());
189-
#endif
190140
}
191141
return result;
192142
}
@@ -195,95 +145,56 @@ bool TessdataManager::LoadZipFile(const char *filename) {
195145
#if defined(HAVE_MINIZIP)
196146
bool TessdataManager::LoadMinizipFile(const char *filename) {
197147
bool result = false;
198-
#ifndef NDEBUG
199-
tprintf("TessdataManager::%s(%s)\n", __func__, filename);
200-
#endif
201-
std::string zipfile(filename);
202-
unzFile uf = unzOpen(zipfile.c_str());
203-
if (uf == nullptr) {
204-
zipfile += ".zip";
205-
uf = unzOpen(zipfile.c_str());
206-
}
148+
unzFile uf = unzOpen(filename);
207149
if (uf != nullptr) {
208-
#ifndef NDEBUG
209-
tprintf("unzOpen(%s) passed\n", zipfile.c_str());
210-
#endif
211150
unz_global_info global_info;
212151
int err;
213152
err = unzGetGlobalInfo(uf, &global_info);
214153
if (err == UNZ_OK) {
215-
#ifndef NDEBUG
216-
tprintf("unzGetGlobalInfo(...) passed, zip file with %lu entries\n",
217-
global_info.number_entry);
218-
#endif
219154
}
220155
unz_file_info file_info;
221-
char fileName[32];
156+
char component[32];
222157
char extraField[32];
223158
char comment[32];
224159
//~ $1 = {version = 798, version_needed = 20, flag = 0, compression_method = 8, dosDate = 1252768343, crc = 2481269679, compressed_size = 7131663, uncompressed_size = 16109842,
225160
//~ size_filename = 15, size_file_extra = 24, size_file_comment = 0, disk_num_start = 0, internal_fa = 0, external_fa = 2175008768, tmu_date = {tm_sec = 46, tm_min = 18,
226161
//~ tm_hour = 23, tm_mday = 11, tm_mon = 4, tm_year = 2017}}
227162
for (unsigned i = 0; i < global_info.number_entry; i++) {
228163
err = unzGetCurrentFileInfo(uf, &file_info,
229-
fileName, sizeof(fileName),
164+
component, sizeof(component),
230165
extraField, sizeof(extraField),
231166
comment, sizeof(comment));
232167
if (err == UNZ_OK) {
233-
#ifndef NDEBUG
234-
tprintf("unzGetCurrentFileInfo(...) passed, file %s, %lu byte\n",
235-
fileName, file_info.uncompressed_size);
236-
#endif
237168
TessdataType type;
238-
if (TessdataTypeFromFileName(fileName, &type)) {
239-
#ifndef NDEBUG
240-
tprintf("TessdataTypeFromFileName(%s, ...) passed, type %d\n",
241-
fileName, type);
242-
#endif
169+
if (TessdataTypeFromFileName(component, &type)) {
243170
err = unzOpenCurrentFilePassword(uf, nullptr);
244171
if (err != UNZ_OK) {
245-
#ifndef NDEBUG
246172
tprintf("unzOpenCurrentFilePassword(...) failed, err %d\n", err);
247-
#endif
248173
} else {
249174
entries_[type].resize_no_init(file_info.uncompressed_size);
250175
err = unzReadCurrentFile(uf, &entries_[type][0], file_info.uncompressed_size);
251176
if (err < UNZ_OK) {
252-
#ifndef NDEBUG
253177
tprintf("unzReadCurrentFile(...) failed, err %d\n", err);
254-
#endif
255178
}
256179
err = unzCloseCurrentFile(uf);
257180
if (err != UNZ_OK) {
258-
#ifndef NDEBUG
259181
tprintf("unzCloseCurrentFile(...) failed\n");
260-
#endif
261182
}
262183
}
263184
}
264185
}
265-
//~ err = unzGoToFirstFile(uf);
266186

267187
err = unzGoToNextFile(uf);
268188
if (err != UNZ_OK) {
269-
#ifndef NDEBUG
270189
tprintf("unzGoToNextFile(...) failed\n");
271-
#endif
272190
}
273191
}
274192
is_loaded_ = true;
275193
err = unzClose(uf);
276194
if (err != UNZ_OK) {
277-
#ifndef NDEBUG
278195
tprintf("unzClose(...) failed\n");
279-
#endif
280196
}
281197
result = true;
282-
} else {
283-
#ifndef NDEBUG
284-
tprintf("unzOpen(%s) failed\n", zipfile.c_str());
285-
perror(zipfile.c_str());
286-
#endif
287198
}
288199
return result;
289200
}
@@ -292,28 +203,19 @@ bool TessdataManager::LoadMinizipFile(const char *filename) {
292203
#if defined(HAVE_ZZIPLIB)
293204
bool TessdataManager::LoadZzipFile(const char *filename) {
294205
bool result = false;
295-
#ifndef NDEBUG
296-
tprintf("TessdataManager::%s(%s)\n", __func__, filename);
297-
#endif
298206
zzip_error_t err;
299207
ZZIP_DIR *dir = zzip_dir_open(filename, &err);
300208
if (dir != nullptr) {
301209
ZZIP_DIRENT d;
302210
while (zzip_dir_read(dir, &d)) {
303211
TessdataType type;
304212
if (TessdataTypeFromFileName(d.d_name, &type)) {
305-
#ifndef NDEBUG
306-
tprintf("TessdataTypeFromFileName(%s, ...) passed, type %d\n",
307-
d.d_name, type);
308-
#endif
309213
ZZIP_FILE *f = zzip_file_open(dir, d.d_name, 0);
310214
if (f != nullptr) {
311215
entries_[type].resize_no_init(d.st_size);
312216
ssize_t len = zzip_file_read(f, &entries_[type][0], d.st_size);
313217
if (len != d.st_size) {
314-
#ifndef NDEBUG
315218
tprintf("zzip_file_read(...) failed\n");
316-
#endif
317219
}
318220
zzip_file_close(f);
319221
}
@@ -328,9 +230,6 @@ bool TessdataManager::LoadZzipFile(const char *filename) {
328230
#endif
329231

330232
bool TessdataManager::Init(const char *data_file_name) {
331-
#ifndef NDEBUG
332-
tprintf("TessdataManager::%s(%s)\n", __func__, data_file_name);
333-
#endif
334233
GenericVector<char> data;
335234
if (reader_ == nullptr) {
336235
const char *tessarchive = getenv("TESSARCHIVE");
@@ -497,7 +396,6 @@ bool TessdataManager::CombineDataFiles(
497396
const char *language_data_path_prefix,
498397
const char *output_filename) {
499398
// Load individual tessdata components from files.
500-
// TODO: This method supports only the proprietary file format.
501399
for (int i = 0; i < TESSDATA_NUM_ENTRIES; ++i) {
502400
TessdataType type;
503401
ASSERT_HOST(TessdataTypeFromFileSuffix(kTessdataFileSuffixes[i], &type));

0 commit comments

Comments
 (0)