localeless.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #include "localeless.h"
  2. #include <math.h>
  3. #if _MSC_VER >= 1400 /* To avoid compilation error on dSPACE DS1006 (gcc) */
  4. #include <locale.h>
  5. #endif
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #if _MSC_VER >= 1400
  11. #ifdef __cplusplus
  12. class MyCLocal {
  13. MyCLocal(MyCLocal const&);
  14. void operator=(MyCLocal const&);
  15. public:
  16. _locale_t cloc;
  17. MyCLocal() {cloc=_create_locale(LC_ALL, "C");}
  18. ~MyCLocal() {_free_locale(cloc);}
  19. };
  20. static _locale_t *Clocale() {
  21. /* Avoiding memory leak at end */
  22. static MyCLocal myloc;
  23. return &myloc.cloc;
  24. };
  25. void freeClocale(){
  26. /*Emtpy in CPP only relevant for c code*/
  27. };
  28. #else
  29. static _locale_t cloc = NULL;
  30. static int firstTime = 1;
  31. static _locale_t *Clocale() {
  32. /* The c-version has a memory leak; cannot avoid without using atexit and atexit for DLLs seems bad */
  33. if (firstTime) cloc=_create_locale(LC_ALL, "C");
  34. firstTime = 0;
  35. return &cloc;
  36. }
  37. void freeClocale(){
  38. _free_locale(cloc);
  39. cloc = NULL;
  40. firstTime = 1;
  41. }
  42. #endif
  43. #endif
  44. double atofC(const char *str) {
  45. #if _MSC_VER >= 1400
  46. return _atof_l(str, *Clocale());
  47. #else
  48. return atof(str);
  49. #endif
  50. }
  51. int atoiC(const char *str) {
  52. #if _MSC_VER >= 1400
  53. return _atoi_l(str, *Clocale());
  54. #else
  55. return atoi(str);
  56. #endif
  57. }
  58. int atolC(const char *str) {
  59. #if _MSC_VER >= 1400
  60. return _atol_l(str, *Clocale());
  61. #else
  62. return atol(str);
  63. #endif
  64. }
  65. #if _MSC_VER
  66. int _stricmpC(const char *string1,const char *string2) {
  67. #if _MSC_VER >= 1400
  68. return _stricmp_l(string1, string2, *Clocale());
  69. #else
  70. return _stricmp(string1, string2);
  71. #endif
  72. }
  73. #endif
  74. int isalnumC(int c) {
  75. #if _MSC_VER >= 1400
  76. return _isalnum_l(c, *Clocale());
  77. #else
  78. return isalnum(c);
  79. #endif
  80. }
  81. int isalphaC(int c) {
  82. #if _MSC_VER >= 1400
  83. return _isalpha_l(c, *Clocale());
  84. #else
  85. return isalpha(c);
  86. #endif
  87. }
  88. int isdigitC(int c) {
  89. #if _MSC_VER >= 1400
  90. return _isdigit_l(c, *Clocale());
  91. #else
  92. return isdigit(c);
  93. #endif
  94. }
  95. int isupperC(int c) {
  96. #if _MSC_VER >= 1400
  97. return _isupper_l(c, *Clocale());
  98. #else
  99. return isupper(c);
  100. #endif
  101. }
  102. int islowerC(int c) {
  103. #if _MSC_VER >= 1400
  104. return _islower_l(c, *Clocale());
  105. #else
  106. return islower(c);
  107. #endif
  108. }
  109. int isspaceC(int c) {
  110. #if _MSC_VER >= 1400
  111. return _isspace_l(c, *Clocale());
  112. #else
  113. return isspace(c);
  114. #endif
  115. }
  116. int toupperC(int c) {
  117. #if _MSC_VER >= 1400
  118. return _toupper_l(c, *Clocale());
  119. #else
  120. #if !defined(NO_FILE)
  121. return toupper(c);
  122. #else
  123. return c;
  124. #endif
  125. #endif
  126. }
  127. int tolowerC(int c) {
  128. #if _MSC_VER >= 1400
  129. return _tolower_l(c, *Clocale());
  130. #else
  131. return tolower(c);
  132. #endif
  133. }
  134. extern int fscanfC_Bracket_255s(FILE*f, char*s, char *s2) {
  135. #if !defined(NO_FILE)
  136. #if _MSC_VER >= 1400
  137. _fscanf_l(f, "%[^\\[]255s", *Clocale(), s);
  138. return _fscanf_l(f, "%1s", *Clocale(), s2);
  139. #else
  140. fscanf(f, "%[^\\[]255s", s);
  141. return fscanf(f, "%1s", s2);
  142. #endif
  143. #else
  144. return 0;
  145. #endif
  146. }
  147. extern int fscanfC255s(FILE*f, char*s) {
  148. #if !defined(NO_FILE)
  149. #if _MSC_VER >= 1400
  150. return _fscanf_l(f, "%255s", *Clocale(), s);
  151. #else
  152. return fscanf(f, "%255s", s);
  153. #endif
  154. #else
  155. return 0;
  156. #endif
  157. }
  158. extern int fscanfClf(FILE*f, double*x) {
  159. #if !defined(NO_FILE)
  160. #if _MSC_VER >= 1400
  161. return _fscanf_l(f, " %lg", *Clocale(), x);
  162. #else
  163. return fscanf(f, " %lg", x);
  164. #endif
  165. #else
  166. return 0;
  167. #endif
  168. }
  169. extern int fscanfClf3(FILE*f, double*x, double*y, double*z) {
  170. #if !defined(NO_FILE)
  171. #if _MSC_VER >= 1400
  172. return _fscanf_l(f, " %lg %lg %lg", *Clocale(), x,y,z);
  173. #else
  174. return fscanf(f, " %lg %lg %lg", x,y,z);
  175. #endif
  176. #else
  177. return 0;
  178. #endif
  179. }
  180. extern int fscanfCg(FILE*f, float*x) {
  181. #if !defined(NO_FILE)
  182. #if _MSC_VER >= 1400
  183. return _fscanf_l(f, " %g", *Clocale(), x);
  184. #else
  185. return fscanf(f, " %g", x);
  186. #endif
  187. #else
  188. return 0;
  189. #endif
  190. }
  191. extern int fscanfCd(FILE*f, int*x) {
  192. #if !defined(NO_FILE)
  193. #if _MSC_VER >= 1400
  194. return _fscanf_l(f, " %d", *Clocale(), x);
  195. #else
  196. return fscanf(f, " %d", x);
  197. #endif
  198. #else
  199. return 0;
  200. #endif
  201. }
  202. extern int fscanfCu3(FILE*f, unsigned*r,unsigned*g,unsigned*b) {
  203. #if !defined(NO_FILE)
  204. #if _MSC_VER >= 1400
  205. return _fscanf_l(f, " %u %u %u", *Clocale(), r,g,b);
  206. #else
  207. return fscanf(f, " %u %u %u", r,g,b);
  208. #endif
  209. #else
  210. return 0;
  211. #endif
  212. }
  213. extern int fscanfCd3(FILE*f, int*r,int*g,int*b) {
  214. #if !defined(NO_FILE)
  215. #if _MSC_VER >= 1400
  216. return _fscanf_l(f, " %d %d %d", *Clocale(), r,g,b);
  217. #else
  218. return fscanf(f, " %d %d %d", r,g,b);
  219. #endif
  220. #else
  221. return 0;
  222. #endif
  223. }
  224. extern int fscanfCcond_version_s(FILE*f, char*cond, char*vers,char*s) {
  225. #if !defined(NO_FILE)
  226. #if _MSC_VER >= 1400
  227. return _fscanf_l(f," %[<>=] \"%[^\"]\" %s)",*Clocale(), cond,vers,s);
  228. #else
  229. return fscanf(f," %[<>=] \"%[^\"]\" %s)",cond,vers,s);
  230. #endif
  231. #else
  232. return 0;
  233. #endif
  234. }
  235. extern int sscanfCd(const char*line, int*x) {
  236. #if _MSC_VER >= 1400
  237. return _sscanf_l(line, " %d", *Clocale(), x);
  238. #else
  239. #if !defined(NO_FILE)
  240. return sscanf(line, " %d", x);
  241. #else
  242. return 0;
  243. #endif
  244. #endif
  245. }
  246. extern int sscanfCd5(const char*line, int*a, int*b,int*c,int*d,int*e) {
  247. #if _MSC_VER >= 1400
  248. return _sscanf_l(line, " %d %d %d %d %d", *Clocale(), a,b,c,d,e);
  249. #else
  250. #if !defined(NO_FILE)
  251. return sscanf(line, " %d %d %d %d %d", a,b,c,d,e);
  252. #else
  253. return 0;
  254. #endif
  255. #endif
  256. }
  257. extern int sscanfCd_minus3(const char*line, int*y,int*m,int*d) {
  258. #if _MSC_VER >= 1400
  259. return _sscanf_l(line, "%d-%d-%d", *Clocale(), y, m, d);
  260. #else
  261. #if !defined(NO_FILE)
  262. return sscanf(line, "%d-%d-%d", y, m, d);
  263. #else
  264. return 0;
  265. #endif
  266. #endif
  267. }
  268. extern int sscanfClg_49s(const char*line, double*x, char*s) {
  269. #if _MSC_VER >= 1400
  270. return _sscanf_l(line, "%lg %49s", *Clocale(), x, s);
  271. #else
  272. #if !defined(NO_FILE)
  273. return sscanf(line, "%lg %49s", x, s);
  274. #else
  275. return 0;
  276. #endif
  277. #endif
  278. }
  279. extern int sscanfCg(const char*line, float*x){
  280. #if _MSC_VER >= 1400
  281. return _sscanf_l(line, " %g", *Clocale(), x);
  282. #else
  283. #if !defined(NO_FILE)
  284. return sscanf(line, " %g", x);
  285. #else
  286. return 0;
  287. #endif
  288. #endif
  289. }
  290. extern int sscanfCx2_underscore(const char*line,unsigned int*a,unsigned int*b) {
  291. #if _MSC_VER >= 1400
  292. return _sscanf_l(line, "%lx_%lx", *Clocale(), a,b);
  293. #else
  294. #if !defined(NO_FILE)
  295. return sscanf(line, "%lx_%lx", a,b);
  296. #else
  297. return 0;
  298. #endif
  299. #endif
  300. }
  301. extern int sscanfClu(const char*line, unsigned int*a) {
  302. #if _MSC_VER >= 1400
  303. return _sscanf_l(line, "%lu", *Clocale(), a);
  304. #else
  305. #if !defined(NO_FILE)
  306. return sscanf(line, "%lu", a);
  307. #else
  308. return 0;
  309. #endif
  310. #endif
  311. }
  312. extern int sscanfCg_comma(const char*line, float*x){
  313. #if _MSC_VER >= 1400
  314. return _sscanf_l(line, " %g,", *Clocale(),x);
  315. #else
  316. #if !defined(NO_FILE)
  317. return sscanf(line, " %g,", x);
  318. #else
  319. return 0;
  320. #endif
  321. #endif
  322. }
  323. extern int sscanfClg(const char*line, double*x) {
  324. #if _MSC_VER >= 1400
  325. return _sscanf_l(line, " %lg", *Clocale(), x);
  326. #else
  327. #if !defined(NO_FILE)
  328. return sscanf(line, " %lg", x);
  329. #else
  330. return 0;
  331. #endif
  332. #endif
  333. }
  334. extern int sscanfCcomp_s_par(const char*line, char*cond, char*vers) {
  335. #if _MSC_VER >= 1400
  336. return _sscanf_l(line, " %[<>=] %s)",*Clocale(), cond, vers);
  337. #else
  338. #if !defined(NO_FILE)
  339. return sscanf(line, " %[<>=] %s)", cond, vers);
  340. #else
  341. return 0;
  342. #endif
  343. #endif
  344. }
  345. extern int sscanfCu(const char*line, unsigned*x) {
  346. #if _MSC_VER >= 1400
  347. return _sscanf_l(line, " %u", *Clocale(),x);
  348. #else
  349. #if !defined(NO_FILE)
  350. return sscanf(line, " %u", x);
  351. #else
  352. return 0;
  353. #endif
  354. #endif
  355. }
  356. extern int sscanfCg_comma4(const char*line, float*a,float*b,float*c,float*d) {
  357. #if _MSC_VER >= 1400
  358. return _sscanf_l(line, "%g,%g,%g,%g,",*Clocale(), a,b,c,d);
  359. #else
  360. #if !defined(NO_FILE)
  361. return sscanf(line, "%g,%g,%g,%g,", a,b,c,d);
  362. #else
  363. return 0;
  364. #endif
  365. #endif
  366. }
  367. extern int sscanfCi_comma4(const char*line, int*a,int*b,int*c,int*d) {
  368. #if _MSC_VER >= 1400
  369. return _sscanf_l(line, "%i,%i,%i,%i,", *Clocale(), a,b,c,d);
  370. #else
  371. #if !defined(NO_FILE)
  372. return sscanf(line, "%i,%i,%i,%i,", a,b,c,d);
  373. #else
  374. return 0;
  375. #endif
  376. #endif
  377. }
  378. extern int sscanfCi(const char*line, int*a) {
  379. #if _MSC_VER >= 1400
  380. return _sscanf_l(line, "%i",*Clocale(),a);
  381. #else
  382. #if !defined(NO_FILE)
  383. return sscanf(line, "%i",a);
  384. #else
  385. return 0;
  386. #endif
  387. #endif
  388. }
  389. extern int sscanfClfc(const char*line, double*d, char*dummy) {
  390. #if _MSC_VER >= 1400
  391. return _sscanf_l(line, "%lg %c",*Clocale(), d, dummy);
  392. #else
  393. #if !defined(NO_FILE)
  394. return sscanf(line, "%lg %c", d, dummy);
  395. #else
  396. return 0;
  397. #endif
  398. #endif
  399. }
  400. extern int sscanfCtext_ssdd(const char*line, const char*fmt, char*a,char*b, int*c,int *d) {
  401. #if _MSC_VER >= 1400
  402. return _sscanf_l(line, fmt,*Clocale(), a,b,c,d);
  403. #else
  404. #if !defined(NO_FILE)
  405. return sscanf(line, fmt, a,b,c,d);
  406. #else
  407. return 0;
  408. #endif
  409. #endif
  410. }
  411. extern int sscanfCincludename(const char*line, char*s) {
  412. #if _MSC_VER >= 1400
  413. return _sscanf_l(line, "#include \"%[^\"]\"",*Clocale(), s);
  414. #else
  415. #if !defined(NO_FILE) && !defined(LABCAR)
  416. return sscanf(line, "#include \"%[^\"]\"", s);
  417. #else
  418. return 0;
  419. #endif
  420. #endif
  421. }
  422. extern int sscanfCversion(const char*line,char*vers) {
  423. #if _MSC_VER >= 1400
  424. return _sscanf_l(line, "( version = \"%[^\"]\" )",*Clocale(), vers);
  425. #else
  426. #if !defined(NO_FILE) && !defined(LABCAR)
  427. return sscanf(line,"( version = \"%[^\"]\" )",vers);
  428. #else
  429. return 0;
  430. #endif
  431. #endif
  432. }
  433. extern int sscanfCtext_d(const char*line, const char*txtWithPercentD, int*x) {
  434. #if _MSC_VER >= 1400
  435. return _sscanf_l(line, txtWithPercentD,*Clocale(), x);
  436. #else
  437. #if !defined(NO_FILE) && !defined(LABCAR)
  438. return sscanf(line, txtWithPercentD, x);
  439. #else
  440. return 0;
  441. #endif
  442. #endif
  443. }
  444. extern int sscanfCtext_d3(const char*line, const char*txtWithPercentD, int*r,int*g,int*b) {
  445. #if _MSC_VER >= 1400
  446. return _sscanf_l(line, txtWithPercentD,*Clocale(), r,g,b);
  447. #else
  448. #if !defined(NO_FILE) && !defined(LABCAR)
  449. return sscanf(line, txtWithPercentD, r,g,b);
  450. #else
  451. return 0;
  452. #endif
  453. #endif
  454. }
  455. extern int sscanfCtext_d4(const char*line, const char*txtWithPercentD, int*a,int*b,int*c,int*d) {
  456. #if _MSC_VER >= 1400
  457. return _sscanf_l(line, txtWithPercentD,*Clocale(), a,b,c,d);
  458. #else
  459. #if !defined(NO_FILE) && !defined(LABCAR)
  460. return sscanf(line, txtWithPercentD, a,b,c,d);
  461. #else
  462. return 0;
  463. #endif
  464. #endif
  465. }
  466. extern int sscanfCtext_d5(const char*line, const char*txtWithPercentD, int*a,int*b,int*c,int*d,int*e) {
  467. #if _MSC_VER >= 1400
  468. return _sscanf_l(line, txtWithPercentD,*Clocale(), a,b,c,d,e);
  469. #else
  470. #if !defined(NO_FILE) && !defined(LABCAR)
  471. return sscanf(line, txtWithPercentD, a,b,c,d,e);
  472. #else
  473. return 0;
  474. #endif
  475. #endif
  476. }
  477. extern int sscanfCi3_slash(const char*line,int*a,int*b,int*c) {
  478. #if _MSC_VER >= 1400
  479. return _sscanf_l(line, "%i/%i/%i",*Clocale(), a,b,c);
  480. #else
  481. #if !defined(NO_FILE) && !defined(LABCAR)
  482. return sscanf(line, "%i/%i/%i", a,b,c);
  483. #else
  484. return 0;
  485. #endif
  486. #endif
  487. }
  488. extern int sscanfCi2_slash(const char*line,int*a,int*b) {
  489. #if _MSC_VER >= 1400
  490. return _sscanf_l(line, "%i/%i",*Clocale(), a,b);
  491. #else
  492. #if !defined(NO_FILE) && !defined(LABCAR)
  493. return sscanf(line, "%i/%i", a,b);
  494. #else
  495. return 0;
  496. #endif
  497. #endif
  498. }
  499. extern int sscanfCi2_slashlash(const char*line,int*a,int*b) {
  500. #if _MSC_VER >= 1400
  501. return _sscanf_l(line, "%i//%i",*Clocale(), a,b);
  502. #else
  503. #if !defined(NO_FILE) && !defined(LABCAR)
  504. return sscanf(line, "%i//%i", a,b);
  505. #else
  506. return 0;
  507. #endif
  508. #endif
  509. }
  510. extern int sscanfC255s_n(const char*line, char*s,int*length) {
  511. #if _MSC_VER >= 1400
  512. return _sscanf_l(line, "%255s%n",*Clocale(), s, length);
  513. #else
  514. #if !defined(NO_FILE) && !defined(LABCAR)
  515. return sscanf(line, "%255s%n", s, length);
  516. #else
  517. return 0;
  518. #endif
  519. #endif
  520. }
  521. extern int sscanfC255s(const char*line, char*s) {
  522. #if _MSC_VER >= 1400
  523. return _sscanf_l(line, "%255s",*Clocale(), s);
  524. #else
  525. #if !defined(NO_FILE) && !defined(LABCAR)
  526. return sscanf(line, "%255s", s);
  527. #else
  528. return 0;
  529. #endif
  530. #endif
  531. }
  532. extern int sscanfCtext_s2(const char*line, const char*txtWithPercentD,char*a,char*b) {
  533. #if _MSC_VER >= 1400
  534. return _sscanf_l(line, txtWithPercentD,*Clocale(), a,b);
  535. #else
  536. #if !defined(NO_FILE) && !defined(LABCAR)
  537. return sscanf(line, txtWithPercentD, a,b);
  538. #else
  539. return 0;
  540. #endif
  541. #endif
  542. }
  543. extern int sscanfCtext_s4(const char*line, const char*txtWithPercentD,char*a,char*b,char*c,char*d) {
  544. #if _MSC_VER >= 1400
  545. return _sscanf_l(line, txtWithPercentD,*Clocale(), a,b,c,d);
  546. #else
  547. #if !defined(NO_FILE) && !defined(LABCAR)
  548. return sscanf(line, txtWithPercentD, a,b,c,d);
  549. #else
  550. return 0;
  551. #endif
  552. #endif
  553. }
  554. extern int sscanfCtext_sd(const char*line, const char*txtWithPercentD,char*a,int*b) {
  555. #if _MSC_VER >= 1400
  556. return _sscanf_l(line, txtWithPercentD,*Clocale(), a,b);
  557. #else
  558. #if !defined(NO_FILE) && !defined(LABCAR)
  559. return sscanf(line, txtWithPercentD, a,b);
  560. #else
  561. return 0;
  562. #endif
  563. #endif
  564. }
  565. extern int sscanfCtext_s2d(const char*line, const char*txtWithPercentD,char*a,char*b,int*c) {
  566. #if _MSC_VER >= 1400
  567. return _sscanf_l(line, txtWithPercentD,*Clocale(), a,b,c);
  568. #else
  569. #if !defined(NO_FILE) && !defined(LABCAR)
  570. return sscanf(line, txtWithPercentD, a,b,c);
  571. #else
  572. return 0;
  573. #endif
  574. #endif
  575. }
  576. extern int sprintfC(char*s, const char*format, ...) {
  577. va_list extra;
  578. int res;
  579. va_start(extra, format);
  580. #if _MSC_VER >= 1400
  581. res = _vsprintf_l(s, format, *Clocale(), extra);
  582. #else
  583. res =vsprintf(s, format, extra);
  584. #endif
  585. va_end(extra);
  586. return res;
  587. }
  588. extern int fprintfC(FILE*f, const char*format, ...) {
  589. va_list extra;
  590. int res;
  591. va_start(extra, format);
  592. #if !defined(NO_FILE)
  593. #if _MSC_VER >= 1400
  594. res = _vfprintf_l(f, format, *Clocale(), extra);
  595. #else
  596. res =vfprintf(f, format, extra);
  597. #endif
  598. #else
  599. res = -1;
  600. #endif
  601. va_end(extra);
  602. return res;
  603. }
  604. extern int vfprintfC(FILE*f, const char*format, va_list extra) {
  605. int res;
  606. #if !defined(NO_FILE)
  607. #if _MSC_VER >= 1400
  608. res = _vfprintf_l(f, format, *Clocale(), extra);
  609. #else
  610. res =vfprintf(f, format, extra);
  611. #endif
  612. #else
  613. res = -1;
  614. #endif
  615. return res;
  616. }
  617. extern int vsnprintfC(char*buf,size_t buf_len,const char*fmt,va_list arg) {
  618. int res;
  619. /* Use len-1 to ensure that string will be nul-terminated. */
  620. #if _MSC_VER >= 1400
  621. res=_vsnprintf_l(buf, buf_len-1, fmt, *Clocale(), arg);
  622. #elif defined(_MSC_VER) && _MSC_VER >= 1200
  623. res=_vsnprintf(buf, buf_len-1, fmt, arg);
  624. #else
  625. res=vsnprintf(buf, buf_len-1, fmt, arg);
  626. #endif
  627. buf[buf_len-1] = 0;
  628. return res;
  629. }