Browse code

lib/srdb1: support types for unsigned int and unsigned long long

- defined db types DB1_UINT and DB1_UBIGINT

Daniel-Constantin Mierla authored on 05/07/2018 11:19:25
Showing 4 changed files
... ...
@@ -69,7 +69,10 @@
69 69
 #include "db_ut.h"
70 70
 
71 71
 
72
-inline int db_str2int(const char* _s, int* _v)
72
+/**
73
+ *
74
+ */
75
+int db_str2int(const char* _s, int* _v)
73 76
 {
74 77
 	long tmp;
75 78
 	char* p = NULL;
... ...
@@ -95,7 +98,39 @@ inline int db_str2int(const char* _s, int* _v)
95 98
 }
96 99
 
97 100
 
98
-inline int db_str2longlong(const char* _s, long long * _v)
101
+/**
102
+ *
103
+ */
104
+int db_str2uint(const char* _s, unsigned int* _v)
105
+{
106
+	unsigned long tmp;
107
+	char* p = NULL;
108
+
109
+	if (!_s || !_v) {
110
+		LM_ERR("Invalid parameter value\n");
111
+		return -1;
112
+	}
113
+
114
+	tmp = strtoul(_s, &p, 10);
115
+	if ((tmp == ULONG_MAX && errno == ERANGE) ||
116
+				(tmp < INT_MIN) || (tmp > UINT_MAX)) {
117
+		LM_ERR("Value out of range\n");
118
+		return -1;
119
+	}
120
+	if (p && *p != '\0') {
121
+		LM_ERR("Unexpected characters: [%s]\n", p);
122
+		return -2;
123
+	}
124
+
125
+	*_v = (unsigned int)tmp;
126
+	return 0;
127
+}
128
+
129
+
130
+/**
131
+ *
132
+ */
133
+int db_str2longlong(const char* _s, long long * _v)
99 134
 {
100 135
 	long long tmp;
101 136
 	char* p = NULL;
... ...
@@ -120,10 +155,38 @@ inline int db_str2longlong(const char* _s, long long * _v)
120 155
 }
121 156
 
122 157
 
158
+/**
159
+ *
160
+ */
161
+int db_str2ulonglong(const char* _s, unsigned long long * _v)
162
+{
163
+	unsigned long long tmp;
164
+	char* p = NULL;
165
+
166
+	if (!_s || !_v) {
167
+		LM_ERR("Invalid parameter value\n");
168
+		return -1;
169
+	}
170
+
171
+	tmp = strtoull(_s, &p, 10);
172
+	if (errno == ERANGE) {
173
+		LM_ERR("Value out of range\n");
174
+		return -1;
175
+	}
176
+	if (p && *p != '\0') {
177
+		LM_ERR("Unexpected characters: [%s]\n", p);
178
+		return -2;
179
+	}
180
+
181
+	*_v = tmp;
182
+	return 0;
183
+}
184
+
185
+
123 186
 /*
124 187
  * Convert a string to double
125 188
  */
