Imported Upstream version 0.12.7
[bowtie.git] / word_io.h
1 #ifndef WORD_IO_H_
2 #define WORD_IO_H_
3
4 #include <stdint.h>
5 #include <iostream>
6 #include <fstream>
7 #include "assert_helpers.h"
8 #include "endian_swap.h"
9
10 /**
11  * Write a 32-bit unsigned to an output stream being careful to
12  * re-endianize if caller-requested endianness differs from current
13  * host.
14  */
15 static inline void writeU32(std::ostream& out, uint32_t x, bool toBigEndian) {
16         uint32_t y = endianizeU32(x, toBigEndian);
17         out.write((const char*)&y, 4);
18 }
19
20 /**
21  * Write a 32-bit unsigned to an output stream using the native
22  * endianness.
23  */
24 static inline void writeU32(std::ostream& out, uint32_t x) {
25         out.write((const char*)&x, 4);
26 }
27
28 /**
29  * Write a 32-bit signed int to an output stream being careful to
30  * re-endianize if caller-requested endianness differs from current
31  * host.
32  */
33 static inline void writeI32(std::ostream& out, int32_t x, bool toBigEndian) {
34         int32_t y = endianizeI32(x, toBigEndian);
35         out.write((const char*)&y, 4);
36 }
37
38 /**
39  * Write a 32-bit unsigned to an output stream using the native
40  * endianness.
41  */
42 static inline void writeI32(std::ostream& out, int32_t x) {
43         out.write((const char*)&x, 4);
44 }
45
46 /**
47  * Read a 32-bit unsigned from an input stream, inverting endianness
48  * if necessary.
49  */
50 static inline uint32_t readU32(std::istream& in, bool swap) {
51         uint32_t x;
52         in.read((char *)&x, 4);
53         assert_eq(4, in.gcount());
54         if(swap) {
55                 return endianSwapU32(x);
56         } else {
57                 return x;
58         }
59 }
60
61 /**
62  * Read a 32-bit unsigned from a file descriptor, optionally inverting
63  * endianness.
64  */
65 static inline uint32_t readU32(int in, bool swap) {
66         uint32_t x;
67         if(read(in, (void *)&x, 4) != 4) {
68                 assert(false);
69         }
70         if(swap) {
71                 return endianSwapU32(x);
72         } else {
73                 return x;
74         }
75 }
76
77 /**
78  * Read a 32-bit unsigned from a FILE*, optionally inverting
79  * endianness.
80  */
81 static inline uint32_t readU32(FILE* in, bool swap) {
82         uint32_t x;
83         if(fread((void *)&x, 1, 4, in) != 4) {
84                 assert(false);
85         }
86         if(swap) {
87                 return endianSwapU32(x);
88         } else {
89                 return x;
90         }
91 }
92
93
94 /**
95  * Read a 32-bit signed from an input stream, inverting endianness
96  * if necessary.
97  */
98 static inline int32_t readI32(std::istream& in, bool swap) {
99         int32_t x;
100         in.read((char *)&x, 4);
101         assert_eq(4, in.gcount());
102         if(swap) {
103                 return endianSwapI32(x);
104         } else {
105                 return x;
106         }
107 }
108
109 /**
110  * Read a 32-bit unsigned from a file descriptor, optionally inverting
111  * endianness.
112  */
113 static inline uint32_t readI32(int in, bool swap) {
114         int32_t x;
115         if(read(in, (void *)&x, 4) != 4) {
116                 assert(false);
117         }
118         if(swap) {
119                 return endianSwapI32(x);
120         } else {
121                 return x;
122         }
123 }
124
125 /**
126  * Read a 32-bit unsigned from a FILE*, optionally inverting
127  * endianness.
128  */
129 static inline uint32_t readI32(FILE* in, bool swap) {
130         int32_t x;
131         if(fread((void *)&x, 1, 4, in) != 4) {
132                 assert(false);
133         }
134         if(swap) {
135                 return endianSwapI32(x);
136         } else {
137                 return x;
138         }
139 }
140
141
142 #endif /*WORD_IO_H_*/