00001 00005 // Written by Curtis Olson, started November 1999. 00006 // 00007 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt 00008 // 00009 // This program is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU General Public License as 00011 // published by the Free Software Foundation; either version 2 of the 00012 // License, or (at your option) any later version. 00013 // 00014 // This program is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with this program; if not, write to the Free Software 00021 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 // 00023 // $Id: sg__file_8hxx_source.html,v 1.3 2010/02/23 22:10:16 curt Exp $ 00024 00025 00026 #ifndef _SG_FILE_HXX 00027 #define _SG_FILE_HXX 00028 00029 00030 #ifndef __cplusplus 00031 # error This library requires C++ 00032 #endif 00033 00034 #include <simgear/compiler.h> 00035 00036 #include <string> 00037 00038 #include <sys/types.h> // for open(), read(), write(), close() 00039 #include <sys/stat.h> // for open(), read(), write(), close() 00040 #include <fcntl.h> // for open(), read(), write(), close() 00041 #if !defined( _MSC_VER ) 00042 # include <unistd.h> // for open(), read(), write(), close() 00043 #endif 00044 00045 #include "iochannel.hxx" 00046 00047 using std::string; 00048 00049 00053 class SGFile : public SGIOChannel { 00054 00055 string file_name; 00056 int fp; 00057 bool eof_flag; 00058 // Number of repetitions to play. -1 means loop infinitely. 00059 const int repeat; 00060 int iteration; // number of current repetition, 00061 // starting at 0 00062 00063 public: 00064 00073 SGFile( const string& file, int repeat_ = 1 ); 00074 00076 ~SGFile(); 00077 00078 // open the file based on specified direction 00079 bool open( const SGProtocolDir dir ); 00080 00081 // read a block of data of specified size 00082 int read( char *buf, int length ); 00083 00084 // read a line of data, length is max size of input buffer 00085 int readline( char *buf, int length ); 00086 00087 // write data to a file 00088 int write( const char *buf, const int length ); 00089 00090 // write null terminated string to a file 00091 int writestring( const char *str ); 00092 00093 // close file 00094 bool close(); 00095 00097 inline string get_file_name() const { return file_name; } 00098 00100 inline bool eof() const { return eof_flag; }; 00101 }; 00102 00103 00104 #endif // _SG_FILE_HXX 00105 00106