aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/gatsc/main.cpp
blob: 5291a333d890d1239fb42f3d7b21a224018f04f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libgats library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include <bu/optparser.h>
#include <bu/string.h>
#include <bu/file.h>
#include <bu/sio.h>
#include <bu/streamstack.h>
#include <bu/deflate.h>

#include "gats/types.h"
#include "gats/gatsstream.h"

using namespace Bu;

class Options : public OptParser
{
public:
    Options( int argc, char *argv[] ) :
        bCompile( true ),
        bCompress( false )
    {
        addHelpBanner("Gats Compiler\nUsage: gatsc [options] [input]\n");

        addOption( sInput, 'i', "input", "Specify input file.");
        addOption( sOutput, 'o', "output", "Specify output file.");

        addOption( bCompile, 'd', "decompile",
            "Convert binary gats to text gats.");

        addOption( bCompress, 'z', "compress", "Compress with deflate.");

        addHelpOption('h', "help", "This Help");

        setNonOption( slot( this, &Options::setInput ) );

        setOverride("decompile", false );
        setOverride("compress", true );

        parse( argc, argv );
    }

    int setInput( StrArray aParam )
    {
        sInput = aParam[0];
        return 0;
    }

    bool bCompile;
    bool bCompress;
    String sInput;
    String sOutput;
};

int main( int argc, char *argv[] )
{
    Options opt( argc, argv );

    if( opt.sInput.isEmpty() )
    {
        sio << "You must specify an input." << sio.nl << sio.nl;
        return 1;
    }

    if( opt.sOutput.isEmpty() )
    {
        opt.sOutput.set( opt.sInput.begin(), opt.sInput.find('.') );
        if( opt.bCompress )
            opt.sOutput += ".gatz";
        else
            opt.sOutput += ".gats";
    }

    if( opt.bCompile )
    {
        File fIn( opt.sInput, File::Read );
        StreamStack ssOut( new File( opt.sOutput, File::WriteNew ) );
        if( opt.bCompress )
        {
            ssOut.pushFilter<Deflate>();
        }
        Gats::GatsStream gs( ssOut );
        Gats::Object *pObj = Gats::Object::strToGats( fIn.readAll() );
        gs.writeObject( pObj );
        delete pObj;
    }

    return 0;
}