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

Go to the SVN repository for this file.

1 /* $Id: xml_value_item.cpp 24311 2011-09-09 20:19:03Z katargir $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Roman Katargin
27 *
28 * File Description:
29 *
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbifloat.h>
34 
35 #include <serial/objectinfo.hpp>
36 
38 
42 
44 
46 {
47  ostream
50 
52  if (objInfo.GetTypeFamily() == eTypeFamilyPointer)
53  objInfo = objInfo.GetPointedObject();
54 
56 
57  const CPrimitiveTypeInfo* primitiveType = objInfo.GetPrimitiveTypeInfo();
58  EPrimitiveValueType primitiveValueType = primitiveType->GetPrimitiveValueType();
59 
60  if (primitiveValueType == ePrimitiveValueEnum) {
61  ostream << " "
66 
67  const CEnumeratedTypeInfo* enumType =
69  const CEnumeratedTypeValues& values = enumType->Values();
70 
72  string valueNum = NStr::IntToString(value);
73  string valueName = values.FindName(value, values.IsInteger());
74  ostream << (valueName.empty() ? valueNum : valueName);
76 
77  if (values.IsInteger()) {
78  ostream
84  }
85  else
86  ostream << CXmlStyles::GetStyle(CXmlStyles::kBracket) << "/>" << NewLine();
87  return;
88  }
89  else if (primitiveValueType == ePrimitiveValueBool) {
90  ostream << " "
95  << (objInfo.GetPrimitiveValueBool() ? "true" : "false")
98  return;
99  }
100  else if (primitiveValueType == ePrimitiveValueSpecial) {
101  ostream << CXmlStyles::GetStyle(CXmlStyles::kBracket) << "/>" << NewLine();
102  return;
103  }
104 
105  ostream << CXmlStyles::GetStyle(CXmlStyles::kBracket) << ">"
107 
108  switch(primitiveValueType) {
109  case ePrimitiveValueInteger : // (signed|unsigned) (char|short|int|long)
110  {{
111  string value;
112  if (objInfo.IsPrimitiveValueSigned())
114  else
116  ostream << value;
117  }}
118  break;
119 
120  case ePrimitiveValueReal :
121  {{
122  double data = objInfo.GetPrimitiveValueDouble();
123  if (isnan(data)) {
124  ostream << CXmlStyles::GetStyle(CXmlStyles::kError) << "NaN"
126  break;
127  }
128  if (!finite(data)) {
129  ostream << CXmlStyles::GetStyle(CXmlStyles::kError) << "Inf"
131  break;
132  }
133 
134  char buffer[512];
135  SIZE_TYPE width;
136  size_t digits = DBL_DIG;
137  if (false) {
138  int shift = int(ceil(log10(fabs(data))));
139  int precision = int(digits - shift);
140  if ( precision < 0 )
141  precision = 0;
142  if ( precision > 64 ) // limit precision of data
143  precision = 64;
144  width = NStr::DoubleToString(data, (unsigned int)precision,
145  buffer, sizeof(buffer));
146  if (precision != 0) {
147  while (buffer[width-1] == '0') {
148  --width;
149  }
150  if (buffer[width-1] == '.') {
151  --width;
152  }
153  }
154  }
155  else
156  width = sprintf(buffer, "%.*g", (int)digits, data);
157 
158  ostream << string(buffer, width);
159  }}
160  break;
161 
162  case ePrimitiveValueString : // string|char*|const char*
163  {{
164  string value = objInfo.GetPrimitiveValueString();
165  x_WriteString(ostream, value);
166  }}
167  break;
168 
170  {{
171  vector<char> value;
173  x_WriteOctetString(ostream, value);
174  }}
175  break;
176 
177  default:
178  _ASSERT(false);
179  break;
180  }
181 
182  ostream
186 }
187 
188 void CXmlValueItem::x_WriteString(CStyledTextOStream& ostream, const string& str) const
189 {
190  char buffer[256];
191  string::const_iterator it;
192  size_t i = 0;
193 
194  for(it = str.begin(); it < str.end(); ++it) {
195  char c = *it;
196 
197  switch(c) {
198  case '&':
199  if ( i > 0) ostream << string(buffer, i), i = 0;
200  ostream << CXmlStyles::GetStyle(CXmlStyles::kEscChar) << "&amp;"
202  break;
203  case '<':
204  if ( i > 0) ostream << string(buffer, i), i = 0;
205  ostream << CXmlStyles::GetStyle(CXmlStyles::kEscChar) << "&lt;"
207  break;
208  case '>':
209  if ( i > 0) ostream << string(buffer, i), i = 0;
210  ostream << CXmlStyles::GetStyle(CXmlStyles::kEscChar) << "&gt;"
212  break;
213  case '\'':
214  if ( i > 0) ostream << string(buffer, i), i = 0;
215  ostream << CXmlStyles::GetStyle(CXmlStyles::kEscChar) << "&apos;"
217  break;
218  case '"':
219  if ( i > 0) ostream << string(buffer, i), i = 0;
220  ostream << CXmlStyles::GetStyle(CXmlStyles::kEscChar) << "&quot;"
222  break;
223  default:
224  if ((unsigned int)c < 0x20) {
225  if ( i > 0) ostream << string(buffer, i), i = 0;
226  strcpy(buffer, "&#x");
227  i = 3;
228  Uint1 ch = c;
229  unsigned hi = ch >> 4;
230  unsigned lo = ch & 0xF;
231  if (hi) buffer[i++] = "0123456789abcdef"[hi];
232  buffer[i++] = "0123456789abcdef"[lo];
233  buffer[i++] = ';';
234 
236  << string(buffer, i)
238  i = 0;
239  }
240  else {
241  buffer[i++] = c;
242  if (i == sizeof(buffer)) ostream << string(buffer, i), i = 0;
243  }
244  }
245  }
246 
247  if ( i > 0) ostream << string(buffer, i);
248 }
249 
250 void CXmlValueItem::x_WriteOctetString(CStyledTextOStream& ostream, const vector<char>& value) const
251 {
252  static const char HEX[] = "0123456789ABCDEF";
253  char buffer[256];
254  size_t i = 0;
255 
256  vector<char>::const_iterator it;
257  for(it = value.begin(); it < value.end(); ++it) {
258  char c = *it;
259  buffer[i++] = HEX[(c >> 4) & 0xf];
260  buffer[i++] = HEX[c & 0xf];
261  if (i == sizeof(buffer)) ostream << string(buffer, i), i = 0;
262  }
263 
264  if ( i > 0) ostream << string(buffer, i);
265 }
266 
CConstObjectInfo –.
Definition: objectinfo.hpp:421
const string m_Tag
static const CTextStyle & GetStyle(Element element)
Definition: xml_styles.cpp:40
virtual void x_RenderXml(CStyledTextOStream &ostream, CTextPanelContext *context) const
void x_WriteOctetString(CStyledTextOStream &ostream, const vector< char > &value) const
void x_WriteString(CStyledTextOStream &ostream, const string &str) const
CConstObjectInfo m_ObjectInfo
static char precision
Definition: genparams.c:28
static const char * str(char *buf, int n)
Definition: stats.c:84
char data[12]
Definition: iconv.c:80
string
Definition: cgiapp.hpp:687
#define isnan
Definition: ncbifloat.h:89
#define finite
Define value of finite (Is Finite).
Definition: ncbifloat.h:109
const string & FindName(TEnumValueType value, bool allowBadValue) const
Find name of the enum by its numeric value.
Definition: enumerated.cpp:146
bool IsInteger(void) const
Check whether the type is defined as INTEGER in ASN.1 spec.
Definition: enumvalues.hpp:75
int TEnumValueType
Definition: serialdef.hpp:232
EPrimitiveValueType
Primitive value type.
Definition: serialdef.hpp:147
static const TObjectType * SafeCast(TTypeInfo type)
Definition: serialutil.hpp:76
@ ePrimitiveValueSpecial
null, void
Definition: serialdef.hpp:148
@ ePrimitiveValueOctetString
vector<(signed|unsigned)? char>
Definition: serialdef.hpp:155
@ ePrimitiveValueString
string|char*|const char*
Definition: serialdef.hpp:153
@ ePrimitiveValueInteger
(signed|unsigned) (char|short|int|long)
Definition: serialdef.hpp:151
@ ePrimitiveValueBool
bool
Definition: serialdef.hpp:149
@ ePrimitiveValueEnum
enum
Definition: serialdef.hpp:154
@ ePrimitiveValueReal
float|double
Definition: serialdef.hpp:152
@ eTypeFamilyPointer
Definition: serialdef.hpp:143
@ eTypeFamilyPrimitive
Definition: serialdef.hpp:139
CConstObjectInfo GetPointedObject(void) const
Get data and type information of object to which this type refers.
Definition: objectinfo.cpp:96
bool GetPrimitiveValueBool(void) const
Get boolean data.
Definition: objectinfo.cpp:144
void GetPrimitiveValueOctetString(vector< char > &value) const
Get octet string data.
Definition: objectinfo.cpp:211
const CPrimitiveTypeInfo * GetPrimitiveTypeInfo(void) const
Definition: objectinfo.cpp:49
ETypeFamily GetTypeFamily(void) const
Get data type family.
void GetPrimitiveValueString(string &value) const
Get string data.
Definition: objectinfo.cpp:199
double GetPrimitiveValueDouble(void) const
Get data as double.
Definition: objectinfo.cpp:194
Int4 GetPrimitiveValueInt4(void) const
Get data as Int4.
Definition: objectinfo.cpp:174
Int8 GetPrimitiveValueInt8(void) const
Get data as Int8.
Definition: objectinfo.cpp:184
bool IsPrimitiveValueSigned(void) const
Is primitive value signed.
Definition: objectinfo.cpp:114
uint8_t Uint1
1-byte (8-bit) unsigned integer
Definition: ncbitype.h:99
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
static string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
Definition: ncbistr.hpp:5159
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static string UInt8ToString(Uint8 value, TNumToStringFlags flags=0, int base=10)
Convert UInt8 to string.
Definition: ncbistr.hpp:5168
EPrimitiveValueType GetPrimitiveValueType(void) const
const CEnumeratedTypeValues & Values(void) const
Definition: enumerated.hpp:57
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define fabs(v)
Definition: ncbi_dispd.c:46
Floating-point support routines.
T log10(T x_)
static const char HEX[]
Definition: objostrasn.cpp:873
static pcre_uint8 * buffer
Definition: pcretest.c:1051
#define _ASSERT
Modified on Wed Apr 17 13:08:55 2024 by modify_doxy.py rev. 669887