00001 #include <windows.h>
00002 #include "Common/Assert.h"
00003
00004 #include "Common/File.h"
00005
00006
00007 KFile::KFile()
00008 {
00009 m_hFile = 0;
00010 m_Mode = KFM_READWRITE;
00011 m_bEOF = false;
00012 }
00013
00014
00015 int KFile::Create( KStr sFileName, KFILEMODE Mode )
00016 {
00017 int Access;
00018
00019 m_bEOF = false;
00020
00021 switch( Mode )
00022 {
00023 case KFM_READ: Access = GENERIC_READ; break;
00024 case KFM_WRITE: Access = GENERIC_WRITE; break;
00025 case KFM_READWRITE: Access = GENERIC_READ | GENERIC_WRITE; break;
00026 default: KASSERT(0);
00027 }
00028
00029 m_Mode = Mode;
00030 m_hFile = CreateFile( sFileName.GetpString(), Access, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
00031
00032 if ( m_hFile == INVALID_HANDLE_VALUE )
00033 return false;
00034
00035 m_sFileName = sFileName;
00036
00037 return true;
00038 }
00039
00040
00041 int KFile::Open( KStr sFileName, KFILEMODE Mode )
00042 {
00043 int Access;
00044 int Share;
00045
00046 m_bEOF = false;
00047
00048 switch( Mode )
00049 {
00050 case KFM_READ:
00051 Access = GENERIC_READ;
00052 Share = FILE_SHARE_READ;
00053 break;
00054 case KFM_WRITE:
00055 Access = GENERIC_WRITE;
00056 Share = FILE_SHARE_WRITE;
00057 break;
00058 case KFM_READWRITE:
00059 Access = GENERIC_READ | GENERIC_WRITE;
00060 Share = FILE_SHARE_READ | FILE_SHARE_WRITE;
00061 break;
00062 default:
00063 KASSERT(0);
00064 }
00065
00066 m_Mode = Mode;
00067 m_hFile = CreateFile( sFileName.GetpString(), Access, Share, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
00068
00069 if ( m_hFile == INVALID_HANDLE_VALUE )
00070 {
00071 m_hFile = 0;
00072 return false;
00073 }
00074
00075 m_sFileName = sFileName;
00076
00077 return true;
00078 }
00079
00080
00081 int KFile::Close()
00082 {
00083 m_bEOF = false;
00084 m_sFileName = "";
00085
00086 if( !CloseHandle( m_hFile ) )
00087 return false;
00088
00089 return true;
00090 }
00091
00092
00093 int KFile::Read( unsigned char* pBuffer, unsigned long ByteToRead, unsigned long* pByteRead )
00094 {
00095 if( !m_hFile )
00096 return false;
00097
00098 unsigned long pRead;
00099
00100 m_bEOF = false;
00101
00102 if( !ReadFile( m_hFile, pBuffer, ByteToRead, pByteRead ? pByteRead : &pRead, NULL ) )
00103 {
00104 if( GetLastError() == ERROR_HANDLE_EOF )
00105 m_bEOF = true;
00106
00107 return false;
00108 }
00109
00110 if( pByteRead )
00111 {
00112 if( ByteToRead != *pByteRead )
00113 m_bEOF = true;
00114 }
00115 else
00116 {
00117 if( ByteToRead != pRead )
00118 m_bEOF = true;
00119 }
00120
00121 return true;
00122 }
00123
00124
00125 int KFile::Write( unsigned char* pBuffer, unsigned long ByteToWrite, unsigned long* pByteWrite )
00126 {
00127 if( !m_hFile )
00128 return false;
00129
00130 unsigned long pWrite;
00131
00132 m_bEOF = false;
00133
00134 if( !WriteFile( m_hFile, pBuffer, ByteToWrite, pByteWrite ? pByteWrite : &pWrite, NULL ) )
00135 return false;
00136
00137 return true;
00138 }
00139
00140
00141 int KFile::Seek( long Distance, KFILESEEK SeekMode )
00142 {
00143 if( !m_hFile )
00144 return false;
00145
00146 unsigned long MoveMethod;
00147
00148 m_bEOF = false;
00149
00150 switch( SeekMode )
00151 {
00152 case KFS_BEGIN: MoveMethod = FILE_BEGIN; break;
00153 case KFS_CURRENT: MoveMethod = FILE_CURRENT; break;
00154 case KFS_END: MoveMethod = FILE_END; break;
00155 default: KASSERT(0);
00156 }
00157
00158 SetFilePointer( m_hFile, Distance, NULL, MoveMethod );
00159
00160 return true;
00161 }
00162
00163
00164 int KFile::Tell( unsigned long* pCurrentPos )
00165 {
00166 if( !m_hFile )
00167 return false;
00168
00169 *pCurrentPos = SetFilePointer( m_hFile, 0, NULL, FILE_CURRENT );
00170
00171 return true;
00172 }
00173
00174
00175
00176 int KFile::Rewind()
00177 {
00178 if( !m_hFile )
00179 return false;
00180
00181 if( !Seek( 0, KFS_BEGIN ) )
00182 return false;
00183
00184 return true;
00185 }
00186
00187
00188 int KFile::ReadChar( char* pChar )
00189 {
00190 if( !m_hFile )
00191 return false;
00192
00193 unsigned long pByteRead;
00194
00195 if( !Read( (unsigned char*)pChar, 1, &pByteRead ) || pByteRead != 1 )
00196 return false;
00197
00198 return true;
00199 }
00200
00201
00202 int KFile::ReadString( KStr& sString )
00203 {
00204 if( !m_hFile )
00205 return false;
00206
00207 char Char;
00208 char pString[1024];
00209 u32 i = 0;
00210
00211 while( i < sizeof( pString ) )
00212 {
00213 if( !ReadChar( &Char ) )
00214 {
00215 sString = "";
00216 return false;
00217 }
00218
00219 if( (Char == '\0') )
00220 {
00221 pString[i] = '\0';
00222 sString = pString;
00223 return true;
00224 }
00225
00226 pString[i] = Char;
00227 i ++;
00228 }
00229
00230 return false;
00231 }
00232
00233
00234 int KFile::WriteString( KStr& sString )
00235 {
00236 return Write( (u8*)sString.GetpString(), sString.GetLength() + 1 );
00237 }
00238
00239
00240 int KFile::ReadText( KStr& sString )
00241 {
00242 if( !m_hFile )
00243 return false;
00244
00245 char Char;
00246 char pString[1024];
00247 u32 i = 0;
00248
00249 while( i < sizeof( pString ) )
00250 {
00251 if( !ReadChar( &Char ) )
00252 {
00253 sString = "";
00254 return false;
00255 }
00256
00257 if( (Char == '\r') || (Char == '\n') )
00258 {
00259 if( i == 0 )
00260 continue;
00261
00262 pString[i] = '\0';
00263 sString = pString;
00264 return true;
00265 }
00266
00267 pString[i] = Char;
00268 i ++;
00269 }
00270
00271 return false;
00272 }
00273
00274 int KFile::WriteText( KStr& sString )
00275 {
00276 return Write( (u8*)sString.GetpString(), sString.GetLength() );
00277 }
00278
00279
00280 int KFile::GetSize()
00281 {
00282 int Size;
00283
00284 Size = GetFileSize( m_hFile, NULL );
00285
00286 return Size;
00287 }
00288
00289
00290 char* KFile::GetFileExtension( char* pFileName )
00291 {
00292 u32 u;
00293 for( u = (u32)strlen( pFileName ); u > 0; u -- )
00294 if( pFileName[u] == '.' )
00295 return &pFileName[u + 1];
00296
00297 return NULL;
00298 }
00299
00300
00301 char* KFile::GetFileName( char* pFileName )
00302 {
00303 u32 u;
00304 for( u = (u32)strlen( pFileName ); u > 0; u -- )
00305 if( ( pFileName[u] == '\\' ) || ( pFileName[u] == '/' ) )
00306 return &pFileName[u + 1];
00307
00308 return NULL;
00309 }
00310
00311
00312 int KFile::GetLastWriteTime( KFileTime* pFileTime )
00313 {
00314 FILETIME FileTime;
00315 FILETIME LocalFileTime;
00316 SYSTEMTIME SystemTime;
00317
00318 if( !m_hFile )
00319 return false;
00320
00321 if( !GetFileTime( m_hFile, NULL, NULL, &FileTime ) )
00322 return false;
00323
00324 if( !FileTimeToLocalFileTime( &FileTime, &LocalFileTime ) )
00325 return false;
00326
00327 if( !FileTimeToSystemTime( &LocalFileTime, &SystemTime ) )
00328 return false;
00329
00330 pFileTime->m_Year = SystemTime.wYear;
00331 pFileTime->m_Month = SystemTime.wMonth;
00332 pFileTime->m_Day = SystemTime.wDay;
00333 pFileTime->m_Hour = SystemTime.wHour;
00334 pFileTime->m_Minute = SystemTime.wMinute;
00335 pFileTime->m_Second = SystemTime.wSecond;
00336
00337 return true;
00338 }
00339
00340
00341 int KFile::Reset()
00342 {
00343 if( !Seek( 0, KFS_BEGIN ) )
00344 return false;
00345
00346 if( !SetEndOfFile( m_hFile ) )
00347 return false;
00348
00349 return true;
00350 }