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 
5 #ifndef THIRD_PARTY_LIBXML_CHROMIUM_LIBXML_UTILS_H_
6 #define THIRD_PARTY_LIBXML_CHROMIUM_LIBXML_UTILS_H_
7 
8 #include <libxml/xmlreader.h>
9 
10 #include <string>
11 
12 // libxml uses a global error function pointer for reporting errors.
13 // A ScopedXmlErrorFunc object lets you change the global error pointer
14 // for the duration of the object's lifetime.
15 class ScopedXmlErrorFunc {
16  public:
ScopedXmlErrorFunc(void * context,xmlGenericErrorFunc func)17   ScopedXmlErrorFunc(void* context, xmlGenericErrorFunc func) {
18     old_error_func_ = xmlGenericError;
19     old_error_context_ = xmlGenericErrorContext;
20     xmlSetGenericErrorFunc(context, func);
21   }
~ScopedXmlErrorFunc()22   ~ScopedXmlErrorFunc() {
23     xmlSetGenericErrorFunc(old_error_context_, old_error_func_);
24   }
25 
26  private:
27   xmlGenericErrorFunc old_error_func_;
28   void* old_error_context_;
29 };
30 
31 namespace internal {
32 
33 // Converts a libxml xmlChar* into a UTF-8 std::string.
34 // Null inputs produce an empty string.
35 std::string XmlStringToStdString(const xmlChar* xmlstring);
36 
37 }  // namespace internal
38 
39 #endif  // THIRD_PARTY_LIBXML_CHROMIUM_INCLUDE_LIBXML_LIBXML_UTILS_H_
40