head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.39; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.57; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.03; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.00; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# sg_file.cxx

    00001 // sg_file.cxx -- File I/O routines
    00002 //
    00003 // Written by Curtis Olson, started November 1999.
    00004 //
    00005 // Copyright (C) 1999  Curtis L. Olson - curt@@flightgear.org
    00006 //
    00007 // This program is free software; you can redistribute it and/or
    00008 // modify it under the terms of the GNU General Public License as
    00009 // published by the Free Software Foundation; either version 2 of the
    00010 // License, or (at your option) any later version.
    00011 //
    00012 // This program is distributed in the hope that it will be useful, but
    00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015 // General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU General Public License
    00018 // along with this program; if not, write to the Free Software
    00019 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00020 //
    00021 // $Id: sg_file.cxx,v 1.7 2001/08/02 22:56:33 curt Exp $
    00022
    00023
    00024 #include <simgear/compiler.h>
    00025
    00026 #include STL_STRING
    00027
    00028 #if defined(_MSC_VER) || defined(__MINGW32__)
    00029 #  include <io.h>
    00030 #endif
    00031
    00032 #include <simgear/debug/logstream.hxx>
    00033
    00034 #include "sg_file.hxx"
    00035
    00036 SG_USING_STD(string);
    00037
    00038
    00039 SGFile::SGFile( const string &file) {
    00040     set_type( sgFileType );
    00041     file_name = file;
    00042 }
    00043
    00044
    00045 SGFile::~SGFile() {
    00046 }
    00047
    00048
    00049 // open the file based on specified direction
    00050 bool SGFile::open( const SGProtocolDir d ) {
    00051     set_dir( d );
    00052
    00053     if ( get_dir() == SG_IO_OUT ) {
    00054 #if defined(_MSC_VER) || defined(__MINGW32__)
    00055         int mode = 00666;
    00056 #else
    00057         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    00058 #endif
    00059         fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode );
    00060     } else if ( get_dir() == SG_IO_IN ) {
    00061         fp = ::open( file_name.c_str(), O_RDONLY );
    00062     } else {
    00063         SG_LOG( SG_IO, SG_ALERT,
    00064                 "Error:  bidirection mode not available for files." );
    00065         return false;
    00066     }
    00067
    00068     if ( fp == -1 ) {
    00069         SG_LOG( SG_IO, SG_ALERT, "Error opening file: " << file_name );
    00070         return false;
    00071     }
    00072
    00073     return true;
    00074 }
    00075
    00076
    00077 // read a block of data of specified size
    00078 int SGFile::read( char *buf, int length ) {
    00079     // read a chunk
    00080     return ::read( fp, buf, length );
    00081 }
    00082
    00083
    00084 // read a line of data, length is max size of input buffer
    00085 int SGFile::readline( char *buf, int length ) {
    00086     // save our current position
    00087     int pos = lseek( fp, 0, SEEK_CUR );
    00088
    00089     // read a chunk
    00090     int result = ::read( fp, buf, length );
    00091
    00092     // find the end of line and reset position
    00093     int i;
    00094     for ( i = 0; i < result && buf[i] != '\n'; ++i );
    00095     if ( buf[i] == '\n' ) {
    00096         result = i + 1;
    00097     } else {
    00098         result = i;
    00099     }
    00100     lseek( fp, pos + result, SEEK_SET );
    00101
    00102     // just in case ...
    00103     buf[ result ] = '\0';
    00104
    00105     return result;
    00106 }
    00107
    00108
    00109 // write data to a file
    00110 int SGFile::write( const char *buf, const int length ) {
    00111     int result = ::write( fp, buf, length );
    00112     if ( result != length ) {
    00113         SG_LOG( SG_IO, SG_ALERT, "Error writing data: " << file_name );
    00114     }
    00115
    00116     return result;
    00117 }
    00118
    00119
    00120 // write null terminated string to a file
    00121 int SGFile::writestring( const char *str ) {
    00122     int length = strlen( str );
    00123     return write( str, length );
    00124 }
    00125
    00126
    00127 // close the port
    00128 bool SGFile::close() {
    00129     if ( ::close( fp ) == -1 ) {
    00130         return false;
    00131     }
    00132
    00133     return true;
    00134 }

@ 1.3 log @Oops, try again. @ text @d143 1 a143 1