You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.cc 37KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. /*
  2. Misc utilities.
  3. Copyright (C) 2002-2014 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "src/core/xmltag.h"
  18. #include "jeeps/gpsmath.h"
  19. #include <ctype.h>
  20. #include <errno.h>
  21. #include <math.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h> // for va_copy
  26. #include <time.h>
  27. #include <QtCore/QFileInfo>
  28. // First test Apple's clever macro that's really a runtime test so
  29. // that our universal binaries work right.
  30. #if defined __BIG_ENDIAN__
  31. #define i_am_little_endian !__BIG_ENDIAN__
  32. #else
  33. #if defined WORDS_BIGENDIAN
  34. # define i_am_little_endian 0
  35. #else
  36. # define i_am_little_endian 1
  37. #endif
  38. #endif
  39. #ifdef DEBUG_MEM
  40. #define DEBUG_FILENAME "/tmp/gpsbabel.debug"
  41. static FILE* debug_mem_file = NULL;
  42. void
  43. debug_mem_open()
  44. {
  45. debug_mem_file = xfopen(DEBUG_FILENAME, "a", "debug");
  46. }
  47. void
  48. debug_mem_output(char* format, ...)
  49. {
  50. va_list args;
  51. va_start(args, format);
  52. if (debug_mem_file) {
  53. vfprintf(debug_mem_file, format, args);
  54. fflush(debug_mem_file);
  55. }
  56. va_end(args);
  57. }
  58. void
  59. debug_mem_close()
  60. {
  61. if (debug_mem_file) {
  62. fclose(debug_mem_file);
  63. }
  64. debug_mem_file = NULL;
  65. }
  66. #endif
  67. void*
  68. #ifdef DEBUG_MEM
  69. XMALLOC(size_t size, DEBUG_PARAMS)
  70. #else
  71. xmalloc(size_t size)
  72. #endif
  73. {
  74. void* obj = malloc(size);
  75. #ifdef DEBUG_MEM
  76. debug_mem_output("malloc, %x, %d, %s, %d\n",
  77. obj, size, file, line);
  78. #endif
  79. if (!obj) {
  80. fatal("gpsbabel: Unable to allocate %ld bytes of memory.\n", (unsigned long) size);
  81. }
  82. return obj;
  83. }
  84. void*
  85. #ifdef DEBUG_MEM
  86. XCALLOC(size_t nmemb, size_t size, DEBUG_PARAMS)
  87. #else
  88. xcalloc(size_t nmemb, size_t size)
  89. #endif
  90. {
  91. void* obj = calloc(nmemb, size);
  92. #ifdef DEBUG_MEM
  93. debug_mem_output("calloc, %x, %d, %d, %s, %d\n",
  94. obj, nmemb, size, file, line);
  95. #endif
  96. if (!obj) {
  97. fatal("gpsbabel: Unable to allocate %ld units of %ld bytes of memory.\n", (unsigned long) nmemb, (unsigned long) size);
  98. }
  99. return obj;
  100. }
  101. void
  102. #ifdef DEBUG_MEM
  103. XFREE(const void* mem, DEBUG_PARAMS)
  104. #else
  105. xfree(const void* mem)
  106. #endif
  107. {
  108. free((void*) mem);
  109. #ifdef DEBUG_MEM
  110. debug_mem_output("free, %x, %s, %d\n",
  111. mem, file, line);
  112. #endif
  113. }
  114. char*
  115. #ifdef DEBUG_MEM
  116. XSTRDUP(const char* s, DEBUG_PARAMS)
  117. #else
  118. xstrdup(const char* s)
  119. #endif
  120. {
  121. char* o = s ? strdup(s) : strdup("");
  122. #ifdef DEBUG_MEM
  123. debug_mem_output("strdup, %x, %x, %s, %d\n",
  124. o, s, file, line);
  125. #endif
  126. if (!o) {
  127. fatal("gpsbabel: Unable to allocate %ld bytes of memory.\n", (unsigned long) strlen(s));
  128. }
  129. return o;
  130. }
  131. char* xstrdup(const QString& s)
  132. {
  133. return xstrdup(CSTR(s));
  134. }
  135. /*
  136. * Duplicate at most sz bytes in str.
  137. */
  138. char*
  139. #ifdef DEBUG_MEM
  140. XSTRNDUP(const char* str, size_t sz, DEBUG_PARAMS)
  141. #else
  142. xstrndup(const char* str, size_t sz)
  143. #endif
  144. {
  145. size_t newlen = 0;
  146. char* cin = (char*)str;
  147. char* newstr;
  148. while ((newlen < sz) && (*cin != '\0')) {
  149. newlen++;
  150. cin++;
  151. }
  152. newstr = (char*) xmalloc(newlen + 1);
  153. memcpy(newstr, str, newlen);
  154. newstr[newlen] = 0;
  155. return newstr;
  156. }
  157. void*
  158. #ifdef DEBUG_MEM
  159. XREALLOC(void* p, size_t s, DEBUG_PARAMS)
  160. #else
  161. xrealloc(void* p, size_t s)
  162. #endif
  163. {
  164. char* o = (char*) realloc(p,s);
  165. #ifdef DEBUG_MEM
  166. if (p != NULL) {
  167. debug_mem_output("realloc, %x, %x, %x, %s, %d\n", o, p, s, file, line);
  168. } else {
  169. debug_mem_output("malloc, %x, %d, %s, %d\n", o, s, file, line);
  170. }
  171. #endif
  172. if (!o) {
  173. fatal("gpsbabel: Unable to realloc %ld bytes of memory.\n", (unsigned long) s);
  174. }
  175. return o;
  176. }
  177. /*
  178. * For an allocated string, realloc it and append 's'
  179. */
  180. char*
  181. #ifdef DEBUG_MEM
  182. XSTRAPPEND(char* src, const char* newd, DEBUG_PARAMS)
  183. #else
  184. xstrappend(char* src, const char* newd)
  185. #endif
  186. {
  187. size_t newsz;
  188. if (!src) {
  189. return xxstrdup(newd, file, line);
  190. }
  191. if (!newd) {
  192. return xxstrdup(src, file, line);
  193. }
  194. newsz = strlen(src) + strlen(newd) + 1;
  195. src = (char*) xxrealloc(src, newsz, file, line);
  196. strcat(src, newd);
  197. return src;
  198. }
  199. /*
  200. * Wrapper for open that honours - for stdin, stdout, unifies error text.
  201. */
  202. FILE*
  203. xfopen(const char* fname, const char* type, const char* errtxt)
  204. {
  205. FILE* f;
  206. int am_writing = strchr(type, 'w') != NULL;
  207. if (fname == NULL) {
  208. fatal("%s must have a filename specified for %s.\n",
  209. errtxt, am_writing ? "write" : "read");
  210. }
  211. if (0 == strcmp(fname, "-")) {
  212. return am_writing ? stdout : stdin;
  213. }
  214. f = fopen(fname, type);
  215. if (NULL == f) {
  216. fatal("%s cannot open '%s' for %s. Error was '%s'.\n",
  217. errtxt, fname,
  218. am_writing ? "write" : "read",
  219. strerror(errno));
  220. }
  221. return f;
  222. }
  223. /*
  224. * Allocate a string using a format list with optional arguments
  225. * Returns -1 on error.
  226. * If return value is anything else, *strp will be populated with an
  227. * allocated string containging the formatted buffer.
  228. *
  229. * Freeing that is the responsbility of the caller.
  230. */
  231. int
  232. xasprintf(char** strp, const char* fmt, ...)
  233. {
  234. va_list args;
  235. int res;
  236. va_start(args, fmt);
  237. res = xvasprintf(strp, fmt, args);
  238. va_end(args);
  239. return res;
  240. }
  241. int
  242. xasprintf(QString* strp, const char* fmt, ...)
  243. {
  244. va_list args;
  245. int res;
  246. va_start(args, fmt);
  247. char* cstrp;
  248. res = xvasprintf(&cstrp, fmt, args);
  249. *strp = cstrp;
  250. xfree(cstrp);
  251. va_end(args);
  252. return res;
  253. }
  254. int
  255. xvasprintf(char** strp, const char* fmt, va_list ap)
  256. {
  257. /* From http://perfec.to/vsnprintf/pasprintf.c */
  258. /* size of first buffer malloc; start small to exercise grow routines */
  259. #ifdef DEBUG_MEM
  260. # define FIRSTSIZE 64
  261. #else
  262. # define FIRSTSIZE 1
  263. #endif
  264. char* buf = NULL;
  265. int bufsize;
  266. char* newbuf;
  267. size_t nextsize = 0;
  268. int outsize;
  269. va_list args;
  270. bufsize = 0;
  271. for (;;) {
  272. if (bufsize == 0) {
  273. if ((buf = (char*) xmalloc(FIRSTSIZE)) == NULL) {
  274. *strp = NULL;
  275. return -1;
  276. }
  277. bufsize = FIRSTSIZE;
  278. } else if ((newbuf = (char*) xrealloc(buf, nextsize)) != NULL) {
  279. buf = newbuf;
  280. bufsize = nextsize;
  281. } else {
  282. xfree(buf);
  283. *strp = NULL;
  284. return -1;
  285. }
  286. va_copy(args, ap);
  287. outsize = vsnprintf(buf, bufsize, fmt, args);
  288. va_end(args);
  289. if (outsize == -1) {
  290. /* Clear indication that output was truncated, but no
  291. * clear indication of how big buffer needs to be, so
  292. * simply double existing buffer size for next time.
  293. */
  294. nextsize = bufsize * 2;
  295. } else if (outsize == bufsize) {
  296. /* Output was truncated (since at least the \0 could
  297. * not fit), but no indication of how big the buffer
  298. * needs to be, so just double existing buffer size
  299. * for next time.
  300. */
  301. nextsize = bufsize * 2;
  302. } else if (outsize > bufsize) {
  303. /* Output was truncated, but we were told exactly how
  304. * big the buffer needs to be next time. Add two chars
  305. * to the returned size. One for the \0, and one to
  306. * prevent ambiguity in the next case below.
  307. */
  308. nextsize = outsize + 2;
  309. } else if (outsize == bufsize - 1) {
  310. /* This is ambiguous. May mean that the output string
  311. * exactly fits, but on some systems the output string
  312. * may have been trucated. We can't tell.
  313. * Just double the buffer size for next time.
  314. */
  315. nextsize = bufsize * 2;
  316. } else {
  317. /* Output was not truncated */
  318. break;
  319. }
  320. }
  321. /* Prevent us from allocating millions of unused bytes. */
  322. /* O.K.: I think this is not the final solution. */
  323. if (bufsize > outsize + 1) {
  324. const unsigned ptrsz = sizeof(buf);
  325. if (((bufsize + ptrsz + 1) / ptrsz) > ((outsize + ptrsz + 1) / ptrsz)) {
  326. buf = (char*) xrealloc(buf, outsize + 1);
  327. }
  328. }
  329. *strp = buf;
  330. return outsize;
  331. }
  332. void
  333. rtrim(char* s)
  334. {
  335. char* t = s;
  336. if (!s || !*s) {
  337. return;
  338. }
  339. while (*s) {
  340. s++;
  341. }
  342. s--;
  343. while ((s >= t) && isspace(*s)) {
  344. *s = 0;
  345. s--;
  346. }
  347. }
  348. /*
  349. * Like trim, but trims whitespace from both beginning and end.
  350. */
  351. char*
  352. lrtrim(char* buff)
  353. {
  354. char* c;
  355. if (buff[0] == '\0') {
  356. return buff;
  357. }
  358. c = buff + strlen(buff);
  359. while ((c >= buff) && ((unsigned char)*c <= ' ')) {
  360. *c-- = '\0';
  361. }
  362. c = buff;
  363. while ((*c != '\0') && ((unsigned char)*c <= ' ')) {
  364. c++;
  365. }
  366. if (c != buff) {
  367. char* src = c;
  368. char* dst = buff;
  369. while (*src) {
  370. *dst++ = *src++;
  371. }
  372. *dst = '\0';
  373. }
  374. return buff;
  375. }
  376. /*
  377. * compare str with match
  378. * match may contain wildcards "*" and "?"
  379. *
  380. * examples:
  381. * str_match("ABCDE", "*BC*") -> 1
  382. * str_match("ABCDE", "A*C*E") -> 1
  383. * str_match("?ABCDE", "\\?A*") -> 1
  384. * str_match("", "*A") -> 0
  385. */
  386. int
  387. str_match(const char* str, const char* match)
  388. {
  389. char* m, *s;
  390. s = (char*)str;
  391. m = (char*)match;
  392. while (*m || *s) {
  393. switch (*m) {
  394. case '\0':
  395. /* there is something left in s, FAIL */
  396. return 0;
  397. case '*':
  398. /* skip all wildcards */
  399. while ((*m == '*') || (*m == '?')) {
  400. m++;
  401. }
  402. if (*m == '\0') {
  403. return 1;
  404. }
  405. if (*m == '\\') { /* ? escaped ? */
  406. m++;
  407. if (*m == '\0') {
  408. return 0;
  409. }
  410. }
  411. do {
  412. char* mx, *sx;
  413. while (*s && (*s != *m)) {
  414. s++;
  415. }
  416. if (*s == '\0') {
  417. return 0;
  418. }
  419. sx = s + 1;
  420. mx = m + 1;
  421. while (*sx) {
  422. if (*mx == '\\') { /* ? escaped ? */
  423. mx++;
  424. if (*mx == '\0') {
  425. return 0;
  426. }
  427. }
  428. if (*sx == *mx) {
  429. sx++;
  430. mx++;
  431. } else {
  432. break;
  433. }
  434. }
  435. if (*mx == '\0') { /* end of match */
  436. if (*sx == '\0') {
  437. return 1;
  438. }
  439. s++;
  440. } else if ((*mx == '?') || (*mx == '*')) {
  441. s = sx;
  442. m = mx;
  443. break;
  444. } else {
  445. s++;
  446. }
  447. } while (*s);
  448. break;
  449. case '?':
  450. if (*s == '\0') {
  451. return 0; /* no character left */
  452. }
  453. m++;
  454. s++;
  455. break;
  456. case '\\':
  457. m++;
  458. if (*m == '\0') {
  459. return 0; /* incomplete escape sequence */
  460. }
  461. /* pass-through next character */
  462. default:
  463. if (*m != *s) {
  464. return 0;
  465. }
  466. m++;
  467. s++;
  468. }
  469. }
  470. return ((*s == '\0') && (*m == '\0'));
  471. }
  472. /*
  473. * as str_match, but case insensitive
  474. */
  475. int
  476. case_ignore_str_match(const char* str, const char* match)
  477. {
  478. char* s1, *s2;
  479. int res;
  480. s1 = strupper(xstrdup(str));
  481. s2 = strupper(xstrdup(match));
  482. res = str_match(s1, s2);
  483. xfree(s1);
  484. xfree(s2);
  485. return res;
  486. }
  487. // for ruote_char = "
  488. // make str = blank into nothing
  489. // make str = foo into "foo"
  490. // make str = foo"bar into "foo""bar"
  491. // No, that doesn't seem obvious to me, either...
  492. QString
  493. strenquote(const QString& str, const QChar quot_char)
  494. {
  495. QString replacement = QString("%1%1").arg(quot_char);
  496. QString t = str;
  497. t.replace(quot_char, replacement);
  498. QString r = quot_char + t + quot_char;
  499. return r;
  500. }
  501. void
  502. printposn(const double c, int is_lat)
  503. {
  504. char d;
  505. if (is_lat) {
  506. if (c < 0) {
  507. d = 'S';
  508. } else {
  509. d = 'N';
  510. }
  511. } else {
  512. if (c < 0) {
  513. d = 'W';
  514. } else {
  515. d = 'E';
  516. }
  517. }
  518. printf("%f%c ", fabs(c), d);
  519. }
  520. void
  521. is_fatal(const int condition, const char* fmt, ...)
  522. {
  523. va_list args;
  524. char buff[128];
  525. if (condition == 0) {
  526. return;
  527. }
  528. va_start(args, fmt);
  529. vsnprintf(buff, sizeof(buff), fmt, args);
  530. va_end(args);
  531. fatal("%s\n", buff);
  532. }
  533. /*
  534. * Read 4 bytes in big-endian. Return as "int" in native endianness.
  535. */
  536. signed int
  537. be_read32(const void* p)
  538. {
  539. unsigned char* i = (unsigned char*) p;
  540. return i[0] << 24 | i[1] << 16 | i[2] << 8 | i[3];
  541. }
  542. signed int
  543. be_read16(const void* p)
  544. {
  545. unsigned char* i = (unsigned char*) p;
  546. return i[0] << 8 | i[1];
  547. }
  548. unsigned int
  549. be_readu16(const void* p)
  550. {
  551. const unsigned char* i = (unsigned char*) p;
  552. return i[0] << 8 | i[1];
  553. }
  554. void
  555. be_write16(void* addr, const unsigned value)
  556. {
  557. unsigned char* p = (unsigned char*) addr;
  558. p[0] = value >> 8;
  559. p[1] = value;
  560. }
  561. void
  562. be_write32(void* pp, const unsigned i)
  563. {
  564. char* p = (char*)pp;
  565. p[0] = (i >> 24) & 0xff;
  566. p[1] = (i >> 16) & 0xff;
  567. p[2] = (i >> 8) & 0xff;
  568. p[3] = i & 0xff;
  569. }
  570. signed int
  571. le_read16(const void* addr)
  572. {
  573. const unsigned char* p = (const unsigned char*) addr;
  574. return p[0] | (p[1] << 8);
  575. }
  576. unsigned int
  577. le_readu16(const void* addr)
  578. {
  579. const unsigned char* p = (const unsigned char*) addr;
  580. return p[0] | (p[1] << 8);
  581. }
  582. signed int
  583. le_read32(const void* addr)
  584. {
  585. const unsigned char* p = (const unsigned char*) addr;
  586. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  587. }
  588. unsigned int
  589. le_readu32(const void* addr)
  590. {
  591. const unsigned char* p = (const unsigned char*) addr;
  592. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  593. }
  594. /*
  595. * Read a little-endian 64-bit value from 'src' and return it in 'dest'
  596. * in host endianness.
  597. */
  598. void
  599. le_read64(void* dest, const void* src)
  600. {
  601. char* cdest = (char*) dest;
  602. const char* csrc = (const char*) src;
  603. if (i_am_little_endian) {
  604. memcpy(dest, src, 8);
  605. } else {
  606. int i;
  607. for (i = 0; i < 8; i++) {
  608. cdest[i] = csrc[7-i];
  609. }
  610. }
  611. }
  612. void
  613. le_write16(void* addr, const unsigned value)
  614. {
  615. unsigned char* p = (unsigned char*) addr;
  616. p[0] = value;
  617. p[1] = value >> 8;
  618. }
  619. void
  620. le_write32(void* addr, const unsigned value)
  621. {
  622. unsigned char* p = (unsigned char*) addr;
  623. p[0] = value;
  624. p[1] = value >> 8;
  625. p[2] = value >> 16;
  626. p[3] = value >> 24;
  627. }
  628. signed int
  629. si_round(double d)
  630. {
  631. if (d < 0) {
  632. return (signed int)(d-0.5);
  633. } else {
  634. return (signed int)(d+0.5);
  635. }
  636. }
  637. /*
  638. mkgmtime -- convert tm struct in UTC to time_t
  639. works just like mktime but without all the mucking
  640. around with timezones and daylight savings
  641. obsoletes get_tz_offset()
  642. Borrowed from lynx GPL source code
  643. http://lynx.isc.org/release/lynx2-8-5/src/mktime.c
  644. Written by Philippe De Muyter <phdm@macqel.be>.
  645. */
  646. time_t
  647. mkgmtime(struct tm* t)
  648. {
  649. short month, year;
  650. time_t result;
  651. static int m_to_d[12] =
  652. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  653. month = t->tm_mon;
  654. year = t->tm_year + month / 12 + 1900;
  655. month %= 12;
  656. if (month < 0) {
  657. year -= 1;
  658. month += 12;
  659. }
  660. result = (year - 1970) * 365 + m_to_d[month];
  661. if (month <= 1) {
  662. year -= 1;
  663. }
  664. result += (year - 1968) / 4;
  665. result -= (year - 1900) / 100;
  666. result += (year - 1600) / 400;
  667. result += t->tm_mday;
  668. result -= 1;
  669. result *= 24;
  670. result += t->tm_hour;
  671. result *= 60;
  672. result += t->tm_min;
  673. result *= 60;
  674. result += t->tm_sec;
  675. return (result);
  676. }
  677. /*
  678. * mklocaltime: same as mktime, but try to recover the "Summer time flag",
  679. * which is evaluated by mktime
  680. */
  681. time_t
  682. mklocaltime(struct tm* t)
  683. {
  684. time_t result;
  685. struct tm check = *t;
  686. check.tm_isdst = 0;
  687. result = mktime(&check);
  688. check = *localtime(&result);
  689. if (check.tm_isdst == 1) { /* DST is in effect */
  690. check = *t;
  691. check.tm_isdst = 1;
  692. result = mktime(&check);
  693. }
  694. return result;
  695. }
  696. /*
  697. * Historically, when we were C, this was A wrapper for time(2) that
  698. * allowed us to "freeze" time for testing. The UNIX epoch
  699. * (1970-1-1-00:00:00UTC) was a convenient value for that. Now in the
  700. * world of Qt, sub-second time is convenient, but regenerating all the
  701. * reference files would be tedious, so we uphold that convention.
  702. */
  703. gpsbabel::DateTime
  704. current_time(void)
  705. {
  706. if (getenv("GPSBABEL_FREEZE_TIME")) {
  707. return QDateTime::fromTime_t(0);
  708. }
  709. return QDateTime::currentDateTime();
  710. }
  711. /*
  712. * Return the (zero based) month number of the year or -1 for failure.
  713. */
  714. signed int
  715. month_lookup(const char* m)
  716. {
  717. static const char* months[] = {
  718. "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
  719. "JUL", "AUG", "SEP", "OCT", "NOV", "DEC", NULL
  720. };
  721. const char** mp;
  722. for (mp = months; *mp; mp++) {
  723. if (0 == case_ignore_strcmp(*mp, m)) {
  724. return mp - months;
  725. }
  726. }
  727. return -1;
  728. }
  729. /*
  730. * Microsoft dot net's time format is the number of 100 nanosecond intervals
  731. * since midnight Jan 1, 0001. We have time_t deeply ingrained into our
  732. * internals and since we're in the GPS biz, timestamps before 1/1/1970 aren't
  733. * that interesting to us anyway.
  734. */
  735. #define EPOCH_TICKS 621355968000000000.0
  736. void dotnet_time_to_time_t(double dotnet, time_t* t, int* millisecs)
  737. {
  738. // TODO: replace this with better interface with normal return values
  739. // and called via a QDateTime.
  740. *t = (dotnet - EPOCH_TICKS) / 10000000.;
  741. #if LATER
  742. // TODO: work out fractional seconds.
  743. if (millisecs) {
  744. *millisecs = dotnet % 10000;
  745. }
  746. #else
  747. (void)millisecs;
  748. #endif
  749. }
  750. /*
  751. * Return a pointer to a constant string that is suitable for icon lookup
  752. * based on geocache attributes. The strings used are those present in
  753. * a GPX file from geocaching.com. Thus we sort of make all the other
  754. * formats do lookups based on these strings.
  755. */
  756. const char*
  757. get_cache_icon(const Waypoint* waypointp)
  758. {
  759. if (!global_opts.smart_icons) {
  760. return NULL;
  761. }
  762. /*
  763. * For icons, type overwrites container. So a multi-micro will
  764. * get the icons for "multi".
  765. */
  766. switch (waypointp->gc_data->type) {
  767. case gt_virtual:
  768. return "Virtual cache";
  769. case gt_multi:
  770. return "Multi-Cache";
  771. case gt_event:
  772. return "Event Cache";
  773. case gt_suprise:
  774. return "Unknown Cache";
  775. case gt_webcam:
  776. return "Webcam Cache";
  777. default:
  778. break;
  779. }
  780. switch (waypointp->gc_data->container) {
  781. case gc_micro:
  782. return "Micro-Cache";
  783. break;
  784. default:
  785. break;
  786. }
  787. if (waypointp->gc_data->diff > 1) {
  788. return "Geocache";
  789. }
  790. return NULL;
  791. }
  792. double
  793. endian_read_double(const void* ptr, int read_le)
  794. {
  795. double ret;
  796. char r[8];
  797. const void* p;
  798. int i;
  799. if (i_am_little_endian == read_le) {
  800. p = ptr;
  801. } else {
  802. for (i = 0; i < 8; i++) {
  803. r[i] = ((char*)ptr)[7-i];
  804. }
  805. p = r;
  806. }
  807. // Word order is different on arm, but not on arm-eabi.
  808. #if defined(__arm__) && !defined(__ARM_EABI__)
  809. memcpy(&ret, p + 4, 4);
  810. memcpy(((void*)&ret) + 4, p, 4);
  811. #else
  812. memcpy(&ret, p, 8);
  813. #endif
  814. return ret;
  815. }
  816. float
  817. endian_read_float(const void* ptr, int read_le)
  818. {
  819. float ret;
  820. char r[4];
  821. const void* p;
  822. int i;
  823. if (i_am_little_endian == read_le) {
  824. p = ptr;
  825. } else {
  826. for (i = 0; i < 4; i++) {
  827. r[i] = ((char*)ptr)[3-i];
  828. }
  829. p = r;
  830. }
  831. memcpy(&ret, p, 4);
  832. return ret;
  833. }
  834. void
  835. endian_write_double(void* ptr, double d, int write_le)
  836. {
  837. int i;
  838. char* optr = (char*) ptr;
  839. // Word order is different on arm, but not on arm-eabi.
  840. #if defined(__arm__) && !defined(__ARM_EABI__)
  841. char r[8];
  842. memcpy(r + 4, &d, 4);
  843. memcpy(r, ((void*)&d) + 4, 4);
  844. #else
  845. char* r = (char*)(void*)&d;
  846. #endif
  847. if (i_am_little_endian == write_le) {
  848. memcpy(ptr, r, 8);
  849. } else {
  850. for (i = 0; i < 8; i++) {
  851. *optr++ = r[7-i];
  852. }
  853. }
  854. }
  855. void
  856. endian_write_float(void* ptr, float f, int write_le)
  857. {
  858. char* r = (char*)(void*)&f;
  859. int i;
  860. char* optr = (char*) ptr;
  861. if (i_am_little_endian == write_le) {
  862. memcpy(ptr, &f, 4);
  863. } else {
  864. for (i = 0; i < 4; i++) {
  865. *optr++ = r[3-i];
  866. }
  867. }
  868. }
  869. float
  870. le_read_float(const void* ptr)
  871. {
  872. return endian_read_float(ptr, 1);
  873. }
  874. void
  875. le_write_float(void* ptr, float f)
  876. {
  877. endian_write_float(ptr,f,1);
  878. }
  879. float
  880. be_read_float(void* ptr)
  881. {
  882. return endian_read_float(ptr, 0);
  883. }
  884. void
  885. be_write_float(void* ptr, float f)
  886. {
  887. endian_write_float(ptr,f,0);
  888. }
  889. double
  890. le_read_double(const void* ptr)
  891. {
  892. return endian_read_double(ptr,1);
  893. }
  894. void
  895. le_write_double(void* ptr, double d)
  896. {
  897. endian_write_double(ptr,d,1);
  898. }
  899. double
  900. be_read_double(void* ptr)
  901. {
  902. return endian_read_double(ptr,0);
  903. }
  904. void
  905. be_write_double(void* ptr, double d)
  906. {
  907. endian_write_double(ptr,d,0);
  908. }
  909. /* Magellan and PCX formats use this DDMM.mm format */
  910. double ddmm2degrees(double pcx_val)
  911. {
  912. double minutes;
  913. signed int deg;
  914. deg = (signed int)(pcx_val / 100.0);
  915. minutes = (((pcx_val / 100.0) - deg) * 100.0) / 60.0;
  916. return (double) deg + minutes;
  917. }
  918. double degrees2ddmm(double deg_val)
  919. {
  920. signed int deg;
  921. deg = (signed int) deg_val;
  922. return (double)(deg * 100.0) + ((deg_val - deg) * 60.0);
  923. }
  924. /*
  925. * replace a single occurrence of "search" in "s" with "replace".
  926. * Returns an allocated copy if substitution was made, otherwise returns NULL.
  927. * Doesn't try to make an optimally sized dest buffer.
  928. */
  929. char*
  930. strsub(const char* s, const char* search, const char* replace)
  931. {
  932. const char* p;
  933. int len = strlen(s);
  934. int slen = strlen(search);
  935. int rlen = strlen(replace);
  936. char* d;
  937. p = strstr(s, search);
  938. if (!slen || !p) {
  939. return NULL;
  940. }
  941. d = (char*) xmalloc(len + rlen + 1);
  942. /* Copy first part */
  943. len = p - s;
  944. memcpy(d, s, len);
  945. d[len] = 0;
  946. /* Copy replacement */
  947. strcat(d, replace);
  948. /* Copy last part */
  949. strcat(d, p + slen);
  950. return d;
  951. }
  952. /*
  953. * As strsub, but do it globally.
  954. */
  955. char*
  956. gstrsub(const char* s, const char* search, const char* replace)
  957. {
  958. int ooffs = 0;
  959. char* o, *c;
  960. char* src = (char*)s;
  961. int olen = strlen(src);
  962. int slen = strlen(search);
  963. int rlen = strlen(replace);
  964. o = (char*) xmalloc(olen + 1);
  965. while ((c = strstr(src, search))) {
  966. olen += (rlen - slen);
  967. o = (char*) xrealloc(o, olen + 1);
  968. memcpy(o + ooffs, src, c - src);
  969. ooffs += (c - src);
  970. src = c + slen;
  971. if (rlen) {
  972. memcpy(o + ooffs, replace, rlen);
  973. ooffs += rlen;
  974. }
  975. }
  976. if (ooffs < olen) {
  977. memcpy(o + ooffs, src, olen - ooffs);
  978. }
  979. o[olen] = '\0';
  980. return o;
  981. }
  982. /*
  983. *
  984. */
  985. char*
  986. strupper(char* src)
  987. {
  988. char* c;
  989. for (c = src; *c; c++) {
  990. *c = toupper(*c);
  991. }
  992. return src;
  993. }
  994. /*
  995. *
  996. */
  997. char*
  998. strlower(char* src)
  999. {
  1000. char* c;
  1001. for (c = src; *c; c++) {
  1002. *c = tolower(*c);
  1003. }
  1004. return src;
  1005. }
  1006. QString
  1007. rot13(const QString& s)
  1008. {
  1009. static const QChar A('A');
  1010. static const QChar M('M');
  1011. static const QChar N('N');
  1012. static const QChar Z('Z');
  1013. QString r = s;
  1014. int i = r.length();
  1015. while (i--) {
  1016. QChar letter = r[i].toUpper();
  1017. if (letter >= A && letter <= M) {
  1018. r[i] = QChar(r[i].toLatin1() + 13);
  1019. } else if (letter >= N && letter <= Z) {
  1020. r[i] = QChar(r[i].toLatin1() - 13);
  1021. }
  1022. }
  1023. return r;
  1024. }
  1025. /*
  1026. * Convert a human readable date format (i.e. "YYYY/MM/DD") into
  1027. * a format usable for strftime and others
  1028. */
  1029. char*
  1030. convert_human_date_format(const char* human_datef)
  1031. {
  1032. char* result, *cin, *cout;
  1033. char prev;
  1034. int ylen;
  1035. result = (char*) xcalloc((2*strlen(human_datef)) + 1, 1);
  1036. cout = result;
  1037. prev = '\0';
  1038. ylen = 0;
  1039. for (cin = (char*)human_datef; *cin; cin++) {
  1040. char okay = 1;
  1041. if (toupper(*cin) != 'Y') {
  1042. ylen = 0;
  1043. }
  1044. if (isalpha(*cin)) {
  1045. switch (*cin) {
  1046. case 'y':
  1047. case 'Y':
  1048. if (prev != 'Y') {
  1049. strcat(cout, "%y");
  1050. cout += 2;
  1051. prev = 'Y';
  1052. }
  1053. ylen++;
  1054. if (ylen > 2) {
  1055. *(cout-1) = 'Y';
  1056. }
  1057. break;
  1058. case 'm':
  1059. case 'M':
  1060. if (prev != 'M') {
  1061. strcat(cout, "%m");
  1062. cout += 2;
  1063. prev = 'M';
  1064. }
  1065. break;
  1066. case 'd':
  1067. case 'D':
  1068. if (prev != 'D') {
  1069. strcat(cout, "%d");
  1070. cout += 2;
  1071. prev = 'D';
  1072. }
  1073. break;
  1074. default:
  1075. okay = 0;
  1076. }
  1077. } else if (ispunct(*cin)) {
  1078. *cout++ = *cin;
  1079. prev = '\0';
  1080. } else {
  1081. okay = 0;
  1082. }
  1083. is_fatal(okay == 0, "Invalid character \"%c\" in date format!", *cin);
  1084. }
  1085. return result;
  1086. }
  1087. /*
  1088. * Convert a human readable time format (i.e. "HH:mm:ss") into
  1089. * a format usable for strftime and others
  1090. */
  1091. char*
  1092. convert_human_time_format(const char* human_timef)
  1093. {
  1094. char* result, *cin, *cout;
  1095. char prev;
  1096. result = (char*) xcalloc((2*strlen(human_timef)) + 1, 1);
  1097. cout = result;
  1098. prev = '\0';
  1099. for (cin = (char*)human_timef; *cin; cin++) {
  1100. int okay = 1;
  1101. if (isalpha(*cin)) {
  1102. switch (*cin) {
  1103. case 'S':
  1104. case 's':
  1105. if (prev != 'S') {
  1106. strcat(cout, "%S");
  1107. cout += 2;
  1108. prev = 'S';
  1109. }
  1110. break;
  1111. case 'M':
  1112. case 'm':
  1113. if (prev != 'M') {
  1114. strcat(cout, "%M");
  1115. cout += 2;
  1116. prev = 'M';
  1117. }
  1118. break;
  1119. case 'h': /* 12-hour-clock */
  1120. if (prev != 'H') {
  1121. strcat(cout, "%l"); /* 1 .. 12 */
  1122. cout += 2;
  1123. prev = 'H';
  1124. } else {
  1125. *(cout-1) = 'I'; /* 01 .. 12 */
  1126. }
  1127. break;
  1128. case 'H': /* 24-hour-clock */
  1129. if (prev != 'H') {
  1130. strcat(cout, "%k");
  1131. cout += 2;
  1132. prev = 'H';
  1133. } else {
  1134. *(cout-1) = 'H';
  1135. }
  1136. break;
  1137. case 'x':
  1138. if (prev != 'X') {
  1139. strcat(cout, "%P");
  1140. cout += 2;
  1141. prev = 'X';
  1142. } else {
  1143. *(cout-1) = 'P';
  1144. }
  1145. break;
  1146. case 'X':
  1147. if (prev != 'X') {
  1148. strcat(cout, "%p");
  1149. cout += 2;
  1150. prev = 'X';
  1151. } else {
  1152. *(cout-1) = 'p';
  1153. }
  1154. break;
  1155. default:
  1156. okay = 0;
  1157. }
  1158. } else if (ispunct(*cin) || isspace(*cin)) {
  1159. *cout++ = *cin;
  1160. prev = '\0';
  1161. } else {
  1162. okay = 0;
  1163. }
  1164. is_fatal(okay == 0, "Invalid character \"%c\" in time format!", *cin);
  1165. }
  1166. return result;
  1167. }
  1168. /*
  1169. * Return a decimal degree pair as
  1170. * DD.DDDDD DD MM.MMM or DD MM SS.S
  1171. * fmt = ['d', 'm', 's']
  1172. * sep = string between lat and lon (separator)
  1173. * html = 1 for html output otherwise text
  1174. */
  1175. char*
  1176. pretty_deg_format(double lat, double lon, char fmt, const char* sep, int html)
  1177. {
  1178. double latmin, lonmin, latsec, lonsec;
  1179. int latint, lonint;
  1180. char latsig, lonsig;
  1181. char* result;
  1182. latsig = lat < 0 ? 'S':'N';
  1183. lonsig = lon < 0 ? 'W':'E';
  1184. latint = abs((int) lat);
  1185. lonint = abs((int) lon);
  1186. latmin = 60.0 * (fabs(lat) - latint);
  1187. lonmin = 60.0 * (fabs(lon) - lonint);
  1188. latsec = 60.0 * (latmin - floor(latmin));
  1189. lonsec = 60.0 * (lonmin - floor(lonmin));
  1190. if (sep == NULL) {
  1191. sep = " "; /* default " " */
  1192. }
  1193. if (fmt == 'd') { /* ddd */
  1194. xasprintf(&result, "%c%6.5f%s%s%c%6.5f%s",
  1195. latsig, fabs(lat), html?"&deg;":"", sep,
  1196. lonsig, fabs(lon), html?"&deg;":"");
  1197. } else if (fmt == 's') { /* dms */
  1198. xasprintf(&result, "%c%d%s%02d'%04.1f\"%s%c%d%s%02d'%04.1f\"",
  1199. latsig, latint, html?"&deg;":" ", (int)latmin, latsec, sep,
  1200. lonsig, lonint, html?"&deg;":" ", (int)lonmin, lonsec);
  1201. } else { /* default dmm */
  1202. xasprintf(&result, "%c%d%s%06.3f%s%c%d%s%06.3f",
  1203. latsig, latint, html?"&deg;":" ", latmin, sep,
  1204. lonsig, lonint, html?"&deg;":" ", lonmin);
  1205. }
  1206. return result;
  1207. }
  1208. /*
  1209. * Get rid of potentially nasty HTML that would influence another record
  1210. * that includes;
  1211. * <body> - to stop backgrounds/background colours from being loaded
  1212. * </body> and </html>- stop processing altogether
  1213. * <style> </style> - stop overriding styles for everything
  1214. */
  1215. char*
  1216. strip_nastyhtml(const QString& in)
  1217. {
  1218. char* returnstr, *sp;
  1219. char* lcstr, *lcp;
  1220. sp = returnstr = xstrdup(in);
  1221. lcp = lcstr = strlower(xstrdup(in));
  1222. while (lcp = strstr(lcstr, "<body>"), NULL != lcp) {
  1223. sp = returnstr + (lcp - lcstr) ; /* becomes <! > */
  1224. sp++;
  1225. *sp++ = '!';
  1226. *sp++ = ' ';
  1227. *sp++ = ' ';
  1228. *sp++ = ' ';
  1229. *lcp = '*'; /* so we wont find it again */
  1230. }
  1231. while (lcp = strstr(lcstr, "<body"), lcp != NULL) { /* becomes <!-- --> */
  1232. sp = returnstr + (lcp - lcstr) ;
  1233. sp++;
  1234. *sp++ = '!';
  1235. *sp++ = '-';
  1236. *sp++ = '-';
  1237. while ((*sp) && (*sp != '>')) {
  1238. sp++;
  1239. }
  1240. *--sp = '-';
  1241. *--sp = '-';
  1242. *lcp = '*'; /* so we wont find it again */
  1243. }
  1244. while (lcp = strstr(lcstr, "</body>"), NULL != lcp) {
  1245. sp = returnstr + (lcp - lcstr) ; /* becomes <!---- */
  1246. sp++;
  1247. *sp++ = '!';
  1248. *sp++ = '-';
  1249. *sp++ = '-';
  1250. *sp++ = '-';
  1251. *sp++ = '-';
  1252. *lcp = '*'; /* so we wont find it again */
  1253. }
  1254. while (lcp = strstr(lcstr, "</html>"), NULL != lcp) {
  1255. sp = returnstr + (lcp - lcstr) ; /* becomes </---- */
  1256. sp++;
  1257. *sp++ = '!';
  1258. *sp++ = '-';
  1259. *sp++ = '-';
  1260. *sp++ = '-';
  1261. *sp++ = '-';
  1262. *lcp = '*'; /* so we wont find it again */
  1263. }
  1264. while (lcp = strstr(lcstr, "<style"), NULL != lcp) {
  1265. sp = returnstr + (lcp - lcstr) ; /* becomes <!-- */
  1266. sp++;
  1267. *sp++ = '!';
  1268. *sp++ = '-';
  1269. *sp++ = '-';
  1270. *sp++ = ' ';
  1271. *sp++ = ' ';
  1272. *sp = ' ';
  1273. *lcp = '*'; /* so we wont find it again */
  1274. }
  1275. while (lcp = strstr(lcstr, "</style>"), NULL != lcp) {
  1276. sp = returnstr + (lcp - lcstr) ; /* becomes --> */
  1277. *sp++ = ' ';
  1278. *sp++ = ' ';
  1279. *sp++ = ' ';
  1280. *sp++ = ' ';
  1281. *sp++ = ' ';
  1282. *sp++ = '-';
  1283. *sp++ = '-';
  1284. *lcp = '*'; /* so we wont find it again */
  1285. }
  1286. while (lcp = strstr(lcstr, "<image"), NULL != lcp) {
  1287. sp = returnstr + (lcp - lcstr) ; /* becomes <img */
  1288. sp+=3;
  1289. *sp++ = 'g';
  1290. *sp++ = ' ';
  1291. *sp++ = ' ';
  1292. *lcp = '*';
  1293. }
  1294. xfree(lcstr);
  1295. return (returnstr);
  1296. }
  1297. /*
  1298. * Without getting into all the complexity of technically legal HTML,
  1299. * this function tries to strip "ugly" parts of it to make it more
  1300. * pleasant for a human reader. Yes, this falls down in all kinds of
  1301. * ways such as spaces within the tags, etc.
  1302. */
  1303. char*
  1304. strip_html(const utf_string* in)
  1305. {
  1306. #if 0
  1307. // If we were willing to link core against QtGui (not out of the question)
  1308. // we could just do...and either decide whether to add handling for [IMG]
  1309. // or just say we don't do that any more.
  1310. QTextDocument doc;
  1311. doc.setHtml(in->utfstring);
  1312. return xstrdup(CSTR(doc.toPlainText().simplified()));
  1313. #else
  1314. char* outstring, *out;
  1315. char* incopy, *instr;
  1316. char tag[8];
  1317. unsigned short int taglen = 0;
  1318. incopy = instr = xstrdup(in->utfstring);
  1319. if (!in->is_html) {
  1320. return instr;
  1321. }
  1322. /*
  1323. * We only shorten, so just dupe the input buf for space.
  1324. */
  1325. outstring = out = xstrdup(in->utfstring);
  1326. tag[0] = 0;
  1327. while (*instr) {
  1328. if ((*instr == '<') || (*instr == '&')) {
  1329. tag[0] = *instr;
  1330. taglen = 0;
  1331. }
  1332. if (! tag[0]) {
  1333. if (*instr == '\n') {
  1334. *out++ = ' ';
  1335. do {
  1336. instr++;
  1337. } while (isspace(*instr));
  1338. continue;
  1339. } else {
  1340. *out++ = *instr;
  1341. }
  1342. } else {
  1343. if (taglen < (sizeof(tag)-1)) {
  1344. tag[taglen++] = tolower(*instr);
  1345. tag[taglen] = 0;
  1346. }
  1347. }
  1348. if (((tag[0] == '<') && (*instr == '>')) ||
  1349. ((tag[0] == '&') && (*instr == ';'))) {
  1350. if (! strcmp(tag,"&amp;")) {
  1351. *out++ = '&';
  1352. } else if (! strcmp(tag, "&lt;")) {
  1353. *out++ = '<';
  1354. } else if (! strcmp(tag, "&gt;")) {
  1355. *out++ = '>';
  1356. } else if (! strcmp(tag, "&quot;")) {
  1357. *out++ = '"';
  1358. } else if (! strcmp(tag, "&nbsp;")) {
  1359. *out++ = ' ';
  1360. } else if (! strcmp(tag, "&deg;")) {
  1361. *out++ = 'd';
  1362. *out++ = 'e';
  1363. *out++ = 'g';
  1364. } else if ((tag[0]=='<') && (tag[1]=='p')) {
  1365. *out++ = '\n';
  1366. } else if ((tag[0]=='<') && (tag[1]=='b') && (tag[2]=='r')) {
  1367. *out++ = '\n';
  1368. } else if ((tag[0]=='<') && (tag[1]=='/') && (tag[2]=='t') && (tag[3]=='r')) {
  1369. *out++ = '\n';
  1370. } else if ((tag[0]=='<') && (tag[1]=='/') && (tag[2]=='t') && (tag[3]=='d')) {
  1371. *out++ = ' ';
  1372. } else if ((tag[0]=='<') && (tag[1]=='i') && (tag[2]=='m') && (tag[3]=='g')) {
  1373. *out++ = '[';
  1374. *out++ = 'I';
  1375. *out++ = 'M';
  1376. *out++ = 'G';
  1377. *out++ = ']';
  1378. }
  1379. tag[0] = 0;
  1380. }
  1381. instr++;
  1382. }
  1383. *out++ = 0;
  1384. if (incopy) {
  1385. xfree(incopy);
  1386. }
  1387. return (outstring);
  1388. #endif
  1389. }
  1390. typedef struct {
  1391. const char* text;
  1392. const char* entity;
  1393. int not_html;
  1394. } entity_types;
  1395. static
  1396. entity_types stdentities[] = {
  1397. { "&", "&amp;", 0 },
  1398. { "'", "&apos;", 1 },
  1399. { "<", "&lt;", 0 },
  1400. { ">", "&gt;", 0 },
  1401. { "\"", "&quot;", 0 },
  1402. { "\x01", " ", 1 }, // illegal xml 1.0 character
  1403. { "\x02", " ", 1 }, // illegal xml 1.0 character
  1404. { "\x03", " ", 1 }, // illegal xml 1.0 character
  1405. { "\x04", " ", 1 }, // illegal xml 1.0 character
  1406. { "\x05", " ", 1 }, // illegal xml 1.0 character
  1407. { "\x06", " ", 1 }, // illegal xml 1.0 character
  1408. { "\x07", " ", 1 }, // illegal xml 1.0 character
  1409. { "\x08", " ", 1 }, // illegal xml 1.0 character
  1410. // { "\x09", " ", 1 }, legal xml 1.0 character
  1411. // { "\x0a", " ", 1 }, legal xml 1.0 character
  1412. { "\x0b", " ", 1 }, // illegal xml 1.0 character
  1413. { "\x0c", " ", 1 }, // illegal xml 1.0 character
  1414. // { "\x0d", " ", 1 }, legal xml 1.0 character
  1415. { "\x0e", " ", 1 }, // illegal xml 1.0 character
  1416. { "\x0f", " ", 1 }, // illegal xml 1.0 character
  1417. { "\x10", " ", 1 }, // illegal xml 1.0 character
  1418. { "\x11", " ", 1 }, // illegal xml 1.0 character
  1419. { "\x12", " ", 1 }, // illegal xml 1.0 character
  1420. { "\x13", " ", 1 }, // illegal xml 1.0 character
  1421. { "\x14", " ", 1 }, // illegal xml 1.0 character
  1422. { "\x15", " ", 1 }, // illegal xml 1.0 character
  1423. { "\x16", " ", 1 }, // illegal xml 1.0 character
  1424. { "\x17", " ", 1 }, // illegal xml 1.0 character
  1425. { "\x18", " ", 1 }, // illegal xml 1.0 character
  1426. { "\x19", " ", 1 }, // illegal xml 1.0 character
  1427. { "\x1a", " ", 1 }, // illegal xml 1.0 character
  1428. { "\x1b", " ", 1 }, // illegal xml 1.0 character
  1429. { "\x1c", " ", 1 }, // illegal xml 1.0 character
  1430. { "\x1d", " ", 1 }, //illegal xml 1.0 character
  1431. { "\x1e", " ", 1 }, //illegal xml 1.0 character
  1432. { "\x1f", " ", 1 }, //illegal xml 1.0 character
  1433. { NULL, NULL, 0 }
  1434. };
  1435. static
  1436. char*
  1437. entitize(const char* str, int is_html)
  1438. {
  1439. int elen, ecount, nsecount;
  1440. entity_types* ep;
  1441. const char* cp;
  1442. char* p, * tmp, * xstr;
  1443. int bytes = 0;
  1444. int value = 0;
  1445. ep = stdentities;
  1446. elen = ecount = nsecount = 0;
  1447. /* figure # of entity replacements and additional size. */
  1448. while (ep->text) {
  1449. cp = str;
  1450. while ((cp = strstr(cp, ep->text)) != NULL) {
  1451. elen += strlen(ep->entity) - strlen(ep->text);
  1452. ecount++;
  1453. cp += strlen(ep->text);
  1454. }
  1455. ep++;
  1456. }
  1457. /* figure the same for other than standard entities (i.e. anything
  1458. * that isn't in the range U+0000 to U+007F */
  1459. #if 0
  1460. for (cp = str; *cp; cp++) {
  1461. if (*cp & 0x80) {
  1462. cet_utf8_to_ucs4(cp, &bytes, &value);
  1463. cp += bytes-1;
  1464. elen += sprintf(tmpsub, "&#x%x;", value) - bytes;
  1465. nsecount++;
  1466. }
  1467. }
  1468. #endif
  1469. /* enough space for the whole string plus entity replacements, if any */
  1470. tmp = (char*) xcalloc((strlen(str) + elen + 1), 1);
  1471. strcpy(tmp, str);
  1472. /* no entity replacements */
  1473. if (ecount == 0 && nsecount == 0) {
  1474. return (tmp);
  1475. }
  1476. if (ecount != 0) {
  1477. for (ep = stdentities; ep->text; ep++) {
  1478. p = tmp;
  1479. if (is_html && ep->not_html) {
  1480. continue;
  1481. }
  1482. while ((p = strstr(p, ep->text)) != NULL) {
  1483. elen = strlen(ep->entity);
  1484. xstr = xstrdup(p + strlen(ep->text));
  1485. strcpy(p, ep->entity);
  1486. strcpy(p + elen, xstr);
  1487. xfree(xstr);
  1488. p += elen;
  1489. }
  1490. }
  1491. }
  1492. if (nsecount != 0) {
  1493. p = tmp;
  1494. while (*p) {
  1495. if (*p & 0x80) {
  1496. cet_utf8_to_ucs4(p, &bytes, &value);
  1497. if (p[bytes]) {
  1498. xstr = xstrdup(p + bytes);
  1499. } else {
  1500. xstr = NULL;
  1501. }
  1502. sprintf(p, "&#x%x;", value);
  1503. p = p+strlen(p);
  1504. if (xstr) {
  1505. strcpy(p, xstr);
  1506. xfree(xstr);
  1507. }
  1508. } else {
  1509. p++;
  1510. }
  1511. }
  1512. }
  1513. return (tmp);
  1514. }
  1515. /*
  1516. * Public callers for the above to hide the absence of &apos from HTML
  1517. */
  1518. char* xml_entitize(const char* str)
  1519. {
  1520. return entitize(str, 0);
  1521. }
  1522. char* html_entitize(const char* str)
  1523. {
  1524. return entitize(str, 1);
  1525. }
  1526. char* html_entitize(const QString& str)
  1527. {
  1528. return entitize(CSTR(str), 1);
  1529. }
  1530. /*
  1531. * xml_tag utilities
  1532. */
  1533. xml_tag* xml_next(xml_tag* root, xml_tag* cur)
  1534. {
  1535. if (cur->child) {
  1536. cur = cur->child;
  1537. } else if (cur->sibling) {
  1538. cur = cur->sibling;
  1539. } else {
  1540. cur = cur->parent;
  1541. if (cur == root) {
  1542. cur = NULL;
  1543. }
  1544. if (cur) {
  1545. cur = cur->sibling;
  1546. }
  1547. }
  1548. return cur;
  1549. }
  1550. xml_tag* xml_findnext(xml_tag* root, xml_tag* cur, const char* tagname)
  1551. {
  1552. xml_tag* result = cur;
  1553. do {
  1554. result = xml_next(root, result);
  1555. } while (result && result->tagname.compare(tagname, Qt::CaseInsensitive));
  1556. return result;
  1557. }
  1558. xml_tag* xml_findfirst(xml_tag* root, const char* tagname)
  1559. {
  1560. return xml_findnext(root, root, tagname);
  1561. }
  1562. char* xml_attribute(xml_tag* tag, const char* attrname)
  1563. {
  1564. char* result = NULL;
  1565. if (tag->attributes) {
  1566. char** attr = tag->attributes;
  1567. while (attr && *attr) {
  1568. if (0 == case_ignore_strcmp(*attr, attrname)) {
  1569. result = attr[1];
  1570. break;
  1571. }
  1572. attr+=2;
  1573. }
  1574. }
  1575. return result;
  1576. }
  1577. const QString get_filename(const QString& fname)
  1578. {
  1579. return QFileInfo(fname).fileName();
  1580. }
  1581. /* bit manipulation functions */
  1582. /*
  1583. * setbit: Set bit number [nr] of buffer [buf]
  1584. */
  1585. void gb_setbit(void* buf, const uint32_t nr)
  1586. {
  1587. unsigned char* bytes = (unsigned char*) buf;
  1588. bytes[nr / 8] |= (1 << (nr % 8));
  1589. }
  1590. /*
  1591. * setbit: Get state of bit number [nr] of buffer [buf]
  1592. */
  1593. char gb_getbit(const void* buf, const uint32_t nr)
  1594. {
  1595. const unsigned char* bytes = (const unsigned char*) buf;
  1596. return (bytes[nr / 8] & (1 << (nr % 8)));
  1597. }
  1598. /*
  1599. * gb_int2ptr: Needed, when sizeof(*void) != sizeof(int) ! compiler warning !
  1600. */
  1601. void* gb_int2ptr(const int i)
  1602. {
  1603. union {
  1604. void* p;
  1605. int i;
  1606. } x = { NULL };
  1607. x.i = i;
  1608. return x.p;
  1609. }
  1610. /*
  1611. * gb_ptr2int: Needed, when sizeof(*void) != sizeof(int) ! compiler warning !
  1612. */
  1613. int gb_ptr2int(const void* p)
  1614. {
  1615. union {
  1616. const void* p;
  1617. int i;
  1618. } x = { p };
  1619. return x.i;
  1620. }