summaryrefslogtreecommitdiff
path: root/new/test_sch_lib_table.cpp
blob: a38579460448c2bc742e15abbb34d44d84d70555 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2010-2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <sch_lib_table.h>
#include <sch_lib_table_lexer.h>
#include <sch_lpid.h>
#include <sch_part.h>

using namespace SCH;


// If LIB_TABLE::Test() is conditioned on DEBUG being defined, build with
// CMAKE_BUILD_TYPE=Debug, otherwise don't worry about it, because it will work
// on any CMAKE_BUILD_TYPE, including Release.
void LIB_TABLE::Test()
{
    SCH_LIB_TABLE_LEXER  slr(
        "(lib_table \n"
        "  (lib (logical www)           (type http)     (full_uri http://kicad.org/libs)    (options \"\"))\n"
        "  (lib (logical meparts)       (type dir)      (full_uri /tmp/eeschema-lib)        (options useVersioning))\n"
//        "  (lib (logical meparts)       (type dir)      (full_uri /tmp/eeschema-lib)        (options \"\"))\n"
        "  (lib (logical old-project)   (type schematic)(full_uri /tmp/old-schematic.sch)   (options \"\"))\n"
        ")\n"
        ,

        wxT( "inline text" )        // source
        );

    // read the "( lib_table" pair of tokens
    slr.NextTok();
    slr.NextTok();

    // parse the rest of input to slr
    Parse( &slr );

    STRING_FORMATTER    sf;

    // format this whole table into sf, it will be sorted by logicalName.
    Format( &sf, 0 );

    printf( "test 'Parse() <-> Format()' round tripping:\n" );
    printf( "%s", sf.GetString().c_str() );

    printf( "\ntest a lookup of 'www':\n" );
    const LIB_TABLE::ROW* www = FindRow( "www" );
    if( www )
    {
        // found, print it just to prove it.
        sf.Clear();

        www->Format( &sf, 1 );
        printf( "%s", sf.GetString().c_str() );
    }
    else
        printf( "not found\n" );

    printf( "\nlist of logical libraries:\n" );

    STRINGS logNames = GetLogicalLibs();
    for( STRINGS::const_iterator it = logNames.begin(); it!=logNames.end();  ++it )
    {
        printf( "logicalName: %s\n", it->c_str() );
    }

    // find a part
    LPID    lpid( "meparts:tigers/ears" );

    PART*   part = LookupPart( lpid );

    sf.Clear();
    part->Format( &sf, 0, 0 );

    printf( "%s", sf.GetString().c_str() );
}


int main( int argc, char** argv )
{
    LIB_TABLE   lib_table;

    try
    {
        // test exceptions:
        // THROW_PARSE_ERROR( wxT("test problem"), wxT("input"), 23, 46 );
        //THROW_IO_ERROR( wxT("io test") );

        lib_table.Test();
    }
    catch( PARSE_ERROR& ioe )   // most derived class first
    {
        printf( "%s\n", (const char*) ioe.errorText.ToUTF8() );

        printf( "%s%s",
                ioe.inputLine.c_str(),
                ioe.inputLine.empty() || *ioe.inputLine.rbegin() != '\n' ?
                        "\n" : "" );      // rare not to have \n at end

        printf( "%*s^\n", ioe.byteIndex>=1 ? ioe.byteIndex-1 : 0, "" );
    }
    catch( const IO_ERROR& ioe )
    {
        printf( "%s\n", (const char*) ioe.errorText.ToUTF8() );
    }

    return 0;
}