]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rpc/Arguments.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rpc / Arguments.cpp
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "Arguments.h"
17 #include "pinmap.h"
18
19 using namespace std;
20
21 namespace mbed {
22
23 Arguments::Arguments(const char* rqs) {
24     obj_name = NULL;
25     method_name = NULL;
26     argc = 0;
27
28     // This copy can be removed if we can assume the request string is
29     // persistent and writable for the duration of the call
30     strcpy(request, rqs);
31
32     // Initial '/'
33     char* p = request;
34     if (*p != '/') return;
35     p++;
36
37     // Object Name
38     p = search_arg(&obj_name, p, '/');
39     if (p == NULL) return;
40
41     // Method Name
42     p = search_arg(&method_name, p, ' ');
43     if (p == NULL) return;
44
45     // Arguments
46     while (true) {
47         argv[argc] = NULL;
48         p = search_arg(&argv[argc], p, ' ');
49         if (argv[argc] != NULL) argc++;
50         if (p == NULL) break;
51     }
52
53     index = -1;
54 }
55
56 char* Arguments::search_arg(char **arg, char *p, char next_sep) {
57     char *s = p;
58     while (true) {
59         if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break;
60         p++;
61     }
62     if (p == s) return NULL;
63     *arg = s;
64     char separator = *p;
65     *p = '\0';
66     p++;
67     return (separator == next_sep) ? (p) : (NULL);
68 }
69
70 template<> PinName Arguments::getArg<PinName>(void) {
71     index++;
72     return parse_pins(argv[index]);
73 }
74
75 template<> int Arguments::getArg<int>(void) {
76     index++;
77     char *pEnd;
78     return strtol(argv[index], &pEnd, 10);
79 }
80
81 template<> const char* Arguments::getArg<const char*>(void) {
82     index++;
83     return argv[index];
84 }
85
86 template<> char Arguments::getArg<char>(void) {
87     index++;
88     return *argv[index];
89 }
90
91 template<> double Arguments::getArg<double>(void) {
92     index++;
93     return atof(argv[index]);
94 }
95
96 template<> float Arguments::getArg<float>(void) {
97     index++;
98     return atof(argv[index]);
99 }
100
101 Reply::Reply(char* r) {
102     first = true;
103     *r = '\0';
104     reply = r;
105 }
106
107 void Reply::separator(void) {
108     if (first) {
109         first = false;
110     } else {
111         *reply = ' '; reply++;
112     }
113 }
114
115 template<> void Reply::putData<const char*>(const char* s) {
116     separator();
117     reply += sprintf(reply, "%s", s);
118 }
119
120 template<> void Reply::putData<char*>(char* s) {
121     separator();
122     reply += sprintf(reply, "%s", s);
123 }
124
125 template<> void Reply::putData<char>(char c) {
126     separator();
127     reply += sprintf(reply, "%c", c);
128 }
129
130 template<> void Reply::putData<int>(int v) {
131     separator();
132     reply += sprintf(reply, "%d", v);
133 }
134
135 template<> void Reply::putData<float>(float f) {
136     separator();
137     reply += sprintf(reply, "%.17g", f);
138 }
139
140 } // namespace mbed