libxml_utils.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef THIRD_PARTY_LIBXML_CHROMIUM_LIBXML_UTILS_H_
  5. #define THIRD_PARTY_LIBXML_CHROMIUM_LIBXML_UTILS_H_
  6. #include <libxml/xmlreader.h>
  7. #include <string>
  8. // libxml uses a global error function pointer for reporting errors.
  9. // A ScopedXmlErrorFunc object lets you change the global error pointer
  10. // for the duration of the object's lifetime.
  11. class ScopedXmlErrorFunc {
  12. public:
  13. ScopedXmlErrorFunc(void* context, xmlGenericErrorFunc func) {
  14. old_error_func_ = xmlGenericError;
  15. old_error_context_ = xmlGenericErrorContext;
  16. xmlSetGenericErrorFunc(context, func);
  17. }
  18. ~ScopedXmlErrorFunc() {
  19. xmlSetGenericErrorFunc(old_error_context_, old_error_func_);
  20. }
  21. private:
  22. xmlGenericErrorFunc old_error_func_;
  23. void* old_error_context_;
  24. };
  25. namespace internal {
  26. // Converts a libxml xmlChar* into a UTF-8 std::string.
  27. // Null inputs produce an empty string.
  28. std::string XmlStringToStdString(const xmlChar* xmlstring);
  29. } // namespace internal
  30. #endif // THIRD_PARTY_LIBXML_CHROMIUM_INCLUDE_LIBXML_LIBXML_UTILS_H_