2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #define STRING_MAXLEN 1024
24 buff_addc (char* buff, char* buffEnd, int c)
26 int avail = buffEnd - buff;
28 if (avail <= 0) /* already in overflow mode */
31 if (avail == 1) { /* overflowing, the last byte is reserved for zero */
36 buff[0] = (char) c; /* add char and terminating zero */
42 buff_adds (char* buff, char* buffEnd, const char* s)
46 return buff_addb(buff, buffEnd, s, slen);
50 buff_addb (char* buff, char* buffEnd, const void* data, int len)
52 int avail = (buffEnd - buff);
54 if (avail <= 0 || len <= 0) /* already overflowing */
60 memcpy(buff, data, len);
64 /* ensure there is a terminating zero */
65 if (buff >= buffEnd) { /* overflow */
74 buff_add (char* buff, char* buffEnd, const char* format, ... )
78 avail = (buffEnd - buff);
84 va_start(args, format);
85 nn = vsnprintf( buff, avail, format, args);
89 /* some C libraries return -1 in case of overflow,
90 * but they will also do that if the format spec is
91 * invalid. We assume SDB is not buggy enough to
92 * trigger that last case. */
95 else if (nn > avail) {
101 /* ensure that there is a terminating zero */
110 char *str_trim(const char* string)
112 const char* s = string;
113 const char* e = string + (strlen(string) - 1);
116 while(*s == ' ' || *s == '\t') // ltrim
118 while(*e == ' ' || *e == '\t') // rtrim