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