Skip to content

Commit 2e248db

Browse files
committed
Fix posix file storage again
1 parent 4e460e3 commit 2e248db

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

base/vks/storage.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ class MemoryStorage : public Storage {
7272
std::vector<uint8_t> _data;
7373
};
7474

75+
#if defined(__ANDROID__) || defined(WIN32)
76+
#define MAPPED_FILES 1
77+
#else
78+
#define MAPPED_FILES 0
79+
#endif
80+
81+
#if MAPPED_FILES
82+
7583
class FileStorage : public Storage {
7684
public:
7785
static StoragePointer create(const std::string& filename, size_t size, const uint8_t* data);
@@ -121,27 +129,6 @@ FileStorage::FileStorage(const std::string& filename) {
121129
throw std::runtime_error("Failed to create mapping");
122130
}
123131
_mapped = (uint8_t*)MapViewOfFile(_mapFile, FILE_MAP_READ, 0, 0, 0);
124-
#else
125-
// FIXME move to posix memory mapped files
126-
// open the file:
127-
std::ifstream file(filename, std::ios::binary);
128-
// Stop eating new lines in binary mode!!!
129-
file.unsetf(std::ios::skipws);
130-
131-
// get its size:
132-
std::streampos fileSize;
133-
134-
file.seekg(0, std::ios::end);
135-
fileSize = file.tellg();
136-
file.seekg(0, std::ios::beg);
137-
138-
// reserve capacity
139-
_data.reserve(fileSize);
140-
141-
// read the data:
142-
_data.insert(_data.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
143-
_size = _data.size();
144-
file.close();
145132
#endif
146133
}
147134

@@ -152,15 +139,40 @@ FileStorage::~FileStorage() {
152139
UnmapViewOfFile(_mapped);
153140
CloseHandle(_mapFile);
154141
CloseHandle(_file);
155-
#else
156142
#endif
157143
}
158144

145+
#endif
146+
159147
StoragePointer Storage::create(size_t size, uint8_t* data) {
160148
return std::make_shared<MemoryStorage>(size, data);
161149
}
162150
StoragePointer Storage::readFile(const std::string& filename) {
151+
#if MAPPED_FILES
163152
return std::make_shared<FileStorage>(filename);
153+
#else
154+
// FIXME move to posix memory mapped files
155+
// open the file:
156+
std::ifstream file(filename, std::ios::binary);
157+
// Stop eating new lines in binary mode!!!
158+
file.unsetf(std::ios::skipws);
159+
160+
// get its size:
161+
std::streampos fileSize;
162+
163+
file.seekg(0, std::ios::end);
164+
fileSize = file.tellg();
165+
file.seekg(0, std::ios::beg);
166+
167+
168+
std::vector<uint8_t> fileData;
169+
// reserve capacity
170+
fileData.reserve(fileSize);
171+
// read the data:
172+
fileData.insert(fileData.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
173+
file.close();
174+
return std::make_shared<MemoryStorage>(fileData.size(), fileData.data());
175+
#endif
164176
}
165177

166178
}} // namespace vks::storage

0 commit comments

Comments
 (0)