XmlRpc::XmlRpcValue Class Reference

#include <XmlRpcValue.h>

List of all members.


Detailed Description

A class to represent RPC arguments and results.

Each XmlRpcValue object contains a typed value, where the type is determined by the initial value assigned to the object.


Public Types

enum  Type {
  TypeInvalid, TypeBoolean, TypeInt, TypeDouble,
  TypeString, TypeDateTime, TypeBase64, TypeArray,
  TypeStruct
}
 XmlRpcValue types. More...
typedef std::vector< char > BinaryData
typedef std::vector< XmlRpcValueValueArray
typedef std::map< std::string,
XmlRpcValue
ValueStruct

Public Member Functions

 XmlRpcValue ()
 Construct an empty XmlRpcValue.
 XmlRpcValue (bool value)
 Construct an XmlRpcValue with a bool value.
 XmlRpcValue (int value)
 Construct an XmlRpcValue with an int value.
 XmlRpcValue (double value)
 Construct an XmlRpcValue with a double value.
 XmlRpcValue (std::string const &value)
 Construct an XmlRpcValue with a string value.
 XmlRpcValue (const char *value)
 Construct an XmlRpcValue with a string value.
 XmlRpcValue (struct tm *value)
 Construct an XmlRpcValue with a date/time value.
 XmlRpcValue (void *value, int nBytes)
 Construct an XmlRpcValue with a binary data value.
 XmlRpcValue (std::string const &xml, int *offset)
 Construct from xml, beginning at *offset chars into the string, updates offset.
 XmlRpcValue (XmlRpcValue const &rhs)
 Copy constructor.
 ~XmlRpcValue ()
 Destructor (make virtual if you want to subclass).
void clear ()
 Erase the current value.
XmlRpcValueoperator= (XmlRpcValue const &rhs)
 Assignment from one XmlRpcValue to this one.
XmlRpcValueoperator= (int const &rhs)
 Assign an int to this XmlRpcValue.
XmlRpcValueoperator= (double const &rhs)
 Assign a double to this XmlRpcValue.
XmlRpcValueoperator= (const char *rhs)
 Assign a string to this XmlRpcValue.
bool operator== (XmlRpcValue const &other) const
 Tests two XmlRpcValues for equality.
bool operator!= (XmlRpcValue const &other) const
 Tests two XmlRpcValues for inequality.
 operator bool & ()
 Treat an XmlRpcValue as a bool.
 operator int & ()
 Treat an XmlRpcValue as an int.
 operator double & ()
 Treat an XmlRpcValue as a double.
 operator std::string & ()
 Treat an XmlRpcValue as a string.
 operator BinaryData & ()
 Access the BinaryData value.
 operator struct tm & ()
 Access the DateTime value.
XmlRpcValue const & operator[] (int i) const
 Const array value accessor.
XmlRpcValueoperator[] (int i)
 Array value accessor.
XmlRpcValueoperator[] (std::string const &k)
 Struct entry accessor.
XmlRpcValueoperator[] (const char *k)
 Struct entry accessor.
 operator ValueStruct const & ()
 Access the struct value map.
bool valid () const
 Return true if the value has been set to something.
Type const & getType () const
 Return the type of the value stored.
int size () const
 Return the size for string, base64, array, and struct values.
void setSize (int size)
 Specify the size for array values. Array values will grow beyond this size if needed.
bool hasMember (const std::string &name) const
 Check for the existence of a struct member by name.
bool fromXml (std::string const &valueXml, int *offset)
 Decode xml. Destroys any existing value.
std::string toXml () const
 Encode the Value in xml.
std::ostream & write (std::ostream &os) const
 Write the value (no xml encoding).

Static Public Member Functions

static std::string const & getDoubleFormat ()
 Return the format used to write double values.
static void setDoubleFormat (const char *f)
 Specify the format used to write double values.

Protected Member Functions

