#define LONGSTRING __extension__ in imports.h and use it to silence gcc
[mesa.git] / src / mesa / shader / slang / slang_utility.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.6
4 *
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
26 * \file slang_utility.c
27 * slang utilities
28 * \author Michal Krol
29 */
30
31 #include "imports.h"
32 #include "slang_utility.h"
33
34 char *slang_string_concat (char *dst, const char *src)
35 {
36 return _mesa_strcpy (dst + _mesa_strlen (dst), src);
37 }
38
39 /* slang_string */
40
41 GLvoid
42 slang_string_init (slang_string *self)
43 {
44 self->data = NULL;
45 self->capacity = 0;
46 self->length = 0;
47 self->fail = GL_FALSE;
48 }
49
50 GLvoid
51 slang_string_free (slang_string *self)
52 {
53 if (self->data != NULL)
54 _mesa_free (self->data);
55 }
56
57 GLvoid
58 slang_string_reset (slang_string *self)
59 {
60 self->length = 0;
61 self->fail = GL_FALSE;
62 }
63
64 static GLboolean
65 grow (slang_string *self, GLuint size)
66 {
67 if (self->fail)
68 return GL_FALSE;
69 if (size > self->capacity) {
70 /* do not overflow 32-bit range */
71 assert (size < 0x80000000);
72
73 self->data = (char *) (_mesa_realloc (self->data, self->capacity, size * 2));
74 self->capacity = size * 2;
75 if (self->data == NULL) {
76 self->capacity = 0;
77 self->fail = GL_TRUE;
78 return GL_FALSE;
79 }
80 }
81 return GL_TRUE;
82 }
83
84 GLvoid
85 slang_string_push (slang_string *self, const slang_string *str)
86 {
87 if (str->fail) {
88 self->fail = GL_TRUE;
89 return;
90 }
91 if (grow (self, self->length + str->length)) {
92 _mesa_memcpy (&self->data[self->length], str->data, str->length);
93 self->length += str->length;
94 }
95 }
96
97 GLvoid
98 slang_string_pushc (slang_string *self, const char c)
99 {
100 if (grow (self, self->length + 1)) {
101 self->data[self->length] = c;
102 self->length++;
103 }
104 }
105
106 GLvoid
107 slang_string_pushs (slang_string *self, const char *cstr, GLuint len)
108 {
109 if (grow (self, self->length + len)) {
110 _mesa_memcpy (&self->data[self->length], cstr, len);
111 self->length += len;
112 }
113 }
114
115 GLvoid
116 slang_string_pushi (slang_string *self, GLint i)
117 {
118 char buffer[12];
119
120 _mesa_sprintf (buffer, "%d", i);
121 slang_string_pushs (self, buffer, strlen (buffer));
122 }
123
124 const char *
125 slang_string_cstr (slang_string *self)
126 {
127 if (grow (self, self->length + 1))
128 self->data[self->length] = '\0';
129 return self->data;
130 }
131
132 /* slang_atom_pool */
133
134 void slang_atom_pool_construct (slang_atom_pool *pool)
135 {
136 GLuint i;
137
138 for (i = 0; i < SLANG_ATOM_POOL_SIZE; i++)
139 pool->entries[i] = NULL;
140 }
141
142 void slang_atom_pool_destruct (slang_atom_pool *pool)
143 {
144 GLuint i;
145
146 for (i = 0; i < SLANG_ATOM_POOL_SIZE; i++)
147 {
148 slang_atom_entry *entry;
149
150 entry = pool->entries[i];
151 while (entry != NULL)
152 {
153 slang_atom_entry *next;
154
155 next = entry->next;
156 slang_alloc_free (entry->id);
157 slang_alloc_free (entry);
158 entry = next;
159 }
160 }
161 }
162
163 slang_atom slang_atom_pool_atom (slang_atom_pool *pool, const char *id)
164 {
165 GLuint hash;
166 const char *p = id;
167 slang_atom_entry **entry;
168
169 hash = 0;
170 while (*p != '\0')
171 {
172 GLuint g;
173
174 hash = (hash << 4) + (GLuint) *p++;
175 g = hash & 0xf0000000;
176 if (g != 0)
177 hash ^= g >> 24;
178 hash &= ~g;
179 }
180 hash %= SLANG_ATOM_POOL_SIZE;
181
182 entry = &pool->entries[hash];
183 while (*entry != NULL)
184 {
185 if (slang_string_compare ((**entry).id, id) == 0)
186 return (slang_atom) (**entry).id;
187 entry = &(**entry).next;
188 }
189
190 *entry = (slang_atom_entry *) slang_alloc_malloc (sizeof (slang_atom_entry));
191 if (*entry == NULL)
192 return SLANG_ATOM_NULL;
193
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;
199 }
200
201 const char *slang_atom_pool_id (slang_atom_pool *pool, slang_atom atom)
202 {
203 return (const char *) atom;
204 }
205