StrToJust.c revision e120bd27
1/*
2
3Copyright 1988, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <string.h>
31#include <X11/Intrinsic.h>
32#include "Converters.h"
33#include "CharSet.h"
34
35/*
36 * Prototypes
37 */
38static void InitializeQuarks(void);
39
40/*
41 * Initialization
42 */
43static XrmQuark Qleft, Qcenter, Qright;
44static Boolean haveQuarks;
45
46/*
47 * Implementation
48 */
49static void
50InitializeQuarks(void)
51{
52  if (!haveQuarks)
53    {
54      Qleft = XrmPermStringToQuark(XtEleft);
55      Qcenter = XrmPermStringToQuark(XtEcenter);
56      Qright = XrmPermStringToQuark(XtEright);
57      haveQuarks = True;
58    }
59}
60
61/*ARGSUSED*/
62void
63XmuCvtStringToJustify(XrmValuePtr args, Cardinal *num_args,
64		      XrmValuePtr fromVal, XrmValuePtr toVal)
65{
66    static XtJustify	e;
67    XrmQuark    q;
68  char *s = (char *)fromVal->addr;
69  char name[7];
70
71  if (s == NULL)
72    return;
73
74  InitializeQuarks();
75  XmuNCopyISOLatin1Lowered(name, s, sizeof(name));
76
77  q = XrmStringToQuark(name);
78
79    toVal->size = sizeof(XtJustify);
80  toVal->addr = (XPointer)&e;
81
82  if (q == Qleft)
83    e = XtJustifyLeft;
84  else if (q == Qcenter)
85    e = XtJustifyCenter;
86  else if (q == Qright)
87    e = XtJustifyRight;
88  else
89    {
90    toVal->addr = NULL;
91    XtStringConversionWarning((char *)fromVal->addr, XtRJustify);
92    }
93}
94
95/*ARGSUSED*/
96Boolean
97XmuCvtJustifyToString(Display *dpy, XrmValue* args, Cardinal *num_args,
98		      XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
99{
100  static String buffer;
101  Cardinal size;
102
103  switch (*(XtJustify *)fromVal->addr)
104    {
105    case XtJustifyLeft:
106      buffer = XtEleft;
107      break;
108    case XtJustifyCenter:
109      buffer = XtEcenter;
110      break;
111    case XtJustifyRight:
112      buffer = XtEright;
113      break;
114    default:
115      XtWarning("Cannot convert Justify to String");
116      toVal->addr = NULL;
117      toVal->size = 0;
118      return (False);
119    }
120
121  size = strlen(buffer) + 1;
122  if (toVal->addr != NULL)
123    {
124      if (toVal->size < size)
125	{
126	  toVal->size = size;
127	  return (False);
128	}
129      strcpy((char *)toVal->addr, buffer);
130    }
131  else
132    toVal->addr = (XPointer)buffer;
133  toVal->size = sizeof(String);
134
135  return (True);
136}
137