Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

file_inputprovider.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: file_inputprovider.cpp,v 1.6 2001/02/27 14:49:32 japj Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 
00014         File purpose:
00015                 File input source provider, part of the i/o data component.
00016 
00017 */
00018 
00019 #include "Core/precomp.h"
00020 
00021 #include <stdio.h>
00022 #include <strstream>
00023 
00024 #include <API/Core/System/error.h>
00025 #include <API/Core/IOData/inputsource.h>
00026 #include <API/Core/IOData/inputsource_provider.h>
00027 #include "API/Core/System/cl_assert.h"
00028 #include <API/Core/IOData/file_inputprovider.h>
00029 
00030 CL_InputSourceProvider_File::CL_InputSourceProvider_File(const char *path)
00031 {
00032         provider_path=path;
00033         if (provider_path==".")
00034                 provider_path+='/';
00035 }
00036 
00037 CL_InputSourceProvider *CL_InputSourceProvider::create_file_provider(const char *path)
00038 {
00039         return new CL_InputSourceProvider_File(path);
00040 }
00041 
00042 CL_InputSource *CL_InputSourceProvider_File::open_source(const char *filename)
00043 {
00044         std::string filepath=provider_path + filename;
00045         return new CL_InputSource_File(filepath.c_str());
00046 }
00047 
00048 CL_InputSourceProvider *CL_InputSourceProvider_File::clone()
00049 {
00050         return new CL_InputSourceProvider_File(provider_path.c_str());
00051 }
00052 
00053 
00054 
00055 /**********************
00056         CL_InputSource_File
00057 **********************/
00058 
00059 CL_InputSource_File::CL_InputSource_File(const char *_filename)
00060 {
00061         filename = _filename;
00062         filehandle = NULL;
00063         open();
00064 }
00065 
00066 CL_InputSource_File::CL_InputSource_File(const CL_InputSource_File *source)
00067 {
00068         filename = source->filename;
00069         filehandle = NULL;
00070 
00071         open();
00072         fseek(filehandle, ftell(source->filehandle), SEEK_SET);
00073 }
00074 
00075 CL_InputSource_File::~CL_InputSource_File()
00076 {
00077         close();
00078 }
00079 
00080 void CL_InputSource_File::set_system_mode()
00081 {
00082         // BUG BUG: Not implemented!
00083 }
00084 
00085 void CL_InputSource_File::set_big_endian_mode()
00086 {
00087         // BUG BUG: Not implemented!
00088 }
00089 
00090 void CL_InputSource_File::set_little_endian_mode()
00091 {
00092         // BUG BUG: Not implemented!
00093 }
00094 
00095 int CL_InputSource_File::read_int32()
00096 {
00097         int input;
00098         if (fread(&input, 4, 1, filehandle) != 1)
00099         {
00100                 throw CL_Error("CL_InputSource_File::read_int32() failed");
00101         }
00102         return input;
00103 }
00104 
00105 unsigned int CL_InputSource_File::read_uint32()
00106 {
00107         unsigned int input;
00108         if (fread(&input, 4, 1, filehandle) != 1)
00109         {
00110                 throw CL_Error("CL_InputSource_File::read_uint32() failed");
00111         }
00112         return input;
00113 }
00114 
00115 short CL_InputSource_File::read_short16()
00116 {
00117         short input;
00118         if (fread(&input, 2, 1, filehandle) != 1)
00119         {
00120                 throw CL_Error("CL_InputSource_File::read_short16() failed");
00121         }
00122 
00123         return input;
00124 }
00125 
00126 unsigned short CL_InputSource_File::read_ushort16()
00127 {
00128         unsigned short input;
00129         if (fread(&input, 2, 1, filehandle) != 1)
00130         {
00131                 throw CL_Error("CL_InputSource_File::read_ushort16() failed");
00132         }
00133 
00134         return input;
00135 }
00136 
00137 char CL_InputSource_File::read_char8()
00138 {
00139         char input;
00140         if (fread(&input, 1, 1, filehandle) != 1)
00141         {
00142                 throw CL_Error("CL_InputSource_File::read_char8() failed");
00143         }
00144 
00145         return input;
00146 }
00147 
00148 unsigned char CL_InputSource_File::read_uchar8()
00149 {
00150         unsigned char input;
00151         if (fread(&input, 1, 1, filehandle) != 1)
00152         {
00153                 throw CL_Error("CL_InputSource_File::read_uchar8() failed");
00154         }
00155         return input;
00156 }
00157 
00158 float CL_InputSource_File::read_float32()
00159 {
00160         float input;
00161         if (fread(&input, 4, 1, filehandle) != 1)
00162         {
00163                 throw CL_Error("CL_InputSource_File::read_float32() failed");
00164         }
00165         return input;
00166 }
00167 
00168 int CL_InputSource_File::read(void *data, int size)
00169 {
00170         return fread(data, 1, size, filehandle);
00171 }
00172 
00173 void CL_InputSource_File::open()
00174 {
00175         if (filehandle != NULL) return;
00176 
00177 #ifndef WIN32 // hate win32 non posix conform
00178         if (filename[0] == '!')
00179         {
00180                 filehandle = popen(std::string(filename, 1).c_str(), "rb");
00181                 if (filehandle == NULL)
00182                 {
00183                         std::strstream err;
00184                         err << "Could not open pipe: " << std::string(filename,1 ) << '\0';
00185                         throw CL_Error(err.str());
00186                 }
00187                 filesize = 99999999;
00188         }
00189         else
00190 #endif
00191         {
00192                 filehandle = fopen(filename.c_str(), "rb");
00193                 if (filehandle == NULL)
00194                 {
00195                         std::strstream err;
00196                         err << "Could not open file: " << filename.c_str() << '\0';
00197                         throw CL_Error(err.str());
00198                 }
00199                 fseek(filehandle, 0, SEEK_END);
00200                 filesize = ftell(filehandle);
00201                 fseek(filehandle, 0, SEEK_SET);
00202         }
00203 //      cl_assert(filehandle != NULL);
00204 
00205 }
00206 
00207 void CL_InputSource_File::close()
00208 {
00209         if (filehandle == NULL) return;
00210         fclose(filehandle);
00211 
00212         filehandle = NULL;
00213 }
00214 
00215 CL_InputSource *CL_InputSource_File::clone() const
00216 {
00217         return new CL_InputSource_File(this);
00218 }
00219 
00220 int CL_InputSource_File::tell() const
00221 {
00222         return ftell(filehandle);
00223 }
00224 
00225 void CL_InputSource_File::seek(int pos, SeekEnum seek_type)
00226 {
00227         switch (seek_type)
00228         {
00229         case seek_cur:
00230         fseek(filehandle, pos, SEEK_CUR);
00231                 break;
00232 
00233         case seek_set:
00234         fseek(filehandle, pos, SEEK_SET);
00235                 break;
00236 
00237         case seek_end:
00238         fseek(filehandle, pos, SEEK_END);
00239                 break;
00240         }
00241 }
00242 
00243 int CL_InputSource_File::size() const
00244 {
00245         return filesize;
00246 }
00247 
00248 std::string CL_InputSource_File::read_string()
00249 {
00250         int size = read_int32();
00251 
00252         char *str = new char[size];
00253         read(str, size);
00254         
00255         std::string ret = str;
00256         delete[] str;
00257 
00258         return ret;
00259 }
00260 
00261 void CL_InputSource_File::push_position()
00262 {
00263         int a = ftell(filehandle);
00264 
00265         stack.push(a);
00266 }
00267 
00268 void CL_InputSource_File::pop_position()
00269 {
00270         int a = stack.top();
00271         stack.pop();
00272 
00273         fseek(filehandle, a, SEEK_SET);
00274 }

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001