Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Platform/Aperios1.3.2/File.cpp

Go to the documentation of this file.
00001 /**
00002 * @file Platform/Aperios1.3.2/File.cpp
00003 * 
00004 * Implementation of class File for Aperios 1.3.2.
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); // sorry, text streams are unbuffered
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 }
00096 
00097 /*
00098 * Change log :
00099 * 
00100 * $Log: File.cpp,v $
00101 * Revision 1.1.1.1  2004/05/22 17:23:25  cvsadm
00102 * created new repository GT2004_WM
00103 *
00104 * Revision 1.5  2004/03/11 17:17:38  roefer
00105 * 64k bug fix fixed
00106 *
00107 * Revision 1.4  2004/03/10 08:14:59  roefer
00108 * 64k bug fixed
00109 *
00110 * Revision 1.3  2004/01/22 12:42:25  dueffert
00111 * flush removed because it doesnt work properly on robots
00112 *
00113 * Revision 1.2  2004/01/16 15:46:27  dueffert
00114 * flush added to File and OutFile
00115 *
00116 * Revision 1.1  2003/10/07 10:06:59  cvsadm
00117 * Created GT2004 (M.J.)
00118 *
00119 * Revision 1.1.1.1  2003/07/02 09:40:24  cvsadm
00120 * created new repository for the competitions in Padova from the 
00121 * tamara CVS (Tuesday 2:00 pm)
00122 *
00123 * removed unused solutions
00124 *
00125 * Revision 1.2  2002/11/18 17:32:35  dueffert
00126 * RobotControl should be startable in any path now
00127 *
00128 * Revision 1.1  2002/09/10 15:40:04  cvsadm
00129 * Created new project GT2003 (M.L.)
00130 * - Cleaned up the /Src/DataTypes directory
00131 * - Removed challenge related source code
00132 * - Removed processing of incoming audio data
00133 * - Renamed AcousticMessage to SoundRequest
00134 *
00135 * Revision 1.1  2002/07/23 13:38:22  loetzsch
00136 * moved Implementation of File into an own C++ file
00137 *
00138 */

Generated on Thu Sep 23 19:57:34 2004 for GT2004 by doxygen 1.3.6