]> git.mxchange.org Git - flightgear.git/blob - src/Network/fg_file.cxx
Replaced Durk's mymath.* with plib/sg.h (contributed by Durk).
[flightgear.git] / src / Network / fg_file.cxx
1 // fg_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 "fg_file.hxx"
35
36 FG_USING_STD(string);
37
38
39 FGFile::FGFile() {
40 }
41
42
43 FGFile::~FGFile() {
44 }
45
46
47 // open the file based on specified direction
48 bool FGFile::open( FGProtocol::fgProtocolDir dir ) {
49     if ( dir == FGProtocol::out ) {
50 #ifdef _MSC_VER
51         fp = _open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
52                         00666 );
53 #else
54         fp = std::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
55                         S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | 
56                         S_IROTH | S_IWOTH );
57 #endif
58     } else if ( dir == FGProtocol::in ) {
59 #ifdef _MSC_VER
60         fp = _open( file_name.c_str(), O_RDONLY );
61 #else
62         fp = std::open( file_name.c_str(), O_RDONLY );
63 #endif
64     } else {
65         FG_LOG( FG_IO, FG_ALERT, 
66                 "Error:  bidirection mode not available for files." );
67         return false;
68     }
69
70     if ( fp == -1 ) {
71         FG_LOG( FG_IO, FG_ALERT, "Error opening file: " << file_name );
72         return false;
73     }
74
75     return true;
76 }
77
78
79 // read a block of data of specified size
80 int FGFile::read( char *buf, int length ) {
81     // read a chunk
82 #ifdef _MSC_VER
83     int result = _read( fp, buf, length );
84 #else
85     int result = std::read( fp, buf, length );
86 #endif
87
88     return result;
89 }
90
91
92 // read a line of data, length is max size of input buffer
93 int FGFile::readline( char *buf, int length ) {
94     // save our current position
95     int pos = lseek( fp, 0, SEEK_CUR );
96
97     // read a chunk
98 #ifdef _MSC_VER
99     int result = _read( fp, buf, length );
100 #else
101     int result = std::read( fp, buf, length );
102 #endif
103
104     // find the end of line and reset position
105     int i;
106     for ( i = 0; i < result && buf[i] != '\n'; ++i );
107     if ( buf[i] == '\n' ) {
108         result = i + 1;
109     } else {
110         result = i;
111     }
112     lseek( fp, pos + result, SEEK_SET );
113     
114     // just in case ...
115     buf[ result ] = '\0';
116
117     return result;
118 }
119
120
121 // write data to a file
122 int FGFile::write( char *buf, int length ) {
123 #ifdef _MSC_VER
124     int result = _write( fp, buf, length );
125 #else
126     int result = std::write( fp, buf, length );
127 #endif
128     if ( result != length ) {
129         FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name );
130     }
131
132     return result;
133 }
134
135
136 // write null terminated string to a file
137 int FGFile::writestring( char *str ) {
138     int length = strlen( str );
139     return write( str, length );
140 }
141
142
143 // close the port
144 bool FGFile::close() {
145 #ifdef _MSC_VER
146     if ( _close( fp ) == -1 ) {
147 #else
148     if ( std::close( fp ) == -1 ) {
149 #endif
150         return false;
151     }
152
153     return true;
154 }