]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_file.hxx
Terrasync: implement HTTP service lookup via DNS
[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
43 public:
44
45     /**
46      * Create an instance of SGFile.
47      * When calling the constructor you need to provide a file
48      * name. This file is not opened immediately, but instead will be
49      * opened when the open() method is called.
50      * @param file name of file to open
51      * @param repeat On eof restart at the beginning of the file
52      */
53     SGFile( const std::string& file, int repeat_ = 1 );
54
55     /**
56      * Create an SGFile from an existing, open file-descriptor
57      */
58     SGFile( int existingFd );
59
60     /** Destructor */
61     ~SGFile();
62
63     // open the file based on specified direction
64     bool open( const SGProtocolDir dir );
65
66     // read a block of data of specified size
67     int read( char *buf, int length );
68
69     // read a line of data, length is max size of input buffer
70     int readline( char *buf, int length );
71
72     // write data to a file
73     int write( const char *buf, const int length );
74
75     // write null terminated string to a file
76     int writestring( const char *str );
77
78     // close file
79     bool close();
80
81     /** @return the name of the file being manipulated. */
82     inline std::string get_file_name() const { return file_name; }
83
84     /** @return true of eof conditions exists */
85     virtual bool eof() const { return eof_flag; };
86 };
87
88 #endif // _SG_FILE_HXX