SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLIONative.cpp
Go to the documentation of this file.
1 /**
2  * \file SLIONative.cpp
3  * \date October 2022
4  * \authors Marino von Wattenwyl
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <SLIONative.h>
11 
12 #ifdef SL_STORAGE_FS
13 //-----------------------------------------------------------------------------
15  : _stream(path, std::ios::binary)
16 {
17  //assert(!_stream.fail());
18 }
19 //-----------------------------------------------------------------------------
20 size_t SLIOReaderNative::read(void* buffer, size_t size)
21 {
22  _stream.read((char*)buffer, (std::streamsize)size);
23  return _stream.gcount();
24 }
25 //-----------------------------------------------------------------------------
27 {
28  return (size_t)_stream.tellg();
29 }
30 //-----------------------------------------------------------------------------
31 bool SLIOReaderNative::seek(size_t offset, Origin origin)
32 {
33  std::ios::seekdir nativeOrigin = std::ios::beg;
34  if (origin == IOO_beg)
35  nativeOrigin = std::ios::beg;
36  else if (origin == IOO_cur)
37  nativeOrigin = std::ios::cur;
38  else if (origin == IOO_end)
39  nativeOrigin = std::ios::end;
40 
41  return (bool)_stream.seekg((std::streamsize)offset, nativeOrigin);
42 }
43 //-----------------------------------------------------------------------------
45 {
46  std::streamsize pos = _stream.tellg();
47  _stream.seekg(0, std::ios::end);
48  size_t size = (size_t)_stream.tellg();
49  _stream.seekg(pos, std::ios::beg);
50  return size;
51 }
52 //-----------------------------------------------------------------------------
54  : _stream(path, std::ios::binary)
55 {
56 }
57 //-----------------------------------------------------------------------------
58 size_t SLIOWriterNative::write(const void* buffer, size_t size)
59 {
60  size_t offsetBefore = (size_t)_stream.tellp();
61  _stream.write(static_cast<const char*>(buffer), (std::streamsize)size);
62  size_t offsetAfter = (size_t)_stream.tellp();
63  return offsetAfter - offsetBefore;
64 }
65 //-----------------------------------------------------------------------------
67 {
68  return (size_t)_stream.tellp();
69 }
70 //-----------------------------------------------------------------------------
71 bool SLIOWriterNative::seek(size_t offset, Origin origin)
72 {
73  std::ios::seekdir nativeOrigin = std::ios::beg;
74  if (origin == IOO_beg)
75  nativeOrigin = std::ios::beg;
76  else if (origin == IOO_cur)
77  nativeOrigin = std::ios::cur;
78  else if (origin == IOO_end)
79  nativeOrigin = std::ios::end;
80 
81  return (bool)_stream.seekp((std::streamsize)offset, nativeOrigin);
82 }
83 //-----------------------------------------------------------------------------
85 {
86  _stream.flush();
87 }
88 //-----------------------------------------------------------------------------
89 #endif
SLIOReaderNative(std::string path)
Definition: SLIONative.cpp:14
bool seek(size_t offset, Origin origin)
Definition: SLIONative.cpp:31
size_t read(void *buffer, size_t size)
Definition: SLIONative.cpp:20
std::ifstream _stream
Definition: SLIONative.h:28
virtual size_t size()
Definition: SLFileStorage.h:76
size_t write(const void *buffer, size_t size)
Definition: SLIONative.cpp:58
std::ofstream _stream
Definition: SLIONative.h:42
bool seek(size_t offset, Origin origin)
Definition: SLIONative.cpp:71
SLIOWriterNative(std::string path)
Definition: SLIONative.cpp:53