upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Arguments.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. using namespace std;
  19. namespace mbed {
  20. Arguments::Arguments(const char* rqs) {
  21. obj_name = NULL;
  22. method_name = NULL;
  23. argc = 0;
  24. // This copy can be removed if we can assume the request string is
  25. // persistent and writable for the duration of the call
  26. strcpy(request, rqs);
  27. // Initial '/'
  28. char* p = request;
  29. if (*p != '/') return;
  30. p++;
  31. // Object Name
  32. p = search_arg(&obj_name, p, '/');
  33. if (p == NULL) return;
  34. // Method Name
  35. p = search_arg(&method_name, p, ' ');
  36. if (p == NULL) return;
  37. // Arguments
  38. while (true) {
  39. argv[argc] = NULL;
  40. p = search_arg(&argv[argc], p, ' ');
  41. if (argv[argc] != NULL) argc++;
  42. if (p == NULL) break;
  43. }
  44. index = -1;
  45. }
  46. char* Arguments::search_arg(char **arg, char *p, char next_sep) {
  47. char *s = p;
  48. while (true) {
  49. if ((*p == '/') || (*p == ' ') || (*p == '\n') || (*p == '\0')) break;
  50. p++;
  51. }
  52. if (p == s) return NULL;
  53. *arg = s;
  54. char separator = *p;
  55. *p = '\0';
  56. p++;
  57. return (separator == next_sep) ? (p) : (NULL);
  58. }
  59. template<> PinName Arguments::getArg<PinName>(void) {
  60. index++;
  61. return parse_pins(argv[index]);
  62. }
  63. template<> int Arguments::getArg<int>(void) {
  64. index++;
  65. char *pEnd;
  66. return strtol(argv[index], &pEnd, 10);
  67. }
  68. template<> const char* Arguments::getArg<const char*>(void) {
  69. index++;
  70. return argv[index];
  71. }
  72. template<> char Arguments::getArg<char>(void) {
  73. index++;
  74. return *argv[index];
  75. }
  76. template<> double Arguments::getArg<double>(void) {
  77. index++;
  78. return atof(argv[index]);
  79. }
  80. template<> float Arguments::getArg<float>(void) {
  81. index++;
  82. return atof(argv[index]);
  83. }
  84. Reply::Reply(char* r) {
  85. first = true;
  86. *r = '\0';
  87. reply = r;
  88. }
  89. void Reply::separator(void) {
  90. if (first) {
  91. first = false;
  92. } else {
  93. *reply = ' '; reply++;
  94. }
  95. }
  96. template<> void Reply::putData<const char*>(const char* s) {
  97. separator();
  98. reply += sprintf(reply, "%s", s);
  99. }
  100. template<> void Reply::putData<char*>(char* s) {
  101. separator();
  102. reply += sprintf(reply, "%s", s);
  103. }
  104. template<> void Reply::putData<char>(char c) {
  105. separator();
  106. reply += sprintf(reply, "%c", c);
  107. }
  108. template<> void Reply::putData<int>(int v) {
  109. separator();
  110. reply += sprintf(reply, "%d", v);
  111. }
  112. template<> void Reply::putData<float>(float f) {
  113. separator();
  114. reply += sprintf(reply, "%.17g", f);
  115. }
  116. } // namespace mbed