From 4b53fa6f2956ccf4b0cdb98adeceeaf66b67612b Mon Sep 17 00:00:00 2001 From: weidai Date: Sat, 11 Jul 2009 01:48:12 +0000 Subject: handle Unicode filenames git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@472 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- files.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 9 deletions(-) (limited to 'files.cpp') diff --git a/files.cpp b/files.cpp index f9fecbe..4d4f936 100644 --- a/files.cpp +++ b/files.cpp @@ -12,31 +12,58 @@ NAMESPACE_BEGIN(CryptoPP) using namespace std; +#ifndef NDEBUG void Files_TestInstantiations() { FileStore f0; FileSource f1; FileSink f2; } +#endif void FileStore::StoreInitialize(const NameValuePairs ¶meters) { - m_file.reset(new std::ifstream); + m_waiting = false; + m_stream = NULL; + m_file.release(); + const char *fileName; + const wchar_t *fileNameWide; +#ifdef CRYPTOPP_UNIX_AVAILABLE + std::string narrowed; +#endif + if (parameters.GetValue(Name::InputFileName(), fileName)) { +#ifdef CRYPTOPP_UNIX_AVAILABLE +narrowName: +#endif ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0); + m_file.reset(new std::ifstream); m_file->open(fileName, ios::in | binary); if (!*m_file) throw OpenErr(fileName); m_stream = m_file.get(); } - else +#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400 + else if (parameters.GetValue(Name::InputFileNameWide(), fileNameWide)) { - m_stream = NULL; - parameters.GetValue(Name::InputStreamPointer(), m_stream); + #if _MSC_VER >= 1400 + ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0); + m_file.reset(new std::ifstream); + m_file->open(fileNameWide, ios::in | binary); + if (!*m_file) + throw OpenErr(StringNarrow(fileNameWide)); + m_stream = m_file.get(); + #else + narrowed = StringNarrow(fileNameWide); + fileName = narrowed.c_str(); + goto narrowName; + #endif } - m_waiting = false; +#endif + else + parameters.GetValue(Name::InputStreamPointer(), m_stream); } lword FileStore::MaxRetrievable() const @@ -144,6 +171,9 @@ size_t FileStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lwo lword FileStore::Skip(lword skipMax) { + if (!m_stream) + return 0; + lword oldPos = m_stream->tellg(); std::istream::off_type offset; if (!SafeConvert(skipMax, offset)) @@ -154,21 +184,46 @@ lword FileStore::Skip(lword skipMax) void FileSink::IsolatedInitialize(const NameValuePairs ¶meters) { - m_file.reset(new std::ofstream); + m_stream = NULL; + m_file.release(); + const char *fileName; + const wchar_t *fileNameWide; +#ifdef CRYPTOPP_UNIX_AVAILABLE + std::string narrowed; +#endif + if (parameters.GetValue(Name::OutputFileName(), fileName)) { +#ifdef CRYPTOPP_UNIX_AVAILABLE +narrowName: +#endif ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0); + m_file.reset(new std::ofstream); m_file->open(fileName, ios::out | ios::trunc | binary); if (!*m_file) throw OpenErr(fileName); m_stream = m_file.get(); } - else +#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400 + else if (parameters.GetValue(Name::OutputFileNameWide(), fileNameWide)) { - m_stream = NULL; - parameters.GetValue(Name::OutputStreamPointer(), m_stream); + #if _MSC_VER >= 1400 + ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0); + m_file.reset(new std::ofstream); + m_file->open(fileNameWide, ios::out | ios::trunc | binary); + if (!*m_file) + throw OpenErr(StringNarrow(fileNameWide)); + m_stream = m_file.get(); + #else + narrowed = StringNarrow(fileNameWide); + fileName = narrowed.c_str(); + goto narrowName; + #endif } +#endif + else + parameters.GetValue(Name::OutputStreamPointer(), m_stream); } bool FileSink::IsolatedFlush(bool hardFlush, bool blocking) -- cgit v1.2.1