win32filepeer.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 1,123 行 · 第 1/3 页
CPP
1,123 行
if ( true == fileDir.empty() ){ TCHAR currentDir[MAX_PATH]; memset( currentDir, 0 , sizeof(currentDir) ); GetCurrentDirectory( MAX_PATH, currentDir ); winFileName = "\\" + winFileName; winFileName = currentDir + winFileName; } DWORD rwFlags = 0; DWORD shFlags = 0; DWORD createFlags = OPEN_EXISTING; if ( openFlags & File::ofRead ) { rwFlags |= GENERIC_READ; } if ( openFlags & File::ofWrite || openFlags & File::ofAppend ) { rwFlags |= GENERIC_WRITE; } if ( shareFlags & File::shRead ) { shFlags |= FILE_SHARE_READ; } if ( shareFlags & File::shWrite ) { shFlags |= FILE_SHARE_WRITE; } if ( System::isUnicodeEnabled() ) { fileHandle_ = ::CreateFileW( winFileName.c_str(), rwFlags, shFlags, NULL, createFlags, FILE_ATTRIBUTE_NORMAL, NULL ); } else { fileHandle_ = ::CreateFileA( winFileName.ansi_c_str(), rwFlags, shFlags, NULL, createFlags, FILE_ATTRIBUTE_NORMAL, NULL ); } if ( (NULL == fileHandle_) || (INVALID_HANDLE_VALUE == fileHandle_) ){ fileHandle_ = NULL; //throw exception String errmsg = VCFWin32::Win32Utils::getErrorString( GetLastError() ); StringUtils::trimWhiteSpaces( errmsg ); throw BasicFileError( MAKE_ERROR_MSG_2( errmsg + " - file: " + winFileName ) ); }}void Win32FilePeer::close(){ ::CloseHandle( fileHandle_ ); fileHandle_ = NULL;}void Win32FilePeer::remove(){ if ( NULL != fileHandle_ ){ ::CloseHandle( fileHandle_ ); fileHandle_ = NULL; } String filename = getName(); if ( filename[filename.size()-1] == '\\' ) { BOOL res = FALSE; if ( System::isUnicodeEnabled() ) { res = ::RemoveDirectoryW( filename.c_str() ); } else { res = ::RemoveDirectoryA( filename.ansi_c_str() ); } if ( !res ) { throw BasicFileError( MAKE_ERROR_MSG_2("Unable to remove directory \"" + filename + "\",\nprobably because the directory still contains objects.") ); } } else { BOOL res = FALSE; if ( System::isUnicodeEnabled() ) { res = ::DeleteFileW( filename.c_str() ); } else { res = ::DeleteFileA( filename.ansi_c_str() ); } if ( !res ) { throw BasicFileError( MAKE_ERROR_MSG_2("Unable to remove file \"" + filename + "\".") ); } }}void Win32FilePeer::move( const String& newFileName ){ BOOL err = FALSE; String fileName = getName(); if ( System::isUnicodeEnabled() ) { err = ::MoveFileW( fileName.c_str(), FilePath::transformToOSSpecific( newFileName ).c_str() ); } else { err = ::MoveFileA( fileName.ansi_c_str(), FilePath::transformToOSSpecific( newFileName ).ansi_c_str() ); } if ( !err ) { throw BasicFileError( MAKE_ERROR_MSG_2("Unable to move file \"" + fileName + "\" to \"" + newFileName + "\".") ); }}void Win32FilePeer::copyTo( const String& copyFileName ){ BOOL res = FALSE; String src = FilePath::transformToOSSpecific( getName() ); String dest = FilePath::transformToOSSpecific( copyFileName ); if ( System::isUnicodeEnabled() ) { res = ::CopyFileW( src.c_str(), dest.c_str(), FALSE ); } else { res = ::CopyFileA( src.ansi_c_str(), dest.ansi_c_str(), FALSE ); } if ( ! res ) { throw BasicFileError( MAKE_ERROR_MSG_2("Unable to copy \"" + src + "\" to \"" + copyFileName + "\".") ); }}DateTime Win32FilePeer::convertFileTimeToDateTime( const FILETIME& ft ){ bool ok = false; DateTime dt; SYSTEMTIME st; // ft --> st if ( ::FileTimeToSystemTime( &ft, &st ) ) { dt.set( st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds ); ok = true; } if ( !ok ) { String error = VCFWin32::Win32Utils::getErrorString( GetLastError() ); throw BasicException( MAKE_ERROR_MSG_2(error) ); } return dt;}/*static*/ DateTime Win32FilePeer::convertUtcFileTimeToLocalDateTime( const FILETIME& ftUtc ){ bool ok = false; FILETIME ftLoc; SYSTEMTIME stLoc; DateTime dt; // ftUTC --> ftLocal // see help of VisualStudio.net for "File Times", at the end, though if ( ::FileTimeToLocalFileTime( &ftUtc, &ftLoc ) ) { // ftLocal --> stLocal if ( ::FileTimeToSystemTime( &ftLoc, &stLoc ) ) { dt.set( stLoc.wYear, stLoc.wMonth, stLoc.wDay, stLoc.wHour, stLoc.wMinute, stLoc.wSecond, stLoc.wMilliseconds ); ok = true; } } if ( !ok ) { String error = VCFWin32::Win32Utils::getErrorString( GetLastError() ); throw BasicException( MAKE_ERROR_MSG_2(error) ); } return dt;}/*static*/ String Win32FilePeer::getAlternateFileName( const String& fileName ){ String dosName; try { bool unicodeEnabled = System::isUnicodeEnabled(); if ( unicodeEnabled ) { WIN32_FIND_DATAW findData; HANDLE searchHandle = ::FindFirstFileW( fileName.c_str(), &findData ); if ( INVALID_HANDLE_VALUE != searchHandle ) { String error = VCFWin32::Win32Utils::getErrorString( GetLastError() ); throw BasicException( MAKE_ERROR_MSG_2(error) ); } dosName = findData.cAlternateFileName; } else { WIN32_FIND_DATAA findData; HANDLE searchHandle = ::FindFirstFileA( fileName.ansi_c_str(), &findData ); dosName = findData.cAlternateFileName; if ( INVALID_HANDLE_VALUE != searchHandle ) { String error = VCFWin32::Win32Utils::getErrorString( GetLastError() ); throw BasicException( MAKE_ERROR_MSG_2(error) ); } } } catch( ... ) { String error = VCFWin32::Win32Utils::getErrorString( GetLastError() ); throw RuntimeException( MAKE_ERROR_MSG_2( error ) ); } return dosName;}/***CVS Log info*$Log$*Revision 1.5 2005/07/09 23:15:06 ddiego*merging in changes from devmain-0-6-7 branch.**Revision 1.4 2005/01/02 03:04:23 ddiego*merged over some of the changes from the dev branch because they're important resoource loading bug fixes. Also fixes a few other bugs as well.**Revision 1.3.2.6 2005/07/01 15:47:59 marcelloptr*minor improvements on a message**Revision 1.3.2.5 2005/04/09 17:21:32 marcelloptr*bugfix [ 1179853 ] memory fixes around memset. Documentation. DocumentManager::saveAs and DocumentManager::reload**Revision 1.3.2.4 2005/02/27 01:46:10 ddiego*fixed bug in testing whether a path should be loaded as a bundle.*added some additional rtti info for certain classes in app kit.**Revision 1.3.2.3 2005/02/16 17:08:42 marcelloptr*improved an error message**Revision 1.3.2.2 2004/12/11 17:50:00 ddiego*added 2 new projects that are command line tools. One is for*creating the basic shell for app bundles, the other is for filling in, or*updating an info.plist (or info.xml) file.**Revision 1.3.2.1 2004/12/10 22:32:53 ddiego*fixed bug in the Win32 file peer class that was not properly*creating directories.**Revision 1.3 2004/12/01 04:31:42 ddiego*merged over devmain-0-6-6 code. Marcello did a kick ass job*of fixing a nasty bug (1074768VCF application slows down modal dialogs.)*that he found. Many, many thanks for this Marcello.**Revision 1.2.2.2 2004/11/07 19:32:19 marcelloptr*more documentation**Revision 1.2.2.1 2004/08/26 04:05:48 marcelloptr*minor change on name of getMillisecond**Revision 1.2 2004/08/07 02:49:16 ddiego*merged in the devmain-0-6-5 branch to stable**Revision 1.1.2.8 2004/08/03 20:57:22 marcelloptr*minor change on name DateTime:getSecond DateTime:getMillisecond**Revision 1.1.2.7 2004/07/29 02:39:14 ddiego*fixed a bug with File::getINputStream and File::getOutputStream.**Revision 1.1.2.6 2004/07/26 03:40:31 ddiego*minor changes**Revision 1.1.2.5 2004/07/24 01:40:42 ddiego*committed changes requested by Marcello. Got rid of the remaining*date time members on the File class - now the dat time function call the*FilePeer directly each time. Also added 2 functions to DateTime to convert*directly to UTC or Local time.**Revision 1.1.2.4 2004/07/20 02:03:13 ddiego*fixed some miscellaneous bugs in directory search code. Many*thanks to Marcello for helping out on this.**Revision 1.1.2.3 2004/07/19 04:08:53 ddiego*more files and directories integration. Added Marcello's Directories example as well**Revision 1.1.2.2 2004/07/18 14:45:19 ddiego*integrated Marcello's new File/Directory API changes into both*the FoundationKit and the ApplicationKit. Many, many thanks go out*to Marcello for a great job with this. This adds much better file searching*capabilities, with many options for how to use it and extend it in the*future.**Revision 1.1.2.1 2004/06/05 01:18:41 marcelloptr*moved some files to the directory where they logically belong**Revision 1.1.2.2 2004/04/29 03:43:15 marcelloptr*reformatting of source files: macros and csvlog and copyright sections**Revision 1.1.2.1 2004/04/28 00:28:20 ddiego*migration towards new directory structure**Revision 1.16.2.1 2004/04/21 02:17:25 ddiego*checking in change to FoundationKit, GraphicsKit and Application*Kit to support unicode in Win32**Revision 1.16 2004/04/03 15:48:47 ddiego*Merged over code from the 0-6-3 branch.**Revision 1.15.2.1 2004/03/19 21:25:57 ddiego*just some minor noodlin**Revision 1.15 2003/12/18 05:16:01 ddiego*merge from devmain-0-6-2 branch into the stable branch**Revision 1.14.4.1 2003/08/20 22:55:18 ddiego*got rid of some older methods for StringUtils, should be using the FilePath*class instead**Revision 1.14 2003/05/17 20:37:36 ddiego*this is the checkin for the 0.6.1 release - represents the merge over from*the devmain-0-6-0 branch plus a few minor bug fixes**Revision 1.13.2.1 2003/03/12 03:12:34 ddiego*switched all member variable that used the "m_"<name> prefix to* <name>"_" suffix nameing standard.*Also changed all vcf builder files to accomadate this.*Changes were made to the Stream classes to NOT multiple inheritance and to*be a little more correct. Changes include breaking the FileStream into two*distinct classes, one for input and one for output.**Revision 1.13 2003/02/26 04:30:50 ddiego*merge of code in the devmain-0-5-9 branch into the current tree.*most additions are in the area of the current linux port, but the major*addition to this release is the addition of a Condition class (currently*still under development) and the change over to using the Delegate class*exclusively from the older event handler macros.**Revision 1.12.8.1 2003/01/08 00:19:53 marcelloptr*mispellings and newlines at the end of all source files**Revision 1.12 2002/09/12 03:26:05 ddiego*merged over the changes from the devmain-0-5-5b branch**Revision 1.11.6.2 2002/07/29 04:46:40 ddiego*add some files to docs and added navigating to a file when*clicking on a class node in the workspace tree**Revision 1.11.6.1 2002/07/15 21:03:11 ddiego*mods to VCFBuilder, fixed some bugs in FilePath**Revision 1.11 2002/05/09 03:10:44 ddiego*merged over code from development branch devmain-0-5-1a into the main CVS trunk**Revision 1.10.4.1 2002/03/20 21:56:56 zzack*Changed Include Style of FoundationKit**Revision 1.10 2002/01/29 04:41:43 ddiego*fixed leak in Win32Button, plus cleaned up some other GetDC stuff and*fixed the Tab problem in Win98.**Revision 1.9 2002/01/24 01:46:49 ddiego*added a cvs "log" comment to the top of all files in vcf/src and vcf/include*to facilitate change tracking**/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?