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