SOL9 2.0 Class: HTMLDecoder

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * HTMLDecoder.h 
 * Copyright (c) 2011 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 Object {

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[] = {"&amp;", "&lt;", "&gt;", "&#039;", "&quot;",};// "&nbsp;"};
        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;
    }
};


}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.