Station API

Dependents:   GMCStation

Committer:
yamaguch
Date:
Mon Dec 12 02:33:21 2011 +0000
Revision:
1:a22e390c70b3
Child:
2:a9d1a9c92927
0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 1:a22e390c70b3 1 /*
yamaguch 1:a22e390c70b3 2 Copyright (c) 2011, Senio Networks, Inc.
yamaguch 1:a22e390c70b3 3
yamaguch 1:a22e390c70b3 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yamaguch 1:a22e390c70b3 5 of this software and associated documentation files (the "Software"), to deal
yamaguch 1:a22e390c70b3 6 in the Software without restriction, including without limitation the rights
yamaguch 1:a22e390c70b3 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yamaguch 1:a22e390c70b3 8 copies of the Software, and to permit persons to whom the Software is
yamaguch 1:a22e390c70b3 9 furnished to do so, subject to the following conditions:
yamaguch 1:a22e390c70b3 10
yamaguch 1:a22e390c70b3 11 The above copyright notice and this permission notice shall be included in
yamaguch 1:a22e390c70b3 12 all copies or substantial portions of the Software.
yamaguch 1:a22e390c70b3 13
yamaguch 1:a22e390c70b3 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yamaguch 1:a22e390c70b3 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yamaguch 1:a22e390c70b3 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yamaguch 1:a22e390c70b3 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yamaguch 1:a22e390c70b3 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yamaguch 1:a22e390c70b3 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yamaguch 1:a22e390c70b3 20 THE SOFTWARE.
yamaguch 1:a22e390c70b3 21 */
yamaguch 1:a22e390c70b3 22
yamaguch 1:a22e390c70b3 23 #ifndef UTILS_H
yamaguch 1:a22e390c70b3 24 #define UTILS_H
yamaguch 1:a22e390c70b3 25
yamaguch 1:a22e390c70b3 26 #include "mbed.h"
yamaguch 1:a22e390c70b3 27 #include <ctype.h>
yamaguch 1:a22e390c70b3 28 #include <stdarg.h>
yamaguch 1:a22e390c70b3 29
yamaguch 1:a22e390c70b3 30 class Utils {
yamaguch 1:a22e390c70b3 31 public:
yamaguch 1:a22e390c70b3 32 static int encodeFormUrl(char *s, char *t) {
yamaguch 1:a22e390c70b3 33 char *head = t;
yamaguch 1:a22e390c70b3 34 for (char c; (c = *s) != 0; s++)
yamaguch 1:a22e390c70b3 35 switch (c) {
yamaguch 1:a22e390c70b3 36 case '\r':
yamaguch 1:a22e390c70b3 37 break;
yamaguch 1:a22e390c70b3 38 case ' ' :
yamaguch 1:a22e390c70b3 39 *t++ = '+';
yamaguch 1:a22e390c70b3 40 break;
yamaguch 1:a22e390c70b3 41 default:
yamaguch 1:a22e390c70b3 42 t += sprintf(t, isalnum(c) ? "%c" : (c == '\n') ? "\r%c" : "%%%02X", c);
yamaguch 1:a22e390c70b3 43 }
yamaguch 1:a22e390c70b3 44 *t = '\0';
yamaguch 1:a22e390c70b3 45 return t - head;
yamaguch 1:a22e390c70b3 46 }
yamaguch 1:a22e390c70b3 47
yamaguch 1:a22e390c70b3 48 static void encodeBase64(char ibuf[], int length, char *obuf) {
yamaguch 1:a22e390c70b3 49 const char BASE64[] =
yamaguch 1:a22e390c70b3 50 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
yamaguch 1:a22e390c70b3 51 int i, j;
yamaguch 1:a22e390c70b3 52 for (i = j = 0; j < length; j += 3, i += 4) {
yamaguch 1:a22e390c70b3 53 long a = ibuf[j] << 16 |
yamaguch 1:a22e390c70b3 54 (j + 1 < length ? ibuf[j + 1] << 8 : 0) |
yamaguch 1:a22e390c70b3 55 (j + 2 < length ? ibuf[j + 2] : 0);
yamaguch 1:a22e390c70b3 56 for (int k = 3; k >= 0; k--, a >>= 6)
yamaguch 1:a22e390c70b3 57 obuf[i + k] = (j + k - 1) < length ? BASE64[a & 63] : '=';
yamaguch 1:a22e390c70b3 58 }
yamaguch 1:a22e390c70b3 59 obuf[i] = '\0';
yamaguch 1:a22e390c70b3 60 }
yamaguch 1:a22e390c70b3 61
yamaguch 1:a22e390c70b3 62 static void encodeBase64(char *ibuf, char *obuf) {
yamaguch 1:a22e390c70b3 63 encodeBase64(ibuf, strlen(ibuf), obuf);
yamaguch 1:a22e390c70b3 64 }
yamaguch 1:a22e390c70b3 65
yamaguch 1:a22e390c70b3 66 static bool fgetValues(FILE *fp, const char *pattern, ...) {
yamaguch 1:a22e390c70b3 67 va_list argv;
yamaguch 1:a22e390c70b3 68 va_start(argv, pattern);
yamaguch 1:a22e390c70b3 69
yamaguch 1:a22e390c70b3 70 rewind(fp);
yamaguch 1:a22e390c70b3 71 while (!feof(fp)) {
yamaguch 1:a22e390c70b3 72 char buf[128];
yamaguch 1:a22e390c70b3 73 fgets(buf, sizeof(buf) - 1, fp);
yamaguch 1:a22e390c70b3 74 int ret = vsscanf(buf, pattern, argv);
yamaguch 1:a22e390c70b3 75 if (ret > 0) {
yamaguch 1:a22e390c70b3 76 return true;
yamaguch 1:a22e390c70b3 77 }
yamaguch 1:a22e390c70b3 78 }
yamaguch 1:a22e390c70b3 79 return false;
yamaguch 1:a22e390c70b3 80 }
yamaguch 1:a22e390c70b3 81 };
yamaguch 1:a22e390c70b3 82 #endif