diff --git a/src/pugixml.cpp b/src/pugixml.cpp index ff04421..20da23b 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -4981,20 +4981,12 @@ PUGI__NS_BEGIN #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__) || (defined(__MINGW32__) && (!defined(__STRICT_ANSI__) || defined(__MINGW64_VERSION_MAJOR))) PUGI__FN FILE* open_file_wide(const wchar_t* path, const wchar_t* mode) { -#if defined(_MSC_VER) && _MSC_VER >= 1900 - FILE* file = nullptr; - if (_wfopen_s(&file, path, mode) == 0) - { - return file; - } - else - { - return nullptr; - } +#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 + FILE* file = 0; + return _wfopen_s(&file, path, mode) == 0 ? file : 0; #else return _wfopen(path, mode); #endif - } #else PUGI__FN char* convert_path_heap(const wchar_t* str) @@ -5038,6 +5030,16 @@ PUGI__NS_BEGIN } #endif + PUGI__FN FILE* open_file(const char* path, const char* mode) + { +#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 + FILE* file = 0; + return fopen_s(&file, path, mode) == 0 ? file : 0; +#else + return fopen(path, mode); +#endif + } + PUGI__FN bool save_file_impl(const xml_document& doc, FILE* file, const char_t* indent, unsigned int flags, xml_encoding encoding) { if (!file) return false; @@ -7200,22 +7202,7 @@ namespace pugi reset(); using impl::auto_deleter; // MSVC7 workaround - - -#if defined(_MSC_VER) && _MSC_VER >= 1900 - FILE* filePtr = nullptr; - auto success = fopen_s(&filePtr, path_, "rb") == 0; - auto_deleter file(filePtr, impl::close_file); - - if (success == false) - { - xml_parse_result res{}; - res.status = status_file_not_found; - return res; - } -#else //_MSC_VER >= 1900 - auto_deleter file(fopen(path_, "rb"), impl::close_file); -#endif + auto_deleter file(impl::open_file(path_, "rb"), impl::close_file); return impl::load_file_impl(static_cast(_root), file.data, options, encoding, &_buffer); } @@ -7298,21 +7285,7 @@ namespace pugi PUGI__FN bool xml_document::save_file(const char* path_, const char_t* indent, unsigned int flags, xml_encoding encoding) const { using impl::auto_deleter; // MSVC7 workaround - -#if defined(_MSC_VER) && _MSC_VER >= 1900 - FILE* filePtr = nullptr; - auto success = fopen_s(&filePtr, path_, (flags & format_save_file_text) ? "w" : "wb") == 0; - - auto_deleter file(filePtr, impl::close_file); - - if (success == false) - { - return false; - } -#else //_MSC_VER >= 1900 - auto_deleter file(fopen(path_, (flags & format_save_file_text) ? "w" : "wb"), impl::close_file); -#endif - + auto_deleter file(impl::open_file(path_, (flags & format_save_file_text) ? "w" : "wb"), impl::close_file); return impl::save_file_impl(*this, file.data, indent, flags, encoding); }