summaryrefslogtreecommitdiffstats
path: root/cpukit/libmisc/shell/str2int.c
blob: a569772bb629024527052f791aeebf1444ea65d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 *  Author: Fernando RUIZ CASAS
 *  Work: fernando.ruiz@ctv.es
 *  Home: correo@fernando-ruiz.com
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


#include <rtems/shell.h>
#include "internal.h"

/*
 * str to int "0xaffe" "0b010010" "0123" "192939"
 */
int rtems_shell_str2int(char * s) {
  int sign=1;
  int base=10;
  int value=0;
  int digit;

  if (!s) return 0;
  if (*s) {
    if (*s=='-') {
      sign=-1;
      s++;
      if (!*s) return 0;
    }
    if (*s=='0') {
      s++;
      switch(*s) {
        case 'x':
        case 'X':
          s++;
 	  base=16;
	  break;
        case 'b':
        case 'B':
          s++;
	  base=2;
	  break;
        default:
          base=8;
	  break;
      }
    }
    while (*s) {
      switch(*s) {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
          digit=*s-'0';
 	  break;
        case 'A':
        case 'B':
        case 'C':
        case 'D':
        case 'E':
        case 'F':
          digit=*s-'A'+10;
	  break;
        case 'a':
        case 'b':
        case 'c':
        case 'd':
        case 'e':
        case 'f':
          digit=*s-'a'+10;
	  break;
        default:
          return value*sign;
      }
      if (digit>base)
        return value*sign;
      value=value*base+digit;
      s++;
    }
 }
 return value*sign;
}