Text.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. //---Include---
  2. #include "Text.h"
  3. #include <iomanip>
  4. #include <math.h>
  5. #include <sstream>
  6. #ifndef WIN32
  7. # include <string.h>
  8. #endif
  9. #include "Regex.h"
  10. using namespace Framework;
  11. DynamicBuffer::DynamicBuffer(std::function<int(std::stringbuf&)> onAppend)
  12. : std::stringbuf(),
  13. onAppend(onAppend)
  14. {}
  15. int DynamicBuffer::sync()
  16. {
  17. return onAppend(*this);
  18. }
  19. FlushingOStream::FlushingOStream(
  20. DynamicBuffer* buffer, std::function<void()> onDestroy)
  21. : std::ostream(buffer),
  22. onDestroy(onDestroy)
  23. {}
  24. FlushingOStream::FlushingOStream(const Framework::FlushingOStream& stream)
  25. : std::ostream(stream.rdbuf())
  26. {}
  27. FlushingOStream::~FlushingOStream()
  28. {
  29. flush();
  30. onDestroy();
  31. }
  32. Text::Text(char* t, int l)
  33. : ReferenceCounter(),
  34. txt(0),
  35. length(0),
  36. suchGBeg(0),
  37. suchGEnd(0),
  38. precision(-1),
  39. stringWriter(0)
  40. {
  41. length = l;
  42. txt = t; // create new text
  43. }
  44. // Content of the Text class from Text.h
  45. // Constructor
  46. Text::Text()
  47. : ReferenceCounter(),
  48. txt(0),
  49. length(0),
  50. suchGBeg(0),
  51. suchGEnd(0),
  52. precision(-1),
  53. stringWriter(0)
  54. {
  55. setText("");
  56. }
  57. Text::Text(const Text& txt)
  58. : ReferenceCounter(),
  59. txt(0),
  60. length(0),
  61. suchGBeg(0),
  62. suchGEnd(0),
  63. precision(-1),
  64. stringWriter(0)
  65. {
  66. setText(txt, txt.length);
  67. }
  68. Text::Text(const char* t)
  69. : ReferenceCounter(),
  70. txt(0),
  71. length(0),
  72. suchGBeg(0),
  73. suchGEnd(0),
  74. precision(-1),
  75. stringWriter(0)
  76. {
  77. setText(t); // set text
  78. }
  79. Framework::Text::Text(const char* txt, int offset, int length)
  80. : ReferenceCounter(),
  81. txt(0),
  82. length(0),
  83. suchGBeg(0),
  84. suchGEnd(0),
  85. precision(-1),
  86. stringWriter(0)
  87. {
  88. this->length = length;
  89. this->txt = new char[length + 1];
  90. memcpy(this->txt, txt + offset, length);
  91. this->txt[length] = 0;
  92. }
  93. Text::Text(int zahl)
  94. : ReferenceCounter(),
  95. txt(0),
  96. length(0),
  97. suchGBeg(0),
  98. suchGEnd(0),
  99. precision(-1),
  100. stringWriter(0)
  101. {
  102. *this = zahl;
  103. }
  104. // Creates a new Text object with a number as text
  105. // num: The number that should be in the text
  106. Text::Text(double num)
  107. : Text()
  108. {
  109. *this = num;
  110. }
  111. // Creates a new Text object with a number as text
  112. // num: The number that should be in the text
  113. Text::Text(float num)
  114. : Text()
  115. {
  116. *this = num;
  117. }
  118. // Destructor
  119. Text::~Text()
  120. {
  121. if (stringWriter) delete stringWriter;
  122. delete[] txt;
  123. }
  124. void Framework::Text::setTextZ(char* t, int l)
  125. {
  126. delete[] txt; // delete old text
  127. length = l;
  128. txt = t; // create new text
  129. }
  130. void Text::toUpperCase()
  131. {
  132. if (!txt) return;
  133. for (int i = 0; i < length; i++)
  134. {
  135. if (txt[i] >= 'a' && txt[i] <= 'z') txt[i] = (char)(txt[i] - 32);
  136. switch (txt[i])
  137. {
  138. case SpecialCharacters::SMALL_UE:
  139. txt[i] = SpecialCharacters::BIG_UE;
  140. break;
  141. case SpecialCharacters::SMALL_OE:
  142. txt[i] = SpecialCharacters::BIG_OE;
  143. break;
  144. case SpecialCharacters::SMALL_AE:
  145. txt[i] = SpecialCharacters::BIG_AE;
  146. break;
  147. }
  148. }
  149. }
  150. void Text::toLowerCase()
  151. {
  152. if (!txt) return;
  153. for (int i = 0; i < length; i++)
  154. {
  155. if (txt[i] >= 'A' && txt[i] <= 'Z') txt[i] = (char)(txt[i] + 32);
  156. switch (txt[i])
  157. {
  158. case SpecialCharacters::BIG_UE:
  159. txt[i] = SpecialCharacters::SMALL_UE;
  160. break;
  161. case SpecialCharacters::BIG_OE:
  162. txt[i] = SpecialCharacters::SMALL_OE;
  163. break;
  164. case SpecialCharacters::BIG_AE:
  165. txt[i] = SpecialCharacters::SMALL_AE;
  166. break;
  167. }
  168. }
  169. }
  170. // non-constant
  171. void Text::setSuchGrenzen(
  172. char gBeg, char gEnd) // for every search function, does not search between
  173. // the characters gBeg and gEnd
  174. {
  175. suchGBeg = gBeg;
  176. suchGEnd = gEnd;
  177. }
  178. void Text::setText(const char* t) // replaces the text
  179. {
  180. setText(t, textLength(t)); // set text
  181. }
  182. // non-constant
  183. void Text::setText(const char* t, int l) // replaces the text
  184. {
  185. delete[] txt; // delete old text
  186. length = l;
  187. txt = new char[(__int64)length + 1]; // create new text
  188. for (int i = 0; i < length; ++i) // fill text
  189. txt[i] = t[i];
  190. txt[length] = '\0'; // set text end
  191. }
  192. void Text::setText(const Text& t)
  193. {
  194. setText(t, t.getLength()); // set text
  195. }
  196. void Text::append(char c) // appends to the text
  197. {
  198. append(&c, 1);
  199. }
  200. void Text::append(const char* t) // appends to the text
  201. {
  202. int tl = (int)textLength(t); // length of the argument
  203. char* res = new char[(__int64)tl + length + 1]; // create new text
  204. for (int i = 0; i < length; ++i) // fill with current text
  205. res[i] = txt[i];
  206. for (int i = 0; i < tl; ++i) // append argument
  207. res[length + i] = t[i];
  208. res[length + tl] = '\0'; // set text end
  209. setTextZ(res, length + tl); // set text
  210. }
  211. void Text::appendHex(char num) // appends the number in hex to the text
  212. {
  213. char* res = new char[(__int64)length + sizeof(char) * 2 + 1];
  214. for (int i = 0; i < length; ++i)
  215. res[i] = txt[i];
  216. std::stringstream stream;
  217. stream << std::setfill('0') << std::setw((int)sizeof(char) * 2) << std::hex
  218. << (int)num;
  219. std::string str = stream.str();
  220. for (int i = length; i < length + sizeof(char) * 2; ++i)
  221. res[i] = str.c_str()[i - length];
  222. res[length + sizeof(char) * 2] = 0;
  223. setTextZ(res, length + sizeof(char) * 2);
  224. }
  225. void Framework::Text::appendHex(short num)
  226. {
  227. char* res = new char[(__int64)length + sizeof(short) * 2 + 1];
  228. for (int i = 0; i < length; ++i)
  229. res[i] = txt[i];
  230. std::stringstream stream;
  231. stream << std::setfill('0') << std::setw((int)sizeof(short) * 2) << std::hex
  232. << num;
  233. std::string str = stream.str();
  234. for (int i = length; i < length + sizeof(short) * 2; ++i)
  235. res[i] = str.c_str()[i - length];
  236. res[length + sizeof(short) * 2] = 0;
  237. setTextZ(res, length + sizeof(short) * 2);
  238. }
  239. void Text::appendHex(int num) // appends the number in hex to the text
  240. {
  241. char* res = new char[(__int64)length + sizeof(int) * 2 + 1];
  242. for (int i = 0; i < length; ++i)
  243. res[i] = txt[i];
  244. std::stringstream stream;
  245. stream << std::setfill('0') << std::setw((int)sizeof(int) * 2) << std::hex
  246. << num;
  247. std::string str = stream.str();
  248. for (int i = length; i < length + sizeof(int) * 2; ++i)
  249. res[i] = str.c_str()[i - length];
  250. res[length + sizeof(int) * 2] = 0;
  251. setTextZ(res, length + sizeof(int) * 2);
  252. }
  253. void Text::appendHex(__int64 num) // appends the number in hex to the text
  254. {
  255. char* res = new char[(__int64)length + sizeof(__int64) * 2 + 1];
  256. for (int i = 0; i < length; ++i)
  257. res[i] = txt[i];
  258. std::stringstream stream;
  259. stream << std::setfill('0') << std::setw((int)sizeof(__int64) * 2)
  260. << std::hex << num;
  261. std::string str = stream.str();
  262. for (int i = length; i < length + sizeof(__int64) * 2; ++i)
  263. res[i] = str.c_str()[i - length];
  264. res[length + sizeof(__int64) * 2] = 0;
  265. setTextZ(res, length + sizeof(__int64) * 2);
  266. }
  267. void Text::append(const char* t, int l) // appends to the text
  268. { // length of the text
  269. char* res = new char[(__int64)l + length + 1]; // create new text
  270. for (int i = 0; i < length; ++i) // fill with current text
  271. res[i] = txt[i];
  272. for (int i = 0; i < l; ++i) // append argument
  273. res[length + i] = t[i];
  274. res[length + l] = '\0'; // set text end
  275. setTextZ(res, length + l); // set text
  276. }
  277. void Text::append(const Text& t)
  278. {
  279. if (t.getLength() > 0) // check for invalid argument
  280. append(t, t.getLength()); // append text
  281. }
  282. void Text::append(int num)
  283. {
  284. std::stringstream ss;
  285. ss << num;
  286. std::string string = ss.str();
  287. append(string.c_str(), (int)string.length());
  288. }
  289. // Appends a number to the end of the text
  290. // num: The number to convert to text and append at the end
  291. void Text::append(__int64 num)
  292. {
  293. std::stringstream ss;
  294. ss << num;
  295. std::string string = ss.str();
  296. append(string.c_str(), (int)string.length());
  297. }
  298. void Text::append(unsigned int num)
  299. {
  300. std::stringstream ss;
  301. ss << num;
  302. std::string string = ss.str();
  303. append(string.c_str(), (int)string.length());
  304. }
  305. void Text::append(double num)
  306. {
  307. std::stringstream ss;
  308. if (precision >= 0) ss.precision(precision);
  309. ss << std::fixed << num;
  310. std::string string = ss.str();
  311. const char* str = string.c_str();
  312. int len = (int)string.length();
  313. for (int i = len - 1; i > 0; i--)
  314. {
  315. if (str[i] == '0')
  316. len--;
  317. else
  318. {
  319. if (str[i] == '.') len--;
  320. break;
  321. }
  322. }
  323. append(str, len);
  324. }
  325. void Text::append(float num)
  326. {
  327. std::stringstream ss;
  328. if (precision >= 0) ss.precision(precision);
  329. ss << std::fixed << num;
  330. std::string string = ss.str();
  331. const char* str = string.c_str();
  332. int len = (int)string.length();
  333. for (int i = len - 1; i > 0; i--)
  334. {
  335. if (str[i] == '0')
  336. len--;
  337. else
  338. {
  339. if (str[i] == '.') len--;
  340. break;
  341. }
  342. }
  343. append(str, len);
  344. }
  345. //! Returns an ostream that appends all output to this text
  346. FlushingOStream Text::append()
  347. {
  348. if (!stringWriter)
  349. {
  350. stringWriter = new DynamicBuffer([this](std::stringbuf& buf) {
  351. std::string str = buf.str();
  352. this->append(str.c_str(), (int)str.length());
  353. buf.str("");
  354. return 0;
  355. });
  356. }
  357. return FlushingOStream(stringWriter);
  358. }
  359. void Text::insert(int p, char c) // inserts at position p
  360. {
  361. if (p > length || p < 0) // check for invalid argument
  362. return;
  363. char* res = new char[(__int64)length + 2]; // create new text
  364. for (int i = 0; i < p; ++i) // fill text
  365. res[i] = txt[i];
  366. res[p] = c;
  367. for (int i = p; i < length; ++i) // fill text
  368. res[i + 1] = txt[i];
  369. res[length + 1] = '\0'; // set text end
  370. setTextZ(res, length + 1); // set text
  371. }
  372. void Text::insert(int p, const char* t) // inserts at position p
  373. {
  374. insert(p, t, textLength(t)); // insert text
  375. }
  376. void Framework::Text::insert(int p, const char* t, int len)
  377. {
  378. if (p > length || p < 0 || len <= 0) // check for invalid argument
  379. return; // length of argument
  380. char* res = new char[(__int64)len + length + 1]; // create new text
  381. for (int i = 0; i < p; ++i) // fill text
  382. res[i] = txt[i];
  383. for (int i = 0; i < len; ++i) // insert text
  384. res[i + p] = t[i];
  385. for (int i = p; i < length; ++i) // fill text
  386. res[i + len] = txt[i];
  387. res[len + length] = '\0'; // set text end
  388. setTextZ(res, length + len); // set text
  389. }
  390. void Text::insert(int p, const Text& t)
  391. {
  392. insert(p, t, t.getLength()); // insert text
  393. }
  394. void Framework::Text::regexReplace(
  395. const char* regex, const char* replacement, Regex::RegexConfig* config)
  396. {
  397. regexReplace(
  398. regex, [replacement](Regex::Result&) { return replacement; }, config);
  399. }
  400. void Framework::Text::regexReplace(const char* regex,
  401. std::function<Text(Regex::Result&)> replacementFunction,
  402. Regex::RegexConfig* config)
  403. {
  404. auto matcher
  405. = config == 0 ? Regex::parse(regex) : Regex::parse(regex, *config);
  406. if (!matcher) return;
  407. auto result = matcher->match(txt, length);
  408. if (result->getEintragAnzahl() > 0)
  409. {
  410. int replacedLength = 0;
  411. int replacementLength = 0;
  412. Text* replacements = new Text[result->getEintragAnzahl()];
  413. int i = 0;
  414. #ifdef WIN32
  415. # pragma warning(push)
  416. # pragma warning(disable : 6385)
  417. #endif
  418. for (Regex::Result* match : *result)
  419. {
  420. replacedLength += match->getEnd() - match->getStart();
  421. replacements[i] = replacementFunction(*match);
  422. replacementLength += replacements[i].getLength();
  423. i++;
  424. }
  425. int newLength = length - replacedLength + replacementLength;
  426. char* newText = new char[(__int64)newLength + 1];
  427. int pos = 0;
  428. int newPos = 0;
  429. int j = 0;
  430. for (Regex::Result* match : *result)
  431. {
  432. for (int i = 0; i < match->getStart() - pos; i++)
  433. {
  434. newText[newPos + i] = txt[pos + i];
  435. }
  436. newPos += match->getStart() - pos;
  437. pos = match->getEnd();
  438. for (int i = 0; i < replacements[j].getLength(); i++)
  439. {
  440. newText[newPos + i] = replacements[j][i];
  441. }
  442. newPos += replacements[j].getLength();
  443. j++;
  444. }
  445. for (int i = pos; i < getLength(); i++)
  446. {
  447. newText[newPos + i - pos] = txt[i];
  448. }
  449. #ifdef WIN32
  450. # pragma warning(pop)
  451. #endif
  452. newText[newLength] = '\0';
  453. setTextZ(newText, newLength);
  454. delete[] replacements;
  455. }
  456. result->release();
  457. matcher->release();
  458. }
  459. void Text::ersetzen(
  460. int p1, int p2, const char* t) // replaces text from p1 to p2
  461. {
  462. ersetzen(p1, p2, t, textLength(t)); // replace text
  463. }
  464. void Framework::Text::ersetzen(int p1, int p2, const char* t, int len)
  465. {
  466. if (p1 > p2)
  467. {
  468. int x = p1; // swap p1 and p2
  469. p1 = p2;
  470. p2 = x;
  471. }
  472. remove(p1, p2); // delete text section from p1 to p2
  473. if (len > 0)
  474. {
  475. insert(p1, t, len); // insert argument at p1
  476. }
  477. }
  478. void Text::ersetzen(int p1, int p2, const Text& t)
  479. {
  480. ersetzen(p1, p2, t, t.getLength()); // replace text
  481. }
  482. void Text::ersetzen(char c1, char c2) // replaces every c1 with c2
  483. {
  484. if (c1 == '\0' || c2 == '\0') // check for invalid argument
  485. return;
  486. if (!hat(c1)) // check if c1 exists
  487. return;
  488. int suchGCount = 0;
  489. if (suchGBeg && suchGEnd)
  490. {
  491. for (int i = 0; i < length; ++i) // search text
  492. {
  493. bool b = suchGCount != 0;
  494. if (txt[i] == c1 && !suchGCount) txt[i] = c2; // replace text
  495. if (txt[i] == suchGBeg) ++suchGCount;
  496. if (txt[i] == suchGEnd) --suchGCount;
  497. if (txt[i] == c1 && !suchGCount && b) txt[i] = c2; // replace text
  498. }
  499. }
  500. else
  501. {
  502. for (int i = 0; i < length; ++i) // search text
  503. {
  504. if (txt[i] == c1) txt[i] = c2; // replace text
  505. }
  506. }
  507. }
  508. void Text::ersetzen(const char* t1, const char* t2) // replaces every t1 with t2
  509. {
  510. ersetzen(t1, textLength(t1), t2, textLength(t2)); // replace text
  511. }
  512. void Framework::Text::ersetzen(
  513. const char* t1, int len1, const char* t2, int len2)
  514. {
  515. if (len1 > length || len1 <= 0) // check for invalid argument
  516. return;
  517. if (!hat(t1, len1)) // check if t1 exists
  518. return;
  519. int anz = anzahlVon(t1, len1); // count of t1 in text
  520. int* begin = new int[anz];
  521. int* end = new int[anz];
  522. int searchStart = 0;
  523. for (int i = 0; i < anz; ++i) // save positions of t1
  524. {
  525. begin[i] = positionVon(searchStart, t1, len1);
  526. end[i] = begin[i] + len1;
  527. searchStart = end[i];
  528. }
  529. int resl
  530. = (length - (anz * len1)) + (anz * len2) + 1; // length of result
  531. char* res = new char[resl]; // create new text
  532. int rep = 0; // stores which t1 we are at
  533. int last = 0; // fill position in txt
  534. int neu = 0; // fill position in res
  535. for (; neu < resl; ++neu) // fill text
  536. {
  537. if (rep < anz && last == begin[rep]) // replace text
  538. {
  539. last = end[rep];
  540. ++rep;
  541. for (int ii = 0; ii < len2; ++ii)
  542. {
  543. if (neu >= resl) break;
  544. res[neu] = t2[ii];
  545. ++neu;
  546. }
  547. if (neu >= resl) break;
  548. --neu;
  549. }
  550. else // copy text
  551. {
  552. res[neu] = txt[last];
  553. ++last;
  554. }
  555. }
  556. res[resl - 1] = '\0'; // set text end
  557. setTextZ(res, resl); // set text
  558. delete[] begin; // free memory
  559. delete[] end;
  560. }
  561. void Text::ersetzen(const Text& t1, const char* t2)
  562. {
  563. ersetzen(t1, t1.getLength(), t2, textLength(t2)); // replace text
  564. }
  565. void Text::ersetzen(const char* t1, const Text& t2)
  566. {
  567. ersetzen(t1, textLength(t1), t2, t2.getLength()); // replace text
  568. }
  569. void Text::ersetzen(const Text& t1, const Text& t2)
  570. {
  571. ersetzen(t1, t1.getLength(), t2, t2.getLength()); // replace text
  572. }
  573. void Text::ersetzen(int index, char c1, char c2) // replaces the i-th c1 with c2
  574. {
  575. if (c1 == '\0' || c2 == '\0'
  576. || index < 0) // check for invalid argument
  577. return;
  578. if (!hat(c1)) // check if c1 exists
  579. return;
  580. int anz = anzahlVon(c1);
  581. if (index >= anz) // check if an i-th c1 exists
  582. return;
  583. txt[positionVon(c1, index)] = c2;
  584. }
  585. void Text::ersetzen(int index,
  586. const char* t1,
  587. const char* t2) // replaces the i-th t1 with t2
  588. {
  589. ersetzen(index, t1, textLength(t1), t2, textLength(t2)); // replace text
  590. }
  591. void Framework::Text::ersetzen(
  592. int index, const char* t1, int len1, const char* t2, int len2)
  593. {
  594. if (len1 >= length || len1 <= 0
  595. || index < 0) // check for invalid argument
  596. return;
  597. if (!hat(t1)) // check if t1 exists
  598. return;
  599. int anz = anzahlVon(t1, len1); // count of t1 in text
  600. if (index >= anz) // check if an i-th t1 exists
  601. return;
  602. int begin = positionVon(index, len1, t1);
  603. int end = begin + len1;
  604. ersetzen(begin, end, t2, len2); // replace
  605. }
  606. void Text::ersetzen(int i, const Text& t1, const char* t2)
  607. {
  608. ersetzen(i, t1, t1.getLength(), t2, textLength(t2)); // replace
  609. }
  610. void Text::ersetzen(int i, const char* t1, const Text& t2)
  611. {
  612. ersetzen(i, t1, textLength(t1), t2, t2.getLength()); // replace
  613. }
  614. void Text::ersetzen(int i, const Text& t1, const Text& t2)
  615. {
  616. ersetzen(i, t1, t1.getLength(), t2,
  617. t2.getLength()); // replace
  618. }
  619. void Text::fillText(
  620. char c, int len) // sets the text to as many c as len is large
  621. {
  622. char* res = new char[(__int64)len + 1];
  623. for (int i = 0; i < len; ++i)
  624. res[i] = c;
  625. res[len] = '\0';
  626. setTextZ(res, len);
  627. }
  628. void Text::remove(int p) // deletes p
  629. {
  630. if (p < 0 || p >= length) // check for invalid argument
  631. return;
  632. char* res = new char[length]; // create new text
  633. for (int i = 0; i < p && i < length; ++i) // fill text
  634. res[i] = txt[i];
  635. for (int i = p + 1; i < length; ++i)
  636. res[i - 1] = txt[i];
  637. res[length - 1] = 0;
  638. setTextZ(res, length - 1); // set text
  639. }
  640. void Text::remove(int p1, int p2) // deletes from p1 to p2 (p2 is kept)
  641. {
  642. if (p1 == p2) return;
  643. if (p1 > p2) // swap p1 and p2
  644. {
  645. int x = p1;
  646. p1 = p2;
  647. p2 = x;
  648. }
  649. if (p1 < 0) // check for invalid argument
  650. p1 = 0;
  651. if (p2 > length) p2 = length;
  652. int resl = length - (p2 - p1); // length of result
  653. char* res = new char[(__int64)resl + 1]; // create new text
  654. memcpy(res, txt, p1);
  655. memcpy(res + p1, txt + p2, length - p2);
  656. res[resl] = '\0'; // set text end
  657. setTextZ(res, resl); // set text
  658. }
  659. void Text::remove(char c) // deletes every c
  660. {
  661. if (!hat(c)) // check if c exists
  662. return;
  663. int anz = anzahlVon(c); // count of c
  664. char* res = new char[(__int64)length - anz + 1]; // create new text
  665. int anz2 = 0;
  666. int suchGCount = 0;
  667. for (int i = 0; i < length; ++i) // fill text
  668. {
  669. bool b = suchGCount != 0;
  670. if (txt[i] == c && !suchGCount)
  671. ++anz2;
  672. else
  673. res[i - anz2] = txt[i];
  674. if (txt[i] == suchGBeg) ++suchGCount;
  675. if (txt[i] == suchGEnd) --suchGCount;
  676. if (b)
  677. {
  678. if (txt[i] == c && !suchGCount)
  679. ++anz2;
  680. else
  681. res[i - anz2] = txt[i];
  682. }
  683. }
  684. res[length - anz] = '\0'; // set text end
  685. setTextZ(res, length - anz); // set text
  686. }
  687. void Text::remove(const char* t) // deletes every t
  688. {
  689. remove(t, textLength(t)); // delete text
  690. }
  691. void Framework::Text::remove(const char* t, int len)
  692. {
  693. if (len <= 0 || len > length) // check for invalid argument
  694. return;
  695. if (!hat(t, len)) // check if text contains t
  696. return;
  697. int anz = anzahlVon(t, len); // count of t
  698. int* begin = new int[anz];
  699. for (int i = 0; i < anz; ++i) // find start of all t's
  700. begin[i] = positionVon(i, len, t);
  701. int resl = length - (anz * len); // length of result
  702. char* res = new char[(__int64)resl + 1]; // create new text
  703. int del = 0;
  704. for (int i = 0; i < length; ++i) // fill text
  705. {
  706. if (del < anz && i == begin[del]) // skip text
  707. {
  708. i += len - 1;
  709. ++del;
  710. }
  711. else
  712. res[i - (del * len)] = txt[i]; // fill text
  713. }
  714. res[resl] = '\0'; // set text end
  715. setTextZ(res, resl); // set text
  716. delete[] begin;
  717. }
  718. void Text::remove(const Text& t)
  719. {
  720. remove(t, t.getLength()); // delete text
  721. }
  722. void Text::remove(int index, char c)
  723. {
  724. if (index < 0 || !hat(c)) // check for invalid argument
  725. return;
  726. int anz = anzahlVon(c); // count of c's
  727. if (index >= anz) // check if an i-th c exists
  728. return;
  729. int pos = positionVon(c, index); // position of the i-th c
  730. if (pos < 0) return;
  731. if (!length) return;
  732. char* res = new char[length]; // create new text
  733. for (int i = 0; i < pos && i < length; ++i) // fill text
  734. res[i] = txt[i];
  735. for (int i = pos + 1; i < length; ++i)
  736. res[i - 1] = txt[i];
  737. res[length - 1] = '\0'; // set text end
  738. setTextZ(res, length - 1); // set text
  739. }
  740. void Text::remove(int index, const char* t) // deletes the i-th t
  741. {
  742. remove(index, t, textLength(t)); // delete text
  743. }
  744. void Framework::Text::remove(int index, const char* t, int len)
  745. {
  746. if (index < 0 || !hat(t, len)
  747. || len <= 0) // check for invalid argument
  748. return;
  749. int anz = anzahlVon(t, len); // count of t's
  750. if (index >= anz) // check if an i-th t exists
  751. return;
  752. int pos = positionVon(index, len, t); // position of the i-th t
  753. if (pos < 0) return;
  754. if (!length) return;
  755. char* res = new char[(__int64)length - len + 1]; // create new text
  756. for (int i = 0; i < pos && i < length - len + 1; ++i) // fill text
  757. res[i] = txt[i];
  758. for (int i = pos + len; i < length; ++i)
  759. res[i - len] = txt[i];
  760. res[length - len] = '\0'; // set text end
  761. setTextZ(res, length - len); // set text
  762. }
  763. void Text::remove(int i, const Text& t)
  764. {
  765. remove(i, t, t.getLength()); // delete text
  766. }
  767. // Deletes all ' ', '\n', '\r', '\t' until a different character
  768. // pos: The position of the first character
  769. int Text::removeWhitespaceAfter(int pos)
  770. {
  771. int removedLength = 0;
  772. for (int i = pos; i < length; i++)
  773. {
  774. if (txt[i] == ' ' || txt[i] == '\n' || txt[i] == '\r' || txt[i] == '\t')
  775. removedLength++;
  776. else
  777. break;
  778. }
  779. remove(pos, pos + removedLength);
  780. return length;
  781. }
  782. // Deletes all ' ', '\n', '\r', '\t' until a different character
  783. // pos: The position of the first character (starts at pos-1)
  784. int Text::removeWhitespaceBefore(int pos)
  785. {
  786. int length = 0;
  787. for (int i = pos - 1; i >= 0; i--)
  788. {
  789. if (txt[i] == ' ' || txt[i] == '\n' || txt[i] == '\r' || txt[i] == '\t')
  790. length++;
  791. else
  792. break;
  793. }
  794. remove(pos - length, pos);
  795. return length;
  796. }
  797. void Text::setPrecision(
  798. int p) // sets the number of decimal places for doubles
  799. {
  800. precision = p;
  801. }
  802. // constant
  803. int Text::getLength() const // returns the text length
  804. {
  805. return length;
  806. }
  807. int Text::getLKick(int pos) const
  808. {
  809. if (txt[pos - 1] == ' ')
  810. {
  811. int ret = 1;
  812. for (; ret < pos && txt[pos - ret - 1] == ' '
  813. && txt[pos - ret - 1] != '\n';
  814. ++ret)
  815. ;
  816. return pos - ret;
  817. }
  818. else
  819. {
  820. int ret = 1;
  821. for (; ret < pos && txt[pos - ret - 1] != ' '
  822. && txt[pos - ret - 1] != '\n';
  823. ++ret)
  824. ;
  825. return pos - ret;
  826. }
  827. }
  828. int Text::getOKick(int pos) const
  829. {
  830. if (!hat('\n')) return 0;
  831. int lpos = 0;
  832. while (pos - lpos - 1 > 0 && txt[pos - lpos - 1] != '\n')
  833. ++lpos;
  834. int vllen = 1;
  835. while (pos - lpos - vllen - 1 >= 0 && txt[pos - lpos - vllen - 1] != '\n')
  836. ++vllen;
  837. if (vllen > lpos)
  838. return pos - vllen;
  839. else
  840. return pos - lpos - 1;
  841. }
  842. int Text::getRKick(int pos) const
  843. {
  844. if (txt[pos] == ' ')
  845. {
  846. int ret = 1;
  847. for (; ret + pos < length && txt[pos + ret] == ' '
  848. && txt[pos + ret] != '\n';
  849. ++ret)
  850. ;
  851. return pos + ret;
  852. }
  853. else
  854. {
  855. int ret = 1;
  856. for (; ret + pos < length && txt[pos + ret] != ' '
  857. && txt[pos + ret] != '\n';
  858. ++ret)
  859. ;
  860. return pos + ret;
  861. }
  862. }
  863. int Text::getUKick(int pos) const
  864. {
  865. if (!hat('\n')) return length;
  866. int lpos = 0;
  867. while (pos - lpos > 0 && txt[pos - lpos - 1] != '\n')
  868. ++lpos;
  869. int llen = 1;
  870. while (pos + llen - 1 < length && txt[pos + llen - 1] != '\n')
  871. ++llen;
  872. int vllen = 1;
  873. while (
  874. pos + llen + vllen - 1 < length && txt[pos + llen + vllen - 1] != '\n')
  875. ++vllen;
  876. if (vllen == 1) return pos + llen < length ? pos + llen : length;
  877. if (vllen < lpos) return pos + llen + vllen - 1;
  878. return pos + llen + lpos;
  879. }
  880. bool Text::hat(const Text& t) const // does the text contain t
  881. {
  882. return hat(t, t.getLength()); // check text
  883. }
  884. bool Text::hat(const char* t) const
  885. {
  886. return hat(t, textLength(t)); // check text
  887. }
  888. bool Framework::Text::hat(const char* t, int len) const
  889. {
  890. return hat(0, t, len);
  891. }
  892. bool Framework::Text::hat(int searchStartIndex, const char* t) const
  893. {
  894. return hat(searchStartIndex, t, textLength(t)); // check text
  895. }
  896. bool Framework::Text::hat(int searchStartIndex, const char* t, int len) const
  897. {
  898. if (len <= 0
  899. || len > length - searchStartIndex) // check for invalid argument
  900. return 0;
  901. int suchGCount = 0;
  902. for (int i = searchStartIndex; i + len <= length; ++i) // search text
  903. {
  904. bool searched = 0;
  905. if (!suchGCount)
  906. {
  907. bool b = 1;
  908. for (int ii = 0; ii < len; ++ii) // check text
  909. b &= txt[i + ii] == t[ii];
  910. if (b) return 1;
  911. searched = 1;
  912. }
  913. if (txt[i] == suchGBeg) ++suchGCount;
  914. if (txt[i] == suchGEnd) --suchGCount;
  915. if (!suchGCount && !searched)
  916. {
  917. bool b = 1;
  918. for (int ii = 0; ii < len; ++ii) // check text
  919. b &= txt[i + ii] == t[ii];
  920. if (b) return 1;
  921. }
  922. }
  923. return 0;
  924. }
  925. // Checks whether the text contains the content of another text
  926. // t: The text whose content to search for
  927. // return: (true) if the content is found. (false) otherwise
  928. // pos: the position where the string should begin
  929. bool Text::hatAt(int pos, const Text& t) const
  930. {
  931. return hatAt(pos, t, t.getLength()); // check text
  932. }
  933. // Checks whether the text contains a specific string
  934. // t: The string to search for
  935. // return: (true) if the string is found. (false) otherwise
  936. // pos: the position where the string should begin
  937. bool Text::hatAt(int pos, const char* t) const
  938. {
  939. return hatAt(pos, t, textLength(t)); // check text
  940. }
  941. bool Framework::Text::hatAt(int pos, const char* t, int len) const
  942. {
  943. if (len <= 0 || len + pos > length) // check for invalid argument
  944. return 0;
  945. bool b = 1;
  946. for (int i = 0; i < len; ++i) // check text
  947. b &= txt[pos + i] == t[i];
  948. return b;
  949. }
  950. bool Text::hat(char c) const // contains c
  951. {
  952. bool ret = 0;
  953. int suchGCount = 0;
  954. for (int i = 0; i < length; ++i) // search
  955. {
  956. bool b = suchGCount != 0;
  957. if (!suchGCount) // check
  958. ret |= txt[i] == c;
  959. if (txt[i] == suchGBeg) ++suchGCount;
  960. if (txt[i] == suchGEnd) --suchGCount;
  961. if (!suchGCount && b) // check
  962. ret |= txt[i] == c;
  963. }
  964. return ret;
  965. }
  966. bool Text::beginnsWith(const char* t) const
  967. {
  968. return hatAt(0, t);
  969. }
  970. bool Text::endsWith(const char* t) const
  971. {
  972. int len = textLength(t);
  973. return hatAt(length - len, t, len);
  974. }
  975. bool Text::istGleich(const char* t) const // checks if the text equals t
  976. {
  977. return istGleich(t, textLength(t)); // check text
  978. }
  979. bool Text::istGleich(
  980. const char* t, int len) const // checks if the text equals t
  981. {
  982. if (length != len) // check for invalid argument
  983. return 0;
  984. if (length == -1) return 1;
  985. bool ret = true;
  986. for (int i = 0; i < len; ++i) // check
  987. ret &= txt[i] == t[i];
  988. return ret;
  989. }
  990. bool Text::istGleich(const Text& t) const
  991. {
  992. return istGleich(t, t.getLength()); // check text
  993. }
  994. const char* Text::getText() const // returns the text
  995. {
  996. return txt;
  997. }
  998. int Text::anzahlVon(char c) const // returns the count of c in the text
  999. {
  1000. int ret = 0;
  1001. int suchGCount = 0;
  1002. if (suchGBeg && suchGEnd)
  1003. {
  1004. for (int i = 0; i < length; ++i) // search
  1005. {
  1006. bool b = suchGCount != 0;
  1007. ret += txt[i] == c && !suchGCount; // count
  1008. if (txt[i] == suchGBeg) ++suchGCount;
  1009. if (txt[i] == suchGEnd) --suchGCount;
  1010. ret += txt[i] == c && !suchGCount && b; // count
  1011. }
  1012. }
  1013. else
  1014. {
  1015. for (int i = 0; i < length; ++i) // search
  1016. {
  1017. ret += txt[i] == c; // count
  1018. }
  1019. }
  1020. return ret;
  1021. }
  1022. int Text::anzahlVon(
  1023. const char* t) const // returns the count of t in the text
  1024. {
  1025. return anzahlVon(t, textLength(t)); // check text
  1026. }
  1027. int Framework::Text::anzahlVon(const char* t, int len) const
  1028. {
  1029. if (len <= 0 || len > length) // check for invalid argument
  1030. return 0;
  1031. int ret = 0;
  1032. int suchGCount = 0;
  1033. for (int i = 0; i + len <= length; ++i) // search
  1034. {
  1035. bool b = suchGCount != 0;
  1036. if (!suchGCount)
  1037. {
  1038. bool b = 1;
  1039. for (int ii = 0; ii < len; ++ii) // check
  1040. b &= txt[i + ii] == t[ii];
  1041. if (b) ++ret; // count
  1042. }
  1043. if (txt[i] == suchGBeg) ++suchGCount;
  1044. if (txt[i] == suchGEnd) --suchGCount;
  1045. if (!suchGCount && b)
  1046. {
  1047. bool b = 1;
  1048. for (int ii = 0; ii < len; ++ii) // check
  1049. b &= txt[i + ii] == t[ii];
  1050. if (b) ++ret; // count
  1051. }
  1052. }
  1053. return ret;
  1054. }
  1055. int Text::anzahlVon(const Text& t) const
  1056. {
  1057. return anzahlVon(t, t.getLength()); // check text
  1058. }
  1059. int Text::positionVon(char c) const // returns the position of the first c
  1060. {
  1061. int suchGCount = 0;
  1062. for (int i = 0; i < length; ++i) // search
  1063. {
  1064. bool b = suchGCount != 0;
  1065. if (txt[i] == c && !suchGCount) // check
  1066. return i;
  1067. if (txt[i] == suchGBeg) ++suchGCount;
  1068. if (txt[i] == suchGEnd) --suchGCount;
  1069. if (txt[i] == c && !suchGCount && b) // check
  1070. return i;
  1071. }
  1072. return -1;
  1073. }
  1074. int Text::positionVon(
  1075. const char* t) const // returns the position of the first t
  1076. {
  1077. return positionVon(textLength(t), t);
  1078. }
  1079. int Framework::Text::positionVon(int len, const char* t) const
  1080. {
  1081. return positionVon(0, t, len);
  1082. }
  1083. int Text::positionVon(int searchStart, const char* t, int len) const
  1084. {
  1085. if (len <= 0
  1086. || len > length - searchStart) // check for invalid argument
  1087. return -1;
  1088. int suchGCount = 0;
  1089. for (int i = searchStart; i + len <= length; ++i) // search
  1090. {
  1091. bool b = suchGCount != 0;
  1092. if (!suchGCount)
  1093. {
  1094. bool b = 1;
  1095. for (int ii = 0; ii < len; ++ii) // check
  1096. b &= txt[i + ii] == t[ii];
  1097. if (b) return i;
  1098. }
  1099. if (txt[i] == suchGBeg) ++suchGCount;
  1100. if (txt[i] == suchGEnd) --suchGCount;
  1101. if (!suchGCount && b)
  1102. {
  1103. bool b = 1;
  1104. for (int ii = 0; ii < len; ++ii) // check
  1105. b &= txt[i + ii] == t[ii];
  1106. if (b) return i;
  1107. }
  1108. }
  1109. return -1;
  1110. }
  1111. int Text::positionVon(const Text& t) const
  1112. {
  1113. return positionVon(t.getLength(), t); // check text
  1114. }
  1115. int Text::positionVon(
  1116. char c, int index) const // returns the position of the i-th c
  1117. {
  1118. int ii = 0;
  1119. int suchGCount = 0;
  1120. for (int i = 0; i < length; ++i) // search
  1121. {
  1122. bool b = suchGCount != 0;
  1123. if (txt[i] == c && !suchGCount) // check
  1124. {
  1125. if (ii == index)
  1126. return i;
  1127. else
  1128. ++ii;
  1129. }
  1130. if (txt[i] == suchGBeg) ++suchGCount;
  1131. if (txt[i] == suchGEnd) --suchGCount;
  1132. if (txt[i] == c && !suchGCount && b) // check
  1133. {
  1134. if (ii == index)
  1135. return i;
  1136. else
  1137. ++ii;
  1138. }
  1139. }
  1140. return -1;
  1141. }
  1142. int Text::positionVon(
  1143. const char* t, int index) const // returns the position of the i-th t
  1144. {
  1145. return positionVon(index, textLength(t), t); // check text
  1146. }
  1147. int Framework::Text::positionVon(int index, int len, const char* t) const
  1148. {
  1149. if (len <= 0 || len > length) // check for invalid argument
  1150. return 0;
  1151. int i2 = 0;
  1152. int suchGCount = 0;
  1153. for (int i = 0; i + len <= length; ++i) // search
  1154. {
  1155. bool b = suchGCount != 0;
  1156. if (!suchGCount)
  1157. {
  1158. bool b = 1;
  1159. for (int ii = 0; ii < len; ++ii) // check
  1160. b &= txt[i + ii] == t[ii];
  1161. if (b)
  1162. {
  1163. if (i2 == index)
  1164. return i;
  1165. else
  1166. ++i2;
  1167. }
  1168. }
  1169. if (txt[i] == suchGBeg) ++suchGCount;
  1170. if (txt[i] == suchGEnd) --suchGCount;
  1171. if (!suchGCount && b)
  1172. {
  1173. bool b = 1;
  1174. for (int ii = 0; ii < len; ++ii) // check
  1175. b &= txt[i + ii] == t[ii];
  1176. if (b)
  1177. {
  1178. if (i2 == index)
  1179. return i;
  1180. else
  1181. ++i2;
  1182. }
  1183. }
  1184. }
  1185. return -1;
  1186. }
  1187. int Text::positionVon(const Text& t, int i) const
  1188. {
  1189. return positionVon(i, t.getLength(), t); // check text
  1190. }
  1191. Text* Text::getTeilText(
  1192. int p1, int p2) const // returns the text from p1 to p2
  1193. {
  1194. if (p1 > p2) // swap p1 and p2
  1195. {
  1196. int x = p1;
  1197. p1 = p2;
  1198. p2 = x;
  1199. }
  1200. if (p1 < 0 || p2 > length) // check for invalid argument
  1201. return new Text("");
  1202. char* cp = new char[(__int64)p2 - p1 + 1]; // create new text
  1203. for (int i = p1; i < p2; ++i) // fill text
  1204. {
  1205. cp[i - p1] = txt[i];
  1206. }
  1207. cp[p2 - p1] = '\0';
  1208. return new Text(cp, p2 - p1); // return text
  1209. }
  1210. Text* Text::getTeilText(int p) const // returns the text from p to the end
  1211. {
  1212. return getTeilText(p, getLength()); // return text
  1213. }
  1214. // calculates the hash code of the text
  1215. int Text::hashCode() const
  1216. {
  1217. int result = 0;
  1218. for (int i = 0; i < length; i++)
  1219. result += (int)pow(txt[i] * 31, length - 1 - i);
  1220. return result;
  1221. }
  1222. // Operators
  1223. Text& Text::operator+=(const int num)
  1224. {
  1225. append(num);
  1226. return *this;
  1227. }
  1228. // Appends a number to the end of the text
  1229. Text& Text::operator+=(const __int64 num)
  1230. {
  1231. append(num);
  1232. return *this;
  1233. }
  1234. Text& Text::operator+=(const double num)
  1235. {
  1236. append(num);
  1237. return *this;
  1238. }
  1239. Text& Text::operator+=(const float num)
  1240. {
  1241. append(num);
  1242. return *this;
  1243. }
  1244. Text& Text::operator+=(const char* txt)
  1245. {
  1246. append(txt);
  1247. return *this;
  1248. }
  1249. Text& Text::operator+=(const Text& txt)
  1250. {
  1251. append(txt, txt.getLength());
  1252. return *this;
  1253. }
  1254. Text& Text::operator=(const int num)
  1255. {
  1256. setText("", 0);
  1257. append(num);
  1258. return *this;
  1259. }
  1260. Text& Text::operator=(const double num)
  1261. {
  1262. setText("", 0);
  1263. append(num);
  1264. return *this;
  1265. }
  1266. Text& Text::operator=(const float num)
  1267. {
  1268. setText("", 0);
  1269. append(num);
  1270. return *this;
  1271. }
  1272. Text& Text::operator=(const char* txt)
  1273. {
  1274. setText(txt);
  1275. return *this;
  1276. }
  1277. Text& Text::operator=(const Text& txt)
  1278. {
  1279. setText(txt, txt.getLength());
  1280. return *this;
  1281. }
  1282. Text::operator const char*() const
  1283. {
  1284. return txt;
  1285. }
  1286. Text::operator int() const
  1287. {
  1288. if (length > 2 && txt[0] == '0' && txt[1] == 'x')
  1289. return TextZuInt((txt + 2), 16);
  1290. return TextZuInt(txt, 10);
  1291. }
  1292. Text::operator __int64() const
  1293. {
  1294. if (length > 2 && txt[0] == '0' && txt[1] == 'x')
  1295. return TextZuInt64((txt + 2), 16);
  1296. return TextZuInt64(txt, 10);
  1297. }
  1298. Text::operator double() const
  1299. {
  1300. return TextZuDouble(txt);
  1301. }
  1302. Text::operator float() const
  1303. {
  1304. return TextZuFloat(txt);
  1305. }
  1306. bool Text::operator>(Text& t) const
  1307. {
  1308. int len2 = t.getLength();
  1309. const char* txt2 = t;
  1310. for (int i = 0; i < length && i < len2; ++i)
  1311. {
  1312. if (txt[i] > txt2[i]) return 1;
  1313. if (txt[i] < txt2[i]) return 0;
  1314. }
  1315. if (length > len2) return 1;
  1316. return 0;
  1317. }
  1318. bool Text::operator<(Text& t) const
  1319. {
  1320. int len2 = t.getLength();
  1321. const char* txt2 = t;
  1322. for (int i = 0; i < length && i < len2; ++i)
  1323. {
  1324. if (txt[i] < txt2[i]) return 1;
  1325. if (txt[i] > txt2[i]) return 0;
  1326. }
  1327. if (length < len2) return 1;
  1328. return 0;
  1329. }
  1330. // Creates a new text consisting of this and t2
  1331. Text Text::operator+(const Text& t2) const
  1332. {
  1333. return Text(*this) += t2;
  1334. }
  1335. // Creates a new text consisting of this and t2
  1336. Text Text::operator+(const char* t2) const
  1337. {
  1338. return Text(*this) += t2;
  1339. }
  1340. // Creates a new text consisting of this and num
  1341. Text Text::operator+(const int num) const
  1342. {
  1343. return Text(*this) += num;
  1344. }
  1345. // Creates a new text consisting of this and num
  1346. Text Text::operator+(const __int64 num) const
  1347. {
  1348. return Text(*this) += num;
  1349. }
  1350. // Creates a new text consisting of this and num
  1351. Text Text::operator+(const double num) const
  1352. {
  1353. return Text(*this) += num;
  1354. }
  1355. // Creates a new text consisting of this and num
  1356. Text Text::operator+(const float num) const
  1357. {
  1358. return Text(*this) += num;
  1359. }
  1360. bool Text::operator==(const Text& right) const
  1361. {
  1362. return istGleich(right, right.getLength());
  1363. }
  1364. Text Text::fromArray(char* arr, int len)
  1365. {
  1366. return Text(arr, len);
  1367. }
  1368. const Text Text::EMPTY = "";
  1369. // Content of the TextReader class
  1370. // Constructor
  1371. // txt: The text to be read. It is not copied but read directly.
  1372. TextReader::TextReader(Text* txt)
  1373. : ReferenceCounter()
  1374. {
  1375. this->txt = txt;
  1376. lPos = 0;
  1377. }
  1378. // Destructor
  1379. TextReader::~TextReader()
  1380. {
  1381. txt->release();
  1382. }
  1383. // Sets the position of the byte to be read next
  1384. // pos: The index of the byte
  1385. // ende: 1 if the index counts from the end of the text. 0 if from the beginning
  1386. void TextReader::setLPosition(__int64 pos, bool ende)
  1387. {
  1388. int l = txt->getLength();
  1389. lPos = ende ? l - pos : pos;
  1390. if (lPos < 0) lPos = 0;
  1391. if (lPos > l) lPos = l;
  1392. }
  1393. // Reads from the text
  1394. // bytes: An array to be filled with bytes from the text
  1395. // len: How many bytes to read from the text
  1396. void TextReader::lese(char* bytes, int len)
  1397. {
  1398. int l = txt->getLength();
  1399. len = (int)MIN(l - lPos, len);
  1400. for (__int64 i = lPos; i < lPos + len; i++)
  1401. bytes[i - lPos] = txt->getText()[i];
  1402. lPos += len;
  1403. }
  1404. // Reads the next line of the text
  1405. // return: The read line as text with line break
  1406. Text* TextReader::leseZeile()
  1407. {
  1408. if (istEnde()) return 0;
  1409. Text* ret = new Text("");
  1410. __int64 len = txt->getLength();
  1411. for (char c = 0; c != '\n' && lPos < len;)
  1412. {
  1413. lese(&c, 1);
  1414. if (c) ret->append(&c, 1);
  1415. }
  1416. return ret;
  1417. }
  1418. // Checks whether the resource has been fully read
  1419. // return 1 if the resource has been fully read. 0 otherwise
  1420. bool TextReader::istEnde() const
  1421. {
  1422. return lPos >= txt->getLength();
  1423. }
  1424. // Returns the index of the byte from the text that would be read next
  1425. // return -1 if an error occurred. Otherwise the position of the read pointer
  1426. __int64 TextReader::getLPosition() const
  1427. {
  1428. return lPos;
  1429. }
  1430. //! Returns the number of bytes to read
  1431. __int64 TextReader::getSize() const
  1432. {
  1433. return txt->getLength();
  1434. }
  1435. // char* operationen
  1436. int Framework::stringPositionVonChar(const char* string,
  1437. char c,
  1438. int num) // finds the position of the num-th c
  1439. // in string, -1 if not found
  1440. {
  1441. int gef = 0;
  1442. int p = 0;
  1443. for (char cc = *string; *string; ++string)
  1444. {
  1445. if (cc == c)
  1446. {
  1447. if (gef == num)
  1448. return p;
  1449. else
  1450. ++gef;
  1451. }
  1452. ++p;
  1453. }
  1454. return -1;
  1455. }
  1456. int Framework::stringPositionVonString(const char* string,
  1457. char* suche,
  1458. int sBegPos) // finds the position of 'suche' in 'string' starting at
  1459. // position 'sBegPos', -1 if not found
  1460. {
  1461. for (int i = 0; i < sBegPos; ++i)
  1462. {
  1463. if (!*string) return -1;
  1464. ++string;
  1465. }
  1466. int tl = textLength(suche); // length of the argument
  1467. int txl = textLength(string); // length of the text
  1468. if (tl <= 0 || tl > txl) // check for invalid argument
  1469. return -1;
  1470. for (int i = 0; i + tl <= txl; ++i) // search
  1471. {
  1472. bool b = 1;
  1473. for (int ii = 0; ii < tl; ++ii) // check
  1474. if (b) b = string[i + ii] == suche[ii];
  1475. if (b) return i + sBegPos;
  1476. }
  1477. return -1;
  1478. }
  1479. //---Other Functions---
  1480. void Framework::TextKopieren(
  1481. const char* txt) // copies the text to the clipboard
  1482. {
  1483. #ifdef WIN32
  1484. int laen = textLength(txt) + 1;
  1485. if (laen == 1) return;
  1486. HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, laen);
  1487. if (!hMem) return;
  1488. memcpy(GlobalLock(hMem), txt, laen);
  1489. GlobalUnlock(hMem);
  1490. OpenClipboard(0);
  1491. EmptyClipboard();
  1492. SetClipboardData(CF_TEXT, hMem);
  1493. CloseClipboard();
  1494. #endif
  1495. }
  1496. const char*
  1497. Framework::TextInsert() // returns the text from the clipboard
  1498. {
  1499. #ifdef WIN32
  1500. if (!OpenClipboard(0)) return "";
  1501. HANDLE hClipData = GetClipboardData(CF_TEXT);
  1502. char* cBuffer = (char*)GlobalLock(hClipData);
  1503. GlobalUnlock(hClipData);
  1504. CloseClipboard();
  1505. return cBuffer;
  1506. #else
  1507. return 0;
  1508. #endif
  1509. }
  1510. char Framework::smallOrBig(char c, bool gr)
  1511. {
  1512. int ret = c;
  1513. if (gr)
  1514. {
  1515. if (c >= 'a' && c <= 'z')
  1516. ret -= 32;
  1517. else
  1518. {
  1519. switch (c)
  1520. {
  1521. case SpecialCharacters::CARET:
  1522. return SpecialCharacters::DEGREE_SIGN;
  1523. case '1':
  1524. return '!';
  1525. case '<':
  1526. return '>';
  1527. case '2':
  1528. return '\"';
  1529. case '3':
  1530. return SpecialCharacters::SECTION_SIGN;
  1531. case '4':
  1532. return '$';
  1533. case '5':
  1534. return '%';
  1535. case '6':
  1536. return '&';
  1537. case '7':
  1538. return '/';
  1539. case '8':
  1540. return '(';
  1541. case '9':
  1542. return ')';
  1543. case '0':
  1544. return '=';
  1545. case ',':
  1546. return ';';
  1547. case '.':
  1548. return ':';
  1549. case 'ß':
  1550. return '?';
  1551. case '-':
  1552. return '_';
  1553. case SpecialCharacters::ACUTE_ACCENT:
  1554. return '`';
  1555. case '+':
  1556. return '*';
  1557. case '#':
  1558. return '\'';
  1559. case SpecialCharacters::SMALL_UE:
  1560. return SpecialCharacters::BIG_UE;
  1561. case SpecialCharacters::SMALL_OE:
  1562. return SpecialCharacters::BIG_OE;
  1563. case SpecialCharacters::SMALL_AE:
  1564. return SpecialCharacters::BIG_AE;
  1565. }
  1566. }
  1567. }
  1568. else
  1569. {
  1570. if (c >= 'A' && c <= 'Z')
  1571. ret += 32;
  1572. else
  1573. {
  1574. switch (c)
  1575. {
  1576. case SpecialCharacters::DEGREE_SIGN:
  1577. return SpecialCharacters::CARET;
  1578. case '!':
  1579. return '1';
  1580. case '>':
  1581. return '<';
  1582. case '\"':
  1583. return '2';
  1584. case SpecialCharacters::SECTION_SIGN:
  1585. return '3';
  1586. case '$':
  1587. return '4';
  1588. case '%':
  1589. return '5';
  1590. case '&':
  1591. return '6';
  1592. case '/':
  1593. return '7';
  1594. case '(':
  1595. return '8';
  1596. case ')':
  1597. return '9';
  1598. case '=':
  1599. return '0';
  1600. case ';':
  1601. return ',';
  1602. case ':':
  1603. return '.';
  1604. case '?':
  1605. return SpecialCharacters::SZ;
  1606. case '_':
  1607. return '-';
  1608. case '`':
  1609. return SpecialCharacters::ACUTE_ACCENT;
  1610. case '*':
  1611. return '+';
  1612. case '\'':
  1613. return '#';
  1614. case SpecialCharacters::BIG_UE:
  1615. return SpecialCharacters::SMALL_UE;
  1616. case SpecialCharacters::BIG_OE:
  1617. return SpecialCharacters::SMALL_OE;
  1618. case SpecialCharacters::BIG_AE:
  1619. return SpecialCharacters::SMALL_AE;
  1620. }
  1621. }
  1622. }
  1623. return (char)ret;
  1624. }
  1625. bool Framework::istSchreibbar(
  1626. unsigned char zeichen) // checks if zeichen is a printable character
  1627. {
  1628. if (zeichen > 32 && zeichen < 127) return 1;
  1629. if (zeichen == 128 || zeichen == 181 || zeichen == 178 || zeichen == 179)
  1630. return 1;
  1631. if (zeichen > 191 && zeichen < 198) return 1;
  1632. if (zeichen > 199 && zeichen < 208) return 1;
  1633. if (zeichen > 209 && zeichen < 215) return 1;
  1634. if (zeichen > 216 && zeichen < 221) return 1;
  1635. if (zeichen > 222 && zeichen < 230) return 1;
  1636. if (zeichen > 231 && zeichen < 240) return 1;
  1637. if (zeichen > 241 && zeichen < 247) return 1;
  1638. if (zeichen > 248 && zeichen < 253) return 1;
  1639. if (zeichen == ' ' || zeichen == '\t') return 1;
  1640. return 0;
  1641. }
  1642. unsigned int Framework::TextZuInt(
  1643. const char* c, int system) // converts c to int
  1644. {
  1645. if (system == 16) return (unsigned int)strtoul(c, 0, system);
  1646. return (unsigned int)strtol(c, 0, system);
  1647. }
  1648. unsigned int Framework::TextZuInt(const char* c, char** c_ende, int system)
  1649. {
  1650. if (system == 16) return (unsigned int)strtoul(c, c_ende, system);
  1651. return (unsigned int)strtol(c, c_ende, system);
  1652. }
  1653. unsigned __int64 Framework::TextZuInt64(const char* c, int system)
  1654. {
  1655. if (system == 16) return strtoull(c, 0, system);
  1656. return (unsigned __int64)strtoll(c, 0, system);
  1657. }
  1658. unsigned __int64 Framework::TextZuInt64(
  1659. const char* c, char** c_ende, int system)
  1660. {
  1661. if (system == 16) return strtoull(c, c_ende, system);
  1662. return (unsigned __int64)strtoll(c, c_ende, system);
  1663. }
  1664. double Framework::TextZuDouble(const char* c) // converts c to double
  1665. {
  1666. return strtod(c, 0);
  1667. }
  1668. float Framework::TextZuFloat(const char* c) // converts c to float
  1669. {
  1670. return strtof(c, 0);
  1671. }
  1672. double Framework::TextZuDouble(const char* c, char** c_ende)
  1673. {
  1674. return strtod(c, c_ende);
  1675. }
  1676. float Framework::TextZuFloat(const char* c, char** c_ende)
  1677. {
  1678. return strtof(c, c_ende);
  1679. }
  1680. int Framework::textLength(const char* txt) // returns the length of txt
  1681. {
  1682. if (!txt) return 0;
  1683. int ret = 0;
  1684. for (; txt[ret]; ++ret)
  1685. ;
  1686. return ret;
  1687. }