dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Managers / CatMgr.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 // $TOG: CatMgr.C /main/7 1998/04/20 12:53:56 mgreess $
24 /*      Copyright (c) 1995 FUJITSU LIMITED      */
25 /*      All Rights Reserved                     */
26
27 #include <assert.h>
28 #include <string.h>
29
30 #include <locale.h>
31 #include <stdlib.h>
32
33 #include <iostream>
34 using namespace std;
35
36 #include <Dt/Dt.h>
37 #include <Dt/EnvControlP.h>
38
39 #include "CatMgr.hh"
40
41 #define DEFAULT_MSG     1
42
43 CatMgr* CatMgr::f_msg_catalog_mgr = NULL;
44
45 CatMgr::CatMgr() : f_msg(NULL), f_default(NULL)
46 {
47 #ifdef DEBUG
48     char* nlspath = getenv("NLSPATH");
49     if (nlspath)
50         cerr << "(DEBUG) NLSPATH=" << nlspath << '\n' << flush;
51     else
52         cerr << "(WARNING) NLSPATH not specified" <<  '\n' << flush;
53 #endif
54
55     f_catd = catopen(CATALOG_PREFIX, NL_CAT_LOCALE);
56
57     // setup default message
58     if (is_open(f_catd)) {
59 #ifdef DEBUG    
60         cerr << "(DEBUG) catopen succeeded." << '\n' << flush;
61 #endif
62         char* msg = ::catgets(f_catd, Set_CatMgr, DEFAULT_MSG,
63                                                 "default message not found.");
64         f_default = new char[strlen(msg) + 1];
65         strcpy(f_default, msg);
66     }
67     else {
68 #ifdef DEBUG    
69         cerr << "(WARNING) catopen failed." << '\n' << flush;
70         static char* cat_not_found = (char*)"default message not found.";
71 #else
72         static char* cat_not_found = (char*)"";
73 #endif
74         f_default = new char[strlen(cat_not_found) + 1];
75         strcpy(f_default, cat_not_found);
76     }
77 #ifdef DEBUG    
78     cerr << "(DEBUG) default msg=" << f_default << '\n' << flush;
79 #endif
80
81     f_msg_catalog_mgr = this;
82 }
83
84 CatMgr::~CatMgr()
85 {
86     if (f_msg)
87         delete[] f_msg;
88
89     if (f_default)
90         delete[] f_default;
91
92     if (is_open(f_catd)) {
93         int status = catclose(f_catd);
94 #ifdef DEBUG
95         if (status < 0) {
96             cerr << "(ERROR) catclose failed." << '\n' << flush;
97             abort();
98         }
99         else {
100             cerr << "(DEBUG) catclose succeeded" << '\n' << flush;
101         }
102 #endif
103     }
104 #ifdef DEBUG
105     else {
106         cerr << "(DEBUG) catclose not attempted" << '\n' << flush;
107     }
108 #endif
109 }
110
111 char*
112 CatMgr::catgets(int set_num, int msg_num, const char* def)
113 {
114     if (f_msg)
115         delete[] f_msg;
116
117     f_msg = NULL;
118
119     if (is_open(f_catd)) {
120         char* msg;
121         if (def)
122             msg = ::catgets(f_catd, set_num, msg_num, def);
123         else
124             msg = ::catgets(f_catd, set_num, msg_num, f_default);
125 #if defined(UXPDS) && defined(GENCAT_BUG)
126         for (; *msg == ' ' || *msg == '\t'; msg++);
127 #endif
128         int msglen = strlen(msg);
129         f_msg = new char[msglen + 1];
130 #if defined(UXPDS) && defined(GENCAT_BUG)
131 #ifdef DEBUG
132     cerr << "(DEBUG) catgets msg=<" << msg << ">\n" << flush;
133 #endif
134         // trim embracing double quotes on uxpds
135         if (msglen > 1 && *(msg + msglen - 1) == '"') {
136             if (*msg == *(msg + msglen - 1)) {
137                 *(msg + msglen - 1) = '\0';
138                 msg++;
139                 msglen -= 2;
140             }
141         }
142         else if (*msg == '"') {
143             msg++;
144             msglen--;
145         }
146 #endif
147         strcpy(f_msg, msg);
148     }
149     else {
150         if (def) {
151             f_msg = new char[strlen(def) + 1];
152             strcpy(f_msg, def);
153         }
154         else {
155             f_msg =new char[strlen(f_default) + 1];
156             strcpy(f_msg, f_default);
157         }
158     }
159
160     assert( f_msg != NULL );
161
162 #ifdef DEBUG
163     cerr << "(DEBUG) retrieved msg=" << f_msg << '\n' << flush;
164 #endif
165
166     return f_msg;
167 }
168