[7d3ae3e] | 1 | /* |
---|
| 2 | * Estonian ID card plugin for web browsers |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2010-2011 Codeborne <info@codeborne.com> |
---|
| 5 | * |
---|
| 6 | * This is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This software is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 19 | * |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <string.h> |
---|
| 23 | #include <stdlib.h> |
---|
| 24 | #include "esteid_json.h" |
---|
| 25 | |
---|
| 26 | int isEscapedSymbol(char c) { |
---|
| 27 | return strchr("\"\\\r\n\t", c) != NULL; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | char *EstEID_jsonString(const char *src) { |
---|
| 31 | int specialSymbolCount = 0; |
---|
| 32 | char *s = (char *)src; |
---|
| 33 | while (*s) if (isEscapedSymbol(*(s++))) specialSymbolCount++; |
---|
| 34 | char *result = (char*)malloc(strlen(src) + specialSymbolCount + 1); |
---|
| 35 | s = (char *)src; |
---|
| 36 | char *r = result; |
---|
| 37 | while (*s) { |
---|
| 38 | if (isEscapedSymbol(*s)) *(r++) = '\\'; |
---|
| 39 | switch (*s) { |
---|
| 40 | case '\n' : *(r++) = 'n'; break; |
---|
| 41 | case '\r' : *(r++) = 'r'; break; |
---|
| 42 | case '\t' : *(r++) = 't'; break; |
---|
| 43 | default: *(r++) = *s; |
---|
| 44 | } |
---|
| 45 | s++; |
---|
| 46 | } |
---|
| 47 | *r = 0; |
---|
| 48 | return result; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | char *EstEID_mapEntryToJson(struct EstEID_MapEntry entry) { |
---|
| 52 | char *k = EstEID_jsonString(entry.key); |
---|
| 53 | char *v = EstEID_jsonString(entry.value); |
---|
| 54 | char *result = (char *)malloc(strlen(k) + strlen(v) + 7); |
---|
| 55 | sprintf(result, "\"%s\": \"%s\"", k, v); |
---|
| 56 | free(k); |
---|
| 57 | free(v); |
---|
| 58 | return result; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | char *EstEID_mapToJson(EstEID_Map map) { |
---|
| 62 | char *result = (char *)malloc(3); |
---|
| 63 | sprintf(result, "{"); |
---|
| 64 | while (map) { |
---|
| 65 | char *entry = EstEID_mapEntryToJson(*map); |
---|
| 66 | result = (char *)realloc(result, strlen(result) + strlen(entry) + 4); |
---|
| 67 | strcat(result, entry); |
---|
| 68 | free(entry); |
---|
| 69 | if (map->next) strcat(result, ", "); |
---|
| 70 | map = map->next; |
---|
| 71 | } |
---|
| 72 | strcat(result, "}"); |
---|
| 73 | return result; |
---|
| 74 | } |
---|
| 75 | |
---|