]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_file.cxx
Introduce SGBinaryFile
[simgear.git] / simgear / io / sg_file.cxx
1 // sg_file.cxx -- File I/O routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/compiler.h>
25
26 #include <string>
27
28 #ifdef _WIN32
29 #  include <io.h>
30 #endif
31
32 #include <cstring>
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37
38 #if !defined(_MSC_VER)
39 # include <unistd.h>
40 #endif
41
42 #include <simgear/misc/stdint.hxx>
43 #include <simgear/debug/logstream.hxx>
44
45 #include "sg_file.hxx"
46
47
48 SGFile::SGFile(const std::string &file, int repeat_, int extraoflags_ )
49     : file_name(file), fp(-1), eof_flag(true), repeat(repeat_), iteration(0),
50       extraoflags(extraoflags_)
51 {
52     set_type( sgFileType );
53 }
54
55 SGFile::SGFile( int existingFd ) :
56     fp(existingFd),
57     eof_flag(false),
58     repeat(1),
59     iteration(0)
60 {
61     set_type( sgFileType );
62 }
63
64 SGFile::~SGFile() {
65 }
66
67
68 // open the file based on specified direction
69 bool SGFile::open( const SGProtocolDir d ) {
70     set_dir( d );
71
72     if ( get_dir() == SG_IO_OUT ) {
73 #ifdef _WIN32
74         int mode = _S_IREAD | _S_IWRITE;
75 #else
76         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
77 #endif
78         fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | extraoflags, mode );
79     } else if ( get_dir() == SG_IO_IN ) {
80         fp = ::open( file_name.c_str(), O_RDONLY | extraoflags );
81     } else {
82         SG_LOG( SG_IO, SG_ALERT, 
83                 "Error:  bidirection mode not available for files." );
84         return false;
85     }
86
87     if ( fp == -1 ) {
88         SG_LOG( SG_IO, SG_ALERT, "Error opening file: " << file_name );
89         return false;
90     }
91
92     eof_flag = false;
93     return true;
94 }
95
96
97 // read a block of data of specified size
98 int SGFile::read( char *buf, int length ) {
99     // read a chunk
100     ssize_t result = ::read( fp, buf, length );
101     if ( length > 0 && result == 0 ) {
102         if (repeat < 0 || iteration < repeat - 1) {
103             iteration++;
104             // loop reading the file, unless it is empty
105             off_t fileLen = ::lseek(fp, 0, SEEK_CUR);
106             if (fileLen == 0) {
107                 eof_flag = true;
108                 return 0;
109             } else {
110                 ::lseek(fp, 0, SEEK_SET);
111                 return ::read(fp, buf, length);
112             }
113         } else {
114             eof_flag = true;
115         }
116     }
117     return result;
118 }
119
120
121 // read a line of data, length is max size of input buffer
122 int SGFile::readline( char *buf, int length ) {
123     // save our current position
124     int pos = lseek( fp, 0, SEEK_CUR );
125
126     // read a chunk
127     ssize_t result = ::read( fp, buf, length );
128     if ( length > 0 && result == 0 ) {
129         if ((repeat < 0 || iteration < repeat - 1) && pos != 0) {
130             iteration++;
131             pos = ::lseek(fp, 0, SEEK_SET);
132             result = ::read(fp, buf, length);
133         } else {
134             eof_flag = true;
135         }
136     }
137
138     // find the end of line and reset position
139     int i;
140     for ( i = 0; i < result && buf[i] != '\n'; ++i );
141     if ( buf[i] == '\n' ) {
142         result = i + 1;
143     } else {
144         result = i;
145     }
146     lseek( fp, pos + result, SEEK_SET );
147     
148     // just in case ...
149     buf[ result ] = '\0';
150
151     return result;
152 }
153
154
155 // write data to a file
156 int SGFile::write( const char *buf, const int length ) {
157     int result = ::write( fp, buf, length );
158     if ( result != length ) {
159         SG_LOG( SG_IO, SG_ALERT, "Error writing data: " << file_name );
160     }
161
162     return result;
163 }
164
165
166 // write null terminated string to a file
167 int SGFile::writestring( const char *str ) {
168     int length = std::strlen( str );
169     return write( str, length );
170 }
171
172
173 // close the port
174 bool SGFile::close() {
175     if ( ::close( fp ) == -1 ) {
176         return false;
177     }
178
179     eof_flag = true;
180     return true;
181 }
182
183 SGBinaryFile::SGBinaryFile( const std::string& file, int repeat_ ) :
184 #ifdef _WIN32
185     SGFile(file,repeat_, _O_BINARY)
186 #else
187     SGFile(file,repeat_, 0)
188 #endif
189 {
190 }