ali@0
|
1 |
#include <stdlib.h>
|
ali@0
|
2 |
#include <string.h>
|
ali@5
|
3 |
#include <bl/blstring.h>
|
ali@5
|
4 |
#include <bl/types.h>
|
ali@5
|
5 |
#include <bl/mem.h>
|
ali@5
|
6 |
#include <bl/strfuncs.h>
|
ali@0
|
7 |
|
ali@0
|
8 |
/*
|
ali@0
|
9 |
* Strings which manage their own memory
|
ali@0
|
10 |
*/
|
ali@0
|
11 |
|
ali@0
|
12 |
String *string_new(const char *init)
|
ali@0
|
13 |
{
|
ali@0
|
14 |
String *string=mem_new(String,1);
|
ali@0
|
15 |
if (!init)
|
ali@0
|
16 |
init="";
|
ali@0
|
17 |
string->len=strlen(init);
|
ali@0
|
18 |
string->alloc=string->len+1;
|
ali@0
|
19 |
string->str=str_dup(init);
|
ali@0
|
20 |
return string;
|
ali@0
|
21 |
}
|
ali@0
|
22 |
|
ali@0
|
23 |
/*
|
ali@0
|
24 |
* Free a string and either return the contents (if free_segment is FALSE)
|
ali@0
|
25 |
* or free the contents as well and return NULL (if free_segment is TRUE).
|
ali@0
|
26 |
*/
|
ali@0
|
27 |
char *string_free(String *string,boolean free_segment)
|
ali@0
|
28 |
{
|
ali@0
|
29 |
char *retval;
|
ali@0
|
30 |
if (free_segment)
|
ali@0
|
31 |
{
|
ali@0
|
32 |
mem_free(string->str);
|
ali@0
|
33 |
retval=NULL;
|
ali@0
|
34 |
}
|
ali@0
|
35 |
else
|
ali@0
|
36 |
retval=string->str;
|
ali@0
|
37 |
mem_free(string);
|
ali@0
|
38 |
return retval;
|
ali@0
|
39 |
}
|
ali@0
|
40 |
|
ali@0
|
41 |
/*
|
ali@0
|
42 |
* Append a byte to string.
|
ali@0
|
43 |
*/
|
ali@0
|
44 |
void string_append_c(String *string,char c)
|
ali@0
|
45 |
{
|
ali@0
|
46 |
if (string->len+1==string->alloc)
|
ali@0
|
47 |
{
|
ali@0
|
48 |
string->alloc*=2;
|
ali@0
|
49 |
string->str=mem_renew(char,string->str,string->alloc);
|
ali@0
|
50 |
}
|
ali@0
|
51 |
string->str[string->len++]=c;
|
ali@0
|
52 |
string->str[string->len]='\0';
|
ali@0
|
53 |
}
|
ali@0
|
54 |
|
ali@0
|
55 |
/*
|
ali@0
|
56 |
* Append len bytes from s to string. len may be passed as <0 if s is
|
ali@0
|
57 |
* a nul-terminated string of unknown length.
|
ali@0
|
58 |
*/
|
ali@0
|
59 |
void string_append_len(String *string,const char *s,ssize_t len)
|
ali@0
|
60 |
{
|
ali@0
|
61 |
if (len<0)
|
ali@0
|
62 |
len=strlen(s);
|
ali@0
|
63 |
if (string->len+len>=string->alloc)
|
ali@0
|
64 |
{
|
ali@0
|
65 |
while (string->len+len>=string->alloc)
|
ali@0
|
66 |
string->alloc*=2;
|
ali@0
|
67 |
string->str=mem_renew(char,string->str,string->alloc);
|
ali@0
|
68 |
}
|
ali@0
|
69 |
memcpy(string->str+string->len,s,len);
|
ali@0
|
70 |
string->len+=len;
|
ali@0
|
71 |
string->str[string->len]='\0';
|
ali@0
|
72 |
}
|
ali@0
|
73 |
|
ali@0
|
74 |
/*
|
ali@0
|
75 |
* Sets the length of a String. If the length is less than the current length,
|
ali@0
|
76 |
* the string will be truncated. If the length is greater than the current
|
ali@0
|
77 |
* length, the contents of the newly added area are undefined. (However, as
|
ali@0
|
78 |
* always, string->str[string->len] will be a nul byte.)
|
ali@0
|
79 |
*/
|
ali@0
|
80 |
void string_set_size(String *string,size_t len)
|
ali@0
|
81 |
{
|
ali@0
|
82 |
if (len>=string->alloc)
|
ali@0
|
83 |
{
|
ali@0
|
84 |
while (len>=string->alloc)
|
ali@0
|
85 |
string->alloc*=2;
|
ali@0
|
86 |
string->str=mem_renew(char,string->str,string->alloc);
|
ali@0
|
87 |
}
|
ali@0
|
88 |
string->len=len;
|
ali@0
|
89 |
string->str[string->len]='\0';
|
ali@0
|
90 |
}
|