]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_file.cxx
dace6454060c5e7bad2b100587f0ef2e6e3f84ab
[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., 675 Mass Ave, Cambridge, MA 02139, 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 <simgear/misc/stdint.hxx>
33 #include <simgear/debug/logstream.hxx>
34
35 #include "sg_file.hxx"
36
37 SG_USING_STD(string);
38
39
40 SGFile::SGFile( const string &file) {
41     set_type( sgFileType );
42     file_name = file;
43     eof_flag = true;
44 }
45
46
47 SGFile::~SGFile() {
48 }
49
50
51 // open the file based on specified direction
52 bool SGFile::open( const SGProtocolDir d ) {
53     set_dir( d );
54
55     if ( get_dir() == SG_IO_OUT ) {
56 #if defined(_MSC_VER) || defined(__MINGW32__)
57         int mode = 00666;
58 #else
59         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
60 #endif
61         fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode );
62     } else if ( get_dir() == SG_IO_IN ) {
63         fp = ::open( file_name.c_str(), O_RDONLY );
64     } else {
65         SG_LOG( SG_IO, SG_ALERT, 
66                 "Error:  bidirection mode not available for files." );
67         return false;
68     }
69
70     if ( fp == -1 ) {
71         SG_LOG( SG_IO, SG_ALERT, "Error opening file: " << file_name );
72         return false;
73     }
74
75     eof_flag = false;
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     ssize_t result = ::read( fp, buf, length );
84     if ( length > 0 && result == 0 ) {
85         eof_flag = true;
86     }
87     return result;
88 }
89
90
91 // read a line of data, length is max size of input buffer
92 int SGFile::readline( char *buf, int length ) {
93     // save our current position
94     int pos = lseek( fp, 0, SEEK_CUR );
95
96     // read a chunk
97     ssize_t result = ::read( fp, buf, length );
98     if ( length > 0 && result == 0 ) {
99         eof_flag = true;
100     }
101
102     // find the end of line and reset position
103     int i;
104     for ( i = 0; i < result && buf[i] != '\n'; ++i );
105     if ( buf[i] == '\n' ) {
106         result = i + 1;
107     } else {
108         result = i;
109     }
110     lseek( fp, pos + result, SEEK_SET );
111     
112     // just in case ...
113     buf[ result ] = '\0';
114
115     return result;
116 }
117
118
119 // write data to a file
120 int SGFile::write( const char *buf, const int length ) {
121     int result = ::write( fp, buf, length );
122     if ( result != length ) {
123         SG_LOG( SG_IO, SG_ALERT, "Error writing data: " << file_name );
124     }
125
126     return result;
127 }
128
129
130 // write null terminated string to a file
131 int SGFile::writestring( const char *str ) {
132     int length = strlen( str );
133     return write( str, length );
134 }
135
136
137 // close the port
138 bool SGFile::close() {
139     if ( ::close( fp ) == -1 ) {
140         return false;
141     }
142
143     eof_flag = true;
144     return true;
145 }