|
|
1.1 paf 1: #include <string.h>
2:
3: #include "pa_pool.h"
4:
5: void *String::operator new(size_t size, Pool *apool) {
6: return apool->alloc(size);
7: }
8:
1.2 paf 9: void String::construct(Pool *apool) {
1.1 paf 10: pool=apool;
1.2 paf 11: head.count=curr_chunk_rows=CR_PREALLOCATED_COUNT;
12: append_here=head.first;
13: head.preallocated_link=0;
14: link_row=&head.first[curr_chunk_rows];
1.1 paf 15: }
16:
17: void String::expand() {
1.2 paf 18: curr_chunk_rows=curr_chunk_rows*100/CR_GROW_PERCENT;
19: Chunk *chunk=static_cast<Chunk *>(
20: pool->calloc(sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
21: chunk->count=curr_chunk_rows;
22: link_row->link=chunk;
23: append_here=chunk->first;
24: link_row=&chunk->first[curr_chunk_rows];
1.1 paf 25: }
26:
27: String& String::operator += (char *src) {
28: if(chunk_is_full())
29: expand();
30:
31: append_here->item.ptr=src;
32: append_here->item.size=strlen(src);
33: append_here++;
34:
35: return *this;
36: }
37:
38: size_t String::size() {
39: int result=0;
1.2 paf 40: Chunk *chunk=&head;
41: do {
42: Chunk::Row *row=chunk->first;
43: for(int i=0; i<chunk->count; i++) {
1.1 paf 44: if(row==append_here)
45: goto break2;
46:
47: result+=row->item.size;
48: row++;
49: }
1.2 paf 50: chunk=row->link;
51: } while(chunk);
1.1 paf 52: break2:
53: return result;
54: }
1.3 ! paf 55: /*
! 56: $Id: parser.C,v 1.3 2001/01/26 15:31:56 paf Exp $
! 57: */
1.1 paf 58:
59: char *String::c_str() {
60: char *result=static_cast<char *>(pool->alloc(size()+1));
61:
62: char *copy_here=result;
1.2 paf 63: Chunk *chunk=&head;
64: do {
65: Chunk::Row *row=chunk->first;
66: for(int i=0; i<chunk->count; i++) {
1.1 paf 67: if(row==append_here)
68: goto break2;
69:
70: memcpy(copy_here, row->item.ptr, row->item.size);
71: copy_here+=row->item.size;
72: row++;
73: }
1.2 paf 74: chunk=row->link;
75: } while(chunk);
1.1 paf 76: break2:
77: *copy_here=0;
78: return result;
79: }
80: