]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_file.cxx
From Benoit Laniel: replace SG threading constructs with those from OpenThreads
[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 STL_STRING
27
28 #if defined(_MSC_VER) || defined(__MINGW32__)
29 #  include <io.h>
30 #endif
31
32 #include <cstring>
33
34 #include <simgear/misc/stdint.hxx>
35 #include <simgear/debug/logstream.hxx>
36
37 #include "sg_file.hxx"
38
39 SG_USING_STD(string);
40
41
42 SGFile::SGFile( const string &file) {
43     set_type( sgFileType );
44     file_name = file;
45     eof_flag = true;
46 }
47
48
49 SGFile::~SGFile() {
50 }
51
52
53 // open the file based on specified direction
54 bool SGFile::open( const SGProtocolDir d ) {
55     set_dir( d );
56
57     if ( get_dir() == SG_IO_OUT ) {
58 #if defined(_MSC_VER) || defined(__MINGW32__)
59         int mode = 00666;
60 #else
61         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
62 #endif
63         fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode );
64     } else if ( get_dir() == SG_IO_IN ) {
65         fp = ::open( file_name.c_str(), O_RDONLY );
66     } else {
67         SG_LOG( SG_IO, SG_ALERT, 
68                 "Error:  bidirection mode not available for files." );
69         return false;
70     }
71
72     if ( fp == -1 ) {
73         SG_LOG( SG_IO, SG_ALERT, "Error opening file: " << file_name );
74         return false;
75     }
76
77     eof_flag = false;
78     return true;
79 }
80
81
82 // read a block of data of specified size
83 int SGFile::read( char *buf, int length ) {
84     // read a chunk
85     ssize_t result = ::read( fp, buf, length );
86     if ( length > 0 && result == 0 ) {
87         eof_flag = true;
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     ssize_t result = ::read( fp, buf, length );
100     if ( length > 0 && result == 0 ) {
101         eof_flag = true;
102     }
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 SGFile::write( const char *buf, const int length ) {
123     int result = ::write( fp, buf, length );
124     if ( result != length ) {
125         SG_LOG( SG_IO, SG_ALERT, "Error writing data: " << file_name );
126     }
127
128     return result;
129 }
130
131
132 // write null terminated string to a file
133 int SGFile::writestring( const char *str ) {
134     int length = std::strlen( str );
135     return write( str, length );
136 }
137
138
139 // close the port
140 bool SGFile::close() {
141     if ( ::close( fp ) == -1 ) {
142         return false;
143     }
144
145     eof_flag = true;
146     return true;
147 }