Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/temp-string.c @ 100:867ec80dbf42 HEAD
Custom flags are now shown in FLAGS and PERMANENTFLAGS lists after SELECT.
It also warns if there's for some reason a duplicate index number in custom
flags file.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 29 Aug 2002 22:21:51 +0300 |
parents | d493b9cc265e |
children | 4a7ab9e94f25 |
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; | |
33 unsigned int len; | |
34 | |
35 unsigned int alloc_size; | |
36 } RealTempString; | |
37 | |
38 TempString *t_string_new(unsigned int initial_size) | |
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 | |
52 static void t_string_inc(TempString *tstr, unsigned int size) | |
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; |
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
58 if (size <= rstr->len || size > INT_MAX) { |
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 */ |
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
60 i_panic("t_string_inc(): Out of memory for %u bytes", 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
|
61 } |
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 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
|
64 rstr->alloc_size = nearest_power(size); |
0 | 65 |
66 if (!t_try_grow(rstr->str, rstr->alloc_size)) { | |
67 str = t_malloc(rstr->alloc_size); | |
68 memcpy(str, rstr->str, rstr->len+1); | |
69 rstr->str = str; | |
70 } | |
71 } | |
72 } | |
73 | |
74 /* Append string/character */ | |
75 void t_string_append(TempString *tstr, const char *str) | |
76 { | |
77 t_string_append_n(tstr, str, strlen(str)); | |
78 } | |
79 | |
80 void t_string_append_n(TempString *tstr, const char *str, unsigned int size) | |
81 { | |
82 i_assert(size < INT_MAX); | |
83 | |
84 t_string_inc(tstr, size); | |
85 memcpy(tstr->str + tstr->len, str, size); | |
86 | |
87 tstr->len += size; | |
88 tstr->str[tstr->len] = '\0'; | |
89 } | |
90 | |
91 void t_string_append_c(TempString *tstr, char chr) | |
92 { | |
93 t_string_inc(tstr, 1); | |
94 tstr->str[tstr->len++] = chr; | |
95 tstr->str[tstr->len] = '\0'; | |
96 } | |
97 | |
98 void t_string_printfa(TempString *tstr, const char *fmt, ...) | |
99 { | |
100 va_list args, args2; | |
101 | |
102 va_start(args, fmt); | |
103 VA_COPY(args2, args); | |
104 | |
105 t_string_inc(tstr, printf_string_upper_bound(fmt, args)); | |
106 tstr->len += vsprintf(tstr->str + tstr->len, fmt, args2); | |
107 | |
108 va_end(args); | |
109 } | |
110 | |
111 void t_string_erase(TempString *tstr, unsigned int pos, unsigned int len) | |
112 { | |
113 i_assert(pos < tstr->len && tstr->len - pos >= len); | |
114 | |
115 memmove(tstr->str + pos + len, tstr->str + pos, | |
116 tstr->len - pos - len + 1); | |
117 } | |
118 | |
119 void t_string_truncate(TempString *tstr, unsigned int len) | |
120 { | |
121 i_assert(len <= tstr->len); | |
122 | |
123 tstr->len = len; | |
124 tstr->str[tstr->len] = '\0'; | |
125 } |