Skip to content

Commit 960761c

Browse files
committed
POSIX's 'open' returns int.
Clang doesn't admit an int -> void* cast for this.
1 parent 04893d8 commit 960761c

File tree

10 files changed

+131
-33
lines changed

10 files changed

+131
-33
lines changed

src/xrCore/FS.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,11 @@ class XRCORE_API IReader : public IReaderBase<IReader>
410410
class XRCORE_API CVirtualFileRW : public IReader
411411
{
412412
private:
413+
#if defined(WINDOWS)
413414
void *hSrcFile, *hSrcMap;
415+
#elif defined(LINUX)
416+
int hSrcFile;
417+
#endif
414418

415419
public:
416420
CVirtualFileRW(const char* cFileName);

src/xrCore/FS_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ class CCompressedReader : public IReader
124124
class CVirtualFileReader : public IReader
125125
{
126126
private:
127+
#if defined(WINDOWS)
127128
void *hSrcFile, *hSrcMap;
129+
#elif defined(LINUX)
130+
int hSrcFile;
131+
#endif
128132

129133
public:
130134
CVirtualFileReader(const char* cFileName);

src/xrCore/LocatorAPI.cpp

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -256,47 +256,33 @@ const CLocatorAPI::file* CLocatorAPI::Register(
256256
return &*result;
257257
}
258258

259+
#if defined(WINDOWS)
259260
IReader* open_chunk(void* ptr, u32 ID)
260261
{
261262
u32 dwType, dwSize;
262263
DWORD read_byte;
263-
#ifdef WINDOWS
264264
u32 pt = SetFilePointer(ptr, 0, nullptr, FILE_BEGIN);
265265
VERIFY(pt != INVALID_SET_FILE_POINTER);
266-
#else
267-
::lseek(ptr, 0L, SEEK_SET);
268-
#endif
266+
269267
while (true)
270268
{
271-
#ifdef WINDOWS
272269
bool res = ReadFile(ptr, &dwType, 4, &read_byte, nullptr);
273-
#elif defined(LINUX)
274-
read_byte = ::read(ptr, &dwType, 4);
275-
bool res = (read_byte != -1);
276-
#endif
270+
277271
if (read_byte == 0)
278272
return nullptr;
279273
//. VERIFY(res&&(read_byte==4));
280274

281-
#ifdef WINDOWS
282275
res = ReadFile(ptr, &dwSize, 4, &read_byte, nullptr);
283-
#else
284-
read_byte = ::read(ptr, &dwSize, 4);
285-
res = (read_byte != -1);
286-
#endif
276+
287277
if (read_byte == 0)
288278
return nullptr;
289279
//. VERIFY(res&&(read_byte==4));
290280

291281
if ((dwType & ~CFS_CompressMark) == ID)
292282
{
293283
u8* src_data = xr_alloc<u8>(dwSize);
294-
#ifdef WINDOWS
295284
res = ReadFile(ptr, src_data, dwSize, &read_byte, nullptr);
296-
#else
297-
read_byte = ::read(ptr, src_data, dwSize);
298-
res = (read_byte != -1);
299-
#endif
285+
300286
VERIFY(res && (read_byte == dwSize));
301287
if (dwType & CFS_CompressMark)
302288
{
@@ -308,17 +294,62 @@ IReader* open_chunk(void* ptr, u32 ID)
308294
}
309295
return new CTempReader(src_data, dwSize, 0);
310296
}
311-
#ifdef WINDOWS
297+
312298
pt = SetFilePointer(ptr, dwSize, nullptr, FILE_CURRENT);
313299
if (pt == INVALID_SET_FILE_POINTER)
314300
return nullptr;
315-
#else
316-
if(-1 == ::lseek(ptr, dwSize, SEEK_CUR))
317-
return nullptr;
301+
}
302+
return nullptr;
303+
};
318304
#endif
305+
306+
#if defined(LINUX)
307+
IReader* open_chunk(int fd, u32 ID)
308+
{
309+
u32 dwType, dwSize;
310+
DWORD read_byte;
311+
::lseek(fd, 0L, SEEK_SET);
312+
313+
while (true)
314+
{
315+
read_byte = ::read(fd, &dwType, 4);
316+
bool res = (read_byte != -1);
317+
318+
if (read_byte == 0)
319+
return nullptr;
320+
//. VERIFY(res&&(read_byte==4));
321+
322+
read_byte = ::read(fd, &dwSize, 4);
323+
res = (read_byte != -1);
324+
325+
if (read_byte == 0)
326+
return nullptr;
327+
//. VERIFY(res&&(read_byte==4));
328+
329+
if ((dwType & ~CFS_CompressMark) == ID)
330+
{
331+
u8* src_data = xr_alloc<u8>(dwSize);
332+
read_byte = ::read(fd, src_data, dwSize);
333+
res = (read_byte != -1);
334+
335+
VERIFY(res && (read_byte == dwSize));
336+
if (dwType & CFS_CompressMark)
337+
{
338+
BYTE* dest;
339+
unsigned dest_sz;
340+
_decompressLZ(&dest, &dest_sz, src_data, dwSize);
341+
xr_free(src_data);
342+
return new CTempReader(dest, dest_sz, 0);
343+
}
344+
return new CTempReader(src_data, dwSize, 0);
345+
}
346+
347+
if(-1 == ::lseek(fd, dwSize, SEEK_CUR))
348+
return nullptr;
319349
}
320350
return nullptr;
321351
};
352+
#endif
322353

323354
void CLocatorAPI::LoadArchive(archive& A, pcstr entrypoint)
324355
{
@@ -416,17 +447,21 @@ void CLocatorAPI::LoadArchive(archive& A, pcstr entrypoint)
416447

417448
void CLocatorAPI::archive::open()
418449
{
450+
#if defined(WINDOWS)
419451
// Open the file
420452
if (hSrcFile && hSrcMap)
421453
return;
422454

423-
#if defined(WINDOWS)
424455
hSrcFile = CreateFile(*path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
425456
R_ASSERT(hSrcFile != INVALID_HANDLE_VALUE);
426457
hSrcMap = CreateFileMapping(hSrcFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
427458
R_ASSERT(hSrcMap != INVALID_HANDLE_VALUE);
428459
size = GetFileSize(hSrcFile, nullptr);
429460
#elif defined(LINUX)
461+
// Open the file
462+
if (hSrcFile)
463+
return;
464+
430465
hSrcFile = ::open(*path, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
431466
R_ASSERT(hSrcFile != -1);
432467
struct stat file_info;
@@ -505,7 +540,11 @@ bool CLocatorAPI::load_all_unloaded_archives()
505540
bool res = false;
506541
for (auto& archive : m_archives)
507542
{
543+
#if defined(WINDOWS)
508544
if (archive.hSrcFile == nullptr)
545+
#elif defined(LINUX)
546+
if (archive.hSrcFile == 0)
547+
#endif
509548
{
510549
LoadArchive(archive);
511550
res = true;
@@ -593,7 +632,7 @@ bool ignore_path(const char* _path)
593632
else
594633
return true;
595634
#elif defined(LINUX)
596-
HANDLE h = ::open(_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
635+
int h = ::open(_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
597636
if (h != -1)
598637
{
599638
::close(h);
@@ -1294,7 +1333,11 @@ void CLocatorAPI::file_from_archive(CStreamReader*& R, pcstr fname, const file&
12941333
make_string("cannot use stream reading for compressed data %s, do not compress data to be streamed", fname));
12951334

12961335
R = new CStreamReader();
1336+
#if defined(WINDOWS)
12971337
R->construct(A.hSrcMap, desc.ptr, desc.size_compressed, A.size, BIG_FILE_READER_WINDOW_SIZE);
1338+
#elif defined(LINUX)
1339+
R->construct(A.hSrcFile, desc.ptr, desc.size_compressed, A.size, BIG_FILE_READER_WINDOW_SIZE);
1340+
#endif
12981341
}
12991342

13001343
void CLocatorAPI::copy_file_to_build(IWriter* W, IReader* r) { W->w(r->pointer(), r->length()); }
@@ -1506,7 +1549,7 @@ void CLocatorAPI::w_close(IWriter*& S)
15061549
Register(fname, 0xffffffff, 0, 0, st.st_size, st.st_size, (u32)st.st_mtime);
15071550
#elif defined(LINUX)
15081551
struct stat st;
1509-
::fstat(fname, &st);
1552+
::stat(fname, &st);
15101553
Register(fname, 0xffffffff, 0, 0, st.st_size, st.st_size, (u32)st.st_mtime);
15111554
#endif
15121555
}

src/xrCore/LocatorAPI.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,18 @@ class XRCORE_API CLocatorAPI : Noncopyable
9494

9595
struct archive
9696
{
97-
u32 size;
98-
u32 vfs_idx;
97+
u32 size = 0;
98+
u32 vfs_idx = u32(-1);
9999
shared_str path;
100-
void *hSrcFile, *hSrcMap;
101-
CInifile* header;
100+
#if defined(WINDOWS)
101+
void *hSrcFile = nullptr;
102+
void *hSrcMap = nullptr
103+
#elif defined(LINUX)
104+
int hSrcFile = 0;
105+
#endif
106+
CInifile* header = nullptr;
102107

103-
archive() : size(0), vfs_idx(u32(-1)), hSrcFile(nullptr), hSrcMap(nullptr), header(nullptr) {}
108+
archive() = default;
104109
void open();
105110
void close();
106111
};

src/xrCore/file_stream_reader.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ void CFileStreamReader::construct(LPCSTR file_name, const u32& window_size)
2222
struct stat file_info;
2323
::fstat(m_file_handle, &file_info);
2424
u32 file_size = (u32)file_info.st_size;
25-
inherited::construct(&m_file_handle, 0, file_size, file_size, window_size);
25+
inherited::construct(m_file_handle, 0, file_size, file_size, window_size);
2626
#endif
2727
}
2828

2929
void CFileStreamReader::destroy()
3030
{
31+
#if defined(WINDOWS)
3132
HANDLE file_mapping_handle = this->file_mapping_handle();
3233
inherited::destroy();
33-
#if defined(WINDOWS)
3434
CloseHandle(file_mapping_handle);
3535
CloseHandle(m_file_handle);
3636
#elif defined(LINUX)
37+
int file_mapping_handle = this->file_mapping_handle();
38+
inherited::destroy();
3739
::close(m_file_handle);
3840
m_file_handle = -1;
3941
#endif

src/xrCore/file_stream_reader.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ class CFileStreamReader : public CStreamReader
99
typedef CStreamReader inherited;
1010

1111
private:
12+
#if defined(WINDOWS)
1213
HANDLE m_file_handle;
14+
#elif defined(LINUX)
15+
int m_file_handle;
16+
#endif
1317

1418
public:
1519
virtual void construct(LPCSTR file_name, const u32& window_size);

src/xrCore/stream_reader.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <sys/mman.h>
66
#endif
77

8+
#if defined(WINDOWS)
89
void CStreamReader::construct(const HANDLE& file_mapping_handle, const u32& start_offset, const u32& file_size,
910
const u32& archive_size, const u32& window_size)
1011
{
@@ -16,6 +17,19 @@ void CStreamReader::construct(const HANDLE& file_mapping_handle, const u32& star
1617

1718
map(0);
1819
}
20+
#elif defined(LINUX)
21+
void CStreamReader::construct(int file_mapping_handle, const u32& start_offset, const u32& file_size,
22+
const u32& archive_size, const u32& window_size)
23+
{
24+
m_file_mapping_handle = file_mapping_handle;
25+
m_start_offset = start_offset;
26+
m_file_size = file_size;
27+
m_archive_size = archive_size;
28+
m_window_size = _max(window_size, FS.dwAllocGranularity);
29+
30+
map(0);
31+
}
32+
#endif
1933

2034
void CStreamReader::destroy() { unmap(); }
2135
void CStreamReader::map(const u32& new_offset)

src/xrCore/stream_reader.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
class XRCORE_API CStreamReader : public IReaderBase<CStreamReader>
55
{
66
private:
7+
#if defined(WINDOWS)
78
HANDLE m_file_mapping_handle;
9+
#elif defined(LINUX)
10+
int m_file_mapping_handle;
11+
#endif
812
u32 m_start_offset;
913
u32 m_file_size;
1014
u32 m_archive_size;
@@ -31,12 +35,21 @@ class XRCORE_API CStreamReader : public IReaderBase<CStreamReader>
3135
IC CStreamReader();
3236

3337
public:
38+
#if defined(WINDOWS)
3439
virtual void construct(const HANDLE& file_mapping_handle, const u32& start_offset, const u32& file_size,
3540
const u32& archive_size, const u32& window_size);
41+
#elif defined(LINUX)
42+
virtual void construct(int file_mapping_handle, const u32& start_offset, const u32& file_size,
43+
const u32& archive_size, const u32& window_size);
44+
#endif
3645
virtual void destroy();
3746

3847
public:
48+
#if defined(WINDOWS)
3949
IC const HANDLE& file_mapping_handle() const;
50+
#elif defined(LINUX)
51+
IC const int& file_mapping_handle() const;
52+
#endif
4053
IC u32 elapsed() const;
4154
IC const u32& length() const;
4255
IC void seek(const int& offset);

src/xrCore/stream_reader_inline.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ IC CStreamReader& CStreamReader::operator=(const CStreamReader&)
1818
return (*this);
1919
}
2020

21+
#if defined(WINDOWS)
2122
IC const HANDLE& CStreamReader::file_mapping_handle() const { return (m_file_mapping_handle); }
23+
#elif defined(LINUX)
24+
IC const int& CStreamReader::file_mapping_handle() const { return (m_file_mapping_handle); }
25+
#endif
26+
2227
#if defined(WINDOWS)
2328
IC void CStreamReader::unmap() { UnmapViewOfFile(m_current_map_view_of_file); }
2429
#else

src/xrEngine/x_ray.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,11 @@ int CApplication::Level_ID(LPCSTR name, LPCSTR ver, bool bSet)
458458
for (; it != it_e; ++it)
459459
{
460460
CLocatorAPI::archive& A = *it;
461+
#if defined(WINDOWS)
461462
if (A.hSrcFile == nullptr)
463+
#elif defined(LINUX)
464+
if (A.hSrcFile == 0)
465+
#endif
462466
{
463467
LPCSTR ln = A.header->r_string("header", "level_name");
464468
LPCSTR lv = A.header->r_string("header", "level_ver");

0 commit comments

Comments
 (0)