I added them, so I get to kill them :-). Finally remove all uses of safe_strcpy and...
[ira/wip.git] / lib / util / util_str.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    
5    Copyright (C) Andrew Tridgell 1992-2001
6    Copyright (C) Simo Sorce      2001-2002
7    Copyright (C) Martin Pool     2003
8    Copyright (C) James Peach     2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/locale.h"
26 #undef strncasecmp
27 #undef strcasemp
28
29 /**
30  * @file
31  * @brief String utilities.
32  **/
33
34 /**
35   format a string into length-prefixed dotted domain format, as used in NBT
36   and in some ADS structures
37 **/
38 _PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
39 {
40         char *ret;
41         int i;
42         if (!s || !*s) {
43                 return talloc_strdup(mem_ctx, "");
44         }
45         ret = talloc_array(mem_ctx, char, strlen(s)+2);
46         if (!ret) {
47                 return ret;
48         }
49         
50         memcpy(ret+1, s, strlen(s)+1);
51         ret[0] = '.';
52
53         for (i=0;ret[i];i++) {
54                 if (ret[i] == '.') {
55                         char *p = strchr(ret+i+1, '.');
56                         if (p) {
57                                 ret[i] = p-(ret+i+1);
58                         } else {
59                                 ret[i] = strlen(ret+i+1);
60                         }
61                 }
62         }
63
64         talloc_set_name_const(ret, ret);
65
66         return ret;
67 }
68
69 /**
70  * Parse a string containing a boolean value.
71  *
72  * val will be set to the read value.
73  *
74  * @retval true if a boolean value was parsed, false otherwise.
75  */
76 _PUBLIC_ bool conv_str_bool(const char * str, bool * val)
77 {
78         char *  end = NULL;
79         long    lval;
80
81         if (str == NULL || *str == '\0') {
82                 return false;
83         }
84
85         lval = strtol(str, &end, 10 /* base */);
86         if (end == NULL || *end != '\0' || end == str) {
87                 return set_boolean(str, val);
88         }
89
90         *val = (lval) ? true : false;
91         return true;
92 }
93
94 /**
95  * Convert a size specification like 16K into an integral number of bytes. 
96  **/
97 _PUBLIC_ bool conv_str_size_error(const char * str, uint64_t * val)
98 {
99         char *              end = NULL;
100         unsigned long long  lval;
101
102         if (str == NULL || *str == '\0') {
103                 return false;
104         }
105
106         lval = strtoull(str, &end, 10 /* base */);
107         if (end == NULL || end == str) {
108                 return false;
109         }
110
111         if (*end) {
112                 if (strwicmp(end, "K") == 0) {
113                         lval *= 1024ULL;
114                 } else if (strwicmp(end, "M") == 0) {
115                         lval *= (1024ULL * 1024ULL);
116                 } else if (strwicmp(end, "G") == 0) {
117                         lval *= (1024ULL * 1024ULL * 1024ULL);
118                 } else if (strwicmp(end, "T") == 0) {
119                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
120                 } else if (strwicmp(end, "P") == 0) {
121                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
122                 } else {
123                         return false;
124                 }
125         }
126
127         *val = (uint64_t)lval;
128         return true;
129 }
130
131 /**
132  * Parse a uint64_t value from a string
133  *
134  * val will be set to the value read.
135  *
136  * @retval true if parsing was successful, false otherwise
137  */
138 _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
139 {
140         char *              end = NULL;
141         unsigned long long  lval;
142
143         if (str == NULL || *str == '\0') {
144                 return false;
145         }
146
147         lval = strtoull(str, &end, 10 /* base */);
148         if (end == NULL || *end != '\0' || end == str) {
149                 return false;
150         }
151
152         *val = (uint64_t)lval;
153         return true;
154 }
155
156 /**
157  * Compare 2 strings.
158  *
159  * @note The comparison is case-insensitive.
160  **/
161 _PUBLIC_ bool strequal(const char *s1, const char *s2)
162 {
163         if (s1 == s2)
164                 return true;
165         if (!s1 || !s2)
166                 return false;
167   
168         return strcasecmp_m(s1,s2) == 0;
169 }
170