Tizen 2.1 base
[sdk/target/sdbd.git] / src / utils.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "utils.h"
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <string.h>
21 #define STRING_MAXLEN 1024
22 char*
23 buff_addc (char*  buff, char*  buffEnd, int  c)
24 {
25     int  avail = buffEnd - buff;
26
27     if (avail <= 0)  /* already in overflow mode */
28         return buff;
29
30     if (avail == 1) {  /* overflowing, the last byte is reserved for zero */
31         buff[0] = 0;
32         return buff + 1;
33     }
34
35     buff[0] = (char) c;  /* add char and terminating zero */
36     buff[1] = 0;
37     return buff + 1;
38 }
39
40 char*
41 buff_adds (char*  buff, char*  buffEnd, const char*  s)
42 {
43     int  slen = strlen(s);
44
45     return buff_addb(buff, buffEnd, s, slen);
46 }
47
48 char*
49 buff_addb (char*  buff, char*  buffEnd, const void*  data, int  len)
50 {
51     int  avail = (buffEnd - buff);
52
53     if (avail <= 0 || len <= 0)  /* already overflowing */
54         return buff;
55
56     if (len > avail)
57         len = avail;
58
59     memcpy(buff, data, len);
60
61     buff += len;
62
63     /* ensure there is a terminating zero */
64     if (buff >= buffEnd) {  /* overflow */
65         buff[-1] = 0;
66     } else
67         buff[0] = 0;
68
69     return buff;
70 }
71
72 char*
73 buff_add  (char*  buff, char*  buffEnd, const char*  format, ... )
74 {
75     int      avail;
76
77     avail = (buffEnd - buff);
78
79     if (avail > 0) {
80         va_list  args;
81         int      nn;
82
83         va_start(args, format);
84         nn = vsnprintf( buff, avail, format, args);
85         va_end(args);
86
87         if (nn < 0) {
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. */
92             nn = avail;
93         }
94         else if (nn > avail) {
95             nn = avail;
96         }
97
98         buff += nn;
99
100         /* ensure that there is a terminating zero */
101         if (buff >= buffEnd)
102             buff[-1] = 0;
103         else
104             buff[0] = 0;
105     }
106     return buff;
107 }
108
109 char *str_trim(const char* string)
110 {
111     const char* s = string;
112     const char* e = string + (strlen(string) - 1);
113     char* ret;
114
115     while(*s == ' ' || *s == '\t') // ltrim
116         s++;
117     while(*e == ' ' || *e == '\t') // rtrim
118         e--;
119
120     ret = strdup(s);
121     ret[e - s + 1] = 0;
122
123     return  ret;
124 }