aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2020-11-25 08:38:22 -0800
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2020-11-25 08:38:22 -0800
commitdf42668e183b368ad2a7128427bfa3681e45db93 (patch)
tree2ab60d0299f2baf8b8fb27124a2d0732479943b4
parentc258fba6f107da8e99f113446340a59e767fed67 (diff)
downloadpugixml-df42668e183b368ad2a7128427bfa3681e45db93.zip
pugixml-df42668e183b368ad2a7128427bfa3681e45db93.tar.gz
pugixml-df42668e183b368ad2a7128427bfa3681e45db93.tar.bz2
Cleanup code and feature detection
We now use open_file similarly to open_file_wide, and activate the workaround for MSVC 2005+ since that's when the _s versions were added in the first place.
-rw-r--r--src/pugixml.cpp57
1 files changed, 15 insertions, 42 deletions
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> 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> file(fopen(path_, "rb"), impl::close_file);
-#endif
+ auto_deleter<FILE> file(impl::open_file(path_, "rb"), impl::close_file);
return impl::load_file_impl(static_cast<impl::xml_document_struct*>(_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> file(filePtr, impl::close_file);
-
- if (success == false)
- {
- return false;
- }
-#else //_MSC_VER >= 1900
- auto_deleter<FILE> file(fopen(path_, (flags & format_save_file_text) ? "w" : "wb"), impl::close_file);
-#endif
-
+ auto_deleter<FILE> 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);
}