1 /*
2 * x11_atom.h - atoms (aka XrmQuark) for faster database handling
3 *
4 * $Id: sdl_atom.c,v 1.7 2006/02/24 21:42:00 fzago Exp $
5 *
6 * Program XBLAST
7 * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2; or (at your option)
12 * any later version
13 *
14 * This program is distributed in the hope that it will be entertaining,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 * Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "xblast.h"
25
26 #include "sdl_common.h"
27
28 #define MAX_ATOMS 5000
29
30 typedef struct
31 {
32 XBAtom atom;
33 const char *name;
34 } AtomsTableStr;
35
36 static AtomsTableStr atomsTable[MAX_ATOMS];
37 static int atomsCount;
38
39 static int
AtomsTableCompare(const void * a,const void * b)40 AtomsTableCompare (const void *a, const void *b)
41 {
42 /* printf("comparing %s with %s\n", ((AtomsTableStr *)a)->name, ((AtomsTableStr *)b)->name); */
43 return strcmp (((const AtomsTableStr *) a)->name, ((const AtomsTableStr *) b)->name);
44 }
45
46 /*
47 * Initialize Atoms
48 */
49 XBBool
GUI_InitAtoms(void)50 GUI_InitAtoms (void)
51 {
52
53 /* atomsTable = calloc( MAX_ATOMS, sizeof(AtomsTableStr *) ); */
54 /* if(atomsTable == NULL) { */
55 /* return XBFalse; */
56 /* } */
57 atomsCount = 0;
58 return XBTrue;
59 } /* InitAtoms */
60
61 /*
62 * conversion string to atom
63 */
64 XBAtom
GUI_StringToAtom(const char * string)65 GUI_StringToAtom (const char *string)
66 {
67
68 AtomsTableStr key;
69 XBAtom atom;
70 AtomsTableStr **atom_ptr;
71 assert (NULL != string);
72
73 key.name = string;
74
75 /* printf("bsearch(%s) cnt = %d\n", string, atomsCount); */
76 atom_ptr = bsearch (&key, atomsTable, atomsCount, sizeof (AtomsTableStr), AtomsTableCompare);
77 if (atom_ptr) {
78 atom = ((AtomsTableStr *) atom_ptr)->atom;
79 }
80 else {
81 /* printf("failed!!!!!!\n"); */
82 atom = atomsCount + 1;
83 key.atom = atom;
84 atomsTable[atomsCount].atom = atom;
85 atomsTable[atomsCount].name = strdup (string);
86 atomsCount++;
87 qsort (atomsTable, atomsCount, sizeof (AtomsTableStr), AtomsTableCompare);
88 }
89
90 return atom;
91 } /* GUI_StringToAtom */
92
93 /*
94 * formatted string to atom
95 */
96 XBAtom
GUI_FormatToAtom(const char * fmt,...)97 GUI_FormatToAtom (const char *fmt, ...)
98 {
99 XBAtom atom;
100 char tmp[256];
101 va_list argList;
102
103 assert (NULL != fmt);
104 /* formatting */
105 va_start (argList, fmt);
106 vsprintf (tmp, fmt, argList);
107 va_end (argList);
108 /* conversion */
109 atom = GUI_StringToAtom (tmp);
110 return atom;
111 } /* GUI_FormatToAtom */
112
113 /*
114 * convert int to atom
115 */
116 XBAtom
GUI_IntToAtom(int value)117 GUI_IntToAtom (int value)
118 {
119 return GUI_FormatToAtom ("%d", value);
120 } /* GUI_IntToAtom */
121
122 /*
123 * conversion atom to string
124 */
125 const char *
GUI_AtomToString(XBAtom atom)126 GUI_AtomToString (XBAtom atom)
127 {
128 int i;
129 for (i = 0; i < atomsCount; i++) {
130 if (atomsTable[i].atom == atom) {
131 return atomsTable[i].name;
132 }
133 }
134 return NULL;
135
136 /* return atomsTable[atom]; */
137 } /* GUI_StringToAtom */
138
139 /*
140 * convert atom int or -1
141 */
142 int
GUI_AtomToInt(XBAtom atom)143 GUI_AtomToInt (XBAtom atom)
144 {
145
146 const char *s;
147 int value;
148 s = GUI_AtomToString (atom);
149 if (NULL == s) {
150 return -1;
151 }
152 if (1 != sscanf (s, "%d", &value)) {
153 return -1;
154 }
155 return value;
156
157 } /* GUI_AtomToInt */
158
159 /*
160 * end of file x11_atom.c
161 */
162