1*ab595ff6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ab595ff6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ab595ff6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ab595ff6SAndrew Rist * distributed with this work for additional information 6*ab595ff6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ab595ff6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ab595ff6SAndrew Rist * "License"); you may not use this file except in compliance 9*ab595ff6SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ab595ff6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ab595ff6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ab595ff6SAndrew Rist * software distributed under the License is distributed on an 15*ab595ff6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ab595ff6SAndrew Rist * KIND, either express or implied. See the License for the 17*ab595ff6SAndrew Rist * specific language governing permissions and limitations 18*ab595ff6SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ab595ff6SAndrew Rist *************************************************************/ 21*ab595ff6SAndrew Rist 22*ab595ff6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir #include <sistr.hxx> 26cdf0e10cSrcweir 27cdf0e10cSrcweir // The following two header-files declare 28cdf0e10cSrcweir // standard ANSI-C++ functions. They may be replaced 29cdf0e10cSrcweir // by the corresponding header-file-names of the 30cdf0e10cSrcweir // actually used runtime library. 31cdf0e10cSrcweir #include <string.h> // strlen(), memcpy(), memset() 32cdf0e10cSrcweir #include <ctype.h> // tolower() 33cdf0e10cSrcweir #include <limits.h> // INT_MAX 34cdf0e10cSrcweir 35cdf0e10cSrcweir #if (_MSC_VER >=1400) 36cdf0e10cSrcweir #pragma warning(disable:4365) 37cdf0e10cSrcweir #endif 38cdf0e10cSrcweir 39cdf0e10cSrcweir const char NULCH = '\0'; 40cdf0e10cSrcweir const int NO_POS = -1; 41cdf0e10cSrcweir 42cdf0e10cSrcweir 43cdf0e10cSrcweir Simstr::Simstr(const char * str_) 44cdf0e10cSrcweir { 45cdf0e10cSrcweir if (str_ == 0) 46cdf0e10cSrcweir { 47cdf0e10cSrcweir len = 0; 48cdf0e10cSrcweir sz = new char[1]; 49cdf0e10cSrcweir *sz = 0; 50cdf0e10cSrcweir } 51cdf0e10cSrcweir else 52cdf0e10cSrcweir { 53cdf0e10cSrcweir len = strlen(str_); 54cdf0e10cSrcweir sz = new char[len+1]; 55cdf0e10cSrcweir memcpy(sz,str_,len+1); 56cdf0e10cSrcweir } 57cdf0e10cSrcweir } 58cdf0e10cSrcweir 59cdf0e10cSrcweir Simstr::Simstr( const char * anybytes, 60cdf0e10cSrcweir int firstBytesPos, 61cdf0e10cSrcweir int nrOfBytes) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir unsigned slen = strlen(anybytes); 64cdf0e10cSrcweir if (anybytes == 0 || slen <= unsigned(firstBytesPos)) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir len = 0; 67cdf0e10cSrcweir sz = new char[1]; 68cdf0e10cSrcweir *sz = 0; 69cdf0e10cSrcweir } 70cdf0e10cSrcweir else 71cdf0e10cSrcweir { 72cdf0e10cSrcweir int maxLen = slen - unsigned(firstBytesPos); 73cdf0e10cSrcweir len = maxLen < nrOfBytes 74cdf0e10cSrcweir ? maxLen 75cdf0e10cSrcweir : nrOfBytes; 76cdf0e10cSrcweir sz = new char[len+1]; 77cdf0e10cSrcweir memcpy(sz,anybytes+firstBytesPos,len); 78cdf0e10cSrcweir *(sz+len) = 0; 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir 83cdf0e10cSrcweir Simstr::Simstr(const Simstr & S) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir len = S.len; 86cdf0e10cSrcweir sz = new char[len+1]; 87cdf0e10cSrcweir memcpy(sz,S.sz,len+1); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir Simstr & Simstr::operator=(const Simstr & S) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir if (sz == S.sz) 93cdf0e10cSrcweir return *this; 94cdf0e10cSrcweir 95cdf0e10cSrcweir delete [] sz; 96cdf0e10cSrcweir 97cdf0e10cSrcweir len = S.len; 98cdf0e10cSrcweir sz = new char[len+1]; 99cdf0e10cSrcweir memcpy(sz,S.sz,len+1); 100cdf0e10cSrcweir 101cdf0e10cSrcweir return *this; 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir Simstr::~Simstr() 105cdf0e10cSrcweir { 106cdf0e10cSrcweir delete [] sz; 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir Simstr 110cdf0e10cSrcweir Simstr::operator+(const Simstr & S) const 111cdf0e10cSrcweir { 112cdf0e10cSrcweir Simstr ret = sz; 113cdf0e10cSrcweir ret.push_back(S); 114cdf0e10cSrcweir return ret; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir Simstr & 118cdf0e10cSrcweir Simstr::operator+=(const Simstr & S) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir push_back(S); 121cdf0e10cSrcweir return *this; 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir 125cdf0e10cSrcweir // REL 126cdf0e10cSrcweir 127cdf0e10cSrcweir bool 128cdf0e10cSrcweir Simstr::operator==(const Simstr & S) const 129cdf0e10cSrcweir { return !strcmp(sz,S.sz) ? true : false; } 130cdf0e10cSrcweir 131cdf0e10cSrcweir bool 132cdf0e10cSrcweir Simstr::operator!=(const Simstr & S) const 133cdf0e10cSrcweir { return strcmp(sz,S.sz) ? true : false; } 134cdf0e10cSrcweir 135cdf0e10cSrcweir bool 136cdf0e10cSrcweir Simstr::operator<(const Simstr & S) const 137cdf0e10cSrcweir { return (strcmp(sz,S.sz) < 0) ? true : false; } 138cdf0e10cSrcweir 139cdf0e10cSrcweir bool 140cdf0e10cSrcweir Simstr::operator>(const Simstr & S) const 141cdf0e10cSrcweir { return (strcmp(sz,S.sz) > 0) ? true : false; } 142cdf0e10cSrcweir 143cdf0e10cSrcweir bool 144cdf0e10cSrcweir Simstr::operator<=(const Simstr & S) const 145cdf0e10cSrcweir { return (strcmp(sz,S.sz) <= 0) ? true : false; } 146cdf0e10cSrcweir 147cdf0e10cSrcweir bool 148cdf0e10cSrcweir Simstr::operator>=(const Simstr & S) const 149cdf0e10cSrcweir { return (strcmp(sz,S.sz) >= 0) ? true : false; } 150cdf0e10cSrcweir 151cdf0e10cSrcweir 152cdf0e10cSrcweir 153cdf0e10cSrcweir 154cdf0e10cSrcweir // ************** LIST - Funktionen ***************** 155cdf0e10cSrcweir 156cdf0e10cSrcweir // Insert 157cdf0e10cSrcweir 158cdf0e10cSrcweir void 159cdf0e10cSrcweir Simstr::push_front(char c) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir char * result = new char[len+2]; 162cdf0e10cSrcweir 163cdf0e10cSrcweir result[0] = c; 164cdf0e10cSrcweir memcpy(result+1,sz,len+1); 165cdf0e10cSrcweir 166cdf0e10cSrcweir delete [] sz; 167cdf0e10cSrcweir sz = result; 168cdf0e10cSrcweir len++; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir void 172cdf0e10cSrcweir Simstr::push_back(char c) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir char * result = new char[len+2]; 175cdf0e10cSrcweir 176cdf0e10cSrcweir memcpy(result,sz,len); 177cdf0e10cSrcweir result[len] = c; 178cdf0e10cSrcweir result[len+1] = 0; 179cdf0e10cSrcweir 180cdf0e10cSrcweir delete [] sz; 181cdf0e10cSrcweir sz = result; 182cdf0e10cSrcweir len++; 183cdf0e10cSrcweir } 184cdf0e10cSrcweir 185cdf0e10cSrcweir void 186cdf0e10cSrcweir Simstr::push_front(const Simstr & S) 187cdf0e10cSrcweir { 188cdf0e10cSrcweir char * result = new char[len+1+S.len]; 189cdf0e10cSrcweir 190cdf0e10cSrcweir memcpy(result,S.sz,S.len); 191cdf0e10cSrcweir memcpy(result+S.len,sz,len+1); 192cdf0e10cSrcweir 193cdf0e10cSrcweir delete [] sz; 194cdf0e10cSrcweir sz = result; 195cdf0e10cSrcweir len += S.len; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir void 199cdf0e10cSrcweir Simstr::push_back(const Simstr & S) 200cdf0e10cSrcweir { 201cdf0e10cSrcweir char * result = new char[len+1+S.len]; 202cdf0e10cSrcweir 203cdf0e10cSrcweir memcpy(result,sz,len); 204cdf0e10cSrcweir memcpy(result+len,S.sz,S.len+1); 205cdf0e10cSrcweir 206cdf0e10cSrcweir delete [] sz; 207cdf0e10cSrcweir sz = result; 208cdf0e10cSrcweir len += S.len; 209cdf0e10cSrcweir } 210cdf0e10cSrcweir 211cdf0e10cSrcweir 212cdf0e10cSrcweir // Remove 213cdf0e10cSrcweir 214cdf0e10cSrcweir void 215cdf0e10cSrcweir Simstr::remove(int pos, int anzahl) 216cdf0e10cSrcweir { 217cdf0e10cSrcweir if (pos >= len || pos < 0 || anzahl < 1) 218cdf0e10cSrcweir return; 219cdf0e10cSrcweir 220cdf0e10cSrcweir int anz = len - pos < anzahl ? len - pos : anzahl; 221cdf0e10cSrcweir 222cdf0e10cSrcweir char * result = new char[len-anz+1]; 223cdf0e10cSrcweir 224cdf0e10cSrcweir memcpy(result,sz,pos); 225cdf0e10cSrcweir memcpy(result+pos,sz+pos+anz,len-pos-anz+1); 226cdf0e10cSrcweir 227cdf0e10cSrcweir delete [] sz; 228cdf0e10cSrcweir sz = result; 229cdf0e10cSrcweir len -= anz; 230cdf0e10cSrcweir } 231cdf0e10cSrcweir 232cdf0e10cSrcweir void 233cdf0e10cSrcweir Simstr::remove_trailing_blanks() 234cdf0e10cSrcweir { 235cdf0e10cSrcweir int newlen = len-1; 236cdf0e10cSrcweir for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {} 237cdf0e10cSrcweir 238cdf0e10cSrcweir if (newlen < len-1) 239cdf0e10cSrcweir remove ( newlen+1, len-newlen); 240cdf0e10cSrcweir } 241cdf0e10cSrcweir 242cdf0e10cSrcweir // Find 243cdf0e10cSrcweir 244cdf0e10cSrcweir int 245cdf0e10cSrcweir Simstr::pos_first(char c) const 246cdf0e10cSrcweir { 247cdf0e10cSrcweir int i = 0; 248cdf0e10cSrcweir for (i = 0; i < len ? sz[i] != c : false; i++) ; 249cdf0e10cSrcweir if (i >= len) 250cdf0e10cSrcweir return NO_POS; 251cdf0e10cSrcweir else 252cdf0e10cSrcweir return i; 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir int 256cdf0e10cSrcweir Simstr::pos_last(char c) const 257cdf0e10cSrcweir { 258cdf0e10cSrcweir int i = 0; 259cdf0e10cSrcweir for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ; 260cdf0e10cSrcweir if (i < 0) 261cdf0e10cSrcweir return NO_POS; 262cdf0e10cSrcweir else 263cdf0e10cSrcweir return i; 264cdf0e10cSrcweir } 265cdf0e10cSrcweir 266cdf0e10cSrcweir bool 267cdf0e10cSrcweir Simstr::is_no_text() const 268cdf0e10cSrcweir { 269cdf0e10cSrcweir if (!len) 270cdf0e10cSrcweir return true; 271cdf0e10cSrcweir 272cdf0e10cSrcweir int i; 273cdf0e10cSrcweir for (i = 0; sz[i] <= 32 && i < len; i++) ; 274cdf0e10cSrcweir if (i < len) 275cdf0e10cSrcweir return false; 276cdf0e10cSrcweir return true; 277cdf0e10cSrcweir } 278cdf0e10cSrcweir 279cdf0e10cSrcweir // Change 280cdf0e10cSrcweir 281cdf0e10cSrcweir void 282cdf0e10cSrcweir Simstr::replace_all(char oldCh, char newCh) 283cdf0e10cSrcweir { 284cdf0e10cSrcweir for (int i=0; i < len; i++) 285cdf0e10cSrcweir if (sz[i] == oldCh) 286cdf0e10cSrcweir sz[i] = newCh; 287cdf0e10cSrcweir } 288cdf0e10cSrcweir 289cdf0e10cSrcweir // Simstr addition 290cdf0e10cSrcweir Simstr 291cdf0e10cSrcweir operator+(const char * str, const Simstr & S) 292cdf0e10cSrcweir { 293cdf0e10cSrcweir Simstr ret = S; 294cdf0e10cSrcweir ret.push_front(str); 295cdf0e10cSrcweir return ret; 296cdf0e10cSrcweir } 297cdf0e10cSrcweir 298cdf0e10cSrcweir Simstr 299cdf0e10cSrcweir operator+(const Simstr & S, const char * str) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir Simstr ret = S; 302cdf0e10cSrcweir ret.push_back(str); 303cdf0e10cSrcweir return ret; 304cdf0e10cSrcweir } 305cdf0e10cSrcweir 306cdf0e10cSrcweir Simstr 307cdf0e10cSrcweir operator+(char c, const Simstr & S) 308cdf0e10cSrcweir { 309cdf0e10cSrcweir Simstr ret = S; 310cdf0e10cSrcweir ret.push_front(c); 311cdf0e10cSrcweir return ret; 312cdf0e10cSrcweir } 313cdf0e10cSrcweir 314cdf0e10cSrcweir Simstr 315cdf0e10cSrcweir operator+(const Simstr & S, char c) 316cdf0e10cSrcweir { 317cdf0e10cSrcweir Simstr ret = S; 318cdf0e10cSrcweir ret.push_back(c); 319cdf0e10cSrcweir return ret; 320cdf0e10cSrcweir } 321cdf0e10cSrcweir 322cdf0e10cSrcweir 323cdf0e10cSrcweir // Simstr-Vergleiche mit char * 324cdf0e10cSrcweir bool 325cdf0e10cSrcweir operator==(const Simstr & S, const char * str) 326cdf0e10cSrcweir { 327cdf0e10cSrcweir return strcmp(S,str) == 0; 328cdf0e10cSrcweir } 329cdf0e10cSrcweir 330cdf0e10cSrcweir bool 331cdf0e10cSrcweir operator!=(const Simstr & S, const char * str) 332cdf0e10cSrcweir { 333cdf0e10cSrcweir return strcmp(S,str) != 0; 334cdf0e10cSrcweir } 335cdf0e10cSrcweir 336cdf0e10cSrcweir bool 337cdf0e10cSrcweir operator<(const Simstr & S, const char * str) 338cdf0e10cSrcweir { 339cdf0e10cSrcweir return strcmp(S,str) < 0; 340cdf0e10cSrcweir } 341cdf0e10cSrcweir 342cdf0e10cSrcweir bool 343cdf0e10cSrcweir operator>(const Simstr & S, const char * str) 344cdf0e10cSrcweir { 345cdf0e10cSrcweir return strcmp(S,str) > 0; 346cdf0e10cSrcweir } 347cdf0e10cSrcweir 348cdf0e10cSrcweir bool 349cdf0e10cSrcweir operator<=(const Simstr & S, const char * str) 350cdf0e10cSrcweir { 351cdf0e10cSrcweir return strcmp(S,str) <= 0; 352cdf0e10cSrcweir } 353cdf0e10cSrcweir 354cdf0e10cSrcweir bool 355cdf0e10cSrcweir operator>=(const Simstr & S, const char * str) 356cdf0e10cSrcweir { 357cdf0e10cSrcweir return strcmp(S,str) >= 0; 358cdf0e10cSrcweir } 359cdf0e10cSrcweir 360cdf0e10cSrcweir bool 361cdf0e10cSrcweir operator==(const char * str, const Simstr & S) 362cdf0e10cSrcweir { 363cdf0e10cSrcweir return strcmp(str,S) == 0; 364cdf0e10cSrcweir } 365cdf0e10cSrcweir 366cdf0e10cSrcweir bool 367cdf0e10cSrcweir operator!=(const char * str, const Simstr & S) 368cdf0e10cSrcweir { 369cdf0e10cSrcweir return strcmp(str,S) != 0; 370cdf0e10cSrcweir } 371cdf0e10cSrcweir 372cdf0e10cSrcweir bool 373cdf0e10cSrcweir operator<(const char * str, const Simstr & S) 374cdf0e10cSrcweir { 375cdf0e10cSrcweir return strcmp(str,S) < 0; 376cdf0e10cSrcweir } 377cdf0e10cSrcweir 378cdf0e10cSrcweir bool 379cdf0e10cSrcweir operator>(const char * str, const Simstr & S) 380cdf0e10cSrcweir { 381cdf0e10cSrcweir return strcmp(str,S) > 0; 382cdf0e10cSrcweir } 383cdf0e10cSrcweir 384cdf0e10cSrcweir bool 385cdf0e10cSrcweir operator<=(const char * str, const Simstr & S) 386cdf0e10cSrcweir { 387cdf0e10cSrcweir return strcmp(str,S) <= 0; 388cdf0e10cSrcweir } 389cdf0e10cSrcweir 390cdf0e10cSrcweir bool 391cdf0e10cSrcweir operator>=(const char * str, const Simstr & S) 392cdf0e10cSrcweir { 393cdf0e10cSrcweir return strcmp(str,S) >= 0; 394cdf0e10cSrcweir } 395cdf0e10cSrcweir 396cdf0e10cSrcweir 397