Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtcm / dtcm / i18nxlate.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 librararies 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: i18nxlate.c /main/5 1995/11/09 13:00:30 rswiston $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <locale.h>
35 #include <time.h>
36
37 #include <LocaleXlate.h>        /* under DtHelp, for Operation constants */
38
39 #ifndef True
40 #define True 1
41 #endif
42 #ifndef False
43 #define False 0
44 #endif
45
46
47 /*
48  * Wrapper functions taken from libHelp/CEUtil.c
49  *
50  * We took these functions and renamed them because
51  * 1. Originally these are called _DtHelpCeXlate* and thus they are private
52  *    to libHelp and not exported to outside of libHelp.
53  * 2. When these functions are moved to another library, then users of these
54  *    functions would only need to link with a different library.  The caller
55  *    doesn't have to modify code.
56  */
57
58 static const char *DfltStdCharset = "ISO-8859-1";
59 static const char *DfltStdLang = "C";
60
61 static char       MyPlatform[_DtPLATFORM_MAX_LEN+1];
62 static _DtXlateDb MyDb = NULL;
63 char       MyProcess = False;
64 char       MyFirst   = True;
65 int        ExecVer;
66 int        CompVer;
67
68
69 /******************************************************************************
70  * Function:    static int OpenLcxDb ()
71  *
72  * Parameters:   none
73  *
74  * Return Value:  0: ok
75  *               -1: error
76  *
77  * errno Values:
78  *
79  * Purpose: Opens the Ce-private Lcx database
80  *
81  *****************************************************************************/
82 static int
83 OpenLcxDb (void)
84 {
85     time_t      time1  = 0;
86     time_t      time2  = 0;
87
88     /*
89      * wait 30 sec. until another thread or enter is done modifying the table
90      */
91     while (MyProcess == True) 
92       {
93         /* if time out, return */
94         if (time(&time2) == (time_t)-1)
95             return -1;
96
97         if (time1 == 0)
98             time1 = time2;
99         else if (time2 - time1 >= (time_t)30)
100             return -1;
101       }
102
103     if (MyFirst == True)
104       {
105         MyProcess = True;
106         if (_DtLcxOpenAllDbs(&MyDb) == 0 &&
107                         _DtXlateGetXlateEnv(MyDb,MyPlatform,&ExecVer,&CompVer) != 0)
108           {
109             _DtLcxCloseDb(&MyDb);
110             MyDb = NULL;
111           }
112         MyFirst = False;
113         MyProcess = False;
114       }
115
116     return (MyDb == NULL ? -1 : 0 );
117 }
118
119 /******************************************************************************
120  * Function:    int DtI18NXlateOpToStdLocale (char *operation, char *opLocale,
121  *                         char **ret_stdLocale, char **ret_stdLang, char **ret_stdSet)
122  *
123  * Parameters:
124  *              operation       Operation associated with the locale value
125  *              opLocale        An operation-specific locale string
126  *              ret_locale      Returns the std locale
127  *                              Caller must free this string.
128  *              ret_stdLang        Returns the std language & territory string.
129  *                              Caller must free this string.
130  *              ret_stdSet         Returns the std code set string.
131  *                              Caller must free this string.
132  *
133  * Return Value:
134  *
135  * Purpose:  Gets the standard locale given an operation and its locale
136  *
137  *****************************************************************************/
138 void
139 _DtI18NXlateOpToStdLocale (
140      char       *operation,
141      char       *opLocale,
142      char       **ret_stdLocale,
143      char       **ret_stdLang,
144      char       **ret_stdSet)
145 {
146     OpenLcxDb();
147
148     if (MyDb != NULL)
149       {
150         (void) _DtLcxXlateOpToStd(MyDb, MyPlatform, CompVer,
151                                 operation,opLocale,
152                                 ret_stdLocale, ret_stdLang, ret_stdSet, NULL);
153       }
154
155     /* if failed, give default values */
156     if (ret_stdLocale != NULL && *ret_stdLocale == NULL)
157     {
158         *ret_stdLocale = (char *)malloc(strlen(DfltStdLang)+strlen(DfltStdCharset)+3);
159         sprintf(*ret_stdLocale,"%s.%s",DfltStdLang,DfltStdCharset);
160     }
161     if (ret_stdLang != NULL && *ret_stdLang == NULL)
162         *ret_stdLang = (char *)strdup(DfltStdLang);
163     if (ret_stdSet != NULL && *ret_stdSet == NULL)
164         *ret_stdSet = (char *)strdup(DfltStdCharset);
165 }
166
167 /******************************************************************************
168  * Function:    int DtI18NXlateStdToOpLocale ( char *operation, char *stdLocale,
169  *                                      char *dflt_opLocale, char **ret_opLocale)
170  *
171  * Parameters:
172  *    operation         operation whose locale value will be retrieved
173  *    stdLocale         standard locale value
174  *    dflt_opLocale     operation-specific locale-value
175  *                      This is the default value used in error case
176  *    ret_opLocale      operation-specific locale-value placed here
177  *                      Caller must free this string.
178  *
179  * Return Value:
180  *
181  * Purpose: Gets an operation-specific locale string given the standard string
182  *
183  *****************************************************************************/
184 void
185 _DtI18NXlateStdToOpLocale (
186      char       *operation,
187      char       *stdLocale,
188      char       *dflt_opLocale,
189      char       **ret_opLocale)
190 {
191     OpenLcxDb();
192
193     if (MyDb != NULL)
194     {
195         (void) _DtLcxXlateStdToOp(MyDb, MyPlatform, CompVer,
196                         operation, stdLocale, NULL, NULL, NULL, ret_opLocale);
197     }
198
199     /* if translation fails, use a default value */
200     if (ret_opLocale && *ret_opLocale == NULL)
201     {
202        if (dflt_opLocale) *ret_opLocale = (char *)strdup(dflt_opLocale);
203        else if (stdLocale) *ret_opLocale = (char *)strdup(stdLocale);
204     }
205 }
206