126
-inline int db_str2double(const char* _s, double* _v)
189
+int db_str2double(const char* _s, double* _v)
127 190
 {
128 191
 	if ((!_s) || (!_v)) {
129 192
 		LM_ERR("Invalid parameter value\n");
... ...
@@ -135,11 +198,10 @@ inline int db_str2double(const char* _s, double* _v)
135 198
 }
136 199
 
137 200
 
138
-
139 201
 /*
140 202
  * Convert an integer to string
141 203
  */
142
-inline int db_int2str(int _v, char* _s, int* _l)
204
+int db_int2str(int _v, char* _s, int* _l)
143 205
 {
144 206
 	int ret;
145 207
 
... ...
@@ -160,9 +222,32 @@ inline int db_int2str(int _v, char* _s, int* _l)
160 222
 
161 223
 
162 224
 /*
163
- * Convert an long long to string
225
+ * Convert an unsigned integer to string
164 226
  */
165
-inline int db_longlong2str(long long _v, char* _s, int* _l)
227
+int db_uint2str(unsigned int _v, char* _s, int* _l)
228
+{
229
+	int ret;
230
+
231
+	if ((!_s) || (!_l) || (!*_l)) {
232
+		LM_ERR("Invalid parameter value\n");
233
+		return -1;
234
+	}
235
+
236
+	ret = snprintf(_s, *_l, "%u", _v);
237
+	if (ret < 0 || ret >= *_l) {
238
+		LM_ERR("Error in snprintf\n");
239
+		return -1;
240
+	}
241
+	*_l = ret;
242
+
243
+	return 0;
244
+}
245
+
246
+
247
+/*
248
+ * Convert a long long to string
249
+ */
250
+int db_longlong2str(long long _v, char* _s, int* _l)
166 251
 {
167 252
 	int ret;
168 253
 
... ...
@@ -182,10 +267,33 @@ inline int db_longlong2str(long long _v, char* _s, int* _l)
182 267
 }
183 268
 
184 269
 
270
+/*
271
+ * Convert an unsigned long long to string
272
+ */
273
+int db_ulonglong2str(unsigned long long _v, char* _s, int* _l)
274
+{
275
+	int ret;
276
+
277
+	if ((!_s) || (!_l) || (!*_l)) {
278
+		LM_ERR("Invalid parameter value\n");
279
+		return -1;
280
+	}
281
+
282
+	ret = snprintf(_s, *_l, "%llu", _v);
283
+	if (ret < 0 || ret >= *_l) {
284
+		LM_ERR("Error in snprintf\n");
285
+		return -1;
286
+	}
287
+	*_l = ret;
288
+
289
+	return 0;
290
+}
291
+
292
+
185 293
 /*
186 294
  * Convert a double to string
187 295
  */
188
-inline int db_double2str(double _v, char* _s, int* _l)
296
+int db_double2str(double _v, char* _s, int* _l)
189 297
 {
190 298
 	int ret;
191 299
 
... ...
@@ -208,7 +316,7 @@ inline int db_double2str(double _v, char* _s, int* _l)
208 316
 /*
209 317
  * Convert a string to time_t
210 318
  */
211
-inline int db_str2time(const char* _s, time_t* _v)
319
+int db_str2time(const char* _s, time_t* _v)
212 320
 {
213 321
 	struct tm time;
214 322
 
... ...
@@ -240,7 +348,7 @@ inline int db_str2time(const char* _s, time_t* _v)
240 348
 /**
241 349
  *
242 350
  */
243
-inline int db_time2str_ex(time_t _v, char* _s, int* _l, int _qmode)
351
+int db_time2str_ex(time_t _v, char* _s, int* _l, int _qmode)
244 352
 {
245 353
 	struct tm* t;
246 354
 	int l;
... ...
@@ -275,7 +383,7 @@ inline int db_time2str_ex(time_t _v, char* _s, int* _l, int _qmode)
275 383
 /**
276 384
  *
277 385
  */
278
-inline int db_time2str(time_t _v, char* _s, int* _l)
386
+int db_time2str(time_t _v, char* _s, int* _l)
279 387
 {
280 388
 	return db_time2str_ex(_v, _s, _l, 1);
281 389
 }
... ...
@@ -48,7 +48,17 @@ int db_str2int(const char* _s, int* _v);
48 48
 
49 49
 
50 50
 /**
51
- * Converts a char into an long long value.
51
+ * Converts a char into an unsigned integer value.
52
+ *
53
+ * \param _s source value
54
+ * \param _v target value
55
+ * \return zero on success, negative on conversion errors
56
+ */
57
+int db_str2uint(const char* _s, unsigned int* _v);
58
+
59
+
60
+/**
61
+ * Converts a char into a long long value.
52 62
  *
53 63
  * \param _s source value
54 64
  * \param _v target value
... ...
@@ -57,6 +67,16 @@ int db_str2int(const char* _s, int* _v);
57 67
 int db_str2longlong(const char* _s, long long* _v);
58 68
 
59 69
 
70
+/**
71
+ * Converts a char into an unsigned long long value.
72
+ *
73
+ * \param _s source value
74
+ * \param _v target value
75
+ * \return zero on success, negative on conversion errors
76
+ */
77
+int db_str2ulonglong(const char* _s, unsigned long long* _v);
78
+
79
+
60 80
 /**
61 81
  * Converts a char into a double value.
62 82
  *
... ...
@@ -68,7 +88,7 @@ int db_str2double(const char* _s, double* _v);
68 88
 
69 89
 
70 90
 /**
71
- * Converts a integer value in a char pointer.
91
+ * Converts an integer value in a char pointer.
72 92
  *
73 93
  * \param _v source value
74 94
  * \param _s target value
... ...
@@ -78,6 +98,17 @@ int db_str2double(const char* _s, double* _v);
78 98
 int db_int2str(int _v, char* _s, int* _l);
79 99
 
80 100
 
101
+/**
102
+ * Converts an unsigned integer value in a char pointer.
103
+ *
104
+ * \param _v source value
105
+ * \param _s target value
106
+ * \param _l available length and target length
107
+ * \return zero on success, negative on conversion errors
108
+ */
109
+int db_uint2str(unsigned int _v, char* _s, int* _l);
110
+
111
+
81 112
 /**
82 113
  * Converts a long long value in a char pointer.
83 114
  *
... ...
@@ -89,6 +120,17 @@ int db_int2str(int _v, char* _s, int* _l);
89 120
 int db_longlong2str(long long _v, char* _s, int* _l);
90 121
 
91 122
 
123
+/**
124
+ * Converts an unsigned long long value in a char pointer.
125
+ *
126
+ * \param _v source value
127
+ * \param _s target value
128
+ * \param _l available length and target length
129
+ * \return zero on success, negative on conversion errors
130
+ */
131
+int db_ulonglong2str(unsigned long long _v, char* _s, int* _l);
132
+
133
+
92 134
 /**
93 135
  * Converts a double value into a char pointer.
94 136
  *
... ...
@@ -81,6 +81,17 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
81 81
 		}
82 82
 		break;
83 83
 
84
+	case DB1_UINT:
85
+		LM_DBG("converting UNSIGNED INT [%s]\n", _s);
86
+		if (db_str2uint(_s, &VAL_UINT(_v)) < 0) {
87
+			LM_ERR("error while converting unsigned integer value from string\n");
88
+			return -2;
89
+		} else {
90
+			VAL_TYPE(_v) = DB1_UINT;
91
+			return 0;
92
+		}
93
+		break;
94
+
84 95
 	case DB1_BIGINT:
85 96
 		LM_DBG("converting BIGINT [%s]\n", _s);
86 97
 		if (db_str2longlong(_s, &VAL_BIGINT(_v)) < 0) {
... ...
@@ -92,6 +103,17 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
92 103
 		}
93 104
 		break;
94 105
 
106
+	case DB1_UBIGINT:
107
+		LM_DBG("converting UNSIGNED BIGINT [%s]\n", _s);
108
+		if (db_str2ulonglong(_s, &VAL_UBIGINT(_v)) < 0) {
109
+			LM_ERR("error while converting unsigned big integer value from string\n");
110
+			return -3;
111
+		} else {
112
+			VAL_TYPE(_v) = DB1_UBIGINT;
113
+			return 0;
114
+		}
115
+		break;
116
+
95 117
 	case DB1_BITMAP:
96 118
 		LM_DBG("converting BITMAP [%s]\n", _s);
97 119
 		if (db_str2int(_s, &VAL_INT(_v)) < 0) {
... ...
@@ -222,7 +244,16 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
222 244
 	switch(VAL_TYPE(_v)) {
223 245
 	case DB1_INT:
224 246
 		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
225
-			LM_ERR("error while converting string to int\n");
247
+			LM_ERR("error while converting int to string\n");
248
+			return -2;
249
+		} else {
250
+			return 0;
251
+		}
252
+		break;
253
+
254
+	case DB1_UINT:
255
+		if (db_uint2str(VAL_UINT(_v), _s, _len) < 0) {
256
+			LM_ERR("error while converting unsigned int to string\n");
226 257
 			return -2;
227 258
 		} else {
228 259
 			return 0;
... ...
@@ -231,7 +262,16 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
231 262
 
232 263
 	case DB1_BIGINT:
233 264
 		if (db_longlong2str(VAL_BIGINT(_v), _s, _len) < 0) {
234
-			LM_ERR("error while converting string to big int\n");
265
+			LM_ERR("error while converting big int to string\n");
266
+			return -3;
267
+		} else {
268
+			return 0;
269
+		}
270
+		break;
271
+
272
+	case DB1_UBIGINT:
273
+		if (db_ulonglong2str(VAL_UBIGINT(_v), _s, _len) < 0) {
274
+			LM_ERR("error while converting unsigned big int to string\n");
235 275
 			return -3;
236 276
 		} else {
237 277
 			return 0;
... ...
@@ -239,8 +279,8 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
239 279
 		break;
240 280
 
241 281
 	case DB1_BITMAP:
242
-		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
243
-			LM_ERR("error while converting string to int\n");
282
+		if (db_uint2str(VAL_BITMAP(_v), _s, _len) < 0) {
283
+			LM_ERR("error while converting bitmap to string\n");
244 284
 			return -4;
245 285
 		} else {
246 286
 			return 0;
... ...
@@ -249,7 +289,7 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
249 289
 
250 290
 	case DB1_DOUBLE:
251 291
 		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
252
-			LM_ERR("error while converting string to double\n");
292
+			LM_ERR("error while converting double to string\n");
253 293
 			return -5;
254 294
 		} else {
255 295
 			return 0;
... ...
@@ -258,7 +298,7 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
258 298
 
259 299
 	case DB1_DATETIME:
260 300
 		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
261
-			LM_ERR("failed to convert string to time_t\n");
301
+			LM_ERR("failed to convert time_t string\n");
262 302
 			return -8;
263 303
 		} else {
264 304
 			return 0;
... ...
@@ -55,6 +55,8 @@ typedef enum {
55 55
 	DB1_DATETIME,   /**< represents date and time                 */
56 56
 	DB1_BLOB,       /**< represents a large binary object         */
57 57
 	DB1_BITMAP,     /**< an one-dimensional array of 32 flags     */
58
+	DB1_UINT,       /**< represents an 32 bit unsigned int number */
59
+	DB1_UBIGINT,    /**< represents an 64 bit unsigned int number */
58 60
 	DB1_UNKNOWN     /**< represents an unknown type               */
59 61
 } db_type_t;
60 62
 
... ...
@@ -79,14 +81,16 @@ typedef struct {
79 81
 	int free;		/**< Means that the value should be freed */
80 82
 	/** Column value structure that holds the actual data in a union.  */
81 83
 	union {
82
-		int           int_val;    /**< integer value              */
83
-		long long     ll_val;     /**< long long value            */
84
-		double        double_val; /**< double value               */
85
-		time_t        time_val;   /**< unix time_t value          */
86
-		const char*   string_val; /**< zero terminated string     */
87
-		str           str_val;    /**< str type string value      */
88
-		str           blob_val;   /**< binary object data         */
89
-		unsigned int  bitmap_val; /**< Bitmap data type           */
84
+		int            int_val;    /**< integer value              */
85
+		long long      ll_val;     /**< long long value            */
86
+		double         double_val; /**< double value               */
87
+		time_t         time_val;   /**< unix time_t value          */
88
+		const char*    string_val; /**< zero terminated string     */
89
+		str            str_val;    /**< str type string value      */
90
+		str            blob_val;   /**< binary object data         */
91
+		unsigned int   bitmap_val; /**< Bitmap data type           */
92
+		unsigned int   uint_val;   /**< unsigned integer value     */
93
+		unsigned long long ull_val;  /**< unsigned long long value */
90 94
 	} val;
91 95
 } db_val_t;
92 96
 
... ...
@@ -125,10 +129,10 @@ typedef struct {
125 129
 
126 130
 
127 131
 /**
128
- * Use this macro if you need to access the integer value in the db_val_t structure
129
- * casted to unsigned int.
132
+ * Use this macro if you need to access the unsigned integer value in
133
+ * the db_val_t structure.
130 134
  */
131
-#define VAL_UINT(dv)    ((unsigned int)(dv)->val.int_val)
135
+#define VAL_UINT(dv)    ((dv)->val.uint_val)
132 136
 
133 137
 
134 138
 /**
... ...
@@ -136,6 +140,11 @@ typedef struct {
136 140
  */
137 141
 #define VAL_BIGINT(dv)    ((dv)->val.ll_val)
138 142
 
143
+/**
144
+ * Use this macro if you need to access the unsigned long long value in
145
+ * the db_val_t structure.
146
+ */
147
+#define VAL_UBIGINT(dv)   ((dv)->val.ull_val)
139 148
 
140 149
 /**
141 150
  * Use this macro if you need to access the double value in the db_val_t structure.