aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/gatscon/treetogats.cpp
blob: df8f87dcd3f8e2db1b8aedce6f2cb5065a4a9114 (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
/*
 * 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 "treetogats.h"

#include <QTreeWidgetItem>

#include <gats/types.h>

Gats::Object *treeToGats( QTreeWidgetItem *pRoot )
{
    QString sType = pRoot->text( 1 );
    QByteArray baDat = pRoot->text( 2 ).toAscii();
    if( sType == "int" )
    {
        return new Gats::Integer( strtoll( baDat.constData(), NULL, 10 ) );
    }
    else if( sType == "str" )
    {
        return new Gats::String( baDat.constData(), baDat.size() );
    }
    else if( sType == "float" )
    {
        return new Gats::Float( strtod( baDat.constData(), NULL ) );
    }
    else if( sType == "bool" )
    {
        return new Gats::Boolean( baDat == "true" );
    }
    else if( sType == "list" )
    {
        Gats::List *pRet = new Gats::List();
        for( int j = 0; j < pRoot->childCount(); j++ )
        {
            pRet->append( treeToGats( pRoot->child( j ) ) );
        }
        return pRet;
    }
    else if( sType == "dict" )
    {
        Gats::Dictionary *pRet = new Gats::Dictionary();
        for( int j = 0; j < pRoot->childCount(); j++ )
        {
            QTreeWidgetItem *pChild = pRoot->child( j );
            pRet->insert(
                pChild->text( 0 ).toAscii().constData(),
                treeToGats( pChild )
                );
        }
        return pRet;
    }

    throw Bu::ExceptionBase("Unhandled type found.");
}