dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtstyle / I18nUtil.c
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 /*+++++++++++++++++++++++++++++++++++++++*/
45 /* include files                         */
46 /*+++++++++++++++++++++++++++++++++++++++*/
47
48
49 #include "I18nUtil.h"
50
51 /*+++++++++++++++++++++++++++++++++++++++*/
52 /* include extern functions              */
53 /*+++++++++++++++++++++++++++++++++++++++*/
54
55 /*+++++++++++++++++++++++++++++++++++++++*/
56 /* Local #defines                        */
57 /*+++++++++++++++++++++++++++++++++++++++*/
58
59 #define  TAG_END_CHAR            ':'
60
61
62 /*+++++++++++++++++++++++++++++++++++++++*/
63 /* Internal Functions                    */
64 /*+++++++++++++++++++++++++++++++++++++++*/
65
66 static char *trim_line (char * );
67
68 /*+++++++++++++++++++++++++++++++++++++++*/
69 /* Internal Variables                    */
70 /*+++++++++++++++++++++++++++++++++++++++*/
71  
72         /* ********  file reading  ******** */
73
74 static int      tag_line_num = 0;
75 static char     *tag_linebuf = NULL;
76 static char     *tag_file = NULL;
77
78
79 void    start_tag_line(
80     char  *fname
81 )
82 {
83     if (fname) {
84         if (!tag_linebuf)
85             tag_linebuf = (char *) XtCalloc(BUFSIZ, sizeof(char));
86         tag_linebuf[0] = 0;
87         tag_file = fname;
88     } else {
89         if (tag_linebuf) 
90             XtFree(tag_linebuf);
91         tag_linebuf = tag_file = 0;
92     }
93     tag_line_num = 0;
94 }
95
96 int     read_tag_line(
97     FILE        *fp,
98     char        **tagp, 
99     char        **valp
100 )
101 {
102     char        *lp, *lp2;
103
104     while (fgets(lp = tag_linebuf, BUFSIZ, fp)) {
105         tag_line_num++;
106         skip_white(lp);         /* lp = trim_line(lp); */
107         if (!*lp || *lp == '\n' || is_comment_char(*lp))
108             continue;
109         if (!(lp2 = strchr(lp, TAG_END_CHAR))) {
110             continue;
111         }
112         *lp2++ = 0;
113         lp2 = trim_line(lp2);
114
115         *tagp = lp;
116         *valp = *lp2 ? lp2 : 0;
117         return tag_line_num;
118     }
119     *tagp = *valp = 0;
120
121     return (ferror(fp)) ? -1 : 0;
122 }
123
124         /* ********  string manupilation  ******** */
125
126 static  char  *
127 trim_line(
128      char *ptr
129 )
130 {
131     register char       *lastp;
132
133     skip_white(ptr);
134     for (lastp = ptr + strlen(ptr) - 1;
135         lastp >= ptr && (is_white(*lastp) || *lastp == '\n'); lastp--) ;
136     *(lastp + 1) = 0;
137     return ptr;         /* return lastp > ptr ? ptr : NULL; */
138 }
139
140 int     
141 str_to_int(
142     char *ptr,
143     int  *val
144 )
145 {
146     int base;
147     char *pp;
148
149     /* if (!ptr || !*ptr || !val)       return(False); */
150     *val = 0;
151     base = ptr[0] == '0' ? (((ptr[1] & 0xdf) == 'X') ? 16 : 8) : 10;
152     *val = strtol(ptr, &pp, base);
153     if (!pp || *pp)     return(False);
154     return(True);
155 }
156
157
158 Bool    
159 str_to_bool(
160     char  *ptr,
161     Bool  def_val
162 )
163 {
164     if (!ptr || !*ptr)  return def_val;
165     skip_white(ptr);
166
167     switch (*ptr) {             /* true/false , 1/0 , yes/no , on/off */
168         case '1':
169         case 'T': case 't':
170         case 'Y': case 'y':
171                 def_val = True; break;
172
173         case '0':
174         case 'F': case 'f':
175         case 'N': case 'n':
176                 def_val = False; break;
177
178         case 'O': case 'o':
179                 if (ptr[1] == 'N' || ptr[1] == 'n')
180                     def_val = True;
181                 else if (ptr[1] == 'F' || ptr[1] == 'f')
182                     def_val = False;
183                 break;
184     }
185     return def_val;
186 }
187