Browse code

str2float and str2int added.

Jan Janak authored on 30/03/2003 20:25:47
Showing 1 changed files
... ...
@@ -28,6 +28,7 @@
28 28
  *
29 29
  * History
30 30
  * ------
31
+ * 2003-03-30 str2int and str2float added (janakj)
31 32
  * 2003-02-28 scratchpad compatibility abandoned (jiri)
32 33
  * 2003-02-13 strlower added (janakj)
33 34
  * 2003-01-29 pathmax added (jiri)
... ...
@@ -372,4 +373,55 @@ static inline void strlower(str* _s)
372 372
 }
373 373
 
374 374
 
375
+/*
376
+ * Convert a str into integer
377
+ */
378
+static inline int str2int(str* _s, unsigned int* _r)
379
+{
380
+	int i;
381
+	
382
+	*_r = 0;
383
+	for(i = 0; i < _s->len; i++) {
384
+		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
385
+			*_r *= 10;
386
+			*_r += _s->s[i] - '0';
387
+		} else {
388
+			return -1;
389
+		}
390
+	}
391
+	
392
+	return 0;
393
+}
394
+
395
+
396
+/*
397
+ * Convert a str to float
398
+ */
399
+static inline int str2float(str* _s, float* _r)
400
+{
401
+	int i, dot = 0;
402
+	float order = 0.1;
403
+
404
+	*_r = 0;
405
+	for(i = 0; i < _s->len; i++) {
406
+		if (_s->s[i] == '.') {
407
+			if (dot) return -1;
408
+			dot = 1;
409
+			continue;
410
+		}
411
+		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
412
+			if (dot) {
413
+				*_r += (_s->s[i] - '0') * order;
414
+				order /= 10;
415
+			} else {
416
+				*_r *= 10;
417
+				*_r += _s->s[i] - '0';
418
+			}
419
+		} else {
420
+			return -2;
421
+		}
422
+	}
423
+	return 0;
424
+}
425
+
375 426
 #endif