]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_file.cxx
Added a touch of error checking to the screen dump routine, i.e. don't
[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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/compiler.h>
25
26 #include STL_STRING
27
28 #ifdef _MSC_VER
29 #  include <io.h>
30 #endif
31
32 #include <simgear/debug/logstream.hxx>
33
34 #include "sg_file.hxx"
35
36 FG_USING_STD(string);
37
38
39 SGFile::SGFile( const string &file) {
40     set_type( sgFileType );
41     file_name = file;
42 }
43
44
45 SGFile::~SGFile() {
46 }
47
48
49 // open the file based on specified direction
50 bool SGFile::open( SGProtocolDir dir ) {
51     if ( dir == SG_IO_OUT ) {
52 #ifdef _MSC_VER
53         fp = _open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
54                         00666 );
55 #else
56         fp = std::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
57                         S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | 
58                         S_IROTH | S_IWOTH );
59 #endif
60     } else if ( dir == SG_IO_IN ) {
61 #ifdef _MSC_VER
62         fp = _open( file_name.c_str(), O_RDONLY );
63 #else
64         fp = std::open( file_name.c_str(), O_RDONLY );
65 #endif
66     } else {
67         FG_LOG( FG_IO, FG_ALERT, 
68                 "Error:  bidirection mode not available for files." );
69         return false;
70     }
71
72     if ( fp == -1 ) {
73         FG_LOG( FG_IO, FG_ALERT, "Error opening file: " << file_name );
74         return false;
75     }
76
77     return true;
78 }
79
80
81 // read a block of data of specified size
82 int SGFile::read( char *buf, int length ) {
83     // read a chunk
84 #ifdef _MSC_VER
85     int result = _read( fp, buf, length );
86 #else
87     int result = std::read( fp, buf, length );
88 #endif
89
90     return result;
91 }
92
93
94 // read a line of data, length is max size of input buffer
95 int SGFile::readline( char *buf, int length ) {
96     // save our current position
97     int pos = lseek( fp, 0, SEEK_CUR );
98
99     // read a chunk
100 #ifdef _MSC_VER
101     int result = _read( fp, buf, length );
102 #else
103     int result = std::read( fp, buf, length );
104 #endif
105
106     // find the end of line and reset position
107     int i;
108     for ( i = 0; i < result && buf[i] != '\n'; ++i );
109     if ( buf[i] == '\n' ) {
110         result = i + 1;
111     } else {
112         result = i;
113     }
114     lseek( fp, pos + result, SEEK_SET );
115     
116     // just in case ...
117     buf[ result ] = '\0';
118
119     return result;
120 }
121
122
123 // write data to a file
124 int SGFile::write( char *buf, int length ) {
125 #ifdef _MSC_VER
126     int result = _write( fp, buf, length );
127 #else
128     int result = std::write( fp, buf, length );
129 #endif
130     if ( result != length ) {
131         FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name );
132     }
133
134     return result;
135 }
136
137
138 // write null terminated string to a file
139 int SGFile::writestring( char *str ) {
140     int length = strlen( str );
141     return write( str, length );
142 }
143
144
145 // close the port
146 bool SGFile::close() {
147 #ifdef _MSC_VER
148     if ( _close( fp ) == -1 ) {
149 #else
150     if ( std::close( fp ) == -1 ) {
151 #endif
152         return false;
153     }
154
155     return true;
156 }