⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 valuestl.cpp

📁 Open VXI. This is a open source.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************License************************************************ * * Copyright 2000-2001.  SpeechWorks International, Inc.     * * Use of this software is subject to notices and obligations set forth * in the SpeechWorks Public License - Software Version 1.1 which is * included with this software. * * SpeechWorks is a registered trademark, and SpeechWorks Here, * DialogModules and the SpeechWorks logo are trademarks of SpeechWorks * International, Inc. in the United States and other countries. *  *********************************************************************** * * Implementation of the complex VXIValue based types using the C++ * Standard Template Library as defined in vxivalue.h: VXIString, * VXIMap, and VXIVector. * * See ValueNoSTL.cpp for the lower performance and more complex, but * more portable, non-STL based version. * ***********************************************************************/#define VXIVALUE_EXPORTS#include "Value.hpp"#include <stdio.h>#include <stdlib.h>// STL headers#include <string>#include <set>#include <vector>#include <algorithm>// Definition of string to use, based on VXIchar choice#define STL_STRING  std::basic_string<VXIchar>#define STRNCPY     wcsncpy/** * Real VXIString class */class VXIString : public VXIValue { public:  // Constructor and destructor  VXIString (const STL_STRING &v) : VXIValue (VALUE_STRING), value(v) { }  VXIString (const VXIchar * v, VXIunsigned u)    : VXIValue (VALUE_STRING), value(v, u) { }  virtual ~VXIString( ) { }  // Get the length of the string  VXIunsigned GetLength( ) const { return value.length( ); }  // Get and set the value  const STL_STRING &GetValue( ) const { return value; }  void SetValue (const STL_STRING &v) { value = v; } private:  STL_STRING  value;};/** * Real VXIMap and supporting classes */class VXIElement { public:  // Constructor and destructor  VXIElement (const STL_STRING &k, VXIValue *v) : key(k), value(v) { }  ~VXIElement( ) { value = NULL; }  // Get the key and value  const STL_STRING &GetKey( ) const { return key; }  const VXIValue *GetValue( ) const { return value; }  // Destroy the value  void DestroyValue( ) { if ( value ) VXIValueDestroy (&value); }  // Set the value  void SetValue (VXIValue *v, bool destroy) {     if (( value ) && ( destroy ))      VXIValueDestroy (&value);    value = v;  }  // Overrides of comparison operators for finding elements based on  // the key name  virtual bool operator< (const VXIElement &e) const {    return key < e.key;  }  virtual bool operator== (const VXIElement &e) const {     return key == e.key;  }  virtual bool operator!= (const VXIElement &e) const {     return key != e.key;  }  // NOTE: Copy constructor and assignment operator are used by STL on  // this object, intentionally just do a shallow copy (copy the pointer) private:  STL_STRING    key;  VXIValue     *value;};class VXIMap : public VXIValue { public:  // Constructor and destructor  VXIMap( ) : VXIValue (VALUE_MAP), container( ) { }  virtual ~VXIMap( );  // Copy constructor  VXIMap (const VXIMap &m); public:  typedef std::set<VXIElement> MAPSET;  MAPSET container;};VXIMap::~VXIMap( ){  // Must manually deep destroy values  MAPSET::iterator vi;  for (vi = container.begin( ); vi != container.end( ); vi++)    (*vi).DestroyValue( );}VXIMap::VXIMap (const VXIMap &m) : VXIValue (VALUE_MAP), container(m.container){  // Must manually deep copy values  MAPSET::iterator vi;  for (vi = container.begin( ); vi != container.end( ); vi++) {    VXIValue *v = VXIValueClone ((*vi).GetValue( ));    if ( v ) {      (*vi).SetValue (v, false);    } else {      container.clear( );      return;    }  }}class VXIMapIterator { public:  // Constructor and destructor  VXIMapIterator(const VXIMap *m) :     map(m), mapIterator(m->container.begin( )) { }  virtual ~VXIMapIterator( ) { }  // Get the key and value at the iterator's position  VXIvalueResult GetKeyValue (const VXIchar **key, 			      const VXIValue **value) const {    if ( mapIterator == map->container.end( ) ) {      *key = NULL;      *value = NULL;      return VXIvalue_RESULT_FAILURE;    }    *key = (*mapIterator).GetKey( ).c_str( );    *value = (*mapIterator).GetValue( );    return VXIvalue_RESULT_SUCCESS;  }  // Increment the iterator  VXIMapIterator &operator++(int) {     if ( mapIterator != map->container.end( ) )      mapIterator++;     return *this; } private:  const VXIMap *map;  VXIMap::MAPSET::const_iterator mapIterator;};/** * Real VXIVector and supporting classes */class VXIVector : public VXIValue { public:  // Constructor and destructor  VXIVector( ) : VXIValue (VALUE_VECTOR), container( ) { }  virtual ~VXIVector( );  // Copy constructor  VXIVector (const VXIVector &v); public:  typedef std::vector<VXIElement> VECTOR;  VECTOR container;};VXIVector::~VXIVector( ){  // Must manually deep destroy values  VECTOR::iterator vi;  for (vi = container.begin( ); vi != container.end( ); vi++)    (*vi).DestroyValue( );}VXIVector::VXIVector (const VXIVector &v) : VXIValue(VALUE_VECTOR),					    container(v.container){  // Must manually deep copy values  VECTOR::iterator vi;  for (vi = container.begin( ); vi != container.end( ); vi++) {    VXIValue *v = VXIValueClone ((*vi).GetValue( ));    if ( v ) {      (*vi).SetValue (v, false);    } else {      container.clear( );      return;    }  }}/** * Create a String from a null-terminated character array * * @param   str   NULL-terminated character array * @return        String with the specified value on success,  *                NULL otherwise */VXIVALUE_API VXIString *VXIStringCreate(const VXIchar *str){  if ( str == NULL )    return NULL;  return new VXIString (str);}/** * Create a String from a known-length character array * * @param   str   Character array (null characters may be embedded in  *                the array) * @param   len   Number of characters which will be copied. * @return        String with the specified value on success,  *                NULL otherwise */VXIVALUE_API VXIString *VXIStringCreateN(const VXIchar *str, VXIunsigned len){  if ( str == NULL )    return NULL;  return new VXIString (str, len);}/** * String destructor * * @param   s   String to destroy */VXIVALUE_API void VXIStringDestroy(VXIString **s){  if (( s ) && ( *s ) && ( (*s)->GetType( ) == VALUE_STRING )) {    delete *s;    *s = NULL;  }}/** * String clone * * Note: functionally redundant with VXIValueClone( ), but provided to * reduce the need for C casts for this common operation * * @param    s   String to clone * @return       Clone of the string on success, NULL otherwise */VXIVALUE_API VXIString *VXIStringClone(const VXIString *s){  if (( s == NULL ) || ( s->GetType( ) != VALUE_STRING ))    return NULL;  return new VXIString (*s);}/** * Set the value of a String from a null-terminated character array * * Note: this functionality is provided to allow defining interfaces * where the caller passes in a VXIString from VXIStringCreate( ) * (typically with an empty string as its value) with the interface * changing that value to return a string as output. This avoids * having to define interfaces where the client has to provide a * fixed length buffer (and thus worry about "buffer too small" errors * and complicated handling). * * @param   s     String to change the value of * @param   str   NULL-terminated character array * @return        VXIvalue_RESULT_SUCCESS on success  */VXIVALUE_API VXIvalueResult VXIStringSetValue(VXIString      *s, 					      const VXIchar  *str){  if (( s == NULL ) || ( s->GetType( ) != VALUE_STRING ) ||       ( str == NULL ))    return VXIvalue_RESULT_INVALID_ARGUMENT;  s->SetValue (str);  return VXIvalue_RESULT_SUCCESS;}/** * Get the value of a String * * @param   s     String to access * @param   buf   Character buffer to copy the value into as a *                NULL-terminated character array.  The buffer size must be *                at least VXIStringLength() + 1. * @param   len   Size of the buffer, in characters * @return        Pointer to buf on success, NULL on failure (most likely *                buffer too small)  */VXIVALUE_API VXIchar *VXIStringValue(const VXIString  *s, 				     VXIchar          *buf, 				     VXIunsigned       len){  if (( s == NULL ) || ( s->GetType( ) != VALUE_STRING ) ||       ( buf == NULL ))    return NULL;  // Make sure the buffer is large enough  if ( len < s->GetLength( ) + 1 ) return NULL;  const STL_STRING & str = s->GetValue();  unsigned int length = s->GetLength();  for (unsigned int i = 0; i < length; ++i)    *(buf + i) = str[i];  *(buf + length) = L'\0';  return buf;}/** * Get direct access to the NULL-terminated character value * * Note: the returned buffer must never be modified, and is only * provided for transient use (i.e. immediately logging it, comparing * it, etc. rather than storing or returning the pointer for longer * term access). * * @param   s   String to retrieve the data from * @return      Pointer to the NULL-terminated character array retrieved */VXIVALUE_API const VXIchar* VXIStringCStr(const VXIString *s){  if (( s == NULL ) || ( s->GetType( ) != VALUE_STRING ))    return NULL;  return s->GetValue( ).c_str( );}/** * Get the number of characters in a String's value * * Note: Add one byte for the NULL terminator when using this to determine * the length of the array required for a VXIStringValue( ) call. * * @param   s   String to access * @return      Length of the string, in characters */VXIVALUE_API VXIunsigned VXIStringLength(const VXIString *s){  if (( s == NULL ) || ( s->GetType( ) != VALUE_STRING ))    return 0;  return s->GetLength( );}/** * Compares two Strings * * @param   s1   First String to compare * @param   s2   Second String to compare * @return       Returns a value that is less than, equal to, or greater *               than zero depending on whether s1 is lexicographically *               less than, equal to, or greater than s2 */VXIVALUE_API VXIint VXIStringCompare(const VXIString *s1, 				     const VXIString *s2){  if (( s1 == NULL ) || ( s1->GetType( ) != VALUE_STRING ))    return -1;  if (( s2 == NULL ) || ( s2->GetType( ) != VALUE_STRING ))    return 1;  return s1->GetValue( ).compare (s2->GetValue( ));}/** * Compares a String to a NULL-terminated character array * * @param   str   String to compare * @param   buf   NULL-terminated character array to compare * @return        Returns a value that is less than, equal to, or greater *                than zero depending on whether str is lexicographically *                less than, equal to, or greater than buf */VXIVALUE_API VXIint VXIStringCompareC(const VXIString *str, 				      const VXIchar   *buf){  if (( str == NULL ) || ( str->GetType( ) != VALUE_STRING ))    return -1;  if ( buf == NULL )    return 1;  return str->GetValue( ).compare (buf);}/** * Create an empty Map

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -