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
|
#include <stdio.h>
#ifdef MEMDBG
#include "memdbg.h"
#endif
#define BLOCKSIZE 256
#include "memory.h"
char *new_string()
/* allocates a new dynamic string structure */
{
dynamic *tmp;
tmp=(dynamic *)malloc(sizeof(dynamic));
if(tmp==NULL)
return(NULL);
tmp->length=0;
tmp->data=(char *)malloc(BLOCKSIZE+1);
if(tmp->data==NULL)
{
free(tmp);
return(NULL);
}
tmp->data[0]='\0';
tmp->alloc=BLOCKSIZE;
return((char *)tmp);
}
char *dstring(string)
char *string;
/* returns a pointer to the string itself. DO NOT MODIFY IT'S LENGTH! */
{
dynamic *tmp;
tmp=(dynamic *)string;
return(tmp->data);
}
int dlength(string)
char *string;
/* returns the length of the given string */
{
dynamic *tmp;
tmp=(dynamic *)string;
return(tmp->length);
}
int del_string(string, loc, len)
/* deletes len characters starting at loc
** returns 1 of ok, 0 if no memory available */
char *string;
int len, loc;
{
dynamic *tmp;
char *check;
char *a,*b, *c;
if(len==0)
return(1);
tmp=(dynamic *)string;
if(loc+len>tmp->length)
len=tmp->length-loc;
/* move end of string */
a=&tmp->data[loc];
b=&tmp->data[loc+len];
c=&tmp->data[tmp->length];
while(b<=c)
*(a++)=*(b++);
tmp->length-=len;
return(1);
}
int ins_string(string, loc, str)
/* inserts a string at the desired location, allocating extra memory if needed.
** returns 1 of ok, 0 if no memory available */
char *string;
char *str;
int loc;
{
dynamic *tmp;
char *check;
char *a,*b, *c;
int len=strlen(str);
if(len==0)
return(1);
tmp=(dynamic *)string;
while(tmp->length+len>tmp->alloc)
{
check=(char *)realloc(tmp->data, tmp->alloc+BLOCKSIZE+1);
if(check==NULL)
return(0);
else
{
tmp->data=check;
tmp->alloc+=BLOCKSIZE;
}
}
/* move end of string */
a=&tmp->data[tmp->length];
b=&tmp->data[tmp->length+len];
c=&tmp->data[loc];
while(a>=c)
*(b--)=*(a--);
strncpy(&tmp->data[loc], str, len);
tmp->length+=len;
return(1);
}
int ins_char(string, loc, ch)
/* inserts a character at the desired location, allocating extra memory if
needed. returns 1 of ok, 0 if no memory available */
char *string;
int ch;
int loc;
{
dynamic *tmp;
char *check;
char *a,*b, *c;
tmp=(dynamic *)string;
if(tmp->length==tmp->alloc)
{
check=(char *)realloc(tmp->data, tmp->alloc+BLOCKSIZE+1);
if(check==NULL)
return(0);
else
{
tmp->data=check;
tmp->alloc+=BLOCKSIZE;
}
}
/* move end of string */
a=&tmp->data[tmp->length];
b=&tmp->data[tmp->length+1];
c=&tmp->data[loc];
while(a>=c)
*(b--)=*(a--);
tmp->data[loc]=ch;
tmp->length+=1;
return(1);
}
int add_string(string, str)
/* adds a string to the dynamic string, allocating extra memory if needed.
** returns 1 of ok, 0 if no memory available */
char *string;
char *str;
{
dynamic *tmp;
char *check;
if(strlen(str)==0)
return(1);
tmp=(dynamic *)string;
while(tmp->length+strlen(str)>tmp->alloc)
{
check=(char *)realloc(tmp->data, tmp->alloc+BLOCKSIZE+1);
if(check==NULL)
return(0);
else
{
tmp->data=check;
tmp->alloc+=BLOCKSIZE;
}
}
strcpy(&tmp->data[tmp->length], str);
tmp->length+=strlen(str);
return(1);
}
int add_char(string, ch)
/* adds a byte to the dynamic string, allocating extra memory if needed.
** returns 1 of ok, 0 if no memory available */
char *string;
int ch;
{
dynamic *tmp;
char *check;
tmp=(dynamic *)string;
if(tmp->length==tmp->alloc)
{
check=(char *)realloc(tmp->data, tmp->alloc+BLOCKSIZE+1);
if(check==NULL)
return(0);
else
{
tmp->data=check;
tmp->alloc+=BLOCKSIZE;
}
}
tmp->data[tmp->length++]=ch;
tmp->data[tmp->length]='\0';
return(1);
}
char *dup_string(string)
dynamic *string;
{
dynamic *tmp;
tmp=(dynamic *)malloc(sizeof(dynamic));
tmp->length=string->length;
tmp->alloc=string->alloc;
tmp->data=(char *)malloc(string->alloc+1);
memcpy(tmp->data, string->data, string->length);
return((char *)tmp);
}
char *freeze_string(string)
/* pulls the string data out of the dynamic string and makes a static string
** out of it. returns NULL if there is no memory for the operation */
char *string;
{
dynamic *tmp;
tmp=(dynamic *)string;
string=(char *)tmp->data;
free(tmp);
return(string);
}
#ifdef TESTING
t_memory()
{
char *test;
char *the;
test=new_string();
add_char(test, 'a');
add_char(test, 'g');
add_char(test, 'a');
add_char(test, 'r');
the=dstring(test);
printf("should be: 'agar'\n");
printf("%s\n", the);
if(strcmp(the, "agar")!=0)
printf("new;add a;add g;add a;add r;dstring!='agar'\n");
add_string(test, " says hello when the moon shines!");
the=dstring(test);
printf("should be: 'agar says hello when the moon shines!'\n");
printf("%s\n", the);
if(strcmp(the, "agar says hello when the moon shines!")!=0)
printf("new;add a;add g;add a;add r;add \"says...\";dstring!='agar says ...!'\n");
del_string(test, 5, 11);
the=dstring(test);
printf("should be: 'agar when the moon shines!'\n");
printf("%s\n", the);
if(strcmp(the, "agar when the moon shines!")!=0)
printf("del_string failed\n");
ins_char(test, 0, 'h');
the=dstring(test);
printf("should be: 'hagar when the moon shines!'\n");
printf("%s\n", the);
if(strcmp(the, "hagar when the moon shines!")!=0)
printf("ins_char failed\n");
ins_string(test, 6, "says hello ");
the=dstring(test);
printf("should be: 'hagar says hello when the moon shines!'\n");
printf("%s\n", the);
if(strcmp(the, "hagar says hello when the moon shines!")!=0)
printf("ins_string failed\n");
del_string(test, 30, 30);
the=dstring(test);
printf("should be: 'hagar says hello when the moon'\n");
printf("%s\n", the);
if(strcmp(the, "hagar says hello when the moon")!=0)
printf("del_string past end failed\n");
add_string(test, " is full!");
the=dstring(test);
printf("should be: 'hagar says hello when the moon is full!'\n");
printf("%s\n", the);
if(strcmp(the, "hagar says hello when the moon is full!")!=0)
printf("add_string after del_string past end failed\n");
test=freeze_string(test);
printf("should be: 'hagar says hello when the moon is full!'\n");
printf("%s\n", test);
if(strcmp(test, "hagar says hello when the moon is full!")!=0)
printf("freeze_string failed\n");
printf("I love it when a plan comes together!!!\n");
}
#endif /* TESTING */
|