2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
24 * (c) Copyright 1995 Digital Equipment Corporation.
25 * (c) Copyright 1995 Hewlett-Packard Company.
26 * (c) Copyright 1995 International Business Machines Corp.
27 * (c) Copyright 1995 Sun Microsystems, Inc.
28 * (c) Copyright 1995 Novell, Inc.
29 * (c) Copyright 1995 FUJITSU LIMITED.
30 * (c) Copyright 1995 Hitachi.
32 * MsgCat.c - public interfaces for the Cached Message Catalog Service
34 * $TOG: MsgCat.c /main/4 1999/07/02 14:02:03 mgreess $
41 #include <X11/Intrinsic.h>
42 #include <Dt/MsgCatP.h>
43 #include <DtSvcLock.h>
45 typedef struct _dt_msg_cache
52 struct _dt_msg_cache *next;
55 static _DtMsgCache *catalog_message_caches = NULL;
57 static _DtMsgCache *get_msg_cache(nl_catd catd)
59 #define INITIAL_NMSGS_PER_SET 300
60 #define INITIAL_NSETS 50
64 for (c=catalog_message_caches; NULL!=c; c=c->next)
65 if (catd == c->catd) return c;
67 c = (_DtMsgCache*) XtMalloc(sizeof(_DtMsgCache));
68 c->cached_msgs = NULL;
69 c->nmsgs_per_set = INITIAL_NMSGS_PER_SET;
70 c->nsets = INITIAL_NSETS;
72 c->next = catalog_message_caches;
73 catalog_message_caches = c;
79 * Wrapper around catgets -- this makes sure the message string is saved
80 * in a safe location; so repeated calls to catgets() do not overwrite
81 * the catgets() internal buffer. This has been a problem on HP systems.
83 char *_DtCatgetsCached(nl_catd catd, int set, int num, char *dflt)
88 message = catgets(catd, set, num, dflt);
95 /* convert to a zero based index */
101 c = get_msg_cache(catd);
104 message = catgets(catd, set, num, dflt);
108 if (NULL == c->cached_msgs)
110 size = sizeof(char**) * c->nsets;
111 c->cached_msgs = (char***) XtMalloc(size);
112 memset((char*) c->cached_msgs, 0, size);
114 else if (setIdx >= c->nsets)
116 for (multiplier=2; setIdx > multiplier*c->nsets; multiplier++) {}
117 size = sizeof(char**) * c->nsets;
119 (char***) XtRealloc((char*) c->cached_msgs, multiplier*size);
120 memset((char*) (c->cached_msgs + size), 0, multiplier*size);
121 c->nsets *= multiplier;
124 if (NULL == c->cached_msgs[setIdx])
126 size = sizeof(char*) * c->nmsgs_per_set;
127 c->cached_msgs[setIdx] = (char**) XtMalloc(size);
128 memset((char*) c->cached_msgs[setIdx], 0, size);
130 else if (numIdx >= c->nmsgs_per_set)
132 for (multiplier=2; numIdx > multiplier*c->nsets; multiplier++) {}
133 size = sizeof(char*) * c->nmsgs_per_set;
135 for (i=0; i<c->nmsgs_per_set; i++)
137 if (NULL != c->cached_msgs[i])
140 (char**) XtRealloc((char*)c->cached_msgs[i], multiplier*size);
141 memset((char*) (c->cached_msgs[i] + size), 0, multiplier*size);
144 c->nmsgs_per_set *= multiplier;
147 setptr = c->cached_msgs[setIdx];
148 if (NULL == setptr[numIdx])
149 setptr[numIdx] = strdup(catgets(catd, set, num, dflt));
151 message = setptr[numIdx];
153 _DtSvcProcessUnlock();