00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 00022 #include "String.hpp" 00023 #include "../error/Log.hpp" 00024 #include <cstring> 00025 00026 namespace se_core { 00027 String 00028 ::String() : data_(0), doDestroy_(false) { 00029 } 00030 00031 String 00032 ::String(const char* data) : data_(data), doDestroy_(false) { 00033 } 00034 00035 String 00036 ::String(char* data) : data_(data), doDestroy_(true) { 00037 } 00038 00039 00040 String 00041 ::String(const char* name, int toPos) : data_(0), doDestroy_(false) { 00042 copy(name, toPos); 00043 } 00044 00045 String 00046 ::~String() { 00047 if(doDestroy_) delete[] data_; 00048 } 00049 00050 void String 00051 ::set(char* data) { 00052 if(doDestroy_) delete[] data_; 00053 data_ = data; 00054 doDestroy_ = true; 00055 } 00056 00057 00058 void String 00059 ::set(const char* data) { 00060 if(doDestroy_) delete[] data_; 00061 data_ = data; 00062 doDestroy_ = false; 00063 } 00064 00065 00066 void String 00067 ::reset() { 00068 if(doDestroy_) delete[] data_; 00069 data_ = 0; 00070 doDestroy_ = false; 00071 } 00072 00073 void String 00074 ::copy(const char* s) { 00075 if(doDestroy_) delete[] data_; 00076 char* buffer = new char[strlen(s) + 1]; 00077 strcpy(buffer, s); 00078 data_ = buffer; 00079 doDestroy_ = true; 00080 } 00081 00082 00083 void String 00084 ::copy(const char* s, int toPos) { 00085 int len = strlen(s); 00086 if(toPos <= 0) { 00087 len += toPos; 00088 if(len < 0) 00089 len = 0; 00090 } 00091 else if(toPos < len) { 00092 len = toPos; 00093 } 00094 if(doDestroy_) delete[] data_; 00095 char* buffer = new char[len + 1]; 00096 strncpy(buffer, s, len); 00097 buffer[ len ] = 0; 00098 data_ = buffer; 00099 doDestroy_ = true; 00100 } 00101 00102 00103 const char* String 00104 ::ext(char delimiter, bool mustFind) const { 00105 const char* ch = data_, *ret = 0; 00106 00107 while(*ch != 0) { 00108 if(*ch == delimiter) { 00109 ret = ch + 1; 00110 } 00111 ++ch; 00112 } 00113 AssertFatal(!mustFind || ret, "Couldn't find delimiter '" << delimiter << "' in " << get()); 00114 return ret; 00115 } 00116 00117 char* String 00118 ::copyValue() const { 00119 char* v = new char[strlen(data_) + 1]; 00120 strcpy(v, data_); 00121 return v; 00122 } 00123 00124 00125 bool String 00126 ::isEmpty() const { 00127 return (data_ == 0 || data_[0] == 0); 00128 } 00129 00130 00131 void String 00132 ::append(const char* s) { 00133 char* v = new char[strlen(data_) + strlen(s) + 1]; 00134 strcpy(v, data_); 00135 strcat(v, s); 00136 set(v); 00137 } 00138 00139 00140 int String 00141 ::compare(const String& s) const { 00142 if(get() == s.get()) 00143 return 0; 00144 if(get() == 0) 00145 return -1; 00146 if(s.get() == 0) 00147 return 1; 00148 return strcmp(get(), s.get()); 00149 } 00150 00151 00152 int String 00153 ::compare(const char* s) const { 00154 if(get() == s) 00155 return 0; 00156 if(get() == 0) 00157 return -1; 00158 if(s == 0) 00159 return 1; 00160 return strcmp(get(), s); 00161 } 00162 00163 00164 bool String 00165 ::equals(const String& s) const { 00166 return (compare(s) == 0); 00167 } 00168 00169 00170 bool String 00171 ::equals(const char* s) const { 00172 return (compare(s) == 0); 00173 } 00174 00175 int String 00176 ::len() const { 00177 if(!data_) 00178 return 0; 00179 return strlen(data_); 00180 } 00181 00182 }
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:13 2007 by Doxygen version 1.3.9.1.