Debian Codegraph @

3rdparty/xspublic/xstypes/xsfile.h

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSFILE_H
34 #define XSFILE_H
35
36 #include "xstypesconfig.h"
37 #include "pstdint.h"
38 #include <stdio.h>
39 #include "xsresultvalue.h"
40 #include "xsfilepos.h"
41 #include "xsstring.h"
42
43 #ifdef _MSC_VER
44 # define XS_MAX_FILENAME_LENGTH 512
45 #else
46 # define XS_MAX_FILENAME_LENGTH PATH_MAX
47 #endif
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 #ifndef __cplusplus
53 #define XSFILE_INITIALIZER { 0 }
54 #endif
55
56 struct XsFile;
57
58 XSTYPES_DLL_API void XsFile_destruct(struct XsFile* thisPtr);
59
60 XSTYPES_DLL_API XsResultValue XsFile_create(struct XsFile *thisPtr, const struct XsString* filename, int writeOnly);
61 XSTYPES_DLL_API XsResultValue XsFile_createText(struct XsFile *thisPtr, const struct XsString* filename, int writeOnly);
62 XSTYPES_DLL_API XsResultValue XsFile_open(struct XsFile *thisPtr, const struct XsString* filename, int readOnly);
63 XSTYPES_DLL_API XsResultValue XsFile_openText(struct XsFile *thisPtr, const struct XsString* filename, int readOnly);
64 XSTYPES_DLL_API XsResultValue XsFile_reopen(struct XsFile *thisPtr, const struct XsString* filename, const struct XsString* mode);
65 XSTYPES_DLL_API int XsFile_isOpen(const struct XsFile *thisPtr);
66 XSTYPES_DLL_API XsResultValue XsFile_close(struct XsFile *thisPtr);
67
68 XSTYPES_DLL_API int XsFile_exists(const struct XsString* filename);
69
70 XSTYPES_DLL_API XsResultValue XsFile_flush(struct XsFile *thisPtr);
71 XSTYPES_DLL_API XsResultValue XsFile_truncate(struct XsFile *thisPtr, XsFilePos fileSize);
72 XSTYPES_DLL_API XsResultValue XsFile_resize(struct XsFile *thisPtr, XsFilePos fileSize);
73 XSTYPES_DLL_API XsResultValue XsFile_erase(const struct XsString* filename);
74
75 XSTYPES_DLL_API XsFilePos XsFile_read(struct XsFile *thisPtr, void *destination, XsFilePos size, XsFilePos count);
76 XSTYPES_DLL_API XsFilePos XsFile_write(struct XsFile *thisPtr, const void *source, XsFilePos size, XsFilePos count);
77 XSTYPES_DLL_API int XsFile_getc(struct XsFile *thisPtr);
78 XSTYPES_DLL_API XsResultValue XsFile_putc(struct XsFile *thisPtr, int character);
79 XSTYPES_DLL_API char* XsFile_gets(struct XsFile *thisPtr, char *str, int num);
80 XSTYPES_DLL_API XsResultValue XsFile_puts(struct XsFile *thisPtr, const char *str);
81
82 XSTYPES_DLL_API XsResultValue XsFile_seek(struct XsFile* thisPtr, XsFilePos offset);
83 XSTYPES_DLL_API XsResultValue XsFile_seek_r(struct XsFile* thisPtr, XsFilePos offset);
84 XSTYPES_DLL_API XsFilePos XsFile_tell(struct XsFile const* thisPtr);
85
86 XSTYPES_DLL_API int XsFile_eof(struct XsFile const* thisPtr);
87 XSTYPES_DLL_API XsResultValue XsFile_error(struct XsFile const* thisPtr);
88 XSTYPES_DLL_API XsResultValue XsFile_fullPath(const struct XsString* filename, struct XsString* fullPath);
89
90 XSTYPES_DLL_API XsResultValue XsFile_getline(struct XsFile *thisPtr, struct XsString *line);
91
92 XSTYPES_DLL_API FILE* XsFile_handle(struct XsFile *thisPtr);
93
94 #ifdef __cplusplus
95 } // extern "C"
96 #endif
97
98 struct XsFile {
99 #ifdef __cplusplus
100 /*! \brief Default constructor, creates an empty file object */
101 explicit inline XsFile()
102 : m_handle(NULL)
103 {
104 // Silence -Wunused-private-field
105 // This field is used in the C back-end, but
106 // some compilers may not notice this
107 (void)m_handle;
108 }
109
110 /*! \brief \copybrief XsFile_destruct */
111 inline ~XsFile()
112 {
113 XsFile_destruct(this);
114 }
115
116 /*! \brief \copybrief XsFile_create */
117 inline XsResultValue create(const XsString &filename, bool writeOnly)
118 {
119 return XsFile_create(this, &filename, writeOnly ? 1 : 0);
120 }
121
122 /*! \brief \copybrief XsFile_createText */
123 inline XsResultValue createText(const XsString &filename, bool writeOnly)
124 {
125 return XsFile_createText(this, &filename, writeOnly ? 1 : 0);
126 }
127
128 /*! \brief \copybrief XsFile_open */
129 inline XsResultValue open(const XsString &fileName, bool readOnly)
130 {
131 return XsFile_open(this, &fileName, readOnly ? 1 : 0);
132 }
133
134 /*! \brief \copybrief XsFile_openText */
135 inline XsResultValue openText(const XsString &fileName, bool readOnly)
136 {
137 return XsFile_openText(this, &fileName, readOnly ? 1 : 0);
138 }
139
140 /*! \brief \copybrief XsFile_openText */
141 inline XsResultValue reopen(const XsString &fileName, const XsString &mode)
142 {
143 return XsFile_reopen(this, &fileName, &mode);
144 }
145
146 /*! \brief \copybrief XsFile_isOpen */
147 inline bool isOpen() const
148 {
149 return (XsFile_isOpen(this) == 0);
150 }
151
152 /*! \brief \copybrief XsFile_exists */
153 static inline bool exists(const XsString &fileName)
154 {
155 return (XsFile_exists(&fileName) == 0);
156 }
157
158 /*! \brief \copybrief XsFile_close */
159 inline XsResultValue close()
160 {
161 return XsFile_close(this);
162 }
163
164 /*! \brief \copybrief XsFile_flush */
165 inline XsResultValue flush()
166 {
167 return XsFile_flush(this);
168 }
169
170 /*! \brief \copybrief XsFile_truncate */
171 inline XsResultValue truncate(XsFilePos fileSize)
172 {
173 return XsFile_truncate(this, fileSize);
174 }
175
176 /*! \brief \copybrief XsFile_resize */
177 inline XsResultValue resize(XsFilePos fileSize)
178 {
179 return XsFile_resize(this, fileSize);
180 }
181
182 /*! \brief \copybrief XsFile_erase */
183 static XsResultValue erase(const XsString& filename)
184 {
185 return XsFile_erase(&filename);
186 }
187
188 /*! \brief \copybrief XsFile_read */
189 inline XsFilePos read(void *destination, XsFilePos size, XsFilePos count)
190 {
191 return XsFile_read(this, destination, size, count);
192 }
193
194 /*! \brief \copybrief XsFile_write */
195 inline XsFilePos write(const void *source, XsFilePos size, XsFilePos count)
196 {
197 return XsFile_write(this, source, size, count);
198 }
199
200 /*! \brief \copybrief XsFile_getc */
201 inline int getc()
202 {
203 return XsFile_getc(this);
204 }
205
206 /*! \brief \copybrief XsFile_putc */
207 inline XsResultValue putc(int character)
208 {
209 return XsFile_putc(this, character);
210 }
211
212 /*! \brief \copybrief XsFile_gets */
213 inline char* gets(char *destination, int maxCount)
214 {
215 return XsFile_gets(this, destination, maxCount);
216 }
217
218 /*! \brief \copybrief XsFile_puts */
219 inline XsResultValue puts(const char *source)
220 {
221 return XsFile_puts(this, source);
222 }
223
224 /*! \brief \copybrief XsFile_seek */
225 inline XsResultValue seek(XsFilePos offset)
226 {
227 return XsFile_seek(this, offset);
228 }
229
230 /*! \brief \copybrief XsFile_seek_r */
231 inline XsResultValue seek_r(XsFilePos offset)
232 {
233 return XsFile_seek_r(this, offset);
234 }
235
236 /*! \brief \copybrief XsFile_tell */
237 inline XsFilePos tell() const
238 {
239 return XsFile_tell(this);
240 }
241
242 /*! \brief \copybrief XsFile_eof */
243 inline bool eof() const
244 {
245 return (0!=XsFile_eof(this));
246 }
247
248 /*! \brief \copybrief XsFile_error */
249 inline XsResultValue error() const
250 {
251 return XsFile_error(this);
252 }
253
254 /*! \brief \copybrief XsFile_fullPath */
255 static XsResultValue fullPath(const XsString &filename, XsString &fullPath)
256 {
257 return XsFile_fullPath(&filename, &fullPath);
258 }
259
260 /*! \brief \copybrief XsFile_getline */
261 inline XsResultValue getline(XsString& line)
262 {
263 return XsFile_getline(this, &line);
264 }
265
266 /*! \brief \copybrief XsFile_getline */
267 inline XsResultValue getline(std::string& line)
268 {
269 XsString tmp;
270 XsResultValue rv = XsFile_getline(this, &tmp);
271 if (rv == XRV_OK)
272 line = tmp.toStdString();
273 return rv;
274 }
275
276 /*! \brief \copybrief XsFile_handle */
277 inline FILE* handle()
278 {
279 return XsFile_handle(this);
280 }
281 private:
282 #endif
283 FILE* m_handle;
284 };
285
286 typedef struct XsFile XsFile;
287
288 #endif