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