strutil.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. Common strings utilities
  3. Copyright (C) 2007, 2011, 2013
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Rostislav Benes, 2007
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <langinfo.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "lib/global.h"
  25. #include "lib/strutil.h"
  26. /*names, that are used for utf-8 */
  27. static const char *str_utf8_encodings[] = {
  28. "utf-8",
  29. "utf8",
  30. NULL
  31. };
  32. /* standard 8bit encodings, no wide or multibytes characters */
  33. static const char *str_8bit_encodings[] = {
  34. "cp-1251",
  35. "cp1251",
  36. "cp-1250",
  37. "cp1250",
  38. "cp-866",
  39. "cp866",
  40. "ibm-866",
  41. "ibm866",
  42. "cp-850",
  43. "cp850",
  44. "cp-852",
  45. "cp852",
  46. "iso-8859",
  47. "iso8859",
  48. "koi8",
  49. NULL
  50. };
  51. /* terminal encoding */
  52. static char *codeset = NULL;
  53. static char *term_encoding = NULL;
  54. /* function for encoding specific operations */
  55. static struct str_class used_class;
  56. GIConv str_cnv_to_term;
  57. GIConv str_cnv_from_term;
  58. GIConv str_cnv_not_convert = INVALID_CONV;
  59. /* if enc is same encoding like on terminal */
  60. static int
  61. str_test_not_convert (const char *enc)
  62. {
  63. return g_ascii_strcasecmp (enc, codeset) == 0;
  64. }
  65. GIConv
  66. str_crt_conv_to (const char *to_enc)
  67. {
  68. return (!str_test_not_convert (to_enc)) ? g_iconv_open (to_enc, codeset) : str_cnv_not_convert;
  69. }
  70. GIConv
  71. str_crt_conv_from (const char *from_enc)
  72. {
  73. return (!str_test_not_convert (from_enc))
  74. ? g_iconv_open (codeset, from_enc) : str_cnv_not_convert;
  75. }
  76. void
  77. str_close_conv (GIConv conv)
  78. {
  79. if (conv != str_cnv_not_convert)
  80. g_iconv_close (conv);
  81. }
  82. static estr_t
  83. _str_convert (GIConv coder, const char *string, int size, GString * buffer)
  84. {
  85. estr_t state = ESTR_SUCCESS;
  86. gchar *tmp_buff = NULL;
  87. gssize left;
  88. gsize bytes_read = 0;
  89. gsize bytes_written = 0;
  90. GError *error = NULL;
  91. errno = 0;
  92. if (coder == INVALID_CONV)
  93. return ESTR_FAILURE;
  94. if (string == NULL || buffer == NULL)
  95. return ESTR_FAILURE;
  96. /*
  97. if (! used_class.is_valid_string (string))
  98. {
  99. return ESTR_FAILURE;
  100. }
  101. */
  102. if (size < 0)
  103. {
  104. size = strlen (string);
  105. }
  106. else
  107. {
  108. left = strlen (string);
  109. if (left < size)
  110. size = left;
  111. }
  112. left = size;
  113. g_iconv (coder, NULL, NULL, NULL, NULL);
  114. while (left)
  115. {
  116. tmp_buff = g_convert_with_iconv ((const gchar *) string,
  117. left, coder, &bytes_read, &bytes_written, &error);
  118. if (error)
  119. {
  120. int code = error->code;
  121. g_error_free (error);
  122. error = NULL;
  123. switch (code)
  124. {
  125. case G_CONVERT_ERROR_NO_CONVERSION:
  126. /* Conversion between the requested character sets is not supported. */
  127. tmp_buff = g_strnfill (strlen (string), '?');
  128. g_string_append (buffer, tmp_buff);
  129. g_free (tmp_buff);
  130. return ESTR_FAILURE;
  131. case G_CONVERT_ERROR_ILLEGAL_SEQUENCE:
  132. /* Invalid byte sequence in conversion input. */
  133. if ((tmp_buff == NULL) && (bytes_read != 0))
  134. /* recode valid byte sequence */
  135. tmp_buff = g_convert_with_iconv ((const gchar *) string,
  136. bytes_read, coder, NULL, NULL, NULL);
  137. if (tmp_buff != NULL)
  138. {
  139. g_string_append (buffer, tmp_buff);
  140. g_free (tmp_buff);
  141. }
  142. if ((int) bytes_read < left)
  143. {
  144. string += bytes_read + 1;
  145. size -= (bytes_read + 1);
  146. left -= (bytes_read + 1);
  147. g_string_append_c (buffer, *(string - 1));
  148. }
  149. else
  150. {
  151. return ESTR_PROBLEM;
  152. }
  153. state = ESTR_PROBLEM;
  154. break;
  155. case G_CONVERT_ERROR_PARTIAL_INPUT:
  156. /* Partial character sequence at end of input. */
  157. g_string_append (buffer, tmp_buff);
  158. g_free (tmp_buff);
  159. if ((int) bytes_read < left)
  160. {
  161. left = left - bytes_read;
  162. tmp_buff = g_strnfill (left, '?');
  163. g_string_append (buffer, tmp_buff);
  164. g_free (tmp_buff);
  165. }
  166. return ESTR_PROBLEM;
  167. case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */
  168. case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH: /* Don't know how handle this error :( */
  169. case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */
  170. default:
  171. g_free (tmp_buff);
  172. return ESTR_FAILURE;
  173. }
  174. }
  175. else
  176. {
  177. if (tmp_buff != NULL)
  178. {
  179. if (*tmp_buff)
  180. {
  181. g_string_append (buffer, tmp_buff);
  182. g_free (tmp_buff);
  183. string += bytes_read;
  184. left -= bytes_read;
  185. }
  186. else
  187. {
  188. g_free (tmp_buff);
  189. g_string_append (buffer, string);
  190. return state;
  191. }
  192. }
  193. else
  194. {
  195. g_string_append (buffer, string);
  196. return ESTR_PROBLEM;
  197. }
  198. }
  199. }
  200. return state;
  201. }
  202. estr_t
  203. str_convert (GIConv coder, const char *string, GString * buffer)
  204. {
  205. return _str_convert (coder, string, -1, buffer);
  206. }
  207. estr_t
  208. str_nconvert (GIConv coder, const char *string, int size, GString * buffer)
  209. {
  210. return _str_convert (coder, string, size, buffer);
  211. }
  212. gchar *
  213. str_conv_gerror_message (GError * error, const char *def_msg)
  214. {
  215. return used_class.conv_gerror_message (error, def_msg);
  216. }
  217. estr_t
  218. str_vfs_convert_from (GIConv coder, const char *string, GString * buffer)
  219. {
  220. estr_t result;
  221. if (coder == str_cnv_not_convert)
  222. {
  223. g_string_append (buffer, string != NULL ? string : "");
  224. result = ESTR_SUCCESS;
  225. }
  226. else
  227. result = _str_convert (coder, string, -1, buffer);
  228. return result;
  229. }
  230. estr_t
  231. str_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
  232. {
  233. return used_class.vfs_convert_to (coder, string, size, buffer);
  234. }
  235. void
  236. str_printf (GString * buffer, const char *format, ...)
  237. {
  238. va_list ap;
  239. va_start (ap, format);
  240. #if GLIB_CHECK_VERSION (2, 14, 0)
  241. g_string_append_vprintf (buffer, format, ap);
  242. #else
  243. {
  244. gchar *tmp;
  245. tmp = g_strdup_vprintf (format, ap);
  246. g_string_append (buffer, tmp);
  247. g_free (tmp);
  248. }
  249. #endif
  250. va_end (ap);
  251. }
  252. void
  253. str_insert_replace_char (GString * buffer)
  254. {
  255. used_class.insert_replace_char (buffer);
  256. }
  257. estr_t
  258. str_translate_char (GIConv conv, const char *keys, size_t ch_size, char *output, size_t out_size)
  259. {
  260. size_t left;
  261. size_t cnv;
  262. g_iconv (conv, NULL, NULL, NULL, NULL);
  263. left = (ch_size == (size_t) (-1)) ? strlen (keys) : ch_size;
  264. cnv = g_iconv (conv, (gchar **) & keys, &left, &output, &out_size);
  265. if (cnv == (size_t) (-1))
  266. {
  267. return (errno == EINVAL) ? ESTR_PROBLEM : ESTR_FAILURE;
  268. }
  269. else
  270. {
  271. output[0] = '\0';
  272. return ESTR_SUCCESS;
  273. }
  274. }
  275. const char *
  276. str_detect_termencoding (void)
  277. {
  278. if (term_encoding == NULL)
  279. {
  280. /* On Linux, nl_langinfo (CODESET) returns upper case UTF-8 whether the LANG is set
  281. to utf-8 or UTF-8.
  282. On Mac OS X, it returns the same case as the LANG input.
  283. So let tranform result of nl_langinfo (CODESET) to upper case unconditionally. */
  284. term_encoding = g_ascii_strup (nl_langinfo (CODESET), -1);
  285. }
  286. return term_encoding;
  287. }
  288. static int
  289. str_test_encoding_class (const char *encoding, const char **table)
  290. {
  291. int t;
  292. int result = 0;
  293. if (encoding == NULL)
  294. return result;
  295. for (t = 0; table[t] != NULL; t++)
  296. {
  297. result += (g_ascii_strncasecmp (encoding, table[t], strlen (table[t])) == 0);
  298. }
  299. return result;
  300. }
  301. static void
  302. str_choose_str_functions (void)
  303. {
  304. if (str_test_encoding_class (codeset, str_utf8_encodings))
  305. {
  306. used_class = str_utf8_init ();
  307. }
  308. else if (str_test_encoding_class (codeset, str_8bit_encodings))
  309. {
  310. used_class = str_8bit_init ();
  311. }
  312. else
  313. {
  314. used_class = str_ascii_init ();
  315. }
  316. }
  317. gboolean
  318. str_isutf8 (const char *codeset_name)
  319. {
  320. return (str_test_encoding_class (codeset_name, str_utf8_encodings) != 0);
  321. }
  322. void
  323. str_init_strings (const char *termenc)
  324. {
  325. codeset = termenc != NULL ? g_ascii_strup (termenc, -1) : g_strdup (str_detect_termencoding ());
  326. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  327. if (str_cnv_not_convert == INVALID_CONV)
  328. {
  329. if (termenc != NULL)
  330. {
  331. g_free (codeset);
  332. codeset = g_strdup (str_detect_termencoding ());
  333. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  334. }
  335. if (str_cnv_not_convert == INVALID_CONV)
  336. {
  337. g_free (codeset);
  338. codeset = g_strdup ("ASCII");
  339. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  340. }
  341. }
  342. str_cnv_to_term = str_cnv_not_convert;
  343. str_cnv_from_term = str_cnv_not_convert;
  344. str_choose_str_functions ();
  345. }
  346. void
  347. str_uninit_strings (void)
  348. {
  349. if (str_cnv_not_convert != INVALID_CONV)
  350. g_iconv_close (str_cnv_not_convert);
  351. g_free (term_encoding);
  352. g_free (codeset);
  353. }
  354. const char *
  355. str_term_form (const char *text)
  356. {
  357. return used_class.term_form (text);
  358. }
  359. const char *
  360. str_fit_to_term (const char *text, int width, align_crt_t just_mode)
  361. {
  362. return used_class.fit_to_term (text, width, just_mode);
  363. }
  364. const char *
  365. str_term_trim (const char *text, int width)
  366. {
  367. return used_class.term_trim (text, width);
  368. }
  369. const char *
  370. str_term_substring (const char *text, int start, int width)
  371. {
  372. return used_class.term_substring (text, start, width);
  373. }
  374. char *
  375. str_get_next_char (char *text)
  376. {
  377. used_class.cnext_char ((const char **) &text);
  378. return text;
  379. }
  380. const char *
  381. str_cget_next_char (const char *text)
  382. {
  383. used_class.cnext_char (&text);
  384. return text;
  385. }
  386. void
  387. str_next_char (char **text)
  388. {
  389. used_class.cnext_char ((const char **) text);
  390. }
  391. void
  392. str_cnext_char (const char **text)
  393. {
  394. used_class.cnext_char (text);
  395. }
  396. char *
  397. str_get_prev_char (char *text)
  398. {
  399. used_class.cprev_char ((const char **) &text);
  400. return text;
  401. }
  402. const char *
  403. str_cget_prev_char (const char *text)
  404. {
  405. used_class.cprev_char (&text);
  406. return text;
  407. }
  408. void
  409. str_prev_char (char **text)
  410. {
  411. used_class.cprev_char ((const char **) text);
  412. }
  413. void
  414. str_cprev_char (const char **text)
  415. {
  416. used_class.cprev_char (text);
  417. }
  418. char *
  419. str_get_next_char_safe (char *text)
  420. {
  421. used_class.cnext_char_safe ((const char **) &text);
  422. return text;
  423. }
  424. const char *
  425. str_cget_next_char_safe (const char *text)
  426. {
  427. used_class.cnext_char_safe (&text);
  428. return text;
  429. }
  430. void
  431. str_next_char_safe (char **text)
  432. {
  433. used_class.cnext_char_safe ((const char **) text);
  434. }
  435. void
  436. str_cnext_char_safe (const char **text)
  437. {
  438. used_class.cnext_char_safe (text);
  439. }
  440. char *
  441. str_get_prev_char_safe (char *text)
  442. {
  443. used_class.cprev_char_safe ((const char **) &text);
  444. return text;
  445. }
  446. const char *
  447. str_cget_prev_char_safe (const char *text)
  448. {
  449. used_class.cprev_char_safe (&text);
  450. return text;
  451. }
  452. void
  453. str_prev_char_safe (char **text)
  454. {
  455. used_class.cprev_char_safe ((const char **) text);
  456. }
  457. void
  458. str_cprev_char_safe (const char **text)
  459. {
  460. used_class.cprev_char_safe (text);
  461. }
  462. int
  463. str_next_noncomb_char (char **text)
  464. {
  465. return used_class.cnext_noncomb_char ((const char **) text);
  466. }
  467. int
  468. str_cnext_noncomb_char (const char **text)
  469. {
  470. return used_class.cnext_noncomb_char (text);
  471. }
  472. int
  473. str_prev_noncomb_char (char **text, const char *begin)
  474. {
  475. return used_class.cprev_noncomb_char ((const char **) text, begin);
  476. }
  477. int
  478. str_cprev_noncomb_char (const char **text, const char *begin)
  479. {
  480. return used_class.cprev_noncomb_char (text, begin);
  481. }
  482. int
  483. str_is_valid_char (const char *ch, size_t size)
  484. {
  485. return used_class.is_valid_char (ch, size);
  486. }
  487. int
  488. str_term_width1 (const char *text)
  489. {
  490. return used_class.term_width1 (text);
  491. }
  492. int
  493. str_term_width2 (const char *text, size_t length)
  494. {
  495. return used_class.term_width2 (text, length);
  496. }
  497. int
  498. str_term_char_width (const char *text)
  499. {
  500. return used_class.term_char_width (text);
  501. }
  502. int
  503. str_offset_to_pos (const char *text, size_t length)
  504. {
  505. return used_class.offset_to_pos (text, length);
  506. }
  507. int
  508. str_length (const char *text)
  509. {
  510. return used_class.length (text);
  511. }
  512. int
  513. str_length_char (const char *text)
  514. {
  515. return str_cget_next_char_safe (text) - text;
  516. }
  517. int
  518. str_length2 (const char *text, int size)
  519. {
  520. return used_class.length2 (text, size);
  521. }
  522. int
  523. str_length_noncomb (const char *text)
  524. {
  525. return used_class.length_noncomb (text);
  526. }
  527. int
  528. str_column_to_pos (const char *text, size_t pos)
  529. {
  530. return used_class.column_to_pos (text, pos);
  531. }
  532. int
  533. str_isspace (const char *ch)
  534. {
  535. return used_class.char_isspace (ch);
  536. }
  537. int
  538. str_ispunct (const char *ch)
  539. {
  540. return used_class.char_ispunct (ch);
  541. }
  542. int
  543. str_isalnum (const char *ch)
  544. {
  545. return used_class.char_isalnum (ch);
  546. }
  547. int
  548. str_isdigit (const char *ch)
  549. {
  550. return used_class.char_isdigit (ch);
  551. }
  552. int
  553. str_toupper (const char *ch, char **out, size_t * remain)
  554. {
  555. return used_class.char_toupper (ch, out, remain);
  556. }
  557. int
  558. str_tolower (const char *ch, char **out, size_t * remain)
  559. {
  560. return used_class.char_tolower (ch, out, remain);
  561. }
  562. int
  563. str_isprint (const char *ch)
  564. {
  565. return used_class.char_isprint (ch);
  566. }
  567. gboolean
  568. str_iscombiningmark (const char *ch)
  569. {
  570. return used_class.char_iscombiningmark (ch);
  571. }
  572. const char *
  573. str_trunc (const char *text, int width)
  574. {
  575. return used_class.trunc (text, width);
  576. }
  577. char *
  578. str_create_search_needle (const char *needle, int case_sen)
  579. {
  580. return used_class.create_search_needle (needle, case_sen);
  581. }
  582. void
  583. str_release_search_needle (char *needle, int case_sen)
  584. {
  585. used_class.release_search_needle (needle, case_sen);
  586. }
  587. const char *
  588. str_search_first (const char *text, const char *search, int case_sen)
  589. {
  590. return used_class.search_first (text, search, case_sen);
  591. }
  592. const char *
  593. str_search_last (const char *text, const char *search, int case_sen)
  594. {
  595. return used_class.search_last (text, search, case_sen);
  596. }
  597. int
  598. str_is_valid_string (const char *text)
  599. {
  600. return used_class.is_valid_string (text);
  601. }
  602. int
  603. str_compare (const char *t1, const char *t2)
  604. {
  605. return used_class.compare (t1, t2);
  606. }
  607. int
  608. str_ncompare (const char *t1, const char *t2)
  609. {
  610. return used_class.ncompare (t1, t2);
  611. }
  612. int
  613. str_casecmp (const char *t1, const char *t2)
  614. {
  615. return used_class.casecmp (t1, t2);
  616. }
  617. int
  618. str_ncasecmp (const char *t1, const char *t2)
  619. {
  620. return used_class.ncasecmp (t1, t2);
  621. }
  622. int
  623. str_prefix (const char *text, const char *prefix)
  624. {
  625. return used_class.prefix (text, prefix);
  626. }
  627. int
  628. str_caseprefix (const char *text, const char *prefix)
  629. {
  630. return used_class.caseprefix (text, prefix);
  631. }
  632. void
  633. str_fix_string (char *text)
  634. {
  635. used_class.fix_string (text);
  636. }
  637. char *
  638. str_create_key (const char *text, int case_sen)
  639. {
  640. return used_class.create_key (text, case_sen);
  641. }
  642. char *
  643. str_create_key_for_filename (const char *text, int case_sen)
  644. {
  645. return used_class.create_key_for_filename (text, case_sen);
  646. }
  647. int
  648. str_key_collate (const char *t1, const char *t2, int case_sen)
  649. {
  650. return used_class.key_collate (t1, t2, case_sen);
  651. }
  652. void
  653. str_release_key (char *key, int case_sen)
  654. {
  655. used_class.release_key (key, case_sen);
  656. }
  657. void
  658. str_msg_term_size (const char *text, int *lines, int *columns)
  659. {
  660. char *p, *tmp;
  661. char *q;
  662. char c = '\0';
  663. int width;
  664. *lines = 1;
  665. *columns = 0;
  666. tmp = g_strdup (text);
  667. p = tmp;
  668. while (TRUE)
  669. {
  670. q = strchr (p, '\n');
  671. if (q != NULL)
  672. {
  673. c = q[0];
  674. q[0] = '\0';
  675. }
  676. width = str_term_width1 (p);
  677. if (width > *columns)
  678. *columns = width;
  679. if (q == NULL)
  680. break;
  681. q[0] = c;
  682. p = q + 1;
  683. (*lines)++;
  684. }
  685. g_free (tmp);
  686. }
  687. /* --------------------------------------------------------------------------------------------- */
  688. char *
  689. strrstr_skip_count (const char *haystack, const char *needle, size_t skip_count)
  690. {
  691. char *semi;
  692. ssize_t len;
  693. len = strlen (haystack);
  694. do
  695. {
  696. semi = g_strrstr_len (haystack, len, needle);
  697. if (semi == NULL)
  698. return NULL;
  699. len = semi - haystack - 1;
  700. }
  701. while (skip_count-- != 0);
  702. return semi;
  703. }
  704. /* --------------------------------------------------------------------------------------------- */
  705. /* Interprete string as a non-negative decimal integer, optionally multiplied by various values.
  706. *
  707. * @param str input value
  708. * @param invalid set to TRUE if "str" does not represent a number in this format
  709. *
  710. * @return non-integer representation of "str", 0 in case of error.
  711. */
  712. uintmax_t
  713. parse_integer (const char *str, gboolean * invalid)
  714. {
  715. uintmax_t n;
  716. char *suffix;
  717. strtol_error_t e;
  718. e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
  719. if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
  720. {
  721. uintmax_t multiplier;
  722. multiplier = parse_integer (suffix + 1, invalid);
  723. if (multiplier != 0 && n * multiplier / multiplier != n)
  724. {
  725. *invalid = TRUE;
  726. return 0;
  727. }
  728. n *= multiplier;
  729. }
  730. else if (e != LONGINT_OK)
  731. {
  732. *invalid = TRUE;
  733. n = 0;
  734. }
  735. return n;
  736. }
  737. /* --------------------------------------------------------------------------------------------- */