1 /* -*- Mode: C++; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*-
2 *
3 * Quadra, an action puzzle game
4 * Copyright (C) 1998-2000 Ludus Design
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "buf.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include "error.h"
28
Buf(const Buf & buf)29 Buf::Buf(const Buf &buf) {
30 data=NULL;
31 size_=0;
32 capacity=0;
33 inc=buf.inc;
34 reserve(buf.capacity);
35 append(buf.data, buf.size());
36 }
37
Buf(Dword size,Dword in)38 Buf::Buf(Dword size, Dword in) {
39 data=NULL;
40 size_=0;
41 capacity=0;
42 inc=in;
43 resize(size);
44 }
45
~Buf()46 Buf::~Buf() {
47 if(data)
48 free(data);
49 }
50
remove_from_start(Dword s)51 void Buf::remove_from_start(Dword s) {
52 Dword current_size=size();
53 if(s<current_size) {
54 memmove(get(), get()+s, current_size-s);
55 resize(current_size-s);
56 }
57 else {
58 resize(0);
59 }
60 }
61
append(const Byte * d,Dword s)62 void Buf::append(const Byte* d, Dword s) {
63 Dword end = size();
64 resize(size()+s);
65 memcpy(get()+end, d, s);
66 }
67
append(const char * d)68 void Buf::append(const char* d) {
69 append((const Byte*)d, strlen(d));
70 }
71
resize(Dword s)72 void Buf::resize(Dword s) {
73 if(s>size()) {
74 Dword end = size();
75 Dword endsize = s-size();
76 reserve(s);
77 memset(get()+end, 0, endsize);
78 }
79 size_=s;
80 }
81
reserve(Dword s)82 void Buf::reserve(Dword s) {
83 //Round up to next inc
84 s=((s+inc-1)/inc)*inc;
85 if(s>capacity) {
86 data = (Byte*)realloc(data, s);
87 if(!data)
88 fatal_msgbox("Out of memory!");
89 capacity=s;
90 }
91 }
92
Textbuf(Dword size)93 Textbuf::Textbuf(Dword size) {
94 data=NULL;
95 capacity=0;
96 reserve(size);
97 }
98
~Textbuf()99 Textbuf::~Textbuf() {
100 if(data)
101 free(data);
102 }
103
len() const104 int Textbuf::len() const {
105 if(data)
106 return strlen(data);
107 else
108 return 0;
109 }
110
get() const111 char* Textbuf::get() const {
112 static char st=0;
113 if(data)
114 return data;
115 else
116 return &st;
117 }
118
append(const char * s,...)119 void Textbuf::append(const char* s, ...) {
120 char st[32768];
121 va_list marker;
122 va_start(marker, s);
123 if (vsnprintf(st, sizeof(st), s, marker) >= static_cast<int>(sizeof(st)))
124 fatal_msgbox("Textbuf::append overflow");
125 va_end(marker);
126 appendraw(st);
127 }
128
appendraw(const char * s)129 void Textbuf::appendraw(const char* s) {
130 if(data) {
131 reserve(strlen(data)+strlen(s)+1);
132 } else {
133 reserve(strlen(s)+1);
134 }
135 if(data)
136 strcat(data, s);
137 }
138
reserve(Dword size)139 void Textbuf::reserve(Dword size) {
140 Dword wanted=(size+15)/16*16;
141 if(wanted<=capacity)
142 return;
143 bool init=data? false:true;
144 data=(char*)realloc(data, wanted);
145 if(!data)
146 fatal_msgbox("Out of memory!");
147 capacity=wanted;
148 if(init)
149 data[0]=0;
150 }
151