void invalidate ()
void assertTypeOrInvalid (Type t)
void assertArray (int size) const
void assertArray (int size)
void assertStruct ()
bool boolFromXml (std::string const &valueXml, int *offset)
bool intFromXml (std::string const &valueXml, int *offset)
bool doubleFromXml (std::string const &valueXml, int *offset)
bool stringFromXml (std::string const &valueXml, int *offset)
bool timeFromXml (std::string const &valueXml, int *offset)
bool binaryFromXml (std::string const &valueXml, int *offset)
bool arrayFromXml (std::string const &valueXml, int *offset)
bool structFromXml (std::string const &valueXml, int *offset)
std::string boolToXml () const
std::string intToXml () const
std::string doubleToXml () const
std::string stringToXml () const
std::string timeToXml () const
std::string binaryToXml () const
std::string arrayToXml () const
std::string structToXml () const

Protected Attributes

Type _type
union {
   bool   asBool
   int   asInt
   double   asDouble
   tm *   asTime
   std::string *   asString
   BinaryData *   asBinary
   ValueArray *   asArray
   ValueStruct *   asStruct
_value

Static Protected Attributes

static std::string _doubleFormat


Member Typedef Documentation

typedef std::vector<char> XmlRpc::XmlRpcValue::BinaryData

typedef std::vector<XmlRpcValue> XmlRpc::XmlRpcValue::ValueArray

typedef std::map<std::string, XmlRpcValue> XmlRpc::XmlRpcValue::ValueStruct


Member Enumeration Documentation

enum XmlRpc::XmlRpcValue::Type

XmlRpcValue types.

Enumerator:
TypeInvalid 
TypeBoolean 
TypeInt 
TypeDouble 
TypeString 
TypeDateTime 
TypeBase64 
TypeArray 
TypeStruct 
00030               {
00031       TypeInvalid,
00032       TypeBoolean,
00033       TypeInt,
00034       TypeDouble,
00035       TypeString,
00036       TypeDateTime,
00037       TypeBase64,
00038       TypeArray,
00039       TypeStruct
00040     };


Constructor & Destructor Documentation

XmlRpc::XmlRpcValue::XmlRpcValue (  )  [inline]

Construct an empty XmlRpcValue.

00050 : _type(TypeInvalid) { _value.asBinary = 0; }

XmlRpc::XmlRpcValue::XmlRpcValue ( bool  value  )  [inline]

Construct an XmlRpcValue with a bool value.

00053 : _type(TypeBoolean) { _value.asBool = value; }

XmlRpc::XmlRpcValue::XmlRpcValue ( int  value  )  [inline]

Construct an XmlRpcValue with an int value.

00056 : _type(TypeInt) { _value.asInt = value; }

XmlRpc::XmlRpcValue::XmlRpcValue ( double  value  )  [inline]

Construct an XmlRpcValue with a double value.

00059 : _type(TypeDouble) { _value.asDouble = value; }

XmlRpc::XmlRpcValue::XmlRpcValue ( std::string const &  value  )  [inline]

Construct an XmlRpcValue with a string value.

00062                                         : _type(TypeString) 
00063     { _value.asString = new std::string(value); }

XmlRpc::XmlRpcValue::XmlRpcValue ( const char *  value  )  [inline]

Construct an XmlRpcValue with a string value.

Parameters:
value A null-terminated (C) string.
00067                                     : _type(TypeString)
00068     { _value.asString = new std::string(value); }

XmlRpc::XmlRpcValue::XmlRpcValue ( struct tm *  value  )  [inline]

Construct an XmlRpcValue with a date/time value.

Parameters:
value A pointer to a struct tm (see localtime)
00072                                    : _type(TypeDateTime) 
00073     { _value.asTime = new struct tm(*value); }

XmlRpc::XmlRpcValue::XmlRpcValue ( void *  value,
int  nBytes 
) [inline]

Construct an XmlRpcValue with a binary data value.

Parameters:
value A pointer to data
nBytes The length of the data pointed to, in bytes
00078                                           : _type(TypeBase64)
00079     {
00080       _value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
00081     }

XmlRpc::XmlRpcValue::XmlRpcValue ( std::string const &  xml,
int *  offset 
) [inline]

Construct from xml, beginning at *offset chars into the string, updates offset.

00084                                                    : _type(TypeInvalid)
00085     { if ( ! fromXml(xml,offset)) _type = TypeInvalid; }

XmlRpc::XmlRpcValue::XmlRpcValue ( XmlRpcValue const &  rhs  )  [inline]

Copy constructor.

00088 : _type(TypeInvalid) { *this = rhs; }

XmlRpc::XmlRpcValue::~XmlRpcValue (  )  [inline]

Destructor (make virtual if you want to subclass).

00091 { invalidate(); }


Member Function Documentation

void XmlRpc::XmlRpcValue::clear (  )  [inline]

Erase the current value.

00094 { invalidate(); }

XmlRpcValue & XmlRpc::XmlRpcValue::operator= ( XmlRpcValue const &  rhs  ) 

Assignment from one XmlRpcValue to this one.

Parameters:
rhs The value in rhs is copied to this value.
00120   {
00121     if (this != &rhs)
00122     {
00123       invalidate();
00124       _type = rhs._type;
00125       switch (_type) {
00126         case TypeBoolean:  _value.asBool = rhs._value.asBool; break;
00127         case TypeInt:      _value.asInt = rhs._value.asInt; break;
00128         case TypeDouble:   _value.asDouble = rhs._value.asDouble; break;
00129         case TypeDateTime: _value.asTime = new struct tm(*rhs._value.asTime); break;
00130         case TypeString:   _value.asString = new std::string(*rhs._value.asString); break;
00131         case TypeBase64:   _value.asBinary = new BinaryData(*rhs._value.asBinary); break;
00132         case TypeArray:    _value.asArray = new ValueArray(*rhs._value.asArray); break;
00133         case TypeStruct:   _value.asStruct = new ValueStruct(*rhs._value.asStruct); break;
00134         default:           _value.asBinary = 0; break;
00135       }
00136     }
00137     return *this;
00138   }

XmlRpcValue& XmlRpc::XmlRpcValue::operator= ( int const &  rhs  )  [inline]

Assign an int to this XmlRpcValue.

00102 { return operator=(XmlRpcValue(rhs)); }

XmlRpcValue& XmlRpc::XmlRpcValue::operator= ( double const &  rhs  )  [inline]

Assign a double to this XmlRpcValue.

00105 { return operator=(XmlRpcValue(rhs)); }

XmlRpcValue& XmlRpc::XmlRpcValue::operator= ( const char *  rhs  )  [inline]

Assign a string to this XmlRpcValue.

00108 { return operator=(XmlRpcValue(std::string(rhs))); }

bool XmlRpc::XmlRpcValue::operator== ( XmlRpcValue const &  other  )  const

Tests two XmlRpcValues for equality.

00149   {
00150     if (_type != other._type)
00151       return false;
00152 
00153     switch (_type) {
00154       case TypeBoolean:  return ( !_value.asBool && !other._value.asBool) ||
00155                                 ( _value.asBool && other._value.asBool);
00156       case TypeInt:      return _value.asInt == other._value.asInt;
00157       case TypeDouble:   return _value.asDouble == other._value.asDouble;
00158       case TypeDateTime: return tmEq(*_value.asTime, *other._value.asTime);
00159       case TypeString:   return *_value.asString == *other._value.asString;
00160       case TypeBase64:   return *_value.asBinary == *other._value.asBinary;
00161       case TypeArray:    return *_value.asArray == *other._value.asArray;
00162 
00163       // The map<>::operator== requires the definition of value< for kcc
00164       case TypeStruct:   //return *_value.asStruct == *other._value.asStruct;
00165         {
00166           if (_value.asStruct->size() != other._value.asStruct->size())
00167             return false;
00168           
00169           ValueStruct::const_iterator it1=_value.asStruct->begin();
00170           ValueStruct::const_iterator it2=other._value.asStruct->begin();
00171           while (it1 != _value.asStruct->end()) {
00172             const XmlRpcValue& v1 = it1->second;
00173             const XmlRpcValue& v2 = it2->second;
00174             if ( ! (v1 == v2))
00175               return false;
00176             it1++;
00177             it2++;
00178           }
00179           return true;
00180         }
00181       default: break;
00182     }
00183     return true;    // Both invalid values ...
00184   }

bool XmlRpc::XmlRpcValue::operator!= ( XmlRpcValue const &  other  )  const

Tests two XmlRpcValues for inequality.

00187   {
00188     return !(*this == other);
00189   }

XmlRpc::XmlRpcValue::operator bool & (  )  [inline]

Treat an XmlRpcValue as a bool.

Throws XmlRpcException if the value is initialized to a type that is not TypeBoolean.

00119 { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }

XmlRpc::XmlRpcValue::operator int & (  )  [inline]

Treat an XmlRpcValue as an int.

Throws XmlRpcException if the value is initialized to a type that is not TypeInt.

00124 { assertTypeOrInvalid(TypeInt); return _value.asInt; }

XmlRpc::XmlRpcValue::operator double & (  )  [inline]

Treat an XmlRpcValue as a double.

Throws XmlRpcException if the value is initialized to a type that is not TypeDouble.

00129 { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }

XmlRpc::XmlRpcValue::operator std::string & (  )  [inline]

Treat an XmlRpcValue as a string.

Throws XmlRpcException if the value is initialized to a type that is not TypeString.

00134 { assertTypeOrInvalid(TypeString); return *_value.asString; }

XmlRpc::XmlRpcValue::operator BinaryData & (  )  [inline]

Access the BinaryData value.

Throws XmlRpcException if the value is initialized to a type that is not TypeBase64.

00139 { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }

XmlRpc::XmlRpcValue::operator struct tm & (  )  [inline]

Access the DateTime value.

Throws XmlRpcException if the value is initialized to a type that is not TypeDateTime.

00144 { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }

XmlRpcValue const& XmlRpc::XmlRpcValue::operator[] ( int  i  )  const [inline]

Const array value accessor.

Access the ith value of the array. Throws XmlRpcException if the value is not an array or if the index i is not a valid index for the array.

00151 { assertArray(i+1); return _value.asArray->at(i); }

XmlRpcValue& XmlRpc::XmlRpcValue::operator[] ( int  i  )  [inline]

Array value accessor.

Access the ith value of the array, growing the array if necessary. Throws XmlRpcException if the value is not an array.

00156 { assertArray(i+1); return _value.asArray->at(i); }

XmlRpcValue& XmlRpc::XmlRpcValue::operator[] ( std::string const &  k  )  [inline]

Struct entry accessor.

Returns the value associated with the given entry, creating one if necessary.

00160 { assertStruct(); return (*_value.asStruct)[k]; }

XmlRpcValue& XmlRpc::XmlRpcValue::operator[] ( const char *  k  )  [inline]

Struct entry accessor.

Returns the value associated with the given entry, creating one if necessary.

00164 { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }

XmlRpc::XmlRpcValue::operator ValueStruct const & (  )  [inline]

Access the struct value map.

Can be used to iterate over the entries in the map to find all defined entries.

00168 { assertStruct(); return *_value.asStruct; } 

bool XmlRpc::XmlRpcValue::valid (  )  const [inline]

Return true if the value has been set to something.

00172 { return _type != TypeInvalid; }

Type const& XmlRpc::XmlRpcValue::getType (  )  const [inline]

Return the type of the value stored.

See also:
Type.
00175 { return _type; }

int XmlRpc::XmlRpcValue::size (  )  const

Return the size for string, base64, array, and struct values.

00194   {
00195     switch (_type) {
00196       case TypeString: return int(_value.asString->size());
00197       case TypeBase64: return int(_value.asBinary->size());
00198       case TypeArray:  return int(_value.asArray->size());
00199       case TypeStruct: return int(_value.asStruct->size());
00200       default: break;
00201     }
00202 
00203     throw XmlRpcException("type error");
00204   }

void XmlRpc::XmlRpcValue::setSize ( int  size  )  [inline]

Specify the size for array values. Array values will grow beyond this size if needed.

00181 { assertArray(size); }

bool XmlRpc::XmlRpcValue::hasMember ( const std::string &  name  )  const

Check for the existence of a struct member by name.

00208   {
00209     return _type == TypeStruct && _value.asStruct->find(name) != _value.asStruct->end();
00210   }

bool XmlRpc::XmlRpcValue::fromXml ( std::string const &  valueXml,
int *  offset 
)

Decode xml. Destroys any existing value.

00215   {
00216     int savedOffset = *offset;
00217 
00218     invalidate();
00219     if ( ! XmlRpcUtil::nextTagIs(VALUE_TAG, valueXml, offset))
00220       return false;       // Not a value, offset not updated
00221 
00222         int afterValueOffset = *offset;
00223     std::string typeTag = XmlRpcUtil::getNextTag(valueXml, offset);
00224     bool result = false;
00225     if (typeTag == BOOLEAN_TAG)
00226       result = boolFromXml(valueXml, offset);
00227     else if (typeTag == I4_TAG || typeTag == INT_TAG)
00228       result = intFromXml(valueXml, offset);
00229     else if (typeTag == DOUBLE_TAG)
00230       result = doubleFromXml(valueXml, offset);
00231     else if (typeTag.empty() || typeTag == STRING_TAG)
00232       result = stringFromXml(valueXml, offset);
00233     else if (typeTag == DATETIME_TAG)
00234       result = timeFromXml(valueXml, offset);
00235     else if (typeTag == BASE64_TAG)
00236       result = binaryFromXml(valueXml, offset);
00237     else if (typeTag == ARRAY_TAG)
00238       result = arrayFromXml(valueXml, offset);
00239     else if (typeTag == STRUCT_TAG)
00240       result = structFromXml(valueXml, offset);
00241     // Watch for empty/blank strings with no <string>tag
00242     else if (typeTag == VALUE_ETAG)
00243     {
00244       *offset = afterValueOffset;   // back up & try again
00245       result = stringFromXml(valueXml, offset);
00246     }
00247 
00248     if (result)  // Skip over the </value> tag
00249       XmlRpcUtil::findTag(VALUE_ETAG, valueXml, offset);
00250     else        // Unrecognized tag after <value>
00251       *offset = savedOffset;
00252 
00253     return result;
00254   }

std::string XmlRpc::XmlRpcValue::toXml (  )  const

Encode the Value in xml.

00258   {
00259     switch (_type) {
00260       case TypeBoolean:  return boolToXml();
00261       case TypeInt:      return intToXml();
00262       case TypeDouble:   return doubleToXml();
00263       case TypeString:   return stringToXml();
00264       case TypeDateTime: return timeToXml();
00265       case TypeBase64:   return binaryToXml();
00266       case TypeArray:    return arrayToXml();
00267       case TypeStruct:   return structToXml();
00268       default: break;
00269     }
00270     return std::string();   // Invalid value
00271   }

std::ostream & XmlRpc::XmlRpcValue::write ( std::ostream &  os  )  const

Write the value (no xml encoding).

00545                                                      {
00546     switch (_type) {
00547       default:           break;
00548       case TypeBoolean:  os << _value.asBool; break;
00549       case TypeInt:      os << _value.asInt; break;
00550       case TypeDouble:   os << _value.asDouble; break;
00551       case TypeString:   os << *_value.asString; break;
00552       case TypeDateTime:
00553         {
00554           struct tm* t = _value.asTime;
00555           char buf[20];
00556           snprintf(buf, sizeof(buf)-1, "%4d%02d%02dT%02d:%02d:%02d", 
00557             t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
00558           buf[sizeof(buf)-1] = 0;
00559           os << buf;
00560           break;
00561         }
00562       case TypeBase64:
00563         {
00564           int iostatus = 0;
00565           std::ostreambuf_iterator<char> out(os);
00566           base64<char> encoder;
00567           encoder.put(_value.asBinary->begin(), _value.asBinary->end(), out, iostatus, base64<>::crlf());
00568           break;
00569         }
00570       case TypeArray:
00571         {
00572           int s = int(_value.asArray->size());
00573           os << '{';
00574           for (int i=0; i<s; ++i)
00575           {
00576             if (i > 0) os << ',';
00577             _value.asArray->at(i).write(os);
00578           }
00579           os << '}';
00580           break;
00581         }
00582       case TypeStruct:
00583         {
00584           os << '[';
00585           ValueStruct::const_iterator it;
00586           for (it=_value.asStruct->begin(); it!=_value.asStruct->end(); ++it)
00587           {
00588             if (it!=_value.asStruct->begin()) os << ',';
00589             os << it->first << ':';
00590             it->second.write(os);
00591           }
00592           os << ']';
00593           break;
00594         }
00595       
00596     }
00597     
00598     return os;
00599   }

static std::string const& XmlRpc::XmlRpcValue::getDoubleFormat (  )  [inline, static]

Return the format used to write double values.

00197 { return _doubleFormat; }

static void XmlRpc::XmlRpcValue::setDoubleFormat ( const char *  f  )  [inline, static]

Specify the format used to write double values.

00200 { _doubleFormat = f; }

void XmlRpc::XmlRpcValue::invalidate (  )  [protected]

00054   {
00055     switch (_type) {
00056       case TypeString:    delete _value.asString; break;
00057       case TypeDateTime:  delete _value.asTime;   break;
00058       case TypeBase64:    delete _value.asBinary; break;
00059       case TypeArray:     delete _value.asArray;  break;
00060       case TypeStruct:    delete _value.asStruct; break;
00061       default: break;
00062     }
00063     _type = TypeInvalid;
00064     _value.asBinary = 0;
00065   }

void XmlRpc::XmlRpcValue::assertTypeOrInvalid ( Type  t  )  [protected]

00070   {
00071     if (_type == TypeInvalid)
00072     {
00073       _type = t;
00074       switch (_type) {    // Ensure there is a valid value for the type
00075         case TypeString:   _value.asString = new std::string(); break;
00076         case TypeDateTime: _value.asTime = new struct tm();     break;
00077         case TypeBase64:   _value.asBinary = new BinaryData();  break;
00078         case TypeArray:    _value.asArray = new ValueArray();   break;
00079         case TypeStruct:   _value.asStruct = new ValueStruct(); break;
00080         default:           _value.asBinary = 0; break;
00081       }
00082     }
00083     else if (_type != t)
00084       throw XmlRpcException("type error");
00085   }

void XmlRpc::XmlRpcValue::assertArray ( int  size  )  const [protected]

00088   {
00089     if (_type != TypeArray)
00090       throw XmlRpcException("type error: expected an array");
00091     else if (int(_value.asArray->size()) < size)
00092       throw XmlRpcException("range error: array index too large");
00093   }

void XmlRpc::XmlRpcValue::assertArray ( int  size  )  [protected]

00097   {
00098     if (_type == TypeInvalid) {
00099       _type = TypeArray;
00100       _value.asArray = new ValueArray(size);
00101     } else if (_type == TypeArray) {
00102       if (int(_value.asArray->size()) < size)
00103         _value.asArray->resize(size);
00104     } else
00105       throw XmlRpcException("type error: expected an array");
00106   }

void XmlRpc::XmlRpcValue::assertStruct (  )  [protected]

00109   {
00110     if (_type == TypeInvalid) {
00111       _type = TypeStruct;
00112       _value.asStruct = new ValueStruct();
00113     } else if (_type != TypeStruct)
00114       throw XmlRpcException("type error: expected a struct");
00115   }

bool XmlRpc::XmlRpcValue::boolFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00276   {
00277     const char* valueStart = valueXml.c_str() + *offset;
00278     char* valueEnd;
00279     long ivalue = strtol(valueStart, &valueEnd, 10);
00280     if (valueEnd == valueStart || (ivalue != 0 && ivalue != 1))
00281       return false;
00282 
00283     _type = TypeBoolean;
00284     _value.asBool = (ivalue == 1);
00285     *offset += int(valueEnd - valueStart);
00286     return true;
00287   }

bool XmlRpc::XmlRpcValue::intFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00301   {
00302     const char* valueStart = valueXml.c_str() + *offset;
00303     char* valueEnd;
00304     long ivalue = strtol(valueStart, &valueEnd, 10);
00305     if (valueEnd == valueStart)
00306       return false;
00307 
00308     _type = TypeInt;
00309     _value.asInt = int(ivalue);
00310     *offset += int(valueEnd - valueStart);
00311     return true;
00312   }

bool XmlRpc::XmlRpcValue::doubleFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00329   {
00330     const char* valueStart = valueXml.c_str() + *offset;
00331     char* valueEnd;
00332     double dvalue = strtod(valueStart, &valueEnd);
00333     if (valueEnd == valueStart)
00334       return false;
00335 
00336     _type = TypeDouble;
00337     _value.asDouble = dvalue;
00338     *offset += int(valueEnd - valueStart);
00339     return true;
00340   }

bool XmlRpc::XmlRpcValue::stringFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00358   {
00359     size_t valueEnd = valueXml.find('<', *offset);
00360     if (valueEnd == std::string::npos)
00361       return false;     // No end tag;
00362 
00363     _type = TypeString;
00364     _value.asString = new std::string(XmlRpcUtil::xmlDecode(valueXml.substr(*offset, valueEnd-*offset)));
00365     *offset += int(_value.asString->length());
00366     return true;
00367   }

bool XmlRpc::XmlRpcValue::timeFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00381   {
00382     size_t valueEnd = valueXml.find('<', *offset);
00383     if (valueEnd == std::string::npos)
00384       return false;     // No end tag;
00385 
00386     std::string stime = valueXml.substr(*offset, valueEnd-*offset);
00387 
00388     struct tm t;
00389     if (sscanf(stime.c_str(),"%4d%2d%2dT%2d:%2d:%2d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec) != 6)
00390       return false;
00391 
00392     t.tm_year -= 1900;
00393     t.tm_isdst = -1;
00394     _type = TypeDateTime;
00395     _value.asTime = new struct tm(t);
00396     *offset += int(stime.length());
00397     return true;
00398   }

bool XmlRpc::XmlRpcValue::binaryFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00419   {
00420     size_t valueEnd = valueXml.find('<', *offset);
00421     if (valueEnd == std::string::npos)
00422       return false;     // No end tag;
00423 
00424     _type = TypeBase64;
00425     std::string asString = valueXml.substr(*offset, valueEnd-*offset);
00426     _value.asBinary = new BinaryData();
00427     // check whether base64 encodings can contain chars xml encodes...
00428 
00429     // convert from base64 to binary
00430     int iostatus = 0;
00431           base64<char> decoder;
00432     std::back_insert_iterator<BinaryData> ins = std::back_inserter(*(_value.asBinary));
00433                 decoder.get(asString.begin(), asString.end(), ins, iostatus);
00434 
00435     *offset += int(asString.length());
00436     return true;
00437   }

bool XmlRpc::XmlRpcValue::arrayFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00461   {
00462     if ( ! XmlRpcUtil::nextTagIs(DATA_TAG, valueXml, offset))
00463       return false;
00464 
00465     _type = TypeArray;
00466     _value.asArray = new ValueArray;
00467     XmlRpcValue v;
00468     while (v.fromXml(valueXml, offset))
00469       _value.asArray->push_back(v);       // copy...
00470 
00471     // Skip the trailing </data>
00472     (void) XmlRpcUtil::nextTagIs(DATA_ETAG, valueXml, offset);
00473     return true;
00474   }

bool XmlRpc::XmlRpcValue::structFromXml ( std::string const &  valueXml,
int *  offset 
) [protected]

00498   {
00499     _type = TypeStruct;
00500     _value.asStruct = new ValueStruct;
00501 
00502     while (XmlRpcUtil::nextTagIs(MEMBER_TAG, valueXml, offset)) {
00503       // name
00504       const std::string name = XmlRpcUtil::parseTag(NAME_TAG, valueXml, offset);
00505       // value
00506       XmlRpcValue val(valueXml, offset);
00507       if ( ! val.valid()) {
00508         invalidate();
00509         return false;
00510       }
00511       const std::pair<const std::string, XmlRpcValue> p(name, val);
00512       _value.asStruct->insert(p);
00513 
00514       (void) XmlRpcUtil::nextTagIs(MEMBER_ETAG, valueXml, offset);
00515     }
00516     return true;
00517   }

std::string XmlRpc::XmlRpcValue::boolToXml (  )  const [protected]

00290   {
00291     std::string xml = VALUE_TAG;
00292     xml += BOOLEAN_TAG;
00293     xml += (_value.asBool ? "1" : "0");
00294     xml += BOOLEAN_ETAG;
00295     xml += VALUE_ETAG;
00296     return xml;
00297   }

std::string XmlRpc::XmlRpcValue::intToXml (  )  const [protected]

00315   {
00316     char buf[256];
00317     snprintf(buf, sizeof(buf)-1, "%d", _value.asInt);
00318     buf[sizeof(buf)-1] = 0;
00319     std::string xml = VALUE_TAG;
00320     xml += I4_TAG;
00321     xml += buf;
00322     xml += I4_ETAG;
00323     xml += VALUE_ETAG;
00324     return xml;
00325   }

std::string XmlRpc::XmlRpcValue::doubleToXml (  )  const [protected]

00343   {
00344     char buf[256];
00345     snprintf(buf, sizeof(buf)-1, getDoubleFormat().c_str(), _value.asDouble);
00346     buf[sizeof(buf)-1] = 0;
00347 
00348     std::string xml = VALUE_TAG;
00349     xml += DOUBLE_TAG;
00350     xml += buf;
00351     xml += DOUBLE_ETAG;
00352     xml += VALUE_ETAG;
00353     return xml;
00354   }

std::string XmlRpc::XmlRpcValue::stringToXml (  )  const [protected]

00370   {
00371     std::string xml = VALUE_TAG;
00372     //xml += STRING_TAG; optional
00373     xml += XmlRpcUtil::xmlEncode(*_value.asString);
00374     //xml += STRING_ETAG;
00375     xml += VALUE_ETAG;
00376     return xml;
00377   }

std::string XmlRpc::XmlRpcValue::timeToXml (  )  const [protected]

00401   {
00402     struct tm* t = _value.asTime;
00403     char buf[20];
00404     snprintf(buf, sizeof(buf)-1, "%04d%02d%02dT%02d:%02d:%02d", 
00405       1900+t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
00406     buf[sizeof(buf)-1] = 0;
00407 
00408     std::string xml = VALUE_TAG;
00409     xml += DATETIME_TAG;
00410     xml += buf;
00411     xml += DATETIME_ETAG;
00412     xml += VALUE_ETAG;
00413     return xml;
00414   }

std::string XmlRpc::XmlRpcValue::binaryToXml (  )  const [protected]

00441   {
00442     // convert to base64
00443     std::vector<char> base64data;
00444     int iostatus = 0;
00445           base64<char> encoder;
00446     std::back_insert_iterator<std::vector<char> > ins = std::back_inserter(base64data);
00447                 encoder.put(_value.asBinary->begin(), _value.asBinary->end(), ins, iostatus, base64<>::crlf());
00448 
00449     // Wrap with xml
00450     std::string xml = VALUE_TAG;
00451     xml += BASE64_TAG;
00452     xml.append(base64data.begin(), base64data.end());
00453     xml += BASE64_ETAG;
00454     xml += VALUE_ETAG;
00455     return xml;
00456   }

std::string XmlRpc::XmlRpcValue::arrayToXml (  )  const [protected]

00480   {
00481     std::string xml = VALUE_TAG;
00482     xml += ARRAY_TAG;
00483     xml += DATA_TAG;
00484 
00485     int s = int(_value.asArray->size());
00486     for (int i=0; i<s; ++i)
00487        xml += _value.asArray->at(i).toXml();
00488 
00489     xml += DATA_ETAG;
00490     xml += ARRAY_ETAG;
00491     xml += VALUE_ETAG;
00492     return xml;
00493   }

std::string XmlRpc::XmlRpcValue::structToXml (  )  const [protected]

00523   {
00524     std::string xml = VALUE_TAG;
00525     xml += STRUCT_TAG;
00526 
00527     ValueStruct::const_iterator it;
00528     for (it=_value.asStruct->begin(); it!=_value.asStruct->end(); ++it) {
00529       xml += MEMBER_TAG;
00530       xml += NAME_TAG;
00531       xml += XmlRpcUtil::xmlEncode(it->first);
00532       xml += NAME_ETAG;
00533       xml += it->second.toXml();
00534       xml += MEMBER_ETAG;
00535     }
00536 
00537     xml += STRUCT_ETAG;
00538     xml += VALUE_ETAG;
00539     return xml;
00540   }


Member Data Documentation

std::string XmlRpc::XmlRpcValue::_doubleFormat [static, protected]

Type XmlRpc::XmlRpcValue::_type [protected]

bool XmlRpc::XmlRpcValue::asBool [protected]

int XmlRpc::XmlRpcValue::asInt [protected]

double XmlRpc::XmlRpcValue::asDouble [protected]

struct tm* XmlRpc::XmlRpcValue::asTime [protected]

std::string* XmlRpc::XmlRpcValue::asString [protected]

BinaryData* XmlRpc::XmlRpcValue::asBinary [protected]

ValueArray* XmlRpc::XmlRpcValue::asArray [protected]

ValueStruct* XmlRpc::XmlRpcValue::asStruct [protected]

union { ... } XmlRpc::XmlRpcValue::_value [protected]


The documentation for this class was generated from the following files:
Generated on Tue Jul 24 16:51:19 2007 for ITM OverSim by  doxygen 1.5.1