SOL9 2.0 Class: HTMLFileEncoder

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

Source code

/*
 * HTMLFileEncoder.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


#pragma once

#include <sol\HTMLEncoder.h>
#include <sol\Folder.h>

// 2008/09/24 Modified to create an output folder if it does not exists.
// 2008/09/30 Created a HTMLFileEncoder.h and added to the folder include\sol.

namespace SOL {

class HTMLFileEncoder: public HTMLEncoder {

public:
    HTMLFileEncoder() {

    }

public:
    /**
     * Write HTML encoded string to an output file.
     * The tab code in the string is converted to 4 space characters.
     */
    virtual bool write(const char* enc, const int len, FILE* out)
    {
        bool rc = false;

        char* spaces_4 = "    ";
        int rlen = 0;
        for (int i = 0; i<len; i++) {
            if (enc[i] == '\t') {
                rlen += fwrite(spaces_4, 1, strlen(spaces_4), out);
            } else {
                rlen +=fwrite(&enc[i], 1, 1, out);
            }

        }
    
        //nt rx = fwrite(enc, 1, len, out);
        if (rlen>0) {
            printf("input =%d bytes: fwritten %d bytes\n", len, rlen);
            rc = true;
        }
        return rc;
    }

public:
    /**
     * HTML-encoding for the file specified by the first argument,
     * and write the encoded string to the file specified by the second argument.
     * If the output file already exists, then the contents of the file will be 
     * replaced by new encoded string.
     */
    bool encode(const TCHAR* htmlFile, const TCHAR* encodeFile) {
        bool rc = false;

        if (GetFileAttributes(htmlFile) == 0xffffffff) {
            printf("HTML File not found: %s\n", htmlFile); 
            return rc;
        }
            
        FILE* in = fopen(htmlFile, _T("r"));
        if (in) {
            printf("Opened %s\n", htmlFile);
            Folder folder(encodeFile);
            if (folder.exists() == false) {
                if (folder.createFolder() == false) {
                    fclose(in);
                    printf("Failed to create a folder for %s\n", encodeFile);
                    return rc;
                }
            }

            FILE* out = fopen(encodeFile, _T("w"));

            if (out) {
                printf("Opened write file=%s\n", encodeFile);
                try {
                    int len = filelength(fileno(in));
                    printf("In file %d bytes\n", len);
                    char* html = new char[len+1];
                    int rlen = fread(html, 1, len, in) ;
                    if (rlen>0) {
                        printf("fread %d bytes\n", rlen);
                        html[rlen] = '\0';
                        char* enc = HTMLEncoder::encode(html);
                        if (enc) {
                            rc = write(enc, strlen(enc), out);
                        }
                        delete [] enc;    
                    }
                    delete [] html;
                } catch (...) {

                }
                fclose(out);
            } else {
                printf("Faild to open an output file %s\n", encodeFile);
            }
            fclose(in);
        } else {
            printf("Failed to open an HTML file %s.\n", htmlFile);

        }
        return rc;
    }
};

}
 

Last modified: 1 Feb 2012

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