2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_utility.c
32 #include "slang_utility.h"
34 char *slang_string_concat (char *dst
, const char *src
)
36 return _mesa_strcpy (dst
+ _mesa_strlen (dst
), src
);
42 slang_string_init (slang_string
*self
)
47 self
->fail
= GL_FALSE
;
51 slang_string_free (slang_string
*self
)
53 if (self
->data
!= NULL
)
54 _mesa_free (self
->data
);
58 slang_string_reset (slang_string
*self
)
61 self
->fail
= GL_FALSE
;
65 grow (slang_string
*self
, GLuint size
)
69 if (size
> self
->capacity
) {
70 /* do not overflow 32-bit range */
71 assert (size
< 0x80000000);
73 self
->data
= (char *) (_mesa_realloc (self
->data
, self
->capacity
, size
* 2));
74 self
->capacity
= size
* 2;
75 if (self
->data
== NULL
) {
85 slang_string_push (slang_string
*self
, const slang_string
*str
)
91 if (grow (self
, self
->length
+ str
->length
)) {
92 _mesa_memcpy (&self
->data
[self
->length
], str
->data
, str
->length
);
93 self
->length
+= str
->length
;
98 slang_string_pushc (slang_string
*self
, const char c
)
100 if (grow (self
, self
->length
+ 1)) {
101 self
->data
[self
->length
] = c
;
107 slang_string_pushs (slang_string
*self
, const char *cstr
, GLuint len
)
109 if (grow (self
, self
->length
+ len
)) {
110 _mesa_memcpy (&self
->data
[self
->length
], cstr
, len
);
116 slang_string_pushi (slang_string
*self
, GLint i
)
120 _mesa_sprintf (buffer
, "%d", i
);
121 slang_string_pushs (self
, buffer
, strlen (buffer
));
125 slang_string_cstr (slang_string
*self
)
127 if (grow (self
, self
->length
+ 1))
128 self
->data
[self
->length
] = '\0';
132 /* slang_atom_pool */
134 void slang_atom_pool_construct (slang_atom_pool
*pool
)
138 for (i
= 0; i
< SLANG_ATOM_POOL_SIZE
; i
++)
139 pool
->entries
[i
] = NULL
;
142 void slang_atom_pool_destruct (slang_atom_pool
*pool
)
146 for (i
= 0; i
< SLANG_ATOM_POOL_SIZE
; i
++)
148 slang_atom_entry
*entry
;
150 entry
= pool
->entries
[i
];
151 while (entry
!= NULL
)
153 slang_atom_entry
*next
;
156 slang_alloc_free (entry
->id
);
157 slang_alloc_free (entry
);
163 slang_atom
slang_atom_pool_atom (slang_atom_pool
*pool
, const char *id
)
167 slang_atom_entry
**entry
;
174 hash
= (hash
<< 4) + (GLuint
) *p
++;
175 g
= hash
& 0xf0000000;
180 hash
%= SLANG_ATOM_POOL_SIZE
;
182 entry
= &pool
->entries
[hash
];
183 while (*entry
!= NULL
)
185 if (slang_string_compare ((**entry
).id
, id
) == 0)
186 return (slang_atom
) (**entry
).id
;
187 entry
= &(**entry
).next
;
190 *entry
= (slang_atom_entry
*) slang_alloc_malloc (sizeof (slang_atom_entry
));
192 return SLANG_ATOM_NULL
;
194 (**entry
).next
= NULL
;
195 (**entry
).id
= slang_string_duplicate (id
);
196 if ((**entry
).id
== NULL
)
197 return SLANG_ATOM_NULL
;
198 return (slang_atom
) (**entry
).id
;
201 const char *slang_atom_pool_id (slang_atom_pool
*pool
, slang_atom atom
)
203 return (const char *) atom
;