// in SDL: interface obj_containing_string { public: attribute string str_attr; attribute text text_attr; }; // signature of C++ binding for string/text types // (The public part of the class declarations for // string and text attributes.) class sdl_string { public: char get(int n) const; // get n'th character void get(char *s) const; // get copy of entire string void get(char *s, int from, int len) const; // get a range of bytes void set(int n, char c); // set n'th character void set(const char *s); // s is null-terminated void set(const char *s, int from, int len); // set a range of bytes int strlen(void) const; //length of currently stored string int blen() const; // available space as binary data. // C Library style names for modifying the string const char *memcpy(const char *s, int len); void bcopy(const char *s, int len); const char *strcpy(const char *s); const char *strcat(const char *s); // conversion to C string. operator const char *() const; // const void * conversion, for use with mem/bcopy routines. operator const void *() const; const char * operator=(const char *); // conversion from C string };
The SDL string type provides a variable length string facility for use in the definition of SDL interface object types. The SDL text type is similar too, and allows the same operations as the string type, but there may at most one text attribute per SDL object. If an SDL object containing a text attribute is a named object any data contained in the text attribute is accessible as a Unix file through the Shore Server. Attributes of SDL objects declared as string or text can be accessed within SDL/C++ programs as if they were instances of a C++ class with the method signature shown above. The C++ binding of the SDL string type is designed for easy inter-operation with C-style null terminated strings and char* pointers, and can be used with many of the functions described in string(3) (declared in the header file <string.h>. )
String attributes also can be used to store memory areas (arrays of characters bounded by a count, not terminated by a null character) String attributes can be used to store such an area by using one of the memcpy or bcopy member functions described below. Use of string-style member functions and memory area member functions cannot be intermixed.
The member functions that modify the value of a string attribute will allocate temporary storage as necessary to store string or memory area values; the value of a string attribute may be treated as a const char * pointer value in many contexts, but the attribute may only be modified through member functions of the string class.
The following examples are base on use of an SDL C++ binding based on the SDL object type definition
interface obj_containing_string { public: attribute string str_attr; attribute text text_attr; };and C++/SDL variable declarations
REF(obj_containing_string) sref_val; char *s; char *s1, *s2; int n; int i; int len; char c;
The get member functions retrieve character values from the string attribute.
sref->str_attr.get(n);returns the value of the n th character of the string, or null if the length of the string is less than n;
sref->str_attr.get(s);copies the entire string into the space pointed to by s, and
sref->str_attr.get(s,i,len)copies len bytes starting at the i th character into the space pointed to by s.
The set member functions set the string value or subranges of the string value in a manner analogous to the get functions;
sref.update()->str_attr.get(n);sets the value of the n th character of the string attribute, extending the length of the attribute as necessary;
sref.update()->str_attr.get(s);copies the entire null-terminated string s into the string attribute, resetting the length of the attribute, and
sref.update()->str_attr.get(s,i,len)copies len bytes from s into the string attribute, starting at the i th character.
A string attribute can be used as a const char * pointer, either via implicit coercion or explicit casting. For example, a string attribute could be printed using printf by explicitly casting the reference to a const char * value.
printf("str_attr: %s\n",(const char *)(sref->str_attr))In a context where a char * pointer is required by context, this conversion will be done implicitly, e.g.
extern "C" long atoi(const char *); int nval = atoi(sref->str_attr);is allowed.
sref->str_attr.strlen()returns the current length of the string attribute; this is equivalent to
strlen(sref->str.attr);except that a null value of the string attribute is handled cleanly and returns 0. If a string attribute is uninitialized, or if it has been set to NULL by an assignment operator, the value returned by the const char * conversion operator will be NULL.
sref->str_attr.blen()is equivalent to strlen, but does not check for nulls. That is, if a string attribute is used to store binary data, blen will return the length of the memory area stored, ignoring any to embedded nulls. scan for embedded nulls.
sref.update()->str_attr.strcpy(s2)copies string s2 to the string attribute str_attr until the null character has been copied. Space is allocated as necessary. This is equivalent to the operation
sref.update()->str_attr = s2;
sref.update()->str_attr.strcat(s2)appends a copy of string s2 to the end of the string attribute str_attr.
sref.update()->str_attr.memcpy(s2,len)copies len bytes from memory at location s2 into the string attribute, and sets the length to len appends a copy of string s2 to the end of the string attribute str_attr.