1 /*
2  * Motif Tools Library, Version 3.1
3  * $Id: StringLstCvt.c,v 1.2 2001/09/19 02:57:18 grmcdorman Exp $
4  *
5  * Written by David Flanagan.
6  * Copyright (c) 1992-2001 by David Flanagan.
7  * All Rights Reserved.  See the file COPYRIGHT for details.
8  * This is open source software.  See the file LICENSE for details.
9  * There is no warranty for this software.  See NO_WARRANTY for details.
10  *
11  * $Log: StringLstCvt.c,v $
12  * Revision 1.2  2001/09/19 02:57:18  grmcdorman
13  * This change makes the following modifications:
14  *   A new program, printConfig, is provided. This is built by a
15  *   simple rule in the Makefile and not installed. It prints
16  *   significant defines from Xmt.tmpl.
17  *
18  *   XmtP.h is now generated from XmtP.h.in using printConfig. As
19  *   a result, code compiled outside of the Xmt Imakefiles will
20  *   have all of the Xmt.tmpl defines.
21  *
22  *   Source files are modified to use XmtP.h instead of Xmt.h.
23  *
24  *   WorkingBox.c is modified to use the new Progress widget.
25  *   It can be compiled in the old style if WORKINGBOX_USE_SCALE is
26  *   defined at compile time.
27  *
28  *   Because XmtP.h is generated dynamically, it is removed from CVS
29  *   with this check-in.
30  *
31  * Revision 1.1.1.1  2001/02/10 13:50:37  motiftools
32  * Initial import of Xmt310 to CVS
33  *
34  *
35  */
36 
37 #include <ctype.h>
38 #include <Xmt/XmtP.h>
39 #include <Xmt/ConvertersP.h>
40 
41 #define skipblanks(s) while (isspace(*s)) s++
42 
43 /*
44  * Convert a string to a NULL-terminated array of strings.
45  * Individual strings must be surrounded by double quotes, and
46  * may be optionally comma-separated.  Newlines and whitespace
47  * outside of double quotes are ignored.
48  */
49 /* ARGSUSED */
50 #if NeedFunctionPrototypes
XmtConvertStringToStringList(Display * dpy,XrmValue * args,Cardinal * num_args,XrmValue * from,XrmValue * to,XtPointer * converter_data)51 Boolean XmtConvertStringToStringList(Display *dpy,
52 				     XrmValue *args, Cardinal *num_args,
53 				     XrmValue *from, XrmValue *to,
54 				     XtPointer *converter_data)
55 #else
56 Boolean XmtConvertStringToStringList(dpy, args, num_args,
57 				     from, to, converter_data)
58 Display *dpy;
59 XrmValue *args;
60 Cardinal *num_args;
61 XrmValue *from;
62 XrmValue *to;
63 XtPointer *converter_data;
64 #endif
65 {
66     char *s = (char *)from->addr;
67     char *mark;
68     int num_items = 0;
69     int max_items = 6;
70     String *items = (String *) XtMalloc(max_items * sizeof(String));
71 
72     skipblanks(s);
73     for(;;) {
74 	if (*s == '\0') break;
75 
76 	if (*s != '"') {
77 	    XtDisplayStringConversionWarning(dpy, (char *)from->addr,
78 					     XmtRStringList);
79 	    XmtWarningMsg("XmtConvertStringToStringList", "expected",
80 			  "open quote expected.");
81 	    XtFree((char *)items);
82 	    return False;
83 	}
84 
85 	mark = ++s;
86 	while (*s && *s != '"') s++;
87 
88 	if (*s == '\0') {
89 	    XtDisplayStringConversionWarning(dpy, (char *)from->addr,
90 					     XmtRStringList);
91 	    XmtWarningMsg("XmtConvertStringToStringList", "expected",
92 			  "close quote expected.");
93 	    XtFree((char *)items);
94 	    return False;
95 	}
96 
97 	if (num_items == max_items) {
98 	    max_items *= 2;
99 	    items = (String *) XtRealloc((char *)items,
100 					 max_items * sizeof(String));
101 	}
102 
103 	items[num_items] = (String) XtMalloc((s - mark) + 1);
104 	strncpy(items[num_items], mark, (s-mark));
105 	items[num_items][s-mark] = '\0';
106 	num_items++;
107 
108 	s++;
109 	skipblanks(s);
110 	if (*s == ',') {
111 	    s++;
112 	    skipblanks(s);
113 	}
114     }
115 
116     /* NULL-terminate the array */
117     items = (String *) XtRealloc((char *)items, (num_items+1)*sizeof(String));
118     items[num_items] = NULL;
119 
120     /* do the right thing: see ConvertersP.h */
121     done(String *, items);
122 }
123 
124 /* ARGSUSED */
125 #if NeedFunctionPrototypes
XmtDestroyStringList(XtAppContext app,XrmValue * to,XtPointer converter_data,XrmValue * args,Cardinal * num_args)126 static void XmtDestroyStringList(XtAppContext app, XrmValue *to,
127 				 XtPointer converter_data,
128 				 XrmValue *args, Cardinal *num_args)
129 #else
130 static void XmtDestroyStringList(app, to, converter_data, args, num_args)
131 XtAppContext app;
132 XrmValue *to;
133 XtPointer converter_data;
134 XrmValue *args;
135 Cardinal *num_args;
136 #endif
137 {
138     String *items = *(String **) to->addr;
139     int i;
140 
141     for(i=0; items[i] != NULL; i++)
142 	XtFree(items[i]);
143     XtFree((char *)items);
144 }
145 
146 #if NeedFunctionPrototypes
XmtRegisterStringListConverter(void)147 void XmtRegisterStringListConverter(void)
148 #else
149 void XmtRegisterStringListConverter()
150 #endif
151 {
152     static Boolean registered = False;
153 
154     if (!registered) {
155 	registered = True;
156 	XtSetTypeConverter(XtRString, XmtRStringList,
157 			   XmtConvertStringToStringList, NULL, 0,
158 			   XtCacheAll | XtCacheRefCount,
159 			   XmtDestroyStringList);
160     }
161 }
162