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