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.
21 #define STRING_MAXLEN 1024
23 buff_addc (char* buff, char* buffEnd, int c)
25 int avail = buffEnd - buff;
27 if (avail <= 0) /* already in overflow mode */
30 if (avail == 1) { /* overflowing, the last byte is reserved for zero */
35 buff[0] = (char) c; /* add char and terminating zero */
41 buff_adds (char* buff, char* buffEnd, const char* s)
45 return buff_addb(buff, buffEnd, s, slen);
49 buff_addb (char* buff, char* buffEnd, const void* data, int len)
51 int avail = (buffEnd - buff);
53 if (avail <= 0 || len <= 0) /* already overflowing */
59 memcpy(buff, data, len);
63 /* ensure there is a terminating zero */
64 if (buff >= buffEnd) { /* overflow */
73 buff_add (char* buff, char* buffEnd, const char* format, ... )
77 avail = (buffEnd - buff);
83 va_start(args, format);
84 nn = vsnprintf( buff, avail, format, args);
88 /* some C libraries return -1 in case of overflow,
89 * but they will also do that if the format spec is
90 * invalid. We assume SDB is not buggy enough to
91 * trigger that last case. */
94 else if (nn > avail) {
100 /* ensure that there is a terminating zero */
109 char *str_trim(const char* string)
111 const char* s = string;
112 const char* e = string + (strlen(string) - 1);
115 while(*s == ' ' || *s == '\t') // ltrim
117 while(*e == ' ' || *e == '\t') // rtrim