]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_file.cxx
Fix VS2010 lack of fminf
[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_)
49     : file_name(file), fp(-1), eof_flag(true), repeat(repeat_), iteration(0)
50 {
51     set_type( sgFileType );
52 }
53
54 SGFile::SGFile( int existingFd ) :
55     fp(existingFd),
56     eof_flag(false),
57     repeat(1),
58     iteration(0)
59 {
60     set_type( sgFileType );
61 }
62
63 SGFile::~SGFile() {
64 }
65
66
67 // open the file based on specified direction
68 bool SGFile::open( const SGProtocolDir d ) {
69     set_dir( d );
70
71     if ( get_dir() == SG_IO_OUT ) {
72 #ifdef _WIN32
73         int mode = 00666;
74 #else
75         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
76 #endif
77         fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode );
78     } else if ( get_dir() == SG_IO_IN ) {
79         fp = ::open( file_name.c_str(), O_RDONLY );
80     } else {
81         SG_LOG( SG_IO, SG_ALERT, 
82                 "Error:  bidirection mode not available for files." );
83         return false;
84     }
85
86     if ( fp == -1 ) {
87         SG_LOG( SG_IO, SG_ALERT, "Error opening file: " << file_name );
88         return false;
89     }
90
91     eof_flag = false;
92     return true;
93 }
94
95
96 // read a block of data of specified size
97 int SGFile::read( char *buf, int length ) {
98     // read a chunk
99     ssize_t result = ::read( fp, buf, length );
100     if ( length > 0 && result == 0 ) {
101         if (repeat < 0 || iteration < repeat - 1) {
102             iteration++;
103             // loop reading the file, unless it is empty
104             off_t fileLen = ::lseek(fp, 0, SEEK_CUR);
105             if (fileLen == 0) {
106                 eof_flag = true;
107                 return 0;
108             } else {
109                 ::lseek(fp, 0, SEEK_SET);
110                 return ::read(fp, buf, length);
111             }
112         } else {
113             eof_flag = true;
114         }
115     }
116     return result;
117 }
118
119
120 // read a line of data, length is max size of input buffer
121 int SGFile::readline( char *buf, int length ) {
122     // save our current position
123     int pos = lseek( fp, 0, SEEK_CUR );
124
125     // read a chunk
126     ssize_t result = ::read( fp, buf, length );
127     if ( length > 0 && result == 0 ) {
128         if ((repeat < 0 || iteration < repeat - 1) && pos != 0) {
129             iteration++;
130             pos = ::lseek(fp, 0, SEEK_SET);
131             result = ::read(fp, buf, length);
132         } else {
133             eof_flag = true;
134         }
135     }
136
137     // find the end of line and reset position
138     int i;
139     for ( i = 0; i < result && buf[i] != '\n'; ++i );
140     if ( buf[i] == '\n' ) {
141         result = i + 1;
142     } else {
143         result = i;
144     }
145     lseek( fp, pos + result, SEEK_SET );
146     
147     // just in case ...
148     buf[ result ] = '\0';
149
150     return result;
151 }
152
153
154 // write data to a file
155 int SGFile::write( const char *buf, const int length ) {
156     int result = ::write( fp, buf, length );
157     if ( result != length ) {
158         SG_LOG( SG_IO, SG_ALERT, "Error writing data: " << file_name );
159     }
160
161     return result;
162 }
163
164
165 // write null terminated string to a file
166 int SGFile::writestring( const char *str ) {
167     int length = std::strlen( str );
168     return write( str, length );
169 }
170
171
172 // close the port
173 bool SGFile::close() {
174     if ( ::close( fp ) == -1 ) {
175         return false;
176     }
177
178     eof_flag = true;
179     return true;
180 }