ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

slang_utility.c
Go to the documentation of this file.
00001 /*
00002  * Mesa 3-D graphics library
00003  * Version:  6.5.3
00004  *
00005  * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a
00008  * copy of this software and associated documentation files (the "Software"),
00009  * to deal in the Software without restriction, including without limitation
00010  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011  * and/or sell copies of the Software, and to permit persons to whom the
00012  * Software is furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included
00015  * in all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00018  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00020  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023  */
00024 
00031 #include "main/imports.h"
00032 #include "slang_utility.h"
00033 #include "slang_mem.h"
00034 
00035 char *
00036 slang_string_concat (char *dst, const char *src)
00037 {
00038    return _mesa_strcpy (dst + _mesa_strlen (dst), src);
00039 }
00040 
00041 
00042 /* slang_string */
00043 
00044 GLvoid
00045 slang_string_init (slang_string *self)
00046 {
00047    self->data = NULL;
00048    self->capacity = 0;
00049    self->length = 0;
00050    self->fail = GL_FALSE;
00051 }
00052 
00053 GLvoid
00054 slang_string_free (slang_string *self)
00055 {
00056    if (self->data != NULL)
00057       _mesa_free (self->data);
00058 }
00059 
00060 GLvoid
00061 slang_string_reset (slang_string *self)
00062 {
00063    self->length = 0;
00064    self->fail = GL_FALSE;
00065 }
00066 
00067 static GLboolean
00068 grow (slang_string *self, GLuint size)
00069 {
00070    if (self->fail)
00071       return GL_FALSE;
00072    if (size > self->capacity) {
00073       /* do not overflow 32-bit range */
00074       assert (size < 0x80000000);
00075 
00076       self->data = (char *) (_mesa_realloc (self->data, self->capacity, size * 2));
00077       self->capacity = size * 2;
00078       if (self->data == NULL) {
00079          self->capacity = 0;
00080          self->fail = GL_TRUE;
00081          return GL_FALSE;
00082       }
00083    }
00084    return GL_TRUE;
00085 }
00086 
00087 GLvoid
00088 slang_string_push (slang_string *self, const slang_string *str)
00089 {
00090    if (str->fail) {
00091       self->fail = GL_TRUE;
00092       return;
00093    }
00094    if (grow (self, self->length + str->length)) {
00095       _mesa_memcpy (&self->data[self->length], str->data, str->length);
00096       self->length += str->length;
00097    }
00098 }
00099 
00100 GLvoid
00101 slang_string_pushc (slang_string *self, const char c)
00102 {
00103    if (grow (self, self->length + 1)) {
00104       self->data[self->length] = c;
00105       self->length++;
00106    }
00107 }
00108 
00109 GLvoid
00110 slang_string_pushs (slang_string *self, const char *cstr, GLuint len)
00111 {
00112    if (grow (self, self->length + len)) {
00113       _mesa_memcpy (&self->data[self->length], cstr, len);
00114       self->length += len;
00115    }
00116 }
00117 
00118 GLvoid
00119 slang_string_pushi (slang_string *self, GLint i)
00120 {
00121    char buffer[12];
00122 
00123    _mesa_sprintf (buffer, "%d", i);
00124    slang_string_pushs (self, buffer, strlen (buffer));
00125 }
00126 
00127 const char *
00128 slang_string_cstr (slang_string *self)
00129 {
00130    if (grow (self, self->length + 1))
00131       self->data[self->length] = '\0';
00132    return self->data;
00133 }
00134 
00135 /* slang_atom_pool */
00136 
00137 void
00138 slang_atom_pool_construct(slang_atom_pool * pool)
00139 {
00140    GLuint i;
00141 
00142    for (i = 0; i < SLANG_ATOM_POOL_SIZE; i++)
00143       pool->entries[i] = NULL;
00144 }
00145 
00146 void
00147 slang_atom_pool_destruct (slang_atom_pool * pool)
00148 {
00149    GLuint i;
00150 
00151    for (i = 0; i < SLANG_ATOM_POOL_SIZE; i++) {
00152       slang_atom_entry * entry;
00153         
00154       entry = pool->entries[i];
00155       while (entry != NULL) {
00156          slang_atom_entry *next = entry->next;
00157          _slang_free(entry->id);
00158          _slang_free(entry);
00159          entry = next;
00160       }
00161    }
00162 }
00163 
00164 /*
00165  * Search the atom pool for an atom with a given name.
00166  * If atom is not found, create and add it to the pool.
00167  * Returns ATOM_NULL if the atom was not found and the function failed
00168  * to create a new atom.
00169  */
00170 slang_atom
00171 slang_atom_pool_atom(slang_atom_pool * pool, const char * id)
00172 {
00173    GLuint hash;
00174    const char * p = id;
00175    slang_atom_entry ** entry;
00176 
00177    /* Hash a given string to a number in the range [0, ATOM_POOL_SIZE). */
00178    hash = 0;
00179    while (*p != '\0') {
00180       GLuint g;
00181 
00182       hash = (hash << 4) + (GLuint) (*p++);
00183       g = hash & 0xf0000000;
00184       if (g != 0)
00185          hash ^= g >> 24;
00186       hash &= ~g;
00187    }
00188    hash %= SLANG_ATOM_POOL_SIZE;
00189 
00190    /* Now the hash points to a linked list of atoms with names that
00191     * have the same hash value.  Search the linked list for a given
00192     * name.
00193     */
00194    entry = &pool->entries[hash];
00195    while (*entry != NULL) {
00196       /* If the same, return the associated atom. */
00197       if (slang_string_compare((**entry).id, id) == 0)
00198          return (slang_atom) (**entry).id;
00199       /* Grab the next atom in the linked list. */
00200       entry = &(**entry).next;
00201    }
00202 
00203    /* Okay, we have not found an atom. Create a new entry for it.
00204     * Note that the <entry> points to the last entry's <next> field.
00205     */
00206    *entry = (slang_atom_entry *) _slang_alloc(sizeof(slang_atom_entry));
00207    if (*entry == NULL)
00208       return SLANG_ATOM_NULL;
00209 
00210    /* Initialize a new entry. Because we'll need the actual name of
00211     * the atom, we use the pointer to this string as an actual atom's
00212     * value.
00213     */
00214    (**entry).next = NULL;
00215    (**entry).id = _slang_strdup(id);
00216    if ((**entry).id == NULL)
00217       return SLANG_ATOM_NULL;
00218    return (slang_atom) (**entry).id;
00219 }
00220 
00224 const char *
00225 slang_atom_pool_id(slang_atom_pool * pool, slang_atom atom)
00226 {
00227    return (const char *) (atom);
00228 }

Generated on Sat May 26 2012 04:19:28 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.