00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "cinterf.h"
00010 #include <stdlib.h>
00011 #ifdef HAVE_MALLOC_H
00012 #include HAVE_MALLOC_H
00013 #endif
00014 #include "error.h"
00015 #include <errno.h>
00016 #include <string.h>
00017 #include <stdio.h>
00018 #include "dtd.h"
00019
00020 #define CHARSET 256
00021
00022 static int
00023 do_quote(prolog_term in, prolog_term quoted, char **map)
00024 { char *ins;
00025 unsigned len;
00026 unsigned char *s;
00027 char outbuf[1024];
00028 char *out = outbuf;
00029 int outlen = sizeof(outbuf);
00030 int o = 0;
00031 int changes = 0;
00032
00033 prolog_term tmp;
00034
00035 ins = p2c_string( in);
00036
00037 len = strlen( ins);
00038
00039 if ( len == 0 )
00040 return p2p_unify(in, quoted);
00041
00042 for(s = (unsigned char*)ins ; len-- > 0; s++ )
00043 { int c = *s;
00044
00045 if ( map[c] )
00046 { int l = strlen(map[c]);
00047 if ( o+l >= outlen )
00048 { outlen *= 2;
00049
00050 if ( out == outbuf )
00051 { out = malloc(outlen);
00052 memcpy(out, outbuf, sizeof(outbuf));
00053 } else
00054 { out = realloc(out, outlen);
00055 }
00056 }
00057 memcpy(&out[o], map[c], l);
00058 o += l;
00059 changes++;
00060 } else
00061 { if ( o >= outlen-1 )
00062 { outlen *= 2;
00063
00064 if ( out == outbuf )
00065 { out = malloc(outlen);
00066 memcpy(out, outbuf, sizeof(outbuf));
00067 } else
00068 { out = realloc(out, outlen);
00069 }
00070 }
00071 out[o++] = c;
00072 }
00073 }
00074 out[o]= 0;
00075
00076 if ( changes > 0 )
00077 {
00078 c2p_string( out, tmp);
00079 return p2p_unify( quoted, tmp);
00080 }
00081 else
00082 return p2p_unify(in, quoted);
00083 }
00084
00085
00086 static int
00087 pl_xml_quote_attribute()
00088 {
00089 prolog_term in = reg_term(1);
00090 prolog_term out = reg_term(2);
00091 static char **map;
00092
00093 if ( !map )
00094 { int i;
00095
00096 if ( !(map = malloc(CHARSET*sizeof(char*))) )
00097 return sgml2pl_error(ERR_ERRNO, errno);
00098
00099 for(i=0; i<CHARSET; i++)
00100 map[i] = NULL;
00101
00102 map['<'] = "<";
00103 map['>'] = ">";
00104 map['&'] = "&";
00105 map['\''] = "'";
00106 map['"'] = """;
00107 }
00108
00109 return do_quote(in, out, map);
00110 }
00111
00112
00113 static int
00114 pl_xml_quote_cdata()
00115 {
00116 prolog_term in = reg_term(1);
00117 prolog_term out = reg_term(2);
00118 static char **map;
00119
00120 if ( !map )
00121 { int i;
00122
00123 if ( !(map = malloc(CHARSET*sizeof(char*))) )
00124 return sgml2pl_error(ERR_ERRNO, errno);
00125
00126 for(i=0; i<CHARSET; i++)
00127 map[i] = NULL;
00128
00129 map['<'] = "<";
00130 map['>'] = ">";
00131 map['&'] = "&";
00132 }
00133
00134 return do_quote(in, out, map);
00135 }
00136
00137
00138 static int
00139 pl_xml_name()
00140 { char *ins;
00141 unsigned len;
00142 static dtd_charclass *map;
00143 unsigned int i;
00144 prolog_term in = reg_term(1);
00145
00146
00147 if ( !map )
00148 map = new_charclass();
00149
00150 ins = p2c_string( in);
00151
00152 len = strlen( ins);
00153
00154 if ( len == 0 )
00155 return FALSE;
00156
00157 if ( !(map->class[ins[0] & 0xff] & CH_NMSTART) )
00158 return FALSE;
00159 for(i=1; i<len; i++)
00160 {
00161 if ( !(map->class[ins[i] & 0xff] & CH_NAME) )
00162 return FALSE;
00163 }
00164
00165 return TRUE;
00166 }