SOL9 2.0 Class: Base64FileDecoder

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

Source code

/*
 * Base64FileDecoder.h 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2009/04/01             
// Modified the decode method:
// 1 open the output file as mode "wb" in fopen().

#include <sol\ByteArray.h>
#include <sol\Base64Decoder.h>
#include <sol\String.h>

#include <sol\Folder.h>


namespace SOL {

class Base64FileDecoder: public Base64Decoder {

public:
    /**
     * Constructor.
     */
    Base64FileDecoder()
    {

    }


public:
    /**
     * Write Base64 decoded string to an output file.
     */
    virtual bool write(unsigned char* enc, const int len, FILE* out)
    {
        bool rc = false;
        int rx = fwrite(enc, 1, len, out);
        if (rx == len) {
            rc = true;
        }
        return rc;
    }

public:
    /**
     * Base64-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.
     */
    //2009/04/01
    bool decode(const TCHAR* filePath, const TCHAR* decodeFile) {
        bool rc = false;
        if (GetFileAttributes(filePath) == 0xffffffff) {
            _tprintf(_T("File not found: %s\n"), filePath);
            return rc;
        }
            
        FILE* in = fopen(filePath, _T("r"));
        if (in) {
            printf("Opened %s\n", filePath);
            Folder folder(decodeFile);

            if (folder.exists() == false) {
                if (folder.createFolder() == false) {
                    fclose(in);
                    _tprintf(_T("Failed to create a folder for %s\n"), decodeFile);
                    return rc;
                }
            }
            //Modified to open the output file as mode "wb" in fopen().

            //FILE* out = fopen(decodeFile, "w");
            FILE* out = fopen(decodeFile, _T("wb"));

            if (out) {
                _tprintf(_T("Opened write file=%s\n"), decodeFile);
                try {
                    char data[80];

                    while (fgets(data,sizeof(data), in)) {
                    
                        unsigned char* dec = NULL;
                        unsigned int  dlen = 0;
                        int r = Base64Decoder::decode(data, strlen(data), &dec, &dlen);
                        if (r>0) {
                            rc = write(dec, dlen, out);
                        }
                        delete [] dec;    
                    }
                } catch (...) {

                }
                fclose(out);
            } else {
                _tprintf(_T("Faild to open an output file %s\n"), decodeFile);
            }
            fclose(in);
        }
        return rc;
    }
};

}
 

Last modified: 19 Dec 2009

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