Skip to content

Commit bb2348b

Browse files
committed
genericvector: Fix and optimize function LoadDataFromFile
It's not necessary to initialize the vector with 0, because the initial values are read from file. Fix also an assertion when trying to read an empty file. Signed-off-by: Stefan Weil <[email protected]>
1 parent 21e739c commit bb2348b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

ccutil/genericvector.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,18 @@ typedef bool (*FileWriter)(const GenericVector<char>& data,
366366
// returning false on error.
367367
inline bool LoadDataFromFile(const STRING& filename,
368368
GenericVector<char>* data) {
369+
bool result = false;
369370
FILE* fp = fopen(filename.string(), "rb");
370-
if (fp == NULL) return false;
371-
fseek(fp, 0, SEEK_END);
372-
size_t size = ftell(fp);
373-
fseek(fp, 0, SEEK_SET);
374-
data->init_to_size(static_cast<int>(size), 0);
375-
bool result = fread(&(*data)[0], 1, size, fp) == size;
376-
fclose(fp);
371+
if (fp != NULL) {
372+
fseek(fp, 0, SEEK_END);
373+
size_t size = ftell(fp);
374+
fseek(fp, 0, SEEK_SET);
375+
if (size > 0) {
376+
data->resize_no_init(size);
377+
result = fread(&(*data)[0], 1, size, fp) == size;
378+
}
379+
fclose(fp);
380+
}
377381
return result;
378382
}
379383
// The default FileWriter writes the vector of char to the filename file,

0 commit comments

Comments
 (0)