Unravel Engine C++ Reference
Loading...
Searching...
No Matches
file_stream_common.cpp
Go to the documentation of this file.
2
4{
5
6#if defined(_WIN32)
7auto stdio_tell64(FILE* file) -> long long
8{
9 return static_cast<long long>(_ftelli64(file));
10}
11
12auto stdio_seek64(FILE* file, long long offset, int origin) -> int
13{
14 return _fseeki64(file, offset, origin);
15}
16#else
17auto stdio_tell64(FILE* file) -> long long
18{
19 return static_cast<long long>(ftello(file));
20}
21
22auto stdio_seek64(FILE* file, long long offset, int origin) -> int
23{
24 return fseeko(file, static_cast<off_t>(offset), origin);
25}
26#endif
27
28auto stdio_file_size(FILE* file) -> long long
29{
30 if(file == nullptr)
31 {
32 return -1;
33 }
34 const long long current = stdio_tell64(file);
35 if(current < 0)
36 {
37 return -1;
38 }
39 if(stdio_seek64(file, 0, SEEK_END) != 0)
40 {
41 return -1;
42 }
43 const long long size = stdio_tell64(file);
44 if(size < 0)
45 {
46 return -1;
47 }
48 if(stdio_seek64(file, current, SEEK_SET) != 0)
49 {
50 return -1;
51 }
52 return size;
53}
54
55auto openmode_to_stdio_mode(std::ios_base::openmode mode, char (&out)[4]) -> bool
56{
57 const bool append = (mode & std::ios_base::app) != 0;
58 const bool binary = (mode & std::ios_base::binary) != 0;
59 const bool input = (mode & std::ios_base::in) != 0;
60 const bool output = (mode & std::ios_base::out) != 0;
61 const bool truncate = (mode & std::ios_base::trunc) != 0;
62
63 char* cursor = out;
64 if(input && !output)
65 {
66 *cursor++ = 'r';
67 }
68 else if(output && !input)
69 {
70 *cursor++ = append ? 'a' : 'w';
71 }
72 else if(input && output)
73 {
74 if(append)
75 {
76 *cursor++ = 'a';
77 }
78 else if(truncate)
79 {
80 *cursor++ = 'w';
81 }
82 else
83 {
84 *cursor++ = 'r';
85 }
86 *cursor++ = '+';
87 }
88 else
89 {
90 out[0] = '\0';
91 return false;
92 }
93
94 if(binary)
95 {
96 *cursor++ = 'b';
97 }
98 *cursor = '\0';
99 return true;
100}
101
102} // namespace fs::file_stream_detail
auto stdio_file_size(FILE *file) -> long long
auto stdio_tell64(FILE *file) -> long long
auto openmode_to_stdio_mode(std::ios_base::openmode mode, char(&out)[4]) -> bool
auto stdio_seek64(FILE *file, long long offset, int origin) -> int
float size