Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/temp-string.c @ 183:4a7ab9e94f25 HEAD
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
C99-like.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 08 Sep 2002 16:20:28 +0300 |
parents | d493b9cc265e |
children | e90b95f6d962 |
rev | line source |
---|---|
0 | 1 /* |
2 temp-string.c : Temporary string | |
3 | |
4 Copyright (c) 2002 Timo Sirainen | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining | |
7 a copy of this software and associated documentation files (the | |
8 "Software"), to deal in the Software without restriction, including | |
9 without limitation the rights to use, copy, modify, merge, publish, | |
10 distribute, sublicense, and/or sell copies of the Software, and to | |
11 permit persons to whom the Software is furnished to do so, subject to | |
12 the following conditions: | |
13 | |
14 The above copyright notice and this permission notice shall be | |
15 included in all copies or substantial portions of the Software. | |
16 | |
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 */ | |
25 | |
26 #include "lib.h" | |
27 #include "temp-string.h" | |
28 | |
29 #include <stdio.h> | |
30 | |
31 typedef struct { | |
32 char *str; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
33 size_t len; |
0 | 34 |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
35 size_t alloc_size; |
0 | 36 } RealTempString; |
37 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
38 TempString *t_string_new(size_t initial_size) |
0 | 39 { |
40 RealTempString *rstr; | |
41 | |
42 if (initial_size <= 0) | |
43 initial_size = 64; | |
44 | |
45 rstr = t_new(RealTempString, 1); | |
46 rstr->alloc_size = initial_size; | |
47 rstr->str = t_malloc(rstr->alloc_size); | |
48 rstr->str[0] = '\0'; | |
49 return (TempString *) rstr; | |
50 } | |
51 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
52 static void t_string_inc(TempString *tstr, size_t size) |
0 | 53 { |
54 RealTempString *rstr = (RealTempString *) tstr; | |
55 char *str; | |
56 | |
50
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
57 size += rstr->len + 1; |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
58 if (size <= rstr->len || size > SSIZE_T_MAX) { |
50
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
59 /* overflow */ |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
60 i_panic("t_string_inc(): Out of memory %"PRIuSIZE_T" bytes", |
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
61 size); |
50
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
62 } |
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
63 |
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
64 if (size > rstr->alloc_size) { |
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
65 rstr->alloc_size = nearest_power(size); |
0 | 66 |
67 if (!t_try_grow(rstr->str, rstr->alloc_size)) { | |
68 str = t_malloc(rstr->alloc_size); | |
69 memcpy(str, rstr->str, rstr->len+1); | |
70 rstr->str = str; | |
71 } | |
72 } | |
73 } | |
74 | |
75 /* Append string/character */ | |
76 void t_string_append(TempString *tstr, const char *str) | |
77 { | |
78 t_string_append_n(tstr, str, strlen(str)); | |
79 } | |
80 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
81 void t_string_append_n(TempString *tstr, const char *str, size_t size) |
0 | 82 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
83 i_assert(size < SSIZE_T_MAX); |
0 | 84 |
85 t_string_inc(tstr, size); | |
86 memcpy(tstr->str + tstr->len, str, size); | |
87 | |
88 tstr->len += size; | |
89 tstr->str[tstr->len] = '\0'; | |
90 } | |
91 | |
92 void t_string_append_c(TempString *tstr, char chr) | |
93 { | |
94 t_string_inc(tstr, 1); | |
95 tstr->str[tstr->len++] = chr; | |
96 tstr->str[tstr->len] = '\0'; | |
97 } | |
98 | |
99 void t_string_printfa(TempString *tstr, const char *fmt, ...) | |
100 { | |
101 va_list args, args2; | |
102 | |
103 va_start(args, fmt); | |
104 VA_COPY(args2, args); | |
105 | |
106 t_string_inc(tstr, printf_string_upper_bound(fmt, args)); | |
107 tstr->len += vsprintf(tstr->str + tstr->len, fmt, args2); | |
108 | |
109 va_end(args); | |
110 } | |
111 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
112 void t_string_erase(TempString *tstr, size_t pos, size_t len) |
0 | 113 { |
114 i_assert(pos < tstr->len && tstr->len - pos >= len); | |
115 | |
116 memmove(tstr->str + pos + len, tstr->str + pos, | |
117 tstr->len - pos - len + 1); | |
118 } | |
119 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
120 void t_string_truncate(TempString *tstr, size_t len) |
0 | 121 { |
122 i_assert(len <= tstr->len); | |
123 | |
124 tstr->len = len; | |
125 tstr->str[tstr->len] = '\0'; | |
126 } |