FEDEM Model Database  R8.0
Source code of the FEDEM model database
FiSwappedIO.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
13 #ifndef FI_SWAPPED_IO_H
14 #define FI_SWAPPED_IO_H
15 
16 #include "FFaLib/FFaOS/FFaIO.H"
17 
18 
19 namespace Fi
20 {
22  template<class T> size_t readSwapped(char* q, T&, FT_FILE fd)
23  {
24  static char p[8];
25  int nChar = sizeof(T);
26  size_t nBytes = FT_read(p,nChar,1,fd);
27  for (int i = nChar-1; i > 0; i--, q++)
28  *q = p[i];
29  *q = p[0];
30 
31  return nBytes;
32  }
33 
35  template<class T> size_t writeSwapped(const T val, FT_FILE fd)
36  {
37  T swappedValue;
38  int nChar = sizeof(T);
39  char* p = (char*)&val;
40  char* q = (char*)(&swappedValue);
41  for (int i = nChar-1; i > 0; i--, q++)
42  *q = p[i];
43  *q = p[0];
44 
45  return FT_write(&swappedValue,nChar,1,fd);
46  }
47 }
48 
49 #endif
Functions and data type for direct access of large binary files.
#define FT_FILE
File pointer.
Definition: FFaIO.H:136
#define FT_read(buf, n, m, f)
Reads an array from file.
Definition: FFaIO.H:118
#define FT_write(buf, n, m, f)
Writes an array to file.
Definition: FFaIO.H:120
File handling utilities.
Definition: FiSwappedIO.H:20
size_t writeSwapped(const T val, FT_FILE fd)
Writes the data word val with byte swapping.
Definition: FiSwappedIO.H:35
size_t readSwapped(char *q, T &, FT_FILE fd)
Reads a data word of type T with byte swapping.
Definition: FiSwappedIO.H:22