Skip to content

Commit 5f4ce94

Browse files
committed
replace error prone FileReader factory class with simple readFile(fn) function
1 parent ba7fca3 commit 5f4ce94

File tree

4 files changed

+21
-89
lines changed

4 files changed

+21
-89
lines changed

src/librawspeed/io/FileReader.cpp

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,79 +24,28 @@
2424
#include <cstdio> // for fclose, fseek, fopen, fread, ftell
2525
#include <fcntl.h> // for SEEK_END, SEEK_SET
2626

27-
#if !defined(__unix__) && !defined(__APPLE__)
28-
#include <io.h>
29-
#include <tchar.h>
30-
#include <windows.h>
31-
#endif // !defined(__unix__) && !defined(__APPLE__)
32-
3327
namespace RawSpeed {
3428

35-
FileReader::FileReader(const char *_filename) : mFilename(_filename) {}
36-
37-
FileMap* FileReader::readFile() {
38-
#if defined(__unix__) || defined(__APPLE__)
39-
int bytes_read = 0;
40-
FILE *file;
41-
char *dest;
42-
long size;
43-
44-
file = fopen(mFilename, "rb");
45-
if (file == nullptr)
46-
ThrowFIE("Could not open file.");
47-
fseek(file, 0, SEEK_END);
48-
size = ftell(file);
49-
if (size <= 0) {
50-
fclose(file);
51-
ThrowFIE("File is 0 bytes.");
52-
}
53-
fseek(file, 0, SEEK_SET);
54-
55-
#if 0
56-
// Not used, as it is slower than sync read
57-
58-
uchar8* pa = (uchar8*)mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
59-
FileMap *fileData = new FileMap(pa, size);
60-
61-
#else
62-
auto *fileData = new FileMap(size);
63-
64-
dest = (char *)fileData->getDataWrt(0, size);
65-
bytes_read = fread(dest, 1, size, file);
66-
fclose(file);
67-
if (size != bytes_read) {
68-
delete fileData;
69-
ThrowFIE("Could not read file.");
70-
}
71-
#endif
72-
73-
#else // __unix__
74-
HANDLE file_h; // File handle
75-
file_h = CreateFile(mFilename, GENERIC_READ, FILE_SHARE_READ, nullptr,
76-
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
77-
if (file_h == INVALID_HANDLE_VALUE) {
29+
Buffer readFile(const char* fn)
30+
{
31+
std::unique_ptr<FILE, decltype(&fclose)> file(fopen(fn, "rb"), &fclose);
32+
if (!file)
7833
ThrowFIE("Could not open file.");
79-
}
8034

81-
LARGE_INTEGER f_size;
82-
GetFileSizeEx(file_h , &f_size);
35+
fseek(file.get(), 0, SEEK_END);
36+
long size = ftell(file.get());
37+
if (size <= 0)
38+
ThrowFIE("File is empty or has unknown size.");
8339

84-
if (!f_size.LowPart)
85-
ThrowFIE("File is 0 bytes.");
40+
fseek(file.get(), 0, SEEK_SET);
8641

87-
FileMap *fileData = new FileMap(f_size.LowPart);
42+
Buffer data(size);
8843

89-
DWORD bytes_read;
90-
if (!ReadFile(file_h, fileData->getDataWrt(0, fileData->getSize()),
91-
fileData->getSize(), &bytes_read, nullptr)) {
92-
CloseHandle(file_h);
93-
delete fileData;
44+
size_t bytes_read = fread((char *)data.getDataWrt(0, size), 1, size, file.get());
45+
if ((size_t)size != bytes_read)
9446
ThrowFIE("Could not read file.");
95-
}
96-
CloseHandle(file_h);
9747

98-
#endif // __unix__
99-
return fileData;
48+
return data;
10049
}
10150

10251
} // namespace RawSpeed

src/librawspeed/io/FileReader.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,10 @@
2020

2121
#pragma once
2222

23-
#include "io/FileMap.h" // for FileMap
23+
#include "io/Buffer.h"
2424

2525
namespace RawSpeed {
2626

27-
class FileReader
28-
{
29-
public:
30-
FileReader(const char *filename);
31-
32-
FileMap* readFile();
33-
const char* Filename() const { return mFilename; }
34-
// void Filename(const char * val) { mFilename = val; }
35-
36-
private:
37-
const char* mFilename;
38-
};
27+
Buffer readFile(const char* filename);
3928

4029
} // namespace RawSpeed

src/utilities/identify/rawspeed-identify.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ int main(int argc, char *argv[]) {
125125

126126
fprintf(stderr, "Loading file: \"%s\"\n", argv[1]);
127127

128-
FileReader f(argv[1]);
128+
auto fileBuffer = readFile(argv[1]);
129129

130-
std::unique_ptr<FileMap> m(f.readFile());
131-
132-
RawParser t(m.get());
130+
RawParser t(&fileBuffer);
133131

134132
std::unique_ptr<RawDecoder> d(t.getDecoder(meta.get()));
135133

src/utilities/rstest/rstest.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,12 @@ size_t process(const string& filename, const CameraMetaData* metadata,
188188
#endif
189189
cout << left << setw(55) << filename << ": starting decoding ... " << endl;
190190

191-
FileReader reader(filename.c_str());
192-
193-
unique_ptr<FileMap> map = unique_ptr<FileMap>(reader.readFile());
194-
// FileMap* map = readFile( argv[1] );
191+
Buffer fileBuffer = readFile( argv[1] );
195192

196193
Timer t;
197194

198-
RawParser parser(map.get());
199-
unique_ptr<RawDecoder> decoder =
200-
unique_ptr<RawDecoder>(parser.getDecoder(metadata));
195+
RawParser parser(&fileBuffer);
196+
unique_ptr<RawDecoder> decoder(parser.getDecoder(metadata));
201197
// RawDecoder* decoder = parseRaw( map );
202198

203199
decoder->failOnUnknown = false;
@@ -213,7 +209,7 @@ size_t process(const string& filename, const CameraMetaData* metadata,
213209
#pragma omp critical(io)
214210
#endif
215211
cout << left << setw(55) << filename << ": " << internal << setw(3)
216-
<< map->getSize() / 1000000 << " MB / " << setw(4) << time << " ms"
212+
<< fileBuffer.getSize() / 1000000 << " MB / " << setw(4) << time << " ms"
217213
<< endl;
218214

219215
if (create) {

0 commit comments

Comments
 (0)