/*
* HTMLDecoder.h
* Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL9
#pragma once
#include <sol\Object.h>
#include <sol\String.h>
#include <sol\StringT.h>
namespace SOL {
/**
*
*/
class HTMLDecoder {
public:
/**
*/
HTMLDecoder() {
}
public:
/**
*
*/
bool decode(const char* string, String& decoded) {
bool rc = false;
if (string) {
char* dec = decode(string);
decoded = dec;
delete [] dec;
rc = true;
}
return rc;
}
public:
/**
*
*/
bool decode(const char* string, __out StringT<char>& decoded) {
bool rc = false;
if (string) {
char* dec = decode(string);
decoded.shallowCopy(dec);
rc = true;
}
return rc;
}
public:
/**
*
*/
char* decode(const char* string) {
static char* entities[] = {"&", "<", ">", "'", """,};// " "};
static char specials[] = {'&', '<', '>', '\'', '"', };//' '};
if (string == NULL) {
return NULL;
}
char* decoded = new char[strlen(string)+1];
const char* src = string;
char* dst = decoded;
int entlen = sizeof(entities)/sizeof(entities[0]);
while (*src) {
if (*src == '&') {
for (int i= 0; i<entlen; i++) {
char* entity = entities[i];
if (memcmp(src, entity, strlen(entity)) ==0) {
char s = specials[i];
*dst++ = s;
src = src + strlen(entity);
break;
}
}
} else {
*dst++ = *src++;
}
}
*dst = '\0';
return decoded;
}
};
}
|