00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <new>
00051
00052 #include "BasicFileIO.h"
00053 #include <string.h>
00054 #include <stdlib.h>
00055
00056
00057
00058
00059
00060
00061
00062 bool CBasicFileIO::ReadBool(char*& pchBuffer)
00063 {
00064 bool bReturn = (*((char *) pchBuffer)) ? true : false;
00065 pchBuffer += sizeof(char);
00066 return bReturn;
00067 }
00068
00069 char CBasicFileIO::ReadChar(char*& pchBuffer)
00070 {
00071 char chReturn = *((char*)pchBuffer);
00072 pchBuffer += sizeof(char);
00073 return chReturn;
00074 }
00075
00076 std::string CBasicFileIO::ReadString(char*& pchBuffer)
00077 {
00078 int nLength = ReadInt(pchBuffer);
00079
00080 if(nLength == 0) return "";
00081
00082 std::string szReturn(pchBuffer, nLength);
00083 pchBuffer += nLength;
00084
00085 return szReturn;
00086 }
00087
00088 short CBasicFileIO::ReadShort(char*& pchBuffer)
00089 {
00090 short nReturn;
00091 memcpy(&nReturn, pchBuffer, sizeof(short));
00092 pchBuffer += sizeof(short);
00093 return nReturn;
00094 }
00095
00096 int CBasicFileIO::ReadInt(char*& pchBuffer)
00097 {
00098 int nReturn;
00099 memcpy(&nReturn, pchBuffer, sizeof(int));
00100 pchBuffer += sizeof(int);
00101 return nReturn;
00102 }
00103
00104 float CBasicFileIO::ReadFloat(char*& pchBuffer)
00105 {
00106 float fReturn;
00107 memcpy(&fReturn, pchBuffer, sizeof(float));
00108 pchBuffer += sizeof(float);
00109 return fReturn;
00110 }
00111
00112 double CBasicFileIO::ReadDouble(char*& pchBuffer)
00113 {
00114 double dReturn;
00115 memcpy(&dReturn, pchBuffer, sizeof(double));
00116 pchBuffer += sizeof(double);
00117 return dReturn;
00118 }
00119
00120 void CBasicFileIO::ReadBytes(char*& pchBuffer, void* pDest, int nSize)
00121 {
00122 memcpy(pDest,(void*) pchBuffer,nSize);
00123 pchBuffer += nSize;
00124 }
00125
00126
00127
00128
00129
00130
00131 bool CBasicFileIO::WriteBool(FILE *fp, bool bValue)
00132 {
00133 char chValue = (char) bValue;
00134 return fwrite(&chValue, 1, 1, fp) == 1;
00135 }
00136
00137 bool CBasicFileIO::WriteChar(FILE *fp, char chValue)
00138 {
00139 return fwrite(&chValue, 1, 1, fp) == 1;
00140 }
00141
00142 bool CBasicFileIO::WriteString(FILE *fp, std::string szValue)
00143 {
00144 if (szValue == "")
00145 return WriteInt(fp,0);
00146
00147 return WriteInt(fp, (int) szValue.length()) && fwrite(szValue.data(), szValue.length(), 1, fp) == 1;
00148 }
00149
00150 bool CBasicFileIO::WriteInt(FILE *fp, int nValue)
00151 {
00152 return fwrite(&nValue, sizeof(int), 1, fp) == 1;
00153 }
00154
00155 bool CBasicFileIO::WriteFloat(FILE *fp, float fValue)
00156 {
00157 return fwrite(&fValue, sizeof(float), 1, fp) == 1;
00158 }
00159
00160 bool CBasicFileIO::WriteDouble(FILE *fp, double dValue)
00161 {
00162 return fwrite(&dValue, sizeof(double), 1, fp) == 1;
00163 }
00164
00165 bool CBasicFileIO::WriteBytes(FILE *fp, void* pSrc, int nSize)
00166 {
00167 return fwrite(pSrc, nSize, 1, fp) == 1;
00168 }
00169
00170
00171 int CBasicFileIO::GetFileSize(FILE *fp)
00172 {
00173 const int nCurrentFilePos = ftell(fp);
00174 fseek(fp, 0, SEEK_END);
00175 const int nFileSize = ftell(fp);
00176 fseek(fp, nCurrentFilePos, SEEK_SET);
00177 return nFileSize;
00178 }