NCBI C++ ToolKit
utility.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3  * All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name of the Author nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * $Id: utility.cpp 63714 2014-07-21 13:22:05Z satskyse $
35  * NOTE: This file was modified from its original version 0.6.0
36  * to fit the NCBI C++ Toolkit build framework and
37  * API and functionality requirements.
38  */
39 
40 #include "utility.hpp"
41 #include <cstdarg>
42 #include <cstdlib>
43 #include <cstring>
44 #include <string>
45 #include <iostream>
46 
47 // libxml2 includes
48 #include <libxml/xmlversion.h>
49 #include <libxml/xmlsave.h>
50 
51 // xmlwrapp includes
53 
54 
55 // hack to pull in vsnprintf for MSVC
56 #if defined(_MSC_VER) || (defined(__COMO__) && defined(__WIN32__))
57 # undef vsnprintf
58 # define vsnprintf _vsnprintf
59 #endif
60 
61 namespace xml {
62 
64  const std::string & msg)
65 {
66  int as_int = static_cast<int>(value);
67 
68  if (as_int < 0)
69  throw xml::exception(msg);
70  if (value != static_cast<std::size_t>(as_int))
71  throw xml::exception(msg);
72  return as_int;
73 }
74 
75 
76 namespace impl {
77 
78  //####################################################################
79  void printf2string (std::string &s, const char *message, va_list ap) {
80  char buffer[512];
81 
82  std::memset(buffer, 0, sizeof(buffer));
83 
84  // XXX vsnprintf is non-standard
85  if (vsnprintf(buffer, sizeof(buffer), message, ap) > 0) {
86  std::string::size_type size = std::strlen(buffer);
87 
88  if (buffer[size-1] == '\n')
89  --size;
90  s.assign(buffer, size);
91  }
92  }
93  //####################################################################
94  bool ns_util::node_ns_match (xmlNode *nd, const ns *nspace) {
95  if (nd == NULL)
96  throw exception( "Internal logic error. Node must be supplied to "
97  "check matching a namespace." );
98 
99  // NULL namespace matches everything
100  if (nspace == NULL)
101  return true;
102 
103  if (nd->ns == NULL)
104  return nspace->is_void();
105 
106  return ns(nd->ns) == (*nspace);
107  }
108  //####################################################################
109  bool ns_util::attr_ns_match (xmlAttr *at, const ns *nspace) {
110  if (at == NULL)
111  throw exception( "Internal logic error. Attribute must be "
112  "supplied to check matching a namespace." );
113 
114  // NULL namespace matches everything
115  if (nspace == NULL)
116  return true;
117 
118  if (at->ns == NULL)
119  return nspace->is_void();
120 
121  return ns(at->ns) == (*nspace);
122  }
123  //####################################################################
124  bool ns_util::default_attr_ns_match (xmlAttribute *dat, const ns *nspace) {
125  if (dat == NULL)
126  throw exception( "Internal logic error. Default attribute must be"
127  " supplied to check matching a namespace." );
128 
129  // NULL namespace matches everything
130  if (nspace == NULL)
131  return true;
132 
133  // Default attributes do not have prefix and uri. They have only
134  // prefix.
135  if (dat->prefix == NULL)
136  return nspace->is_void();
137 
138  return strcmp(nspace->get_prefix(),
139  reinterpret_cast<const char*>(dat->prefix)) == 0;
140  }
141  //####################################################################
143  int libxml2_options = 0;
144 
145  // The if statements below are to avoid problems if the libxml2
146  // enumeration values are extended or change their positions
147  if ((options & save_op_no_format) == 0)
148  libxml2_options |= XML_SAVE_FORMAT;
149  if (options & save_op_no_decl)
150  libxml2_options |= XML_SAVE_NO_DECL;
151  if (options & save_op_no_empty)
152  libxml2_options |= XML_SAVE_NO_EMPTY;
153  if (options & save_op_no_xhtml)
154  libxml2_options |= XML_SAVE_NO_XHTML;
155 
156  #if LIBXML_VERSION >= 20702
157  if (options & save_op_xhtml)
158  libxml2_options |= XML_SAVE_XHTML;
159  if ((options & save_op_not_as_xml) == 0)
160  libxml2_options |= XML_SAVE_AS_XML;
161  if (options & save_op_as_html)
162  libxml2_options |= XML_SAVE_AS_HTML;
163  #endif
164 
165  #if LIBXML_VERSION >= 20708
166  if (options & save_op_with_non_significant_ws) {
167  // libxml2 checks XML_SAVE_WSNONSIG only if XML_SAVE_FORMAT is not
168  // set therefore the XML_SAVE_FORMAT must be removed.
169  libxml2_options &= ~XML_SAVE_FORMAT;
170  libxml2_options |= XML_SAVE_WSNONSIG;
171  }
172  #endif
173 
174  return libxml2_options;
175  }
176  //####################################################################
177  int save_to_stream_cb (void *ctx, const char *buf, int len) {
178  std::ostream *dst = reinterpret_cast<std::ostream*>(ctx);
179  dst->write(buf, len);
180  return len;
181  }
182  //####################################################################
183  int save_to_string_cb (void *ctx, const char *buf, int len) {
184  std::string *dst = reinterpret_cast<std::string*>(ctx);
185  dst->append(buf, len);
186  return len;
187  }
188  //####################################################################
189 
190 } // end impl namespace
191 
192 } // end xml namespace
This exception class is thrown by xmlwrapp for all runtime XML-related errors along with the xml::par...
Definition: exception.hpp:64
The xml::ns class is used to access and handle namespaces of nodes and attributes.
Definition: namespace.hpp:64
const char * get_prefix(void) const
Get the namespace prefix.
Definition: namespace.cpp:95
bool is_void(void) const
If a node or an attribute has no namespace, then a namespace with empty prefix and empty URI is retur...
Definition: namespace.cpp:123
CS_CONTEXT * ctx
Definition: t0006.c:12
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
char * buf
int len
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
int save_to_string_cb(void *ctx, const char *buf, int len)
Definition: utility.cpp:183
int convert_to_libxml2_save_options(int options)
Definition: utility.cpp:142
void printf2string(std::string &s, const char *message, va_list ap)
Definition: utility.cpp:79
int save_to_stream_cb(void *ctx, const char *buf, int len)
Definition: utility.cpp:177
XML library namespace.
Definition: attributes.hpp:57
@ save_op_no_format
Do not format save output.
Definition: xml_save.hpp:59
@ save_op_as_html
Force HTML serialization on XML doc. Ignored if libxml2 version is < 2.7.2.
Definition: xml_save.hpp:65
@ save_op_no_xhtml
Disable XHTML1 specific rules.
Definition: xml_save.hpp:62
@ save_op_with_non_significant_ws
Format with non-significant whitespace.
Definition: xml_save.hpp:70
@ save_op_no_decl
Drop the xml declaration.
Definition: xml_save.hpp:60
@ save_op_no_empty
No empty tags.
Definition: xml_save.hpp:61
@ save_op_not_as_xml
Do not force XML serialization on HTML doc. Ignored if libxml2 version is < 2.7.2.
Definition: xml_save.hpp:64
@ save_op_xhtml
Force XHTML1 specific rules. Ignored if libxml2 version is < 2.7.2.
Definition: xml_save.hpp:63
int size_t_to_int_conversion(std::size_t value, const std::string &msg)
Definition: utility.cpp:63
#define vsnprintf
Definition: ncbiconf_msvc.h:66
static pcre_uint8 * buffer
Definition: pcretest.c:1051
static bool node_ns_match(xmlNode *nd, const ns *nspace)
Definition: utility.cpp:94
static bool attr_ns_match(xmlAttr *at, const ns *nspace)
Definition: utility.cpp:109
static bool default_attr_ns_match(xmlAttribute *dat, const ns *nspace)
Definition: utility.cpp:124
Flags for saving XML documents and nodes.
Modified on Mon May 27 04:37:30 2024 by modify_doxy.py rev. 669887