itom  4.1.0
fileUtils.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom and its software development toolkit (SDK).
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  In addition, as a special exception, the Institut fuer Technische
15  Optik (ITO) gives you certain additional rights.
16  These rights are described in the ITO LGPL Exception version 1.0,
17  which can be found in the file LGPL_EXCEPTION.txt in this package.
18 
19  itom is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22  General Public Licence for more details.
23 
24  You should have received a copy of the GNU Library General Public License
25  along with itom. If not, see <http://www.gnu.org/licenses/>.
26 *********************************************************************** */
27 
28 #ifndef FILEUTILS_H
29 #define FILEUTILS_H
30 
31 #include "retVal.h"
32 #include "typeDefs.h"
33 #include <qendian.h>
34 #include <qiodevice.h>
35 
36 
37 #if !defined(Q_MOC_RUN) || defined(ITOMCOMMONQT_MOC) //only moc this file in itomCommonQtLib but not in other libraries or executables linking against this itomCommonQtLib
38 
39 namespace ito
40 {
41  //all these methods increment the input pointer by the size of the returned number
42  inline ito::int8 getInt8(const uchar **ppval);
43  inline ito::uint8 getUInt8(const uchar **ppval);
44  inline ito::int16 getInt16LE(const uchar **ppval);
45  inline ito::int16 getInt16BE(const uchar **ppval);
46  inline ito::uint16 getUInt16LE(const uchar **ppval);
47  inline ito::uint16 getUInt16BE(const uchar **ppval);
48  inline ito::int32 getInt32LE(const uchar **ppval);
49  inline ito::int16 getInt16BE(const uchar **ppval);
50  inline ito::uint32 getUInt32LE(const uchar **ppval);
51  inline ito::uint32 getUInt32BE(const uchar **ppval);
52  inline qint64 getInt64LE(const uchar **ppval);
53  inline qint64 getInt64BE(const uchar **ppval);
54  inline quint64 getUInt64LE(const uchar **ppval);
55  inline quint64 getUInt64BE(const uchar **ppval);
56  inline ito::float32 getFloat32LE(const uchar **ppval);
57  inline ito::float32 getFloat32BE(const uchar **ppval);
58  inline ito::float64 getFloat64LE(const uchar **ppval);
59  inline ito::float64 getFloat64BE(const uchar **ppval);
60 
61  inline ito::int16 swapInt16(ito::int16 val);
62  inline ito::uint16 swapUInt16(ito::uint16 val);
63  inline ito::int32 swapInt32(ito::int32 val);
64  inline ito::uint32 swapUInt32(ito::uint32 val);
65  inline ito::float32 swapFloat32(ito::float32 val);
66  inline ito::float64 swapFloat64(ito::float64 val);
67 
69  ito::RetVal ITOMCOMMONQT_EXPORT readFromDevice(QIODevice *device, char *data, qint64 numBytes);
70 
71  //sources
72 
73  //--------------------------------------------------------------------------------------------
74  inline ito::int8 getInt8(const uchar **ppval)
75  {
76  const ito::int8 *pval = (const ito::int8*)(*ppval);
77  ito::int8 v = *pval;
78  *ppval += sizeof(ito::uint8);
79  return v;
80  }
81 
82  //--------------------------------------------------------------------------------------------
83  inline ito::uint8 getUInt8(const uchar **ppval)
84  {
85  const ito::uint8 *pval = (const ito::uint8*)(*ppval);
86  ito::uint8 v = *pval;
87  *ppval += sizeof(ito::uint8);
88  return v;
89  }
90 
91  //--------------------------------------------------------------------------------------------
92  inline ito::int16 getInt16LE(const uchar **ppval)
93  {
94  const ito::int16 *pval = (const ito::int16*)(*ppval);
95  ito::int16 v = qFromLittleEndian(*pval);
96  *ppval += sizeof(ito::int16);
97  return v;
98  }
99 
100  //--------------------------------------------------------------------------------------------
101  inline ito::int16 getInt16BE(const uchar **ppval)
102  {
103  const ito::int16 *pval = (const ito::int16*)(*ppval);
104  ito::int16 v = qFromBigEndian(*pval);
105  *ppval += sizeof(ito::int16);
106  return v;
107  }
108 
109  //--------------------------------------------------------------------------------------------
110  inline ito::uint16 getUInt16LE(const uchar **ppval)
111  {
112  const ito::uint16 *pval = (const ito::uint16*)(*ppval);
113  ito::uint16 v = qFromLittleEndian(*pval);
114  *ppval += sizeof(ito::uint16);
115  return v;
116  }
117 
118  //--------------------------------------------------------------------------------------------
119  inline ito::uint16 getUInt16BE(const uchar **ppval)
120  {
121  const ito::uint16 *pval = (const ito::uint16*)(*ppval);
122  ito::uint16 v = qFromBigEndian(*pval);
123  *ppval += sizeof(ito::uint16);
124  return v;
125  }
126 
127  //--------------------------------------------------------------------------------------------
128  inline ito::int32 getInt32LE(const uchar **ppval)
129  {
130  const ito::int32 *pval = (const ito::int32*)(*ppval);
131  ito::int32 v = qFromLittleEndian(*pval);
132  *ppval += sizeof(ito::int32);
133  return v;
134  }
135 
136  //--------------------------------------------------------------------------------------------
137  inline ito::int32 getInt32BE(const uchar **ppval)
138  {
139  const ito::int32 *pval = (const ito::int32*)(*ppval);
140  ito::int32 v = qFromBigEndian(*pval);
141  *ppval += sizeof(ito::int32);
142  return v;
143  }
144 
145  //--------------------------------------------------------------------------------------------
146  inline ito::uint32 getUInt32LE(const uchar **ppval)
147  {
148  const ito::uint32 *pval = (const ito::uint32*)(*ppval);
149  ito::uint32 v = qFromLittleEndian(*pval);
150  *ppval += sizeof(ito::uint32);
151  return v;
152  }
153 
154  //--------------------------------------------------------------------------------------------
155  inline ito::uint32 getUInt32BE(const uchar **ppval)
156  {
157  const ito::uint32 *pval = (const ito::uint32*)(*ppval);
158  ito::uint32 v = qFromBigEndian(*pval);
159  *ppval += sizeof(ito::uint32);
160  return v;
161  }
162 
163  //--------------------------------------------------------------------------------------------
164  inline qint64 getInt64LE(const uchar **ppval)
165  {
166  const qint64 *pval = (const qint64*)(*ppval);
167  qint64 v = qFromLittleEndian(*pval);
168  *ppval += sizeof(qint64);
169  return v;
170  }
171 
172  //--------------------------------------------------------------------------------------------
173  inline qint64 getInt64BE(const uchar **ppval)
174  {
175  const qint64 *pval = (const qint64*)(*ppval);
176  qint64 v = qFromBigEndian(*pval);
177  *ppval += sizeof(qint64);
178  return v;
179  }
180 
181  //--------------------------------------------------------------------------------------------
182  inline quint64 getUInt64LE(const uchar **ppval)
183  {
184  const quint64 *pval = (const quint64*)(*ppval);
185  quint64 v = qFromLittleEndian(*pval);
186  *ppval += sizeof(quint64);
187  return v;
188  }
189 
190  //--------------------------------------------------------------------------------------------
191  inline quint64 getUInt64BE(const uchar **ppval)
192  {
193  const quint64 *pval = (const quint64*)(*ppval);
194  quint64 v = qFromBigEndian(*pval);
195  *ppval += sizeof(quint64);
196  return v;
197  }
198 
199  //--------------------------------------------------------------------------------------------
200  inline ito::float32 getFloat32LE(const uchar **ppval)
201  {
202  union
203  {
204  ito::uint8 pp[4];
205  ito::float32 f;
206  } z;
207 
208 #if (Q_BYTE_ORDER == Q_LITTLE_ENDIAN)
209  memcpy(z.pp, *ppval, sizeof(ito::float32));
210 #else
211  z.pp[0] = (*ppval)[3];
212  z.pp[1] = (*ppval)[2];
213  z.pp[2] = (*ppval)[1];
214  z.pp[3] = (*ppval)[0];
215 #endif
216  *ppval += sizeof(ito::float32);
217  return z.f;
218  }
219 
220  //--------------------------------------------------------------------------------------------
221  inline ito::float32 getFloat32BE(const uchar **ppval)
222  {
223  union
224  {
225  ito::uint8 pp[4];
226  ito::float32 f;
227  } z;
228 
229 #if (Q_BYTE_ORDER == Q_BIG_ENDIAN)
230  memcpy(z.pp, *ppval, sizeof(ito::float32));
231 #else
232  z.pp[0] = (*ppval)[3];
233  z.pp[1] = (*ppval)[2];
234  z.pp[2] = (*ppval)[1];
235  z.pp[3] = (*ppval)[0];
236 #endif
237  *ppval += sizeof(ito::float32);
238  return z.f;
239  }
240 
241  //--------------------------------------------------------------------------------------------
242  inline ito::float64 getFloat64LE(const uchar **ppval)
243  {
244  union
245  {
246  ito::uint8 pp[8];
247  ito::float64 d;
248  } z;
249 
250 #if (Q_BYTE_ORDER == Q_LITTLE_ENDIAN)
251  memcpy(z.pp, *ppval, sizeof(ito::float64));
252 #else
253  z.pp[0] = (*ppval)[7];
254  z.pp[1] = (*ppval)[6];
255  z.pp[2] = (*ppval)[5];
256  z.pp[3] = (*ppval)[4];
257  z.pp[4] = (*ppval)[3];
258  z.pp[5] = (*ppval)[2];
259  z.pp[6] = (*ppval)[1];
260  z.pp[7] = (*ppval)[0];
261 #endif
262  *ppval += sizeof(ito::float64);
263  return z.d;
264  }
265 
266  //--------------------------------------------------------------------------------------------
267  inline ito::float64 getFloat64BE(const uchar **ppval)
268  {
269  union
270  {
271  ito::uint8 pp[8];
272  ito::float64 d;
273  } z;
274 
275 #if (Q_BYTE_ORDER == Q_BIG_ENDIAN)
276  memcpy(z.pp, *ppval, sizeof(ito::float64));
277 #else
278  z.pp[0] = (*ppval)[7];
279  z.pp[1] = (*ppval)[6];
280  z.pp[2] = (*ppval)[5];
281  z.pp[3] = (*ppval)[4];
282  z.pp[4] = (*ppval)[3];
283  z.pp[5] = (*ppval)[2];
284  z.pp[6] = (*ppval)[1];
285  z.pp[7] = (*ppval)[0];
286 #endif
287  *ppval += sizeof(ito::float64);
288  return z.d;
289  }
290 
291 
292 
293 
294 
295  //--------------------------------------------------------------------------------------------
296  inline ito::uint16 swapUInt16(ito::uint16 val)
297  {
298  union s
299  {
300  char sa[2];
301  ito::uint16 res;
302  } temp;
303  temp.res = val;
304  s uint16out;
305  for (int teller = 0; teller<2; ++teller){
306  uint16out.sa[teller] = temp.sa[1 - teller];
307  }
308  return uint16out.res;
309  }
310 
311  //--------------------------------------------------------------------------------------------
312  inline ito::int16 swapInt16(ito::int16 val)
313  {
314  union s
315  {
316  char sa[2];
317  ito::int16 res;
318  } temp;
319  temp.res = val;
320  s int16out;
321  for (int teller = 0; teller<2; ++teller){
322  int16out.sa[teller] = temp.sa[1 - teller];
323  }
324  return int16out.res;
325  }
326 
327  //--------------------------------------------------------------------------------------------
328  inline ito::uint32 swapUInt32(ito::uint32 val)
329  {
330  union s
331  {
332  char sa[4];
333  ito::uint32 res;
334  } temp;
335  temp.res = val;
336  s uint32out;
337  for (int teller = 0; teller<4; ++teller){
338  uint32out.sa[teller] = temp.sa[3 - teller];
339  }
340  return uint32out.res;
341  }
342 
343  //--------------------------------------------------------------------------------------------
344  inline ito::int32 swapInt32(ito::int32 val)
345  {
346  union s
347  {
348  char sa[4];
349  ito::int32 res;
350  } temp;
351  temp.res = val;
352  s int32out;
353  for (int teller = 0; teller<4; ++teller){
354  int32out.sa[teller] = temp.sa[3 - teller];
355  }
356  return int32out.res;
357  }
358 
359  //--------------------------------------------------------------------------------------------
360  inline ito::float32 swapFloat32(ito::float32 val)
361  {
362  union s
363  {
364  char pp[4];
365  ito::float32 f;
366  } temp;
367 
368  temp.f = val;
369  s float32out;
370  float32out.pp[0] = temp.pp[3];
371  float32out.pp[1] = temp.pp[2];
372  float32out.pp[2] = temp.pp[1];
373  float32out.pp[3] = temp.pp[0];
374  return float32out.f;
375  }
376 
377  //--------------------------------------------------------------------------------------------
378  inline ito::float64 swapFloat64(ito::float64 val)
379  {
380  union s
381  {
382  char pp[8];
383  ito::float64 f;
384  } temp;
385 
386  temp.f = val;
387  s float64out;
388  float64out.pp[0] = temp.pp[7];
389  float64out.pp[1] = temp.pp[6];
390  float64out.pp[2] = temp.pp[5];
391  float64out.pp[3] = temp.pp[4];
392  float64out.pp[4] = temp.pp[3];
393  float64out.pp[5] = temp.pp[2];
394  float64out.pp[6] = temp.pp[1];
395  float64out.pp[7] = temp.pp[0];
396  return float64out.f;
397  }
398 
399 }; // end namespace ito
400 
401 #endif //#if !defined(Q_MOC_RUN) || defined(ITOMCOMMONQT_MOC)
402 
403 #endif
Class for managing status values (like errors or warning)
Definition: retVal.h:54
Definition: apiFunctionsGraph.cpp:39
ito::float64 swapFloat64(ito::float64 val)
reads exactly numBytes from device into data and returns an error if less or no data is available ...
Definition: fileUtils.h:378