annotate dynamicstring.c @ 4:691dc670549a noffle

[svn] Add idea with pseudo groups containing only requested articles
author enz
date Fri, 07 Jan 2000 19:01:07 +0000
parents 04124a4423d4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
1 /*
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
2 dynamicstring.c
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
3
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
4 $Id: dynamicstring.c 3 2000-01-04 11:35:42Z enz $
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
5 */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
6
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
7 #include "dynamicstring.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
8
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
9 #include <sys/types.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
10 #include "log.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
11
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
12 struct DynStr
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
13 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
14 size_t len; /* Current length (without trailing '\0') */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
15 size_t max; /* Max length that fits into buffer (incl. trailing '\0') */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
16 char *str;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
17 };
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
18
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
19 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
20 reallocStr( DynStr *self, size_t max )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
21 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
22 if ( max <= self->max )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
23 return;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
24 if ( ! ( self->str = (char *)realloc( self->str, max ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
25 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
26 Log_err( "Realloc of DynStr failed" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
27 exit( EXIT_FAILURE );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
28 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
29 if ( self->max == 0 ) /* First allocation? */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
30 *(self->str) = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
31 self->max = max;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
32 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
33
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
34 DynStr *
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
35 new_DynStr( size_t reserve )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
36 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
37 DynStr *s;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
38
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
39 if ( ! ( s = (DynStr *) malloc( sizeof( DynStr ) ) ) )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
40 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
41 Log_err( "Allocation of DynStr failed" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
42 exit( EXIT_FAILURE );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
43 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
44 s->len = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
45 s->max = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
46 s->str = NULL;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
47 if ( reserve > 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
48 reallocStr( s, reserve + 1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
49 return s;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
50 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
51
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
52 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
53 del_DynStr( DynStr *self )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
54 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
55 if ( ! self )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
56 return;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
57 free( self->str );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
58 self->str = NULL;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
59 free( self );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
60 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
61
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
62 size_t
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
63 DynStr_len( const DynStr *self )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
64 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
65 return self->len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
66 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
67
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
68 const char *
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
69 DynStr_str( const DynStr *self )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
70 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
71 return self->str;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
72 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
73
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
74 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
75 DynStr_app( DynStr *self, const char *s )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
76 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
77 size_t len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
78
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
79 len = strlen( s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
80 if ( self->len + len + 1 > self->max )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
81 reallocStr( self, self->len * 2 + len + 1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
82 strcpy( self->str + self->len, s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
83 self->len += len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
84 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
85
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
86 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
87 DynStr_appDynStr( DynStr *self, const DynStr *s )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
88 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
89 if ( self->len + s->len + 1 > self->max )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
90 reallocStr( self, self->len * 2 + s->len + 1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
91 memcpy( self->str + self->len, s->str, s->len + 1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
92 self->len += s->len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
93 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
94
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
95 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
96 DynStr_appLn( DynStr *self, const char *s )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
97 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
98 DynStr_app( self, s );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
99 DynStr_app( self, "\n" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
100 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
101
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
102 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
103 DynStr_appN( DynStr *self, const char *s, size_t n )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
104 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
105 size_t len = self->len;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
106
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
107 if ( len + n + 1 > self->max )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
108 reallocStr( self, len * 2 + n + 1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
109 strncat( self->str + len, s, n );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
110 self->len = len + strlen( self->str + len );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
111 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
112
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
113 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
114 DynStr_clear( DynStr *self )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
115 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
116 self->len = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
117 if ( self->max > 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
118 *(self->str) = '\0';
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
119 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
120