You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010-2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2010 Kicad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <sch_lib_table.h>
  25. #include <sch_lib_table_lexer.h>
  26. #include <sch_lpid.h>
  27. using namespace SCH;
  28. // If LIB_TABLE::Test() is conditioned on DEBUG being defined, build with
  29. // CMAKE_BUILD_TYPE=Debug, otherwise don't worry about it, because it will work
  30. // on any CMAKE_BUILD_TYPE, including Release.
  31. void LIB_TABLE::Test()
  32. {
  33. SCH_LIB_TABLE_LEXER slr(
  34. "(lib_table \n"
  35. " (lib (logical www) (type http) (full_uri http://kicad.org/libs) (options \"\"))\n"
  36. " (lib (logical meparts) (type dir) (full_uri /tmp/eeschema-lib) (options useVersioning))\n"
  37. // " (lib (logical meparts) (type dir) (full_uri /tmp/eeschema-lib) (options \"\"))\n"
  38. " (lib (logical old-project) (type schematic)(full_uri /tmp/old-schematic.sch) (options \"\"))\n"
  39. ")\n"
  40. ,
  41. wxT( "inline text" ) // source
  42. );
  43. // read the "( lib_table" pair of tokens
  44. slr.NextTok();
  45. slr.NextTok();
  46. // parse the rest of input to slr
  47. Parse( &slr );
  48. STRING_FORMATTER sf;
  49. // format this whole table into sf, it will be sorted by logicalName.
  50. Format( &sf, 0 );
  51. printf( "test 'Parse() <-> Format()' round tripping:\n" );
  52. printf( "%s", sf.GetString().c_str() );
  53. printf( "\ntest a lookup of 'www':\n" );
  54. const LIB_TABLE::ROW* www = FindRow( "www" );
  55. if( www )
  56. {
  57. // found, print it just to prove it.
  58. sf.Clear();
  59. www->Format( &sf, 1 );
  60. printf( "%s", sf.GetString().c_str() );
  61. }
  62. else
  63. printf( "not found\n" );
  64. printf( "\nlist of logical libraries:\n" );
  65. STRINGS logNames = GetLogicalLibs();
  66. for( STRINGS::const_iterator it = logNames.begin(); it!=logNames.end(); ++it )
  67. {
  68. printf( "logicalName: %s\n", it->c_str() );
  69. }
  70. // find a part
  71. LPID lpid( "meparts:tigers/ears" );
  72. LookupPart( lpid );
  73. }
  74. int main( int argc, char** argv )
  75. {
  76. LIB_TABLE lib_table;
  77. try
  78. {
  79. // test exceptions:
  80. // THROW_PARSE_ERROR( wxT("test problem"), wxT("input"), 23, 46 );
  81. //THROW_IO_ERROR( wxT("io test") );
  82. lib_table.Test();
  83. }
  84. catch( PARSE_ERROR& ioe ) // most derived class first
  85. {
  86. printf( "%s\n", (const char*) ioe.errorText.ToUTF8() );
  87. printf( "%s%s",
  88. ioe.inputLine.c_str(),
  89. ioe.inputLine.empty() || *ioe.inputLine.rbegin() != '\n' ?
  90. "\n" : "" ); // rare not to have \n at end
  91. printf( "%*s^\n", ioe.byteIndex>=1 ? ioe.byteIndex-1 : 0, "" );
  92. }
  93. catch( IO_ERROR& ioe )
  94. {
  95. printf( "%s\n", (const char*) ioe.errorText.ToUTF8() );
  96. }
  97. return 0;
  98. }