1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/*
* SPL - The SPL Programming Language
* Copyright (C) 2004, 2005 Clifford Wolf <clifford@clifford.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* mod_encode_xml.c: A simple XML encoding function
*/
/**
* A simple XML encoding function
*/
#include <stdlib.h>
#include "spl.h"
#include "compat.h"
extern void SPL_ABI(spl_mod_encode_xml_init)(struct spl_vm *vm, struct spl_module *mod, int restore);
extern void SPL_ABI(spl_mod_encode_xml_done)(struct spl_vm *vm, struct spl_module *mod);
static char *xml_encode(const char *source)
{
int source_i, target_i;
for (source_i = target_i = 0; source[source_i]; source_i++)
switch (source[source_i]) {
case '&':
/* & */
target_i += 5;
break;
case '<':
/* < */
target_i += 4;
break;
case '>':
/* > */
target_i += 4;
break;
case '"':
/* " */
target_i += 6;
break;
case '\'':
/* ' */
target_i += 6;
break;
default:
target_i++;
}
char *target = malloc(target_i+1);
for (source_i = target_i = 0; source[source_i]; source_i++)
switch (source[source_i]) {
case '&':
/* & */
target[target_i++] = '&';
target[target_i++] = 'a';
target[target_i++] = 'm';
target[target_i++] = 'p';
target[target_i++] = ';';
break;
case '<':
/* < */
target[target_i++] = '&';
target[target_i++] = 'l';
target[target_i++] = 't';
target[target_i++] = ';';
break;
case '>':
/* > */
target[target_i++] = '&';
target[target_i++] = 'g';
target[target_i++] = 't';
target[target_i++] = ';';
break;
case '"':
/* " */
target[target_i++] = '&';
target[target_i++] = 'q';
target[target_i++] = 'u';
target[target_i++] = 'o';
target[target_i++] = 't';
target[target_i++] = ';';
break;
case '\'':
/* ' */
target[target_i++] = '&';
target[target_i++] = 'a';
target[target_i++] = 'p';
target[target_i++] = 'o';
target[target_i++] = 's';
target[target_i++] = ';';
break;
default:
target[target_i++] = source[source_i];
}
target[target_i] = 0;
return target;
}
/**
* This function ecodes a text in XML. I.e. the characters &, <, >, " and '
* are translated to &, <, >, ", and '.
*
* This function is designed to be used with the encoding/quoting operator (::).
*/
// builtin encode_xml(text)
static struct spl_node *handler_encode_xml(struct spl_task *task, void *data UNUSED)
{
char *source = spl_clib_get_string(task);
return SPL_NEW_STRING(xml_encode(source));
}
void SPL_ABI(spl_mod_encode_xml_init)(struct spl_vm *vm, struct spl_module *mod UNUSED, int restore UNUSED)
{
spl_clib_reg(vm, "encode_xml", handler_encode_xml, 0);
}
void SPL_ABI(spl_mod_encode_xml_done)(struct spl_vm *vm UNUSED, struct spl_module *mod UNUSED)
{
return;
}
|