2 * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 * Some definitions copied from somewhere in glibc
33 #define __LITTLE_ENDIAN 1234
34 #define __BIG_ENDIAN 4321
35 #define __PDP_ENDIAN 3412
36 #define __BYTE_ORDER __BIG_ENDIAN
41 * Generic class to process correctly endian-enabled I/O operations
49 * @param input the stream to read from.
50 * @returns the first element readed from the current position of the stream
52 static T
read(FILE *input
)
55 #if __BYTE_ORDER == __BIG_ENDIAN
56 fread(&retval
, sizeof(T
), 1, input
);
58 char *s
= reinterpret_cast<char *>(&retval
);
60 for(int i
= sizeof(T
)-1; i
!= -1; i
--)
62 fread(&(s
[i
]), 1, 1, input
);
69 * Read procedure, C++ I/O version.
70 * @param is the stream to read from.
71 * @returns the first element readed from the current position of the stream
73 static T
read(istream
&is
)
76 #if __BYTE_ORDER == __BIG_ENDIAN
77 is
.read((char *) &retval
, sizeof(T
));
79 char *s
= reinterpret_cast<char *>(&retval
);
81 for(int i
= sizeof(T
)-1; i
!= -1; i
--)
83 is
.read(&(s
[i
]), sizeof(char));
91 * @param output the stream to write to
92 * @param val the value of the generic object to write to the stream
94 static void write(FILE *output
, T
const &val
)
97 #if __BYTE_ORDER == __BIG_ENDIAN
98 fwrite(&val2
, sizeof(T
), 1, output
);
100 char *s
= reinterpret_cast<char *>(&val2
);
102 for(int i
= sizeof(T
)-1; i
!= -1; i
--)
104 fwrite(&(s
[i
]), 1, 1, output
);
110 * Write procedure, C++ I/O version.
111 * @param output the stream to write to
112 * @param val the value of the generic object to write to the stream
114 static void write(ostream
&os
, T
const &val
)
117 #if __BYTE_ORDER == __BIG_ENDIAN
118 os
.write(reinterpret_cast<char *>(&val2
), sizeof(T
));
120 char *s
= reinterpret_cast<char *>(&val2
);
122 for(int i
= sizeof(T
)-1; i
!= -1; i
--)
124 os
.write(&(s
[i
]), sizeof(char));