Skip to content

Commit

Permalink
genericvector: Fix and optimize function LoadDataFromFile
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
stweil committed May 12, 2017
1 parent 21e739c commit bb2348b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions ccutil/genericvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,18 @@ typedef bool (*FileWriter)(const GenericVector<char>& data,
// returning false on error.
inline bool LoadDataFromFile(const STRING& filename,
GenericVector<char>* data) {
bool result = false;
FILE* fp = fopen(filename.string(), "rb");
if (fp == NULL) return false;
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
fseek(fp, 0, SEEK_SET);
data->init_to_size(static_cast<int>(size), 0);
bool result = fread(&(*data)[0], 1, size, fp) == size;
fclose(fp);
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (size > 0) {
data->resize_no_init(size);
result = fread(&(*data)[0], 1, size, fp) == size;
}
fclose(fp);
}
return result;
}
// The default FileWriter writes the vector of char to the filename file,
Expand Down

0 comments on commit bb2348b

Please sign in to comment.