Hiroshi Yamaguchi / Station

Dependents:   GMCStation

Committer:
yamaguch
Date:
Wed Nov 30 04:35:45 2011 +0000
Revision:
0:d81f611c59ec

        

Who changed what in which revision?

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