📄 valuestl.cpp
字号:
/****************License************************************************
*
* Copyright 2000-2003. ScanSoft, Inc.
*
* Use of this software is subject to notices and obligations set forth
* in the SpeechWorks Public License - Software Version 1.2 which is
* included with this software.
*
* ScanSoft is a registered trademark of ScanSoft, Inc., and OpenSpeech,
* SpeechWorks and the SpeechWorks logo are registered trademarks or
* trademarks of SpeechWorks International, Inc. in the United States
* and other countries.
*
***********************************************************************/
#include <vxibuildopts.h>
#if P_VXI
#define VXIVALUE_EXPORTS
#include "Value.hpp"
#include <stdio.h>
#include <stdlib.h>
// STL headers
#include <string>
#include <map>
#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 VXIMap : public VXIValue {
public:
// Constructor and destructor
VXIMap( ) : VXIValue (VALUE_MAP), container( ) { }
virtual ~VXIMap( );
// Copy constructor
VXIMap (const VXIMap &m);
public:
typedef std::map<STL_STRING, VXIValue *> MAP;
MAP container;
};
VXIMap::~VXIMap( )
{
// Must manually deep destroy values
MAP::iterator vi;
for (vi = container.begin( ); vi != container.end( ); vi++)
delete (*vi).second;
}
VXIMap::VXIMap (const VXIMap &m) : VXIValue (VALUE_MAP), container()
{
// Must manually deep copy values
MAP::const_iterator vi;
for (vi = m.container.begin( ); vi != m.container.end( ); vi++) {
VXIValue *v = VXIValueClone ((*vi).second);
if ( v ) {
MAP::value_type element ((*vi).first, v);
container.insert (element);
} else {
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).first.c_str( );
*value = (*mapIterator).second;
return VXIvalue_RESULT_SUCCESS;
}
// Increment the iterator
VXIMapIterator &operator++(int) {
if ( mapIterator != map->container.end( ) )
mapIterator++;
return *this; }
private:
const VXIMap *map;
VXIMap::MAP::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<VXIValue *> VECTOR;
VECTOR container;
};
VXIVector::~VXIVector( )
{
// Must manually deep destroy values
VECTOR::iterator vi;
for (vi = container.begin( ); vi != container.end( ); vi++)
delete *vi;
}
VXIVector::VXIVector (const VXIVector &v) : VXIValue(VALUE_VECTOR), container()
{
// Must manually deep copy values
VECTOR::const_iterator vi;
for (vi = v.container.begin( ); vi != v.container.end( ); vi++) {
VXIValue *v = VXIValueClone (*vi);
if ( v ) {
container.push_back(v);
} else {
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
*
* @return New map on success, NULL otherwise
*/
VXIVALUE_API VXIMap *VXIMapCreate(void)
{
return new VXIMap;
}
/**
* Map destructor
*
* Note: this recursively destroys all the values contained within the
* Map, including all the values of Maps and Vectors stored
* within this map. However, for Ptr values the user is
* responsible for freeing the held memory if appropriate.
*
* @param m Map to destroy
*/
VXIVALUE_API void VXIMapDestroy(VXIMap **m)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -