00001
00002
00003
00004
00005
00006
00007 #include "Platform/File.h"
00008
00009 #include <stdarg.h>
00010 #include <string.h>
00011 #include "Platform/GTAssert.h"
00012 #include "Tools/Debugging/Debugging.h"
00013
00014 File::File(const char* name,const char* mode)
00015 {
00016 ASSERT(name);
00017 char buf[FILENAME_MAX];
00018 if(name[0] != '/' && name[0] != '\\' && (name[0] == 0 || name[1] != ':'))
00019 {
00020 sprintf(buf,"%s/CONF/",getGTDir());
00021 }
00022 else
00023 buf[0] = 0;
00024 ASSERT(strlen(buf) + strlen(name) < FILENAME_MAX);
00025 strcat(buf,name);
00026 stream = OFS::fopen(buf,mode);
00027 isWrite = (mode[0] == 'w')||(mode[0] == 'a');
00028 bufSize = 0;
00029 index = 0;
00030 }
00031
00032 File::~File()
00033 {
00034 if(stream != 0)
00035 {
00036 if(isWrite)
00037 VERIFY(OFS::fwrite(buf,1,index,stream) == index);
00038 OFS::fclose(stream);
00039 }
00040 }
00041
00042 void File::read(void* p,unsigned size)
00043 {
00044 ASSERT(!isWrite);
00045 if(!bufSize)
00046 bufSize = OFS::fread(buf, 1, sizeof(buf), stream);
00047 while(sizeof(buf) - index - 1 < size)
00048 {
00049 memcpy(p, buf + index, sizeof(buf) - index - 1);
00050 (char*&) p += sizeof(buf) - index - 1;
00051 size -= sizeof(buf) - index - 1;
00052 buf[0] = buf[sizeof(buf) - 1];
00053 bufSize = OFS::fread(buf + 1, 1, sizeof(buf) - 1, stream) + 1;
00054 index = 0;
00055 }
00056 memcpy(p, buf + index, size);
00057 index += size;
00058 }
00059
00060 void File::write(const void *p,unsigned size)
00061 {
00062 while(sizeof(buf) - index < size)
00063 {
00064 memcpy(buf + index,p,sizeof(buf) - index);
00065 (char*&) p += sizeof(buf) - index;
00066 size -= sizeof(buf) - index;
00067 VERIFY(OFS::fwrite(buf,1,sizeof(buf),stream) == sizeof(buf));
00068 index = 0;
00069 }
00070 memcpy(buf + index,p,size);
00071 index += size;
00072 }
00073
00074 void File::printf(const char* format, ...)
00075 {
00076 ASSERT(!index);
00077 va_list args;
00078 va_start(args,format);
00079 OFS::vfprintf(stream,format,args);
00080 va_end(args);
00081 }
00082
00083 bool File::eof() const
00084 {
00085 #ifdef __GNUC__
00086 return ((feof(stream) != 0) && (index >= bufSize));
00087 #else
00088 return ((OFS::feof(stream) != 0) && (index >= bufSize));
00089 #endif
00090 }
00091
00092 char* File::getGTDir()
00093 {
00094 return "/MS/open-r/APP";
00095 }