PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libreoffice-3.6.0.2/soltools/support/simstr.cxx

#
C++ | 833 lines | 650 code | 146 blank | 37 comment | 103 complexity | 04ebaa42b4cedf8b9664be7512377c68 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, AGPL-1.0, BSD-3-Clause-No-Nuclear-License-2014, GPL-3.0, LGPL-3.0
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*************************************************************************
  3. *
  4. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5. *
  6. * Copyright 2000, 2010 Oracle and/or its affiliates.
  7. *
  8. * OpenOffice.org - a multi-platform office productivity suite
  9. *
  10. * This file is part of OpenOffice.org.
  11. *
  12. * OpenOffice.org is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License version 3
  14. * only, as published by the Free Software Foundation.
  15. *
  16. * OpenOffice.org is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License version 3 for more details
  20. * (a copy is included in the LICENSE file that accompanied this code).
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * version 3 along with OpenOffice.org. If not, see
  24. * <http://www.openoffice.org/license.html>
  25. * for a copy of the LGPLv3 License.
  26. *
  27. ************************************************************************/
  28. #include <simstr.hxx>
  29. #include <string.h> // strlen(), memcpy(), memset()
  30. #include <ctype.h> // tolower()
  31. #include <limits.h> // INT_MAX
  32. const char NULCH = '\0';
  33. const int NO_POS = -1;
  34. Simstr::Simstr(const char * s_)
  35. {
  36. if (s_ == 0)
  37. {
  38. len = 0;
  39. sz = new char[1];
  40. *sz = 0;
  41. }
  42. else
  43. {
  44. len = (int)strlen(s_);
  45. sz = new char[len+1];
  46. memcpy(sz,s_,len+1);
  47. }
  48. }
  49. Simstr::Simstr(const char * anybytes, int nrOfBytes)
  50. {
  51. if (anybytes == 0)
  52. {
  53. len = 0;
  54. sz = new char[1];
  55. *sz = 0;
  56. return;
  57. }
  58. int slen = static_cast<int>( strlen(anybytes) );
  59. len = slen < nrOfBytes
  60. ? slen
  61. : nrOfBytes;
  62. sz = new char[len+1];
  63. memcpy( sz, anybytes, len );
  64. *( sz + len ) = 0;
  65. }
  66. Simstr::Simstr(char c, int anzahl)
  67. {
  68. if (anzahl < 1)
  69. {
  70. len = 0;
  71. sz = new char[1];
  72. *sz = 0;
  73. }
  74. else
  75. {
  76. len = anzahl;
  77. sz = new char[len+1];
  78. memset(sz,c,anzahl);
  79. sz[len] = 0;
  80. }
  81. }
  82. Simstr::Simstr( const char * anybytes,
  83. int firstBytesPos,
  84. int nrOfBytes)
  85. {
  86. unsigned slen = (unsigned)strlen(anybytes);
  87. if (anybytes == 0 || slen <= unsigned(firstBytesPos))
  88. {
  89. len = 0;
  90. sz = new char[1];
  91. *sz = 0;
  92. }
  93. else
  94. {
  95. int maxLen = slen - unsigned(firstBytesPos);
  96. len = maxLen < nrOfBytes
  97. ? maxLen
  98. : nrOfBytes;
  99. sz = new char[len+1];
  100. memcpy(sz,anybytes+firstBytesPos,len);
  101. *(sz+len) = 0;
  102. }
  103. }
  104. Simstr::Simstr(const Simstr & S)
  105. {
  106. len = S.len;
  107. sz = new char[len+1];
  108. memcpy(sz,S.sz,len+1);
  109. }
  110. Simstr & Simstr::operator=(const Simstr & S)
  111. {
  112. if (sz == S.sz)
  113. return *this;
  114. delete [] sz;
  115. len = S.len;
  116. sz = new char[len+1];
  117. memcpy(sz,S.sz,len+1);
  118. return *this;
  119. }
  120. Simstr::~Simstr()
  121. {
  122. delete [] sz;
  123. }
  124. char &
  125. Simstr::ch(int n)
  126. {
  127. static char nullCh = NULCH;
  128. nullCh = NULCH;
  129. if (n >= long(len) || n < 0)
  130. return nullCh;
  131. else
  132. return sz[unsigned(n)];
  133. }
  134. const Simstr &
  135. Simstr::null_()
  136. {
  137. static Simstr aNull_;
  138. return aNull_;
  139. }
  140. Simstr
  141. Simstr::operator+(const Simstr & S) const
  142. {
  143. Simstr ret = sz;
  144. ret.push_back(S);
  145. return ret;
  146. }
  147. Simstr &
  148. Simstr::operator+=(const Simstr & S)
  149. {
  150. push_back(S);
  151. return *this;
  152. }
  153. Simstr &
  154. Simstr::operator+=(const char * s_)
  155. {
  156. Simstr a(s_);
  157. push_back(a);
  158. return *this;
  159. }
  160. // REL
  161. bool
  162. Simstr::operator==(const Simstr & S) const
  163. { return !strcmp(sz,S.sz) ? true : false; }
  164. bool
  165. Simstr::operator!=(const Simstr & S) const
  166. { return strcmp(sz,S.sz) ? true : false; }
  167. bool
  168. Simstr::operator<(const Simstr & S) const
  169. { return (strcmp(sz,S.sz) < 0) ? true : false; }
  170. bool
  171. Simstr::operator>(const Simstr & S) const
  172. { return (strcmp(sz,S.sz) > 0) ? true : false; }
  173. bool
  174. Simstr::operator<=(const Simstr & S) const
  175. { return (strcmp(sz,S.sz) <= 0) ? true : false; }
  176. bool
  177. Simstr::operator>=(const Simstr & S) const
  178. { return (strcmp(sz,S.sz) >= 0) ? true : false; }
  179. // ************** LIST - Funktionen *****************
  180. // Einzelzugriff
  181. char
  182. Simstr::get(int n) const { return (n >= len || n < 0) ? 0 : sz[n]; }
  183. char
  184. Simstr::get_front() const { return sz[0]; }
  185. char
  186. Simstr::get_back() const { return len ? sz[len-1] : 0; }
  187. Simstr
  188. Simstr::get(int startPos, int anzahl) const
  189. {
  190. if (startPos >= len || startPos < 0 || anzahl < 1)
  191. return "";
  192. int anz = len - startPos < anzahl ? len - startPos : anzahl;
  193. Simstr ret(' ',anz);
  194. memcpy(ret.sz, sz+startPos, anz);
  195. return ret;
  196. }
  197. Simstr
  198. Simstr::get_front(int anzahl) const
  199. {
  200. int anz = len < anzahl ? len : anzahl;
  201. if (anz < 1)
  202. return "";
  203. Simstr ret(' ',anz);
  204. memcpy(ret.sz, sz, anz);
  205. return ret;
  206. }
  207. Simstr
  208. Simstr::get_back(int anzahl) const
  209. {
  210. int anz = len < anzahl ? len : anzahl;
  211. if (anz < 1)
  212. return "";
  213. int start = len-anz;
  214. Simstr ret(' ',anz);
  215. memcpy(ret.sz, sz+start, anz);
  216. return ret;
  217. }
  218. Simstr
  219. Simstr::get_first_token(char c) const
  220. {
  221. int posc = pos_first(c);
  222. if (posc != NO_POS)
  223. return get_front(posc);
  224. else
  225. return sz;
  226. }
  227. Simstr
  228. Simstr::get_last_token(char c) const
  229. {
  230. int posc = pos_last(c);
  231. if (posc != NO_POS)
  232. return get_back(len-posc-1);
  233. else
  234. return sz;
  235. }
  236. // Insert
  237. void
  238. Simstr::insert(int pos, char c)
  239. {
  240. if (pos < 0 || pos > len)
  241. return;
  242. char * result = new char[len+2];
  243. memcpy(result,sz,pos);
  244. result[pos] = c;
  245. memcpy(result+pos+1,sz+pos,len-pos+1);
  246. delete [] sz;
  247. sz = result;
  248. len++;
  249. }
  250. void
  251. Simstr::push_front(char c)
  252. {
  253. char * result = new char[len+2];
  254. result[0] = c;
  255. memcpy(result+1,sz,len+1);
  256. delete [] sz;
  257. sz = result;
  258. len++;
  259. }
  260. void
  261. Simstr::push_back(char c)
  262. {
  263. char * result = new char[len+2];
  264. memcpy(result,sz,len);
  265. result[len] = c;
  266. result[len+1] = 0;
  267. delete [] sz;
  268. sz = result;
  269. len++;
  270. }
  271. void
  272. Simstr::insert(int pos, const Simstr & S)
  273. {
  274. if (pos < 0 || pos > len)
  275. return;
  276. char * result = new char[len+1+S.len];
  277. memcpy(result,sz,pos);
  278. memcpy(result+pos,S.sz,S.len);
  279. memcpy(result+pos+S.len,sz+pos,len-pos+1);
  280. delete [] sz;
  281. sz = result;
  282. len += S.len;
  283. }
  284. void
  285. Simstr::push_front(const Simstr & S)
  286. {
  287. char * result = new char[len+1+S.len];
  288. memcpy(result,S.sz,S.len);
  289. memcpy(result+S.len,sz,len+1);
  290. delete [] sz;
  291. sz = result;
  292. len += S.len;
  293. }
  294. void
  295. Simstr::push_back(const Simstr & S)
  296. {
  297. char * result = new char[len+1+S.len];
  298. memcpy(result,sz,len);
  299. memcpy(result+len,S.sz,S.len+1);
  300. delete [] sz;
  301. sz = result;
  302. len += S.len;
  303. }
  304. // Remove
  305. void
  306. Simstr::remove(int pos, int anzahl)
  307. {
  308. if (pos >= len || pos < 0 || anzahl < 1)
  309. return;
  310. int anz = len - pos < anzahl ? len - pos : anzahl;
  311. char * result = new char[len-anz+1];
  312. memcpy(result,sz,pos);
  313. memcpy(result+pos,sz+pos+anz,len-pos-anz+1);
  314. delete [] sz;
  315. sz = result;
  316. len -= anz;
  317. }
  318. void
  319. Simstr::remove_trailing_blanks()
  320. {
  321. int newlen = len-1;
  322. for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
  323. if (newlen < len-1)
  324. remove ( newlen+1, len-newlen);
  325. }
  326. void
  327. Simstr::pop_front(int anzahl)
  328. {
  329. if (anzahl < 1)
  330. return;
  331. int anz = len < anzahl ? len : anzahl;
  332. char * result = new char[len-anz+1];
  333. memcpy(result,sz+anz,len-anz+1);
  334. delete [] sz;
  335. sz = result;
  336. len -= anz;
  337. }
  338. void
  339. Simstr::pop_back(int anzahl)
  340. {
  341. if (anzahl < 1)
  342. return;
  343. int anz = len < anzahl ? len : anzahl;
  344. char * result = new char[len-anz+1];
  345. memcpy(result,sz,len-anz);
  346. result[len-anz] = 0;
  347. delete [] sz;
  348. sz = result;
  349. len -= anz;
  350. }
  351. void
  352. Simstr::rem_back_from(int removeStartPos)
  353. {
  354. if (removeStartPos != NO_POS)
  355. pop_back(len-removeStartPos);
  356. }
  357. void
  358. Simstr::remove_all(char c)
  359. {
  360. if (!len)
  361. return;
  362. char * result = new char[len];
  363. int i,j=0;
  364. for (i = 0; i < len; i++)
  365. if (sz[i] != c)
  366. result[j++] = sz[i];
  367. delete [] sz;
  368. sz = new char[j+1];
  369. memcpy(sz,result,j);
  370. sz[j] = 0;
  371. len = j;
  372. delete [] result;
  373. }
  374. void
  375. Simstr::remove_all(const Simstr & S)
  376. {
  377. int pos;
  378. while ( (pos=pos_first(S)) != NO_POS )
  379. remove(pos,S.len);
  380. }
  381. void
  382. Simstr::strip(char c)
  383. {
  384. int start = 0;
  385. if (c == ' ')
  386. { // Sonderbehandlung: SPC entfernt auch TABs:
  387. while ( start < len
  388. ? sz[start] == ' '
  389. || sz[start] == '\t'
  390. : false )
  391. start++;
  392. }
  393. else
  394. {
  395. while (start < len && sz[start] == c)
  396. start++;
  397. }
  398. int ende = len-1;
  399. if (c == ' ')
  400. { // Sonderbehandlung: SPC entfernt auch TABs:
  401. while ( ende >= start
  402. ? sz[ende] == ' '
  403. || sz[ende] == '\t'
  404. : false )
  405. ende--;
  406. }
  407. else
  408. {
  409. while (ende >= start && sz[ende] == c)
  410. ende--;
  411. }
  412. *this = get(start,ende-start+1);
  413. }
  414. void
  415. Simstr::empty()
  416. {
  417. if (len > 0)
  418. {
  419. delete [] sz;
  420. sz = new char[1];
  421. *sz = 0;
  422. len = 0;
  423. }
  424. }
  425. Simstr
  426. Simstr::take_first_token(char c)
  427. {
  428. Simstr ret;
  429. int pos = pos_first(c);
  430. if (pos != NO_POS)
  431. {
  432. ret = get_front(pos);
  433. pop_front(pos+1);
  434. }
  435. else
  436. {
  437. ret = sz;
  438. delete [] sz;
  439. sz = new char[1];
  440. *sz = NULCH;
  441. len = 0;
  442. }
  443. return ret;
  444. }
  445. Simstr
  446. Simstr::take_last_token(char c)
  447. {
  448. Simstr ret;
  449. int pos = pos_last(c);
  450. if (pos != NO_POS)
  451. {
  452. ret = get_back(len-pos-1);
  453. pop_back(len-pos);
  454. }
  455. else
  456. {
  457. ret = sz;
  458. delete [] sz;
  459. sz = new char[1];
  460. *sz = NULCH;
  461. len = 0;
  462. }
  463. return ret;
  464. }
  465. // Find
  466. int
  467. Simstr::pos_first(char c) const
  468. {
  469. int i = 0;
  470. for (i = 0; i < len ? sz[i] != c : false; i++) ;
  471. if (i >= len)
  472. return NO_POS;
  473. else
  474. return i;
  475. }
  476. int
  477. Simstr::pos_first_after( char c,
  478. int startSearchPos) const
  479. {
  480. int i = 0;
  481. if (startSearchPos >= i)
  482. i = startSearchPos+1;
  483. for (; i < len ? sz[i] != c : false; i++) ;
  484. if (i >= len)
  485. return NO_POS;
  486. else
  487. return i;
  488. }
  489. int
  490. Simstr::pos_last(char c) const
  491. {
  492. int i = 0;
  493. for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
  494. if (i < 0)
  495. return NO_POS;
  496. else
  497. return i;
  498. }
  499. int
  500. Simstr::pos_first(const Simstr & S) const
  501. {
  502. char * ptr = strstr(sz,S.sz);
  503. if (ptr)
  504. return int(ptr-sz);
  505. else
  506. return NO_POS;
  507. }
  508. int
  509. Simstr::pos_last(const Simstr & S) const
  510. {
  511. Simstr vgl;
  512. int i;
  513. for (i = len-S.len; i >= 0 ; i--)
  514. {
  515. vgl = get(i,S.len);
  516. if (vgl == S)
  517. break;
  518. }
  519. if (i >= 0)
  520. return i;
  521. else
  522. return NO_POS;
  523. }
  524. int
  525. Simstr::count(char c) const
  526. {
  527. int ret = 0;
  528. for (int i =0; i < len; i++)
  529. if (sz[i] == c)
  530. ret++;
  531. return ret;
  532. }
  533. bool
  534. Simstr::is_no_text() const
  535. {
  536. if (!len)
  537. return true;
  538. int i;
  539. for (i = 0; i < len && sz[i] <= 32 ; i++) ;
  540. if (i < len)
  541. return false;
  542. return true;
  543. }
  544. // Change
  545. void
  546. Simstr::replace(int pos, char c)
  547. {
  548. if (pos < 0 || pos >= len)
  549. return;
  550. else
  551. sz[unsigned(pos)] = c;
  552. }
  553. void
  554. Simstr::replace(int startPos, int anzahl, const Simstr & S)
  555. {
  556. if (startPos >= len || startPos < 0 || anzahl < 1)
  557. return;
  558. int anz = len - startPos < anzahl ? len - startPos : anzahl;
  559. char * result = new char[len-anz+S.len+1];
  560. memcpy(result,sz,startPos);
  561. memcpy(result+startPos, S.sz, S.len);
  562. memcpy(result+startPos+S.len, sz+startPos+anz, len-startPos-anz+1);
  563. delete [] sz;
  564. sz = result;
  565. len = len-anz+S.len;
  566. }
  567. void
  568. Simstr::replace_all(char oldCh, char newCh)
  569. {
  570. for (int i=0; i < len; i++)
  571. if (sz[i] == oldCh)
  572. sz[i] = newCh;
  573. }
  574. void
  575. Simstr::replace_all(const Simstr & oldS, const Simstr & newS)
  576. {
  577. Simstr vgl;
  578. int i = 0;
  579. while (i <= len-oldS.len)
  580. {
  581. vgl = get(i,oldS.len);
  582. if (strcmp(vgl.sz,oldS.sz) == 0)
  583. {
  584. replace(i,oldS.len,newS);
  585. i += newS.len;
  586. }
  587. else
  588. i++;
  589. }
  590. }
  591. void
  592. Simstr::to_lower()
  593. {
  594. for (int i = 0; i < len; i++)
  595. sz[i] = (char) tolower(sz[i]);
  596. }
  597. // Simstr addition
  598. Simstr
  599. operator+(const char * str, const Simstr & S)
  600. {
  601. Simstr ret = S;
  602. ret.push_front(str);
  603. return ret;
  604. }
  605. Simstr
  606. operator+(const Simstr & S, const char * str)
  607. {
  608. Simstr ret = S;
  609. ret.push_back(str);
  610. return ret;
  611. }
  612. Simstr
  613. operator+(char c, const Simstr & S)
  614. {
  615. Simstr ret = S;
  616. ret.push_front(c);
  617. return ret;
  618. }
  619. Simstr
  620. operator+(const Simstr & S, char c)
  621. {
  622. Simstr ret = S;
  623. ret.push_back(c);
  624. return ret;
  625. }
  626. // Simstr-Vergleiche mit char *
  627. bool
  628. operator==(const Simstr & S, const char * str)
  629. {
  630. return strcmp(S,str) == 0;
  631. }
  632. bool
  633. operator!=(const Simstr & S, const char * str)
  634. {
  635. return strcmp(S,str) != 0;
  636. }
  637. bool
  638. operator<(const Simstr & S, const char * str)
  639. {
  640. return strcmp(S,str) < 0;
  641. }
  642. bool
  643. operator>(const Simstr & S, const char * str)
  644. {
  645. return strcmp(S,str) > 0;
  646. }
  647. bool
  648. operator<=(const Simstr & S, const char * str)
  649. {
  650. return strcmp(S,str) <= 0;
  651. }
  652. bool
  653. operator>=(const Simstr & S, const char * str)
  654. {
  655. return strcmp(S,str) >= 0;
  656. }
  657. bool
  658. operator==(const char * str, const Simstr & S)
  659. {
  660. return strcmp(str,S) == 0;
  661. }
  662. bool
  663. operator!=(const char * str, const Simstr & S)
  664. {
  665. return strcmp(str,S) != 0;
  666. }
  667. bool
  668. operator<(const char * str, const Simstr & S)
  669. {
  670. return strcmp(str,S) < 0;
  671. }
  672. bool
  673. operator>(const char * str, const Simstr & S)
  674. {
  675. return strcmp(str,S) > 0;
  676. }
  677. bool
  678. operator<=(const char * str, const Simstr & S)
  679. {
  680. return strcmp(str,S) <= 0;
  681. }
  682. bool
  683. operator>=(const char * str, const Simstr & S)
  684. {
  685. return strcmp(str,S) >= 0;
  686. }
  687. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */