]> git.mxchange.org Git - flightgear.git/blob - src/Network/fg_file.cxx
UIUC flight model contribution. This is based on LaRCsim, but can read
[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 #include <simgear/debug/logstream.hxx>
29
30 #include "fg_file.hxx"
31
32 FG_USING_STD(string);
33
34
35 FGFile::FGFile() {
36 }
37
38
39 FGFile::~FGFile() {
40 }
41
42
43 // open the file based on specified direction
44 bool FGFile::open( FGProtocol::fgProtocolDir dir ) {
45     if ( dir == FGProtocol::out ) {
46         fp = std::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
47                         S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | 
48                         S_IROTH | S_IWOTH );
49     } else if ( dir == FGProtocol::in ) {
50         fp = std::open( file_name.c_str(), O_RDONLY );
51     } else {
52         FG_LOG( FG_IO, FG_ALERT, 
53                 "Error:  bidirection mode not available for files." );
54         return false;
55     }
56
57     if ( fp == -1 ) {
58         FG_LOG( FG_IO, FG_ALERT, "Error opening file: " << file_name );
59         return false;
60     }
61
62     return true;
63 }
64
65
66 // read a block of data of specified size
67 int FGFile::read( char *buf, int length ) {
68     // read a chunk
69     int result = std::read( fp, buf, length );
70
71     return result;
72 }
73
74
75 // read a line of data, length is max size of input buffer
76 int FGFile::readline( char *buf, int length ) {
77     // save our current position
78     int pos = lseek( fp, 0, SEEK_CUR );
79
80     // read a chunk
81     int result = std::read( fp, buf, length );
82
83     // find the end of line and reset position
84     int i;
85     for ( i = 0; i < result && buf[i] != '\n'; ++i );
86     if ( buf[i] == '\n' ) {
87         result = i + 1;
88     } else {
89         result = i;
90     }
91     lseek( fp, pos + result, SEEK_SET );
92     
93     // just in case ...
94     buf[ result ] = '\0';
95
96     return result;
97 }
98
99
100 // write data to a file
101 int FGFile::write( char *buf, int length ) {
102     int result = std::write( fp, buf, length );
103     if ( result != length ) {
104         FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name );
105     }
106
107     return result;
108 }
109
110
111 // write null terminated string to a file
112 int FGFile::writestring( char *str ) {
113     int length = strlen( str );
114     return write( str, length );
115 }
116
117
118 // close the port
119 bool FGFile::close() {
120     if ( std::close( fp ) == -1 ) {
121         return false;
122     }
123
124     return true;
125 }