lib/util: Remove unused str_format_nbt_domain()
[sfrench/samba-autobuild/.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  * Parse a string containing a boolean value.
36  *
37  * val will be set to the read value.
38  *
39  * @retval true if a boolean value was parsed, false otherwise.
40  */
41 _PUBLIC_ bool conv_str_bool(const char * str, bool * val)
42 {
43         char *  end = NULL;
44         long    lval;
45
46         if (str == NULL || *str == '\0') {
47                 return false;
48         }
49
50         lval = strtol(str, &end, 10 /* base */);
51         if (end == NULL || *end != '\0' || end == str) {
52                 return set_boolean(str, val);
53         }
54
55         *val = (lval) ? true : false;
56         return true;
57 }
58
59 /**
60  * Convert a size specification like 16K into an integral number of bytes. 
61  **/
62 _PUBLIC_ bool conv_str_size_error(const char * str, uint64_t * val)
63 {
64         char *              end = NULL;
65         unsigned long long  lval;
66
67         if (str == NULL || *str == '\0') {
68                 return false;
69         }
70
71         lval = strtoull(str, &end, 10 /* base */);
72         if (end == NULL || end == str) {
73                 return false;
74         }
75
76         if (*end) {
77                 if (strwicmp(end, "K") == 0) {
78                         lval *= 1024ULL;
79                 } else if (strwicmp(end, "M") == 0) {
80                         lval *= (1024ULL * 1024ULL);
81                 } else if (strwicmp(end, "G") == 0) {
82                         lval *= (1024ULL * 1024ULL * 1024ULL);
83                 } else if (strwicmp(end, "T") == 0) {
84                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
85                 } else if (strwicmp(end, "P") == 0) {
86                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
87                 } else {
88                         return false;
89                 }
90         }
91
92         *val = (uint64_t)lval;
93         return true;
94 }
95
96 /**
97  * Parse a uint64_t value from a string
98  *
99  * val will be set to the value read.
100  *
101  * @retval true if parsing was successful, false otherwise
102  */
103 _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
104 {
105         char *              end = NULL;
106         unsigned long long  lval;
107
108         if (str == NULL || *str == '\0') {
109                 return false;
110         }
111
112         lval = strtoull(str, &end, 10 /* base */);
113         if (end == NULL || *end != '\0' || end == str) {
114                 return false;
115         }
116
117         *val = (uint64_t)lval;
118         return true;
119 }
120
121 /**
122  * Compare 2 strings.
123  *
124  * @note The comparison is case-insensitive.
125  **/
126 _PUBLIC_ bool strequal(const char *s1, const char *s2)
127 {
128         if (s1 == s2)
129                 return true;
130         if (!s1 || !s2)
131                 return false;
132   
133         return strcasecmp_m(s1,s2) == 0;
134 }
135