📄 sstr_03.cc
字号:
// file: $isip/class/system/SysString/sstr_03.cc// version: $Id: sstr_03.cc,v 1.29 2002/07/29 15:05:46 zheng Exp $//// system include files// note that we need some basic string functions. these are not included// in Integral.h since users are not expected to use these functions.//#include <strings.h>// isip include files//#include "SysString.h"#include <Console.h>// we need at least two static buffers. one current one, and one// previous one.//static byte* buff[2] = {(byte*)NULL, (byte*)NULL};// buf_offset points to the next available position within the current// static buffer//static long buf_offset = 0;// buf_size is the size of the current static buffer//static long buf_size = 0;// buf_index indicates which buffer is current. this probably will// never change, only if the user tries to get a really large buffer. //static long buf_index = 0;// method: assign//// arguments:// const SysString& arg: (input) string to copy//// return: a boolean value indicating status//// assign object to arg_a//boolean SysString::assign(const SysString& arg_a) { // if these two SysString are already the same, do nothing // if (value_d == arg_a.value_d) { return true; } // possibly delete previous values // clear(Integral::RESET); // allocate a bigger buffer if necessary // if (arg_a.length() > capacity_d) { freeMem(); capacity_d = arg_a.length(); allocateMem(); } // copy over the contents of the source string // if (arg_a.length() > 0) { MemoryManager::memcpy(value_d, arg_a.value_d, sizeof(unichar) * (arg_a.length() + 1)); } // exit gracefully // return true;}// method: assign//// arguments:// const unichar* data: (input) buffer of data//// return: a boolean value indicating status//// assign object to data_a//boolean SysString::assign(const unichar* data_a) { // possibly free previous values // clear(Integral::RESET); // copy over the buffer into our string // if ((data_a != (unichar*)NULL) && (isip_wcslen(data_a) > 0)) { // allocate more memory if necessary // if ((long)isip_wcslen(data_a) > capacity_d) { freeMem(); capacity_d = isip_wcslen(data_a); allocateMem(); } // copy over the buffer // isip_wcscpy(value_d, data_a); } // exit gracefully // return true;}// method: assign//// arguments:// const byte* data: (input) buffer of data// long max_size: (input) maximum number of characters to read// SysChar::ENCODE encoding: (input) what encoding scheme to use//// return: a boolean value indicating status//// assign object to this byte* of data//boolean SysString::assign(const byte* data_a, long max_size_a, SysChar::ENCODE encoding_a) { // possibly free previous values // clear(Integral::RESET); // copy over the buffer into our string // if ((data_a != (byte*)NULL) && (strlen((char*)data_a) > 0)) { // declare local variables // SysChar c; long len; // we need a static buffer of unicode characters. this is done so // the resultant string need not hold as much memory as max_size_a // static long buf_len = BIG_BUFFER_LENGTH; static unichar* buf = (unichar*)MemoryManager::newStatic(buf_len * sizeof(unichar)); // possibly increase the size of our buffer // if (max_size_a > buf_len) { MemoryManager::deleteStatic(buf); buf_len = max_size_a; buf = (unichar*)MemoryManager::newStatic(buf_len * sizeof(unichar)); } // initialize the buffer to a null string // buf[0] = (unichar)NULL; // convert and copy characters // long i; for (i = 0; (i < max_size_a) && (*data_a != (byte)NULL); i++) { if (!c.assign(len, data_a, encoding_a)) { return Error::handle(name(), L"assign", SysChar::ERR, __FILE__, __LINE__, Error::WARNING); } buf[i] = (unichar)c; data_a += len; } buf[i] = (unichar)NULL; // test overflow // if (i == max_size_a) { Error::handle(name(), L"assign", Error::MEM_OFLOW, __FILE__, __LINE__, Error::WARNING); } // assign our string to be this buffer of unichar's // return assign(buf); } // exit gracefully // return true;}// method: assign//// arguments:// const SysString& arg: (input) string to copy// const unichar* fmt: (input) format string//// return: a boolean value indicating status//// assign object to arg_a using format string. the %s will be replaced// by a wide character string.//boolean SysString::assign(const SysString& arg_a, const unichar* fmt_a) { // if these two SysString are already the same, do nothing // if (value_d == arg_a.value_d) { return true; } static byte buf[MAX_LENGTH]; SysString fmt(fmt_a); // possibly delete previous values // clear(Integral::RESET); // create and possibly assign the string // if (sprintf((char*)buf, (char*)(byte*)fmt, (char*)(byte*)arg_a) > 0) { return assign((byte*)buf); } // exit gracefully // return false;}// method: assign//// arguments:// const unichar* data: (input) buffer of data// const unichar* fmt: (input) print format//// return: a boolean value indicating status//// assign object to data_a//boolean SysString::assign(const unichar* data_a, const unichar* fmt_a) { // create the string // SysString ws; ws.assign(data_a); // apply the format // return assign(ws, fmt_a);}// method: assign//// arguments:// const byte* data: (input) buffer of data// const unichar* fmt: (input) print format// long max_size: (input) maximum number of characters to read// SysChar::ENCODE encoding: (input) what encoding scheme to use//// return: a boolean value indicating status//// assign object to this byte* of data//boolean SysString::assign(const byte* data_a, const unichar* fmt_a, long max_size_a, SysChar::ENCODE encoding_a) { // create the string // SysString ws; ws.assign(data_a, max_size_a, encoding_a); // apply the format // return assign(ws, fmt_a);}// method: swap//// arguments:// SysString& arg: (input) string to swap with//// return: a boolean value indicating status//// assign object to arg_a, and arg_a to object. this is done in a very// memory-efficient manner.//boolean SysString::swap(SysString& arg_a) { // we need temporary place-holders // long temp_cap; unichar* temp_val; // make temp be a memory-copy of arg // temp_cap = arg_a.capacity_d; temp_val = arg_a.value_d; // make arg be a memory-copy of *this // arg_a.capacity_d = capacity_d; arg_a.value_d = value_d; // make *this be a memory-copy of temp (was arg) // capacity_d = temp_cap; value_d = temp_val; // exit gracefully // return true;}// method: assign//// arguments:// const void* arg: (input) pointer to convert//// return: a boolean value indicating status//// convert a pointer into a string//boolean SysString::assign(const void* arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value // clear(Integral::RESET); // if the pointer is null, return that string // if (arg_a == NULL) { return assign((unichar*)NULL_PTR); } // create and possibly assign the string // if (sprintf(buf, DEF_FMT_VOIDP_8BIT, arg_a) > 0) { return assign((byte*)buf); } // exit gracefully // return false;}// method: assign//// arguments:// boolean arg: (input) number to convert//// return: a boolean value indicating status//// convert a boolean value into a string//boolean SysString::assign(boolean arg_a) { // we check for specific values in this case to prevent an // out-of-bounds value from being accepted as a boolean. // if (arg_a) { assign((unichar*)BOOL_TRUE); return true; } else { assign((unichar*)BOOL_FALSE); return true; } // exit ungracefully // return false;}// method: assign//// arguments:// byte arg: (input) number to convert//// return: a boolean value indicating status//// convert a byte integer into a string//boolean SysString::assign(byte arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value // clear(Integral::RESET); // create and possibly assign the string // if (sprintf(buf, DEF_FMT_LONG_8BIT, (ulong)arg_a) > 0) { assign((byte*)buf); return true; } // exit gracefully // return false;}// method: assign//// arguments:// unichar arg: (input) character to assign to string//// return: a boolean value indicating status//// assign object to arg_a//boolean SysString::assign(unichar arg_a) { // first clear out the string // clear(Integral::RESET); // make sure we have the capacity to hold a single character // if (capacity_d < 1) { setCapacity(1); } // assign the character // value_d[0] = arg_a; // terminate the string // value_d[1] = (unichar)NULL; // exit gracefully // return true;}// method: assign//// arguments:// ushort arg: (input) number to convert//// return: a boolean value indicating status//// convert an unsigned short integer into a string//boolean SysString::assign(ushort arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value // clear(Integral::RESET); // create and possibly assign the string // if (sprintf(buf, DEF_FMT_ULONG_8BIT, arg_a) > 0) { assign((byte*)buf); return true; } // exit gracefully // return false;}// method: assign//// arguments:// ulong arg: (input) number to convert//// return: a boolean value indicating status//// convert an unsigned long integer into a string//boolean SysString::assign(ulong arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value // clear(Integral::RESET); // create and possibly assign the string // if (sprintf(buf, DEF_FMT_ULONG_8BIT, arg_a) > 0) { assign((byte*)buf); return true; } // exit gracefully // return false;}// method: assign//// arguments:// ullong arg: (input) number to convert//// return: a boolean value indicating status//// convert an unsigned long long integer into a string//boolean SysString::assign(ullong arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value // clear(Integral::RESET); // create and possibly assign the string // if (sprintf(buf, DEF_FMT_ULLONG_8BIT, arg_a) > 0) { assign((byte*)buf); return true; } // exit gracefully // return false;}// method: assign//// arguments:// short arg: (input) number to convert//// return: a boolean value indicating status//// convert a short integer into a string//boolean SysString::assign(short arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value // clear(Integral::RESET); // create and possibly assign the string // if (sprintf(buf, DEF_FMT_LONG_8BIT, arg_a) > 0) { assign((byte*)buf); return true; } // exit gracefully // return false;}// method: assign//// arguments:// long arg: (input) number to convert//// return: a boolean value indicating status//// convert a long integer into a string//boolean SysString::assign(long arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value // clear(Integral::RESET); // create and possibly assign the string // if (sprintf(buf, DEF_FMT_LONG_8BIT, arg_a) > 0) { assign((byte*)buf); return true; } // exit gracefully // return false;}// method: assign//// arguments:// llong arg: (input) number to convert//// return: a boolean value indicating status//// convert a long long integer into a string//boolean SysString::assign(llong arg_a) { // allocate a static buffer for printing // static char buf[MAX_LENGTH]; // clear out the current value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -