📄 valuenostl.cpp
字号:
VXIElement *element = new (&data[size]) VXIElement (NULL, val); size++; return VXIvalue_RESULT_SUCCESS; } /** * 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; // Allocate the string VXIString *s = new VXIString (str); if (( s != NULL ) && ( s->GetValue( ) == NULL )) { delete s; s = NULL; } return s; } /** * 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; // Allocate the string VXIString *s = new VXIString (str, len); if (( s != NULL ) && ( s->GetValue( ) == NULL )) { delete s; s = NULL; } return s; } /** * 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; // Allocate the string VXIString *str = new VXIString (*s); if (( str != NULL ) && ( str->GetValue( ) == NULL )) { delete str; str = NULL; } return str; } /** * 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); if ( s->GetValue( ) == NULL ) return VXIvalue_RESULT_OUT_OF_MEMORY; 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 VXIchar * str = s->GetValue(); unsigned int length = s->GetLength(); for (unsigned int i = 0; i < length; ++i) *(buf + i) = str[i]; *(buf + length) = '\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( ); } /** * 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->Compare (*s2); } /** * 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->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) { if (( m ) && ( *m ) && ( (*m)->GetType( ) == VALUE_MAP )) { delete *m; *m = NULL; } } /** * Map clone * * Recursively copies all values contained within the map, * including all the values of Maps and Vectors stored within this * map. * * Note: functionally redundant with VXIValueClone( ), but provided to * reduce the need for C casts for this common operation * * @param m Map to clone * @return Clone of the Map on success, NULL otherwise */ VXIVALUE_API VXIMap *VXIMapClone(const VXIMap *m) { if (( m == NULL ) || ( m->GetType( ) != VALUE_MAP )) return NULL; VXIMap *theMap = new VXIMap (*m); if (( theMap ) && ( theMap->badState )) { delete theMap; theMap = NULL; } return theMap; } /** * Set a named property on an Map * * The value can be an Map so a tree can be constructed. * * If the property already exists, the existing value is first * destroyed using VXIValueDestroy( ) (thus recursively deleting held * values within it if it is an Map or Vector), then does the * set operation with the new value. * * @param m Map to access * @param key NULL terminated property name * @param val Value to set the property to, ownership is passed * to the Map (a simple pointer copy is done), so on * success the user must not delete, modify, or otherwise * use this. Also be careful to not add a Map as a * property of itself (directly or indirectly), otherwise * infinite loops may occur on access or deletion. * @return VXIvalue_RESULT_SUCCESS on success */ VXIVALUE_API VXIvalueResult VXIMapSetProperty(VXIMap *m, const VXIchar *key, VXIValue *val) { if (( m == NULL ) || ( m->GetType( ) != VALUE_MAP ) || ( key == NULL ) || ( key[0] == 0 ) || ( val == NULL )) return VXIvalue_RESULT_INVALID_ARGUMENT; // Create the element to set VXIElement *element = new VXIElement (key, val); if ( ! element ) { return VXIvalue_RESULT_OUT_OF_MEMORY; } else if ( element->GetKey( ).GetLength( ) == 0 ) { element->DestroyValue( ); delete element; return VXIvalue_RESULT_OUT_OF_MEMORY; } // Search for the insertion point for that element VXIElement *cur = m->head, *prev = NULL; while (( cur ) && ( *cur < *element )) { prev = cur; cur = cur->GetNext( ); } // Replace any existing element with that key or insert it if // completely new if (( cur ) && ( *cur == *element )) { // Replacement element->SetNext (cur->GetNext( )); if ( prev ) prev->SetNext (element); else m->head = element; cur->DestroyValue( ); delete cur; } else { // Insertion if ( cur ) { element->SetNext (cur); } else { } if ( prev ) { prev->SetNext (element); } else { m->head = element; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -