comparison src/dynamicstring.c @ 59:e612b263934f noffle

[svn] Changed some variable types and used some casts to avoid compiler warnings about signedness. In general, int should be used for parameters for allowing a signedness assertion in the function.
author enz
date Fri, 12 May 2000 17:52:41 +0100
parents 125d79c9e586
children 24d4cd032da5
comparison
equal deleted inserted replaced
58:b4e6f7f96135 59:e612b263934f
1 /* 1 /*
2 dynamicstring.c 2 dynamicstring.c
3 3
4 $Id: dynamicstring.c 60 2000-05-09 22:28:38Z uh1763 $ 4 $Id: dynamicstring.c 65 2000-05-12 16:52:41Z enz $
5 */ 5 */
6 6
7 #if HAVE_CONFIG_H 7 #if HAVE_CONFIG_H
8 #include <config.h> 8 #include <config.h>
9 #endif 9 #endif
14 #include "log.h" 14 #include "log.h"
15 #include "portable.h" 15 #include "portable.h"
16 16
17 struct DynStr 17 struct DynStr
18 { 18 {
19 size_t len; /* Current length (without trailing '\0') */ 19 int len; /* Current length (without trailing '\0') */
20 size_t max; /* Max length that fits into buffer (incl. trailing '\0') */ 20 int max; /* Max length that fits into buffer (incl. trailing '\0') */
21 char *str; 21 char *str;
22 }; 22 };
23 23
24 static void 24 static void
25 reallocStr( DynStr *self, size_t max ) 25 reallocStr( DynStr *self, int max )
26 { 26 {
27 ASSERT( max >= 0 );
27 if ( max <= self->max ) 28 if ( max <= self->max )
28 return; 29 return;
29 if ( ! ( self->str = (char *)realloc( self->str, max ) ) ) 30 if ( ! ( self->str = (char *)realloc( self->str, (size_t)max ) ) )
30 { 31 {
31 Log_err( "Realloc of DynStr failed" ); 32 Log_err( "Realloc of DynStr failed" );
32 exit( EXIT_FAILURE ); 33 exit( EXIT_FAILURE );
33 } 34 }
34 if ( self->max == 0 ) /* First allocation? */ 35 if ( self->max == 0 ) /* First allocation? */
35 *(self->str) = '\0'; 36 *(self->str) = '\0';
36 self->max = max; 37 self->max = max;
37 } 38 }
38 39
39 DynStr * 40 DynStr *
40 new_DynStr( size_t reserve ) 41 new_DynStr( int reserve )
41 { 42 {
42 DynStr *s; 43 DynStr *s;
43 44
44 if ( ! ( s = malloc( sizeof( DynStr ) ) ) ) 45 if ( ! ( s = malloc( sizeof( DynStr ) ) ) )
45 { 46 {
62 free( self->str ); 63 free( self->str );
63 self->str = NULL; 64 self->str = NULL;
64 free( self ); 65 free( self );
65 } 66 }
66 67
67 size_t 68 int
68 DynStr_len( const DynStr *self ) 69 DynStr_len( const DynStr *self )
69 { 70 {
70 return self->len; 71 return self->len;
71 } 72 }
72 73
77 } 78 }
78 79
79 void 80 void
80 DynStr_app( DynStr *self, const char *s ) 81 DynStr_app( DynStr *self, const char *s )
81 { 82 {
82 size_t len; 83 int len;
83 84
84 len = strlen( s ); 85 len = strlen( s );
85 if ( self->len + len + 1 > self->max ) 86 if ( self->len + len + 1 > self->max )
86 reallocStr( self, self->len * 2 + len + 1 ); 87 reallocStr( self, self->len * 2 + len + 1 );
87 strcpy( self->str + self->len, s ); 88 strcpy( self->str + self->len, s );
91 void 92 void
92 DynStr_appDynStr( DynStr *self, const DynStr *s ) 93 DynStr_appDynStr( DynStr *self, const DynStr *s )
93 { 94 {
94 if ( self->len + s->len + 1 > self->max ) 95 if ( self->len + s->len + 1 > self->max )
95 reallocStr( self, self->len * 2 + s->len + 1 ); 96 reallocStr( self, self->len * 2 + s->len + 1 );
96 memcpy( self->str + self->len, s->str, s->len + 1 ); 97 memcpy( self->str + self->len, s->str, (size_t)s->len + 1 );
97 self->len += s->len; 98 self->len += s->len;
98 } 99 }
99 100
100 void 101 void
101 DynStr_appLn( DynStr *self, const char *s ) 102 DynStr_appLn( DynStr *self, const char *s )
103 DynStr_app( self, s ); 104 DynStr_app( self, s );
104 DynStr_app( self, "\n" ); 105 DynStr_app( self, "\n" );
105 } 106 }
106 107
107 void 108 void
108 DynStr_appN( DynStr *self, const char *s, size_t n ) 109 DynStr_appN( DynStr *self, const char *s, int n )
109 { 110 {
110 size_t len = self->len; 111 int len = self->len;
111 112
113 ASSERT( n >= 0 );
112 if ( len + n + 1 > self->max ) 114 if ( len + n + 1 > self->max )
113 reallocStr( self, len * 2 + n + 1 ); 115 reallocStr( self, len * 2 + n + 1 );
114 strncat( self->str + len, s, n ); 116 strncat( self->str + len, s, (size_t)n );
115 self->len = len + strlen( self->str + len ); 117 self->len = len + strlen( self->str + len );
116 } 118 }
117 119
118 void 120 void
119 DynStr_clear( DynStr *self ) 121 DynStr_clear( DynStr *self )