File: strpool.C

package info (click to toggle)
its4 1.1.1-1
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 388 kB
  • ctags: 546
  • sloc: cpp: 5,769; ansic: 686; sh: 374; makefile: 129; lisp: 4
file content (68 lines) | stat: -rw-r--r-- 1,241 bytes parent folder | download
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
/* strpool.C
 * John Viega
 *
 * Note that string pool starts at 1 as far as the outside is concerned.
 * This is so that you can initialize to 0 and know the string hasn't been
 * set yet.
 *
 * Feb 8, 2000
 */

#include "fatal.H"
#include "config.H"

#ifdef ITS4_DEBUG
#include <stdio.h>
#endif

// How big does the string pool start out by default?
#define STRPOOL_BASE_SIZE    16384
// How much should we add to it when we have to grow it? 
#define STRPOOL_INCR         8192

char *pool;
int  capacity, incr, size;
void InitStringPool(int c, int i)
{
  capacity = c;
  incr     = i;
  size     = 0;
  pool     = new char[c];
  if(!pool)
    OutOfMemory();
}

void InitStringPool()
{
  InitStringPool(STRPOOL_BASE_SIZE, STRPOOL_INCR);
}

void ExpandPool()
{
#ifdef ITS4_DEBUG
  fprintf(stderr, "Warning: string pool too small.\n");
#endif
  char *newpool = new char[capacity+=incr];
  if(!newpool)
    OutOfMemory();
  memcpy(newpool, pool, size);
  delete[] pool;
  pool = newpool;
}

int AddStringToPool(char *s)
{
  int l = strlen(s) + 1;
  while(capacity < size + l)
    ExpandPool();
  strcpy(&(pool[size]), s); // ITS4: ignore strcpy
  int r  = size + 1;
  size += l;
  return r;
}

char *GetString(int l)
{
  return &pool[l-1];
}