Subversion Repositories spk

Rev

Blame | Last modification | View Log | RSS feed

#include "../../Base Engine/Common/String.h"
#include "../../Base Engine/Common/StringList.h"


String ParseBlock ( FILE *id, String *str, CStringList *strings )
{
        strings->Clear();

        String rest;
        String line = *str;
        int depth = -1;
        while ( !feof(id) )
        {
                if ( (line.Left(2) == "//") || (line.Empty()) )
                {
                        line.GetEndOfLine ( id, NULL, false );
                        continue;
                }

                line = line.GetToken ( "//", 1, 1 );
                line.RemoveChar ( 9 );

                if ( line.IsIn ( "{" ) )
                        ++depth;
                if ( line.IsIn ("}") )
                {
                        if ( !depth )
                        {
                                rest += line.GetToken ( "}", 1, 1 );
                                break;
                        }
                        else
                        {
                                rest += line.GetToken ( "}", 1, 1 );
                                rest += "};";
                                line = line.GetToken ( "}", 2 );
                                --depth;

                                if ( !line.Empty() )
                                {
                                        line.GetEndOfLine ( id, NULL, false );
                                        continue;
                                }
                        }
                }

                rest += line;
                line.GetEndOfLine ( id, NULL, false );
        }

        String cmd = rest.GetToken ( "{", 1, 1 ).RemoveSpaces();
        rest = rest.GetToken ( "{", 2 );

        // parse strings
        String done;
        while ( rest.IsIn("\"") )
        {
                done += rest.GetToken ( "\"", 1, 1 );
                String s = rest.GetToken ( "\"", 2, 2 );

                if ( s.Empty() )
                        break;

                strings->PushBack ( s );
                done += "\"";
                done += "$STRING";
                done += (long)strings->Count();
                done += "\"";

                rest = rest.GetToken ( "\"", 3 );
        }

        if ( !rest.Empty() )
                done += rest;
        done.RemoveChar ( ' ' );

        *str = done;
        return cmd;
}

String ReplaceString ( String s, CStringList *strings )
{
        int num = 1;
        for ( SStringList *it = strings->Head(); it; it = it->next, num++ )
        {
                String str = String("\"$STRING") + (long)num + "\"";
                s = s.FindReplace ( str, it->str );
        }

        return s;
}


int main ()
{
        FILE *id = fopen ( "custommenu.txt", "r+" );
        if ( id )
        {
                String section;
                while ( !feof ( id ) )
                {
                        String line;
                        line.GetEndOfLine ( id, NULL, false );

                        if ( line.Empty() )
                                continue;
                        if ( line.Left(2) == "//" )
                                continue;

                        // do the brack
                        if ( line.IsIn ( "{" ) )
                        {
                                // parse the block
                                CStringList strings;
                                String cmd = ParseBlock ( id, &line, &strings );

                                printf ( "Cmd (%s) Rest=%s\n", cmd.c_str(), line.c_str() );
                                
                                // break the commands
                                int num = 0;
                                String *str = line.SplitToken ( ';', &num );
                                for ( int i = 0; i < num; i++ )
                                {
                                        String c = str[i].GetToken ( ":", 1, 1 ), r;
                                        if ( c.Empty() )
                                                continue;

                                        if ( c.IsIn ("{") )
                                        {
                                                if ( c.IsIn ("}") )
                                                {
                                                        r = String("{") + c.GetToken ( "{", 2 ).GetToken ( "}", 1, 1 ) + "}";
                                                        c = c.GetToken ( "{", 1, 1 );
                                                }
                                                else
                                                {
                                                        section = c.GetToken ( "{", 1, 1 );
                                                        c = c.GetToken ( "{", 2 );
                                                        r = str[i].GetToken ( ":", 2 );
                                                }
                                        }
                                        else if ( c.IsIn ( "}" ) )
                                        {
                                                section = "";
                                                continue;
                                        }
                                        else
                                        {
                                                r = str[i].GetToken ( ":", 2 );
                                                if ( r.IsIn ( "}" ) )
                                                {
                                                        r = r.GetToken ( "}", 1, 1 );
                                                        section = "";
                                                }
                                        }

                                        if ( r.IsIn("\"") )
                                                r = ReplaceString ( r, &strings );

                                        if ( section.Empty() )
                                                printf ( "Cmd: %s, Rest: %s\n", c.c_str(), r.c_str() );
                                        else
                                                printf ( "(%s) Cmd: %s, Rest: %s\n", section.c_str(), c.c_str(), r.c_str() );
                                }
                        }
                }
        }
        char pause;
        scanf ( "%c", &pause );

        return 0;
}