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