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