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 373
 }
373 374
 
374 375
 
376
+/*
377
+ * Convert a str into integer
378
+ */
379
+static inline int str2int(str* _s, unsigned int* _r)
380
+{
381
+	int i;
382
+	
383
+	*_r = 0;
384
+	for(i = 0; i < _s->len; i++) {
385
+		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
386
+			*_r *= 10;
387
+			*_r += _s->s[i] - '0';
388
+		} else {
389
+			return -1;
390
+		}
391
+	}
392
+	
393
+	return 0;
394
+}
395
+
396
+
397
+/*
398
+ * Convert a str to float
399
+ */
400
+static inline int str2float(str* _s, float* _r)
401
+{
402
+	int i, dot = 0;
403
+	float order = 0.1;
404
+
405
+	*_r = 0;
406
+	for(i = 0; i < _s->len; i++) {
407
+		if (_s->s[i] == '.') {
408
+			if (dot) return -1;
409
+			dot = 1;
410
+			continue;
411
+		}
412
+		if ((_s->s[i] >= '0') && (_s->s[i] <= '9')) {
413
+			if (dot) {
414
+				*_r += (_s->s[i] - '0') * order;
415
+				order /= 10;
416
+			} else {
417
+				*_r *= 10;
418
+				*_r += _s->s[i] - '0';
419
+			}
420
+		} else {
421
+			return -2;
422
+		}
423
+	}
424
+	return 0;
425
+}
426
+
375 427
 #endif