Subversion Repositories spk

Rev

Rev 197 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "spkprogress.h"

#ifndef _INCLUDE7ZIP
#include <ansi7zip/7Decoder.h>
#endif

#include <MultiSpkFile.h>
#include "../../HiP/HiP.h"
#include <Logging/log.h>

#include "CyString.h"

#ifdef _DEBUG
#define CLEANUP fclose ( id ); if ( data ) delete data; if ( uncomprData ) delete uncomprData; if ( !removeFile.Empty() ) remove ( removeFile.c_str() ); char pause; printf ( "Press Enter to Close\n" ); scanf ( "%c", &pause );
#else
#define CLEANUP fclose ( id ); if ( data ) delete data; if ( uncomprData ) delete uncomprData; if ( !removeFile.Empty() ) remove ( removeFile.c_str() ); 
#endif

char *ReadNextLine ( char *data, long *len, Utils::String *str )
{
        int pos = 0;
        bool end = false;
        while ( pos < *len )
        {
                if ( data[pos] == '\n' )
                        break;
                if ( data[pos] == '\0' )
                {
                        end = true;
                        break;
                }
                ++pos;
        }

        if ( end )
        {
                *len = 0;
                *str = data;
                return NULL;
        }

        data[pos] = '\0';
        *str = data;
        data[pos] = '\n';
        *len -= (pos + 1);

        return data + (pos + 1);
}

char *LineByLineRead ( char *data, long *len, const Utils::String &end, CyString *readData )
{
        Utils::String line;
        while ( true )
        {
                data = ReadNextLine ( data, len, &line );

                if ( line == end )
                        break;
                *readData += (line + "\n");
        }

        return data;
}

int main ( int argc, char **argv )
{
        CConsoleLog::create();

        Utils::String filename ( argv[0] );
        filename = filename.tokens("\\", -1, -1);

        printf ( "SPKConvert V1.10 (SPK VERSION %.2f) 28/07/2007 by Cycrow\n", FILEVERSION );

        if ( argc < 3 )
        {
                printf ( "Syntax, %s <oldspkfile> <newspkfile>\n", filename.c_str() );
                exit ( 1 );
        }

        Utils::String oldfile(argv[1]);
        Utils::String newfile(argv[2]);


        int ret = CBaseFile::CheckFile ( oldfile );
        if ( ret != SPKFILE_INVALID && ret != SPKFILE_OLD )
        {
                printf ( "Spk file is already in the new format, unable to convert\n" );
                exit ( 1 );
        }

        // firstcheck if the file exists
        FILE *id = fopen ( argv[1], "rb" );
        if ( !id )
        {
                printf ( "Unable to open file: %s\n", argv[1] );
                exit ( 0 );
        }

        // convert the old file
        CSpkFile newspkfile;
        newspkfile.convertOld(oldfile);

        MyProgress *progress = new MyProgress ( 0 );

        // now save the spk file
        printf ( "\nStarting to write new spk file\n" );

        //TODO: add multispk support
        /*
        if ( mspk )
        {
                for ( SMultiSpkFile *it = mspk->GetFileList()->First(); it; it = mspk->GetFileList()->Next() )
                {
                        for ( C_File *f = it->pFile->GetFileList()->First(); f; f = it->pFile->GetFileList()->Next() )
                        {
                                printf ( "* Compressing %s...\n\t>", f->GetNameDirectory(it->pFile).c_str() );
                                if ( f->CompressData ( spkfile->GetDataCompression(), progress ) )
                                {
                                        progress->PrintDone();
                                        printf ( "< (Done)\n" );
                                }
                                else
                                {
                                        progress->Reset();
                                        printf ( "< (Error)\n" );
                                }
                        }

                        FILE *id = tmpfile();
                        if ( id )
                        {
                                it->pFile->WriteData ( id, NULL );
                                it->lSize = ftell ( id );
                                fclose ( id );
                        }
                }
        }
        else
        {
        */
                for ( C_File *f = newspkfile.GetFileList()->First(); f; f = newspkfile.GetFileList()->Next() )
                {
                        wprintf(L"* Compressing %s...\n\t>", f->getNameDirectory(&newspkfile).c_str() );
                        if ( f->CompressData ( newspkfile.dataCompression(), progress ) )
                        {
                                progress->PrintDone();
                                printf ( "< (Done)\n" );
                        }
                        else
                        {
                                progress->Reset();
                                printf ( "< (Error)\n" );
                        }
                }
        //}

        printf ( "* Writing to %s... ", argv[2] );
        /*
        if ( mspk )
        {
                if ( mspk->WriteFile ( CyString(argv[2]) ) )
                        printf ( "(Done)\n" );
                else
                        printf ( "(Error)\n" );
        }
        else
        {*/
                newspkfile.writeFile(Utils::WString::FromString(argv[2]));
                printf ( "(Done)\n" );
        //}

        printf ( "SPK file has been converted successfully\n" );
//      CLEANUP

        return 1;
}