📄 sstr_03.cc
字号:
// check the arguments // if (fmt_a == (unichar*)NULL) { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } SysString temp(fmt_a); temp.getBuffer((byte*)fmt, MAX_LENGTH); fmt_ptr = fmt; // clear out the current value // clear(Integral::RESET); // create and possibly assign the string // if (sprintf(buf, fmt_ptr, arg_a) > 0) { assign((byte*)buf); return true; } // exit gracefully // return false;}// method: assign//// arguments:// const SysComplex<TIntegral>& arg: (input) value to print// const unichar* fmt: (input) print format//// return: a boolean value indicating status//// convert a complex number into a string//template <class TIntegral>boolean SysString::assign(const SysComplex<TIntegral>& arg_a, const unichar* fmt_a) { // first assign to string // SysString c; c.assign(arg_a); // now apply the format // return assign(c, fmt_a);}// explicit instantiations for complex types//templateboolean SysString::assign<float>(const SysComplex<float>&, const unichar*);templateboolean SysString::assign<double>(const SysComplex<double>&, const unichar*);templateboolean SysString::assign<long>(const SysComplex<long>&, const unichar*);// method: assign//// arguments:// Integral::DEBUG arg: (input) value to print//// return: a boolean value indicating status//// convert a debug level into a string//boolean SysString::assign(Integral::DEBUG arg_a) { // branch on debug level and assign // if (arg_a == Integral::NONE) { assign(DBG_NONE); } else if (arg_a == Integral::BRIEF) { assign(DBG_BRIEF); } else if (arg_a == Integral::DETAILED) { assign(DBG_DETAILED); } else if (arg_a == Integral::ALL) { assign(DBG_ALL); } else { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: assign//// arguments:// Integral::COMPARE arg: (input) value to print//// return: a boolean value indicating status//// convert a compare level into a string//boolean SysString::assign(Integral::COMPARE arg_a) { // branch on compare level and assign // if (arg_a == Integral::EQUAL) { assign(CMP_EQUAL); } else if (arg_a == Integral::GREATER) { assign(CMP_GREATER); } else if (arg_a == Integral::LESSER) { assign(CMP_LESSER); } else { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: assign//// arguments:// File::MODE arg: (input) value to print//// return: a boolean value indicating status//// convert a compare level into a string//boolean SysString::assign(File::MODE arg_a) { // branch on file modes and assign // if (arg_a == File::READ_ONLY) { assign(MODE_READ_ONLY); } else if (arg_a == File::READ_PLUS) { assign(MODE_READ_PLUS); } else if (arg_a == File::WRITE_ONLY) { assign(MODE_WRITE_ONLY); } else if (arg_a == File::WRITE_PLUS) { assign(MODE_WRITE_PLUS); } else if (arg_a == File::APPEND_ONLY) { assign(MODE_APPEND_ONLY); } else if (arg_a == File::APPEND_PLUS) { assign(MODE_APPEND_PLUS); } else { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: assign//// arguments:// File::BMODE arg: (input) value to print//// return: a boolean value indicating status//// convert a file binary mode into a string//boolean SysString::assign(File::BMODE arg_a) { // branch on binary modes and assign // if (arg_a == File::NATIVE) { assign(BMODE_NATIVE); } else if (arg_a == File::SWAP) { assign(BMODE_SWAP); } else if (arg_a == File::BIG_ENDIAN) { assign(BMODE_BIG_ENDIAN); } else if (arg_a == File::LITTLE_ENDIAN) { assign(BMODE_LITTLE_ENDIAN); } else { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: assign//// arguments:// File::TYPE arg: (input) value to print//// return: a boolean value indicating status//// convert a compare level into a string//boolean SysString::assign(File::TYPE arg_a) { // branch on file type and assign // if (arg_a == File::TEXT) { assign(TYPE_TEXT); } else if (arg_a == File::BINARY) { assign(TYPE_BINARY); } else { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: assign//// arguments:// SysChar::ENCODE arg: (input) value to print//// return: a boolean value indicating status//// convert a encoding into a string//boolean SysString::assign(SysChar::ENCODE arg_a) { // branch on encodes and assign // if (arg_a == SysChar::ENCODE_ASCII) { assign(ENCODE_ASCII); } else if (arg_a == SysChar::ENCODE_UTF8) { assign(ENCODE_UTF8); } else if (arg_a == SysChar::ENCODE_UTF16) { assign(ENCODE_UTF16); } else { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: assign//// arguments:// MemoryManager::MODE arg: (input) value to print//// return: a boolean value indicating status//// convert a file binary mode into a string//boolean SysString::assign(MemoryManager::MODE arg_a) { // branch on modes and assign // if (arg_a == MemoryManager::OPTIMIZE) { assign(MMGR_MODE_OPTIMIZE); } else if (arg_a == MemoryManager::TRACK) { assign(MMGR_MODE_TRACK); } else { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: getBuffer//// arguments:// unichar* data: (output) buffer of data// long len: (input) maximum number of characters to copy//// return: number of characters in buffer//// this method returns object data in data* buffer//long SysString::getBuffer(unichar* data_a, long len_a) const { // check the arguments // if (data_a == (unichar*)NULL) { return Error::handle(name(), L"getBuffer", Error::ARG, __FILE__, __LINE__); } // copy over the buffer // isip_wcsncpy(data_a, value_d, (len_a - 1)); data_a[len_a - 1] = (unichar)NULL; // return the length // return isip_wcslen(data_a);}// method: getBuffer//// arguments:// byte* data: (output) buffer of data// long len: (input) maximum number of characters to copy// SysChar::ENCODE encoding: (input) what encoding to use//// return: number of bytes in buffer//// this method passes the objects data back in a buffer, encoded as specified//long SysString::getBuffer(byte* data_a, long len_a, SysChar::ENCODE encoding_a) const { // check the arguments // if (data_a == (byte*)NULL) { return Error::handle(name(), L"getBuffer", Error::ARG, __FILE__, __LINE__); } // declare local variables // SysChar c; long len = 0; long slen = (long)data_a; if ((length() + 1) < len_a) { len_a = length() + 1; } // copy over the buffer // for (long i = 0; i < (len_a - 1); i++) { c.assign(value_d[i]); if (!c.get(len, data_a, encoding_a)) { return Error::handle(name(), L"getBuffer", SysChar::ERR, __FILE__, __LINE__, Error::WARNING); } data_a += len; } // figure out the length // slen = (long)data_a - slen; // null terminate the string // *data_a = (byte)NULL; // return the number of bytes written // return slen;}// method: clear//// arguments:// Integral::CMODE cmode: (input) clear mode //// return: a boolean value indicating status//// this method clears the string//boolean SysString::clear(Integral::CMODE cmode_a) { // for release and free, ensure that memory is actually deleted // if (cmode_a >= Integral::RELEASE) { // delete all memory associated with this string // freeMem(); // assign null string to class data // allocateMem(); } // for reset and retain, just clear value // else { // make the string a zero-length string. // if (capacity_d > 0) { value_d[0] = (unichar)NULL; } } // exit gracefully // return true;}// method: setCapacity//// arguments:// long cap: (input) new capacity for this string//// return: a boolean value indicating status//// this method increases the capacity of the string to given value//boolean SysString::setCapacity(long cap_a) { // for now, let's ignore user's requests to shrink memory. i don't // think this will give us a performance hit. the spirit of this // method is to ensure a given capacity before multiple functions // which would each slightly increase the needed memory. // if (cap_a > capacity_d) { // growMem is a nice method in that it saves any current data when // moving to the new memory. // growMem(cap_a); } // exit gracefully // return true;}// method: operator byte*//// arguments: none//// return: a byte* pointer to the data, encoded in ASCII//// this method is a simple interface to the ASCII conversion routine. static// buffers are maintained to prevent memory leaks.//SysString::operator byte*() const { // allocate the static buffer the first time // if (buff[buf_index] == (byte*)NULL) { // set the size of the current static buffer // buf_size = MAX_LENGTH * STATIC_BUFFER_SIZE * sizeof(unichar); // allocate and register the pointer // buff[buf_index] = (byte*)MemoryManager::newStatic(buf_size); } // right now we only deal with fixed length strings // long len = length() + 1; // if the buffer is not large enough to hold N instances of the string, // allocate a larger buffer. we have to keep around the old buffer // for a little while in case other code is still pointing to it, // so it will be left around until another buffer resize is done, // probably forever // if (len > (buf_size / STATIC_BUFFER_SIZE)) { // increment the current buffer // buf_index++; if (buf_index > 1) { buf_index = 0; } if (buff[buf_index] != (byte*)NULL) { // debugging message // if (debug_level_d >= Integral::ALL) { SysString temp; temp.assign((long)buff[buf_index]); temp.insert(L"Deleting cast static memory space ", 0); Console::put(temp); } // unregister and delete the pointer // MemoryManager::deleteStatic(buff[buf_index]); } // set the size of the current static buffer // buf_size = len * STATIC_BUFFER_SIZE * sizeof(unichar); // allocate and register the pointer // buff[buf_index] = (byte*)MemoryManager::newStatic(buf_size); // reset the size of the current static buffer // buf_offset = 0; // debugging message // if (debug_level_d >= Integral::ALL) { SysString temp; temp.assign((void*)buff[buf_index]); temp.insert(L"Increasing cast static memory space with ", 0); Console::put(temp); } } // wrap the pointer within the buffer. hopefully the user is done // with all previous values returned, they are going to be // overwritten. obviously we can't do something as nice as a memset // here, as it would defeat the entire purpose // if (len + buf_offset > buf_size) { buf_offset = 0; } // read the data starting at the buf_offset'th position of the // buf_index'th buffer, increment buf_offset to point to the next // available space // getBuffer(&buff[buf_index][buf_offset], len); buf_offset += len; // return the buffer // return &buff[buf_index][buf_offset - len];}// method: operator unichar*//// arguments: none//// return: a unichar* pointer to the data//// this method is a simple interface to the unichar* conversion routine. static// buffers are maintained to prevent memory leaks.//SysString::operator unichar*() const { // allocate the static buffer the first time // if (buff[buf_index] == (byte*)NULL) { buf_size = MAX_LENGTH * STATIC_BUFFER_SIZE * sizeof(unichar); // allocate and register the pointer // buff[buf_index] = (byte*)MemoryManager::newStatic(buf_size); } // right now we only deal with fixed length strings // long len = length() + 1; // if the buffer is not large enough to hold N instances of the string, // allocate a larger buffer. we have to keep around the old buffer // for a little while in case other code is still pointing to it, // so it will be left around until another buffer resize is done, // probably forever // if (len * (long)sizeof(unichar) > (buf_size / STATIC_BUFFER_SIZE)) { buf_index++; if (buf_index > 1) { buf_index = 0; } if (buff[buf_index] != (byte*)NULL) { // debugging message // if (debug_level_d >= Integral::ALL) { SysString temp; temp.assign((long)buff[buf_index]); temp.insert(L"Deleting cast static memory space ", 0); Console::put(temp); } // unregister and delete the pointer // MemoryManager::deleteStatic(buff[buf_index]); } buf_size = len * STATIC_BUFFER_SIZE * sizeof(unichar); // allocate and register the pointer // buff[buf_index] = (byte*)MemoryManager::newStatic(buf_size); buf_offset = 0; // debugging message // if (debug_level_d >= Integral::ALL) { SysString temp; temp.assign((void*)buff[buf_index]); temp.insert(L"Increasing cast static memory space with ", 0); Console::put(temp); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -