]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_file.hxx
Introduce SGBinaryFile
[simgear.git] / simgear / io / sg_file.hxx
1 ///@file
2 /// File I/O routines.
3 //
4 // Written by Curtis Olson, started November 1999.
5 //
6 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #ifndef _SG_FILE_HXX
23 #define _SG_FILE_HXX
24
25 #include <simgear/compiler.h>
26 #include "iochannel.hxx"
27
28 #include <string>
29
30 /**
31  * A file I/O class based on SGIOChannel.
32  */
33 class SGFile : public SGIOChannel {
34
35     std::string file_name;
36     int fp;
37     bool eof_flag;
38     // Number of repetitions to play. -1 means loop infinitely.
39     const int repeat;
40     int iteration;              // number of current repetition,
41                                 // starting at 0
42     int extraoflags;
43
44 public:
45
46     /**
47      * Create an instance of SGFile.
48      * When calling the constructor you need to provide a file
49      * name. This file is not opened immediately, but instead will be
50      * opened when the open() method is called.
51      * @param file name of file to open
52      * @param repeat On eof restart at the beginning of the file
53      */
54     SGFile( const std::string& file, int repeat_ = 1, int extraoflags = 0);
55
56     /**
57      * Create an SGFile from an existing, open file-descriptor
58      */
59     SGFile( int existingFd );
60
61     /** Destructor */
62     ~SGFile();
63
64     // open the file based on specified direction
65     bool open( const SGProtocolDir dir );
66
67     // read a block of data of specified size
68     int read( char *buf, int length );
69
70     // read a line of data, length is max size of input buffer
71     int readline( char *buf, int length );
72
73     // write data to a file
74     int write( const char *buf, const int length );
75
76     // write null terminated string to a file
77     int writestring( const char *str );
78
79     // close file
80     bool close();
81
82     /** @return the name of the file being manipulated. */
83     inline std::string get_file_name() const { return file_name; }
84
85     /** @return true of eof conditions exists */
86     virtual bool eof() const { return eof_flag; };
87 };
88
89 class SGBinaryFile : public SGFile {
90 public:
91     SGBinaryFile( const std::string& file, int repeat_ = 1 );
92 };
93
94 #endif // _SG_FILE_HXX