d8ba6514291c23e52e51569eb9a907220bcdf793
[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
22 #define STRING_MAXLEN 1024
23 char*
24 buff_addc (char*  buff, char*  buffEnd, int  c)
25 {
26     int  avail = buffEnd - buff;
27
28     if (avail <= 0)  /* already in overflow mode */
29         return buff;
30
31     if (avail == 1) {  /* overflowing, the last byte is reserved for zero */
32         buff[0] = 0;
33         return buff + 1;
34     }
35
36     buff[0] = (char) c;  /* add char and terminating zero */
37     buff[1] = 0;
38     return buff + 1;
39 }
40
41 char*
42 buff_adds (char*  buff, char*  buffEnd, const char*  s)
43 {
44     int  slen = strlen(s);
45
46     return buff_addb(buff, buffEnd, s, slen);
47 }
48
49 char*
50 buff_addb (char*  buff, char*  buffEnd, const void*  data, int  len)
51 {
52     int  avail = (buffEnd - buff);
53
54     if (avail <= 0 || len <= 0)  /* already overflowing */
55         return buff;
56
57     if (len > avail)
58         len = avail;
59
60     memcpy(buff, data, len);
61
62     buff += len;
63
64     /* ensure there is a terminating zero */
65     if (buff >= buffEnd) {  /* overflow */
66         buff[-1] = 0;
67     } else
68         buff[0] = 0;
69
70     return buff;
71 }
72
73 char*
74 buff_add  (char*  buff, char*  buffEnd, const char*  format, ... )
75 {
76     int      avail;
77
78     avail = (buffEnd - buff);
79
80     if (avail > 0) {
81         va_list  args;
82         int      nn;
83
84         va_start(args, format);
85         nn = vsnprintf( buff, avail, format, args);
86         va_end(args);
87
88         if (nn < 0) {
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. */
93             nn = avail;
94         }
95         else if (nn > avail) {
96             nn = avail;
97         }
98
99         buff += nn;
100
101         /* ensure that there is a terminating zero */
102         if (buff >= buffEnd)
103             buff[-1] = 0;
104         else
105             buff[0] = 0;
106     }
107     return buff;
108 }
109
110 char *str_trim(const char* string)
111 {
112     const char* s = string;
113     const char* e = string + (strlen(string) - 1);
114     char* ret;
115
116     while(*s == ' ' || *s == '\t') // ltrim
117         s++;
118     while(*e == ' ' || *e == '\t') // rtrim
119         e--;
120
121     ret = strdup(s);
122     ret[e - s + 1] = 0;
123
124     return  ret;
125 }