1 /*
2  * Seven Kingdoms: Ancient Adversaries
3  *
4  * Copyright 2010 Unavowed <unavowed@vexillium.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 #ifndef OUTPUT_STREAM_H
21 #define OUTPUT_STREAM_H
22 
23 #include <stdint.h>
24 #include <stdio.h>
25 
26 class OutputStream
27 {
28 public:
~OutputStream()29    virtual ~OutputStream() {}
30    virtual long write(const void *data, long length) = 0;
31    virtual bool seek(long offset, int whence) = 0;
32    virtual long tell() = 0;
33    virtual void close() = 0;
34 };
35 
36 /* Writes a little-endian integer */
37 template <typename T>
write_le_integer(OutputStream * os,T val)38 bool write_le_integer(OutputStream *os, T val)
39 {
40    unsigned char c;
41 
42    for (int n = 0; n < static_cast<int>(sizeof(T)); n++)
43    {
44       c = val >> (8 * n);
45 
46       if (os->write(&c, 1) != 1)
47 	 return false;
48    }
49 
50    return true;
51 }
52 
53 template <typename T>
write_le(OutputStream * os,T val)54 bool write_le(OutputStream *os, T val)
55 {
56    return write_le_integer<T>(os, val);
57 }
58 
59 template <> bool write_le<float>(OutputStream *os, float val);
60 template <> bool write_le<double>(OutputStream *os, double val);
61 
62 /* vim: set ts=8 sw=3: */
63 #endif
64