1 // sg_file.cxx -- File I/O routines
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
24 #include <simgear/compiler.h>
28 #if defined(_MSC_VER) || defined(__MINGW32__)
34 #include <simgear/misc/stdint.hxx>
35 #include <simgear/debug/logstream.hxx>
37 #include "sg_file.hxx"
42 SGFile::SGFile(const string &file, bool repeat_)
43 : file_name(file), fp(-1), eof_flag(true), repeat(repeat_)
45 set_type( sgFileType );
53 // open the file based on specified direction
54 bool SGFile::open( const SGProtocolDir d ) {
57 if ( get_dir() == SG_IO_OUT ) {
58 #if defined(_MSC_VER) || defined(__MINGW32__)
61 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
63 fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode );
64 } else if ( get_dir() == SG_IO_IN ) {
65 fp = ::open( file_name.c_str(), O_RDONLY );
67 SG_LOG( SG_IO, SG_ALERT,
68 "Error: bidirection mode not available for files." );
73 SG_LOG( SG_IO, SG_ALERT, "Error opening file: " << file_name );
82 // read a block of data of specified size
83 int SGFile::read( char *buf, int length ) {
85 ssize_t result = ::read( fp, buf, length );
86 if ( length > 0 && result == 0 ) {
88 // loop reading the file, unless it is empty
89 off_t fileLen = ::lseek(fp, 0, SEEK_CUR);
94 ::lseek(fp, 0, SEEK_SET);
95 return ::read(fp, buf, length);
105 // read a line of data, length is max size of input buffer
106 int SGFile::readline( char *buf, int length ) {
107 // save our current position
108 int pos = lseek( fp, 0, SEEK_CUR );
111 ssize_t result = ::read( fp, buf, length );
112 if ( length > 0 && result == 0 ) {
113 if (repeat && pos != 0) {
114 pos = ::lseek(fp, 0, SEEK_SET);
115 result = ::read(fp, buf, length);
121 // find the end of line and reset position
123 for ( i = 0; i < result && buf[i] != '\n'; ++i );
124 if ( buf[i] == '\n' ) {
129 lseek( fp, pos + result, SEEK_SET );
132 buf[ result ] = '\0';
138 // write data to a file
139 int SGFile::write( const char *buf, const int length ) {
140 int result = ::write( fp, buf, length );
141 if ( result != length ) {
142 SG_LOG( SG_IO, SG_ALERT, "Error writing data: " << file_name );
149 // write null terminated string to a file
150 int SGFile::writestring( const char *str ) {
151 int length = std::strlen( str );
152 return write( str, length );
157 bool SGFile::close() {
158 if ( ::close( fp ) == -1 ) {