I18nUtil.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: I18nUtil.c /main/1 1996/03/25 00:52:03 pascale $ */
  24. /*
  25. * (c) Copyright 1996 Digital Equipment Corporation.
  26. * (c) Copyright 1996 Hewlett-Packard Company.
  27. * (c) Copyright 1996 International Business Machines Corp.
  28. * (c) Copyright 1996 Sun Microsystems, Inc.
  29. * (c) Copyright 1996 Novell, Inc.
  30. * (c) Copyright 1996 FUJITSU LIMITED.
  31. * (c) Copyright 1996 Hitachi.
  32. */
  33. /************************************<+>*************************************
  34. ****************************************************************************
  35. **
  36. ** File: I18nEnv.c
  37. **
  38. ** Description: Contains utility functions for the Dtstyle I18N component.
  39. **
  40. **
  41. ****************************************************************************
  42. ************************************<+>*************************************/
  43. /*+++++++++++++++++++++++++++++++++++++++*/
  44. /* include files */
  45. /*+++++++++++++++++++++++++++++++++++++++*/
  46. #include "I18nUtil.h"
  47. /*+++++++++++++++++++++++++++++++++++++++*/
  48. /* include extern functions */
  49. /*+++++++++++++++++++++++++++++++++++++++*/
  50. /*+++++++++++++++++++++++++++++++++++++++*/
  51. /* Local #defines */
  52. /*+++++++++++++++++++++++++++++++++++++++*/
  53. #define TAG_END_CHAR ':'
  54. /*+++++++++++++++++++++++++++++++++++++++*/
  55. /* Internal Functions */
  56. /*+++++++++++++++++++++++++++++++++++++++*/
  57. static char *trim_line (char * );
  58. /*+++++++++++++++++++++++++++++++++++++++*/
  59. /* Internal Variables */
  60. /*+++++++++++++++++++++++++++++++++++++++*/
  61. /* ******** file reading ******** */
  62. static int tag_line_num = 0;
  63. static char *tag_linebuf = NULL;
  64. static char *tag_file = NULL;
  65. void start_tag_line(
  66. char *fname
  67. )
  68. {
  69. if (fname) {
  70. if (!tag_linebuf)
  71. tag_linebuf = (char *) XtCalloc(BUFSIZ, sizeof(char));
  72. tag_linebuf[0] = 0;
  73. tag_file = fname;
  74. } else {
  75. if (tag_linebuf)
  76. XtFree(tag_linebuf);
  77. tag_linebuf = tag_file = 0;
  78. }
  79. tag_line_num = 0;
  80. }
  81. int read_tag_line(
  82. FILE *fp,
  83. char **tagp,
  84. char **valp
  85. )
  86. {
  87. char *lp, *lp2;
  88. while (fgets(lp = tag_linebuf, BUFSIZ, fp)) {
  89. tag_line_num++;
  90. skip_white(lp); /* lp = trim_line(lp); */
  91. if (!*lp || *lp == '\n' || is_comment_char(*lp))
  92. continue;
  93. if (!(lp2 = strchr(lp, TAG_END_CHAR))) {
  94. continue;
  95. }
  96. *lp2++ = 0;
  97. lp2 = trim_line(lp2);
  98. *tagp = lp;
  99. *valp = *lp2 ? lp2 : 0;
  100. return tag_line_num;
  101. }
  102. *tagp = *valp = 0;
  103. return (ferror(fp)) ? -1 : 0;
  104. }
  105. /* ******** string manupilation ******** */
  106. static char *
  107. trim_line(
  108. char *ptr
  109. )
  110. {
  111. char *lastp;
  112. skip_white(ptr);
  113. for (lastp = ptr + strlen(ptr) - 1;
  114. lastp >= ptr && (is_white(*lastp) || *lastp == '\n'); lastp--) ;
  115. *(lastp + 1) = 0;
  116. return ptr; /* return lastp > ptr ? ptr : NULL; */
  117. }
  118. int
  119. str_to_int(
  120. char *ptr,
  121. int *val
  122. )
  123. {
  124. int base;
  125. char *pp;
  126. /* if (!ptr || !*ptr || !val) return(False); */
  127. *val = 0;
  128. base = ptr[0] == '0' ? (((ptr[1] & 0xdf) == 'X') ? 16 : 8) : 10;
  129. *val = strtol(ptr, &pp, base);
  130. if (!pp || *pp) return(False);
  131. return(True);
  132. }
  133. Bool
  134. str_to_bool(
  135. char *ptr,
  136. Bool def_val
  137. )
  138. {
  139. if (!ptr || !*ptr) return def_val;
  140. skip_white(ptr);
  141. switch (*ptr) { /* true/false , 1/0 , yes/no , on/off */
  142. case '1':
  143. case 'T': case 't':
  144. case 'Y': case 'y':
  145. def_val = True; break;
  146. case '0':
  147. case 'F': case 'f':
  148. case 'N': case 'n':
  149. def_val = False; break;
  150. case 'O': case 'o':
  151. if (ptr[1] == 'N' || ptr[1] == 'n')
  152. def_val = True;
  153. else if (ptr[1] == 'F' || ptr[1] == 'f')
  154. def_val = False;
  155. break;
  156. }
  157. return def_val;
  158. }