Merge branch 'master' of git://github.com/microbuilder/LPC1343CodeBase
[hackover2013-badge-firmware.git] / project / commands.c
1 /**************************************************************************/
2 /*!
3 @file commands.c
4 @author K. Townsend (microBuilder.eu)
5
6 @brief Common helper-functions for all commands in the 'core/cmd'
7 command-line interpretter.
8
9 @section LICENSE
10
11 Software License Agreement (BSD License)
12
13 Copyright (c) 2010, microBuilder SARL
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 3. Neither the name of the copyright holders nor the
24 names of its contributors may be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
28 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /**************************************************************************/
39
40 #include <stdio.h>
41 #include <string.h> // memset
42 #include <ctype.h> // isdigit, isspace, etc.
43
44 #include "core/cmd/cmd.h"
45 #include "commands.h"
46
47 /**************************************************************************/
48 /*!
49 @brief Attempts to convert the supplied decimal or hexadecimal
50 string to the matching 32-bit value. All hexadecimal values
51 must be preceded by either '0x' or '0X' to be properly parsed.
52
53 @param[in] s
54 Input string
55 @param[out] result
56 Signed 32-bit integer to hold the conversion results
57
58 @section Example
59
60 @code
61 char *hex = "0xABCD";
62 char *dec = "1234";
63
64 // Convert supplied values to integers
65 int32_t hexValue, decValue;
66 getNumber (hex, &hexValue);
67 getNumber (dec, &decValue);
68
69 @endcode
70 */
71 /**************************************************************************/
72 int getNumber (char *s, int32_t *result)
73 {
74 int32_t value;
75 uint32_t mustBeHex = FALSE;
76 uint32_t sgn = 1;
77 const unsigned char hexToDec [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15};
78
79 if (!s)
80 return 0;
81
82 // Check if this is a hexadecimal value
83 if ((strlen (s) > 2) && (!strncmp (s, "0x", 2) || !strncmp (s, "0X", 2)))
84 {
85 mustBeHex = TRUE;
86 s += 2;
87 }
88
89 // Check for negative sign
90 if (!mustBeHex && *s && (*s == '-'))
91 {
92 sgn = -1;
93 s++;
94 }
95
96 // Try to convert value
97 for (value = 0; *s; s++)
98 {
99 if (mustBeHex && isxdigit ((uint8_t)*s))
100 value = (value << 4) | hexToDec [toupper((uint8_t)*s) - '0'];
101 else if (isdigit ((uint8_t)*s))
102 value = (value * 10) + ((uint8_t)*s - '0');
103 else
104 {
105 printf ("Malformed number. Must be decimal number, or hex value preceeded by '0x'%s", CFG_PRINTF_NEWLINE);
106 return 0;
107 }
108 }
109
110 // Set number to negative value if required
111 if (!mustBeHex)
112 value *= sgn;
113
114 *result = value;
115
116 return 1;
117 }
118
119
120
This page took 0.056563 seconds and 5 git commands to generate.