Moving non-unicode apertium to branches
[apertium.git] / lttoolbox-unicode / lttoolbox / endian_util.h
blobdb8f30b000c8a31b94adaa69e6627d825a689796
1 /*
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
17 * 02111-1307, USA.
19 #ifndef _ENDIANUTIL_
20 #define _ENDIANUTIL_
22 #include <cctype>
23 #include <cstdio>
24 #include <iostream>
26 using namespace std;
29 /**
30 * Some definitions copied from somewhere in glibc
32 #ifndef __BYTE_ORDER
33 #define __LITTLE_ENDIAN 1234
34 #define __BIG_ENDIAN 4321
35 #define __PDP_ENDIAN 3412
36 #define __BYTE_ORDER __BIG_ENDIAN
37 #endif
40 /**
41 * Generic class to process correctly endian-enabled I/O operations
43 template<class T>
44 class EndianUtil
46 public:
47 /**
48 * Read procedure.
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)
54 T retval;
55 #if __BYTE_ORDER == __BIG_ENDIAN
56 fread(&retval, sizeof(T), 1, input);
57 #else
58 char *s = reinterpret_cast<char *>(&retval);
60 for(int i = sizeof(T)-1; i != -1; i--)
62 fread(&(s[i]), 1, 1, input);
64 #endif
65 return retval;
68 /**
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)
75 T retval;
76 #if __BYTE_ORDER == __BIG_ENDIAN
77 is.read((char *) &retval, sizeof(T));
78 #else
79 char *s = reinterpret_cast<char *>(&retval);
81 for(int i = sizeof(T)-1; i != -1; i--)
83 is.read(&(s[i]), sizeof(char));
85 #endif
86 return retval;
89 /**
90 * Write procedure.
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)
96 T val2 = val;
97 #if __BYTE_ORDER == __BIG_ENDIAN
98 fwrite(&val2, sizeof(T), 1, output);
99 #else
100 char *s = reinterpret_cast<char *>(&val2);
102 for(int i = sizeof(T)-1; i != -1; i--)
104 fwrite(&(s[i]), 1, 1, output);
106 #endif
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)
116 T val2 = val;
117 #if __BYTE_ORDER == __BIG_ENDIAN
118 os.write(reinterpret_cast<char *>(&val2), sizeof(T));
119 #else
120 char *s = reinterpret_cast<char *>(&val2);
122 for(int i = sizeof(T)-1; i != -1; i--)
124 os.write(&(s[i]), sizeof(char));
126 #endif
130 #endif