1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* string.c -- String pool for Khepera
* Created: Wed Dec 21 21:32:34 1994 by faith@cs.unc.edu
* Revised: Tue May 20 14:38:20 1997 by faith@acm.org
* Copyright 1994, 1995, 1996, 1997 Rickard E. Faith (faith@acm.org)
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: string.c,v 1.10 1997/05/20 21:30:27 faith Exp $
*
* \section{String Pool Routines}
*
* \intro These routines provide support for string pool objects. In
* general, only the |str_find| and |str_findn| functions will be used.
* This function takes a pointer to a null-terminated string and returns a
* pointer to another null-terminated string which contains the same
* information. The pointer returned will be identical for all identical
* strings. Memory for string storage is automatically reclaimed at
* program termination on systems that support |atexit| or |on_exit|.
*
*/
#include "maaP.h"
static str_Pool global;
typedef struct poolInfo {
mem_String string;
hsh_HashTable hash;
} *poolInfo;
/* \doc |str_pool_create| initialized a string pool object. */
str_Pool str_pool_create( void )
{
poolInfo pool = xmalloc( sizeof( struct poolInfo ) );
pool->string = mem_create_strings();
pool->hash = hsh_create( NULL, NULL );
return pool;
}
/* \doc |str_pool_destroy| destroys the string pool object, |pool|, and all
memory associated with it. */
void str_pool_destroy( str_Pool pool )
{
poolInfo p = (poolInfo)pool;
if (!p || !p->string || !p->hash)
err_fatal( __FUNCTION__, "String pool improperly initialized\n" );
mem_destroy_strings( p->string );
hsh_destroy( p->hash );
xfree( p ); /* terminal */
}
/* \doc |str_pool_exists| returns non-zero if the string, |s|, is already
in the string pool, |pool|. */
int str_pool_exists( str_Pool pool, const char *s )
{
const char *datum;
poolInfo p = (poolInfo)pool;
if ((datum = hsh_retrieve( p->hash, s ))) return 1;
return 0;
}
/* \doc |str_pool_find| looks up the string, |s|, in the memory associated
with the string pool object, |pool|. If the string is found, a pointer
to the previously stored string is returned. Otherwise, the string is
copied into string pool memory, and a pointer to the newly allocated
memory is returned. */
const char *str_pool_find( str_Pool pool, const char *s )
{
const char *datum;
poolInfo p = (poolInfo)pool;
if ((datum = hsh_retrieve( p->hash, s ))) return datum;
datum = mem_strcpy( p->string, s );
hsh_insert( p->hash, datum, datum );
return datum;
}
/* \doc |str_pool_copy| returns a copy of the string, |s|, using memory
from the string pool object, |pool|. This can be used for data that is
known to be unique. No checks are made for uniqueness, however; and a
pointer to the string is not placed in the hash table. */
const char *str_pool_copy( str_Pool pool, const char *s )
{
poolInfo p = (poolInfo)pool;
return mem_strcpy( p->string, s );
}
/* \doc |str_pool_copyn| returns a copy of the string, |s|, using memory
from the string pool object, |pool|. The string will be |length| bytes
long, and will be "NULL" terminated. This can be used for data that is
known to be unique. No checks are made for uniqueness, however; and a
pointer to the string is not placed in the hash table. */
const char *str_pool_copyn( str_Pool pool, const char *s, int length )
{
poolInfo p = (poolInfo)pool;
return mem_strncpy( p->string, s, length );
}
/* \doc |str_pool_grow| will grow a string in the specified |pool| until
|str_pool_finish| is called. There must not be any other calls to
modify the specified string |pool| between the first call to
|str_pool_grow| and the call to |str_pool_finish|. */
void str_pool_grow( str_Pool pool, const char *s, int length )
{
poolInfo p = (poolInfo)pool;
mem_grow( p->string, s, length );
}
/* \doc |str_pool_finish| will finish the growth of a string performed by
multiple calls to |str_pool_grow|. The string will be null terminated
and will be entered into the specified string |pool|. Calls to
|str_pool_grow| follows by a call to |str_pool_finish| is equivalent to
a single call to |str_pool_find|. */
const char *str_pool_finish( str_Pool pool )
{
poolInfo p = (poolInfo)pool;
const char *datum;
mem_grow( p->string, "\0", 1 ); /* guarantee null termination */
datum = mem_finish( p->string );
hsh_insert( p->hash, datum, datum );
return datum;
}
/* \doc |str_pool_get_stats| returns statistics about the specified string
|pool|. The |str_Stats| structure is shown in \grind{str_Stats}. */
str_Stats str_pool_get_stats( str_Pool pool )
{
poolInfo p = (poolInfo)pool;
str_Stats s = xmalloc( sizeof( struct str_Stats ) );
if (p) {
mem_StringStats m = mem_get_string_stats( p->string );
hsh_Stats h = hsh_get_stats( p->hash );
s->count = m->count;
s->bytes = m->bytes;
s->retrievals = h->retrievals;
s->hits = h->hits;
s->misses = h->misses;
xfree( h ); /* rare */
xfree( m ); /* rare */
} else {
s->count = 0;
s->bytes = 0;
s->retrievals = 0;
s->hits = 0;
s->misses = 0;
}
return s;
}
/* \doc |str_pool_print_stats| prints the statistics for the specified
string |pool| on the specified |stream|. If |stream| is "NULL", then
"stdout" will be used. */
void str_pool_print_stats( str_Pool pool, FILE *stream )
{
FILE *str = stream ? stream : stdout;
str_Stats s = str_pool_get_stats( pool );
fprintf( str, "Statistics for %sstring pool at %p:\n",
pool == global ? "global " : "", pool );
fprintf( str, " %d strings using %d bytes\n", s->count, s->bytes );
fprintf( str, " %d retrievals (%d from top, %d failed)\n",
s->retrievals, s->hits, s->misses );
xfree( s ); /* rare */
}
static void _str_check_global( void )
{
if (!global) global = str_pool_create();
}
/* \doc |str_exists| acts like |str_pool_exists|, except the global string
pool is used. */
int str_exists( const char *s )
{
return str_pool_exists( global, s );
}
/* \doc |str_find| acts like |str_pool_find|, except the global string pool
is used. If the global string pool has not been initialized, it will be
initialized automatically. Further, on systems that support |atexit| or
|on_exit|, |str_destroy| will be called automatically at program
termination. */
const char *str_find( const char *s )
{
_str_check_global();
return str_pool_find( global, s );
}
/* \doc |str_findn| acts like |str_find|, except that the length of the
string is specified, and the string does not have to be "NULL"
terminated. */
const char *str_findn( const char *s, int length )
{
char *tmp = alloca( sizeof( char ) * (length + 1) );
_str_check_global();
strncpy( tmp, s, length );
tmp[ length ] = '\0';
return str_pool_find( global, tmp );
}
/* \doc |str_copy| acts like |str_pool_copy|, except the global string pool
is used. If the global string pool has not been initialized, it will be
initialized automatically. Further, on systems that support |atexit| or
|on_exit|, |str_destroy| will be called automatically at program
termination. */
const char *str_copy( const char *s )
{
_str_check_global();
return str_pool_copy( global, s );
}
/* \doc |str_copyn| acts like |str_copy|, except that the length of the
string is specified, and the string does not have to be "NULL"
terminated. */
const char *str_copyn( const char *s, int length )
{
return str_pool_copyn( global, s, length );
}
/* \doc |str_grow| will grow a string until |str_finish| is called. There
must not be any other calls to modify the global string pool between the
first call to |str_grow| and the call to |str_finish|. */
void str_grow( const char *s, int length )
{
_str_check_global();
str_pool_grow( global, s, length );
}
/* \doc |str_finish| will finish the growth of a string performed by
multiple calls to |str_grow|. The string will be null terminated and
will be entered into the global string pool tables. Calls to |str_grow|
follows by a call to |str_finish| is equivalent to a single call to
|str_findn|. */
const char *str_finish( void )
{
_str_check_global();
return str_pool_finish( global );
}
/* \doc |str_unique| returns a unique string with the given prefix. This
is not the most pretty way to generate unique strings, and should be
improved. The string is placed in the string pool and does not need to
be freed. */
const char *str_unique( const char *prefix )
{
static int i = 1;
char *buf = alloca( strlen( prefix ) + 100 );
do {
sprintf( buf, "%s%d", prefix, i++ );
} while (str_exists( buf ));
return str_find( buf );
}
/* \doc |str_destroy| frees all of the memory associated with the global
string pool. Since this function is called automatically at program
termination on systems that support |atexit| or |on_exit|, there should
be no need to call this function explicitly.
If this function is called explicitly, the next call to |str_find| will
re-initialize the global string pool. */
void str_destroy( void )
{
if (global) str_pool_destroy( global );
global = NULL;
}
/* \doc |str_get_stats| returns statistics about the global string pool.
The |str_Stats| structure is shown in \grindref{fig:strStats}. */
str_Stats str_get_stats( void )
{
return str_pool_get_stats( global );
}
/* \doc |str_print_stats| prints the statistics for the global string pool
on the specified |stream|. If |stream| is "NULL", then "stdout" will be
used. */
void str_print_stats( FILE *stream )
{
str_pool_print_stats( global, stream );
}
|