KiCad PCB EDA Suite
s_expr_loader.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012-2013 Alexander Lunev <al.lunev@yahoo.com>
5  * Copyright (C) 2012 KiCad Developers, see CHANGELOG.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 
29 #include <dsnlexer.h>
30 #include <macros.h>
31 #include <wx/xml/xml.h>
32 #include <xnode.h>
33 
34 namespace PCAD2KICAD {
35 
36 static KEYWORD empty_keywords[1] = {};
37 static const char ACCEL_ASCII_KEYWORD[] = "ACCEL_ASCII";
38 
39 void LoadInputFile( wxString aFileName, wxXmlDocument* aXmlDoc )
40 {
41  char line[sizeof( ACCEL_ASCII_KEYWORD )];
42  int tok;
43  XNODE* iNode = NULL, *cNode = NULL;
44  wxString str, propValue, content;
45  wxCSConv conv( wxT( "windows-1251" ) );
46 
47  FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
48 
49  if( !fp )
50  THROW_IO_ERROR( wxT( "Unable to open file: " ) + aFileName );
51 
52  // check file format
53  if( !fgets( line, sizeof( line ), fp )
54  // first line starts with "ACCEL_ASCII" with optional stuff on same line after that.
55  || memcmp( line, ACCEL_ASCII_KEYWORD, sizeof(ACCEL_ASCII_KEYWORD)-1 ) )
56  THROW_IO_ERROR( "Unknown file type" );
57 
58  // rewind the file
59  fseek( fp, 0, SEEK_SET );
60 
61  // lexer now owns fp, will close on exception or return
62  DSNLEXER lexer( empty_keywords, 0, fp, aFileName );
63 
64  iNode = new XNODE( wxXML_ELEMENT_NODE, wxT( "www.lura.sk" ) );
65 
66  while( ( tok = lexer.NextTok() ) != DSN_EOF )
67  {
68  if( tok == DSN_RIGHT )
69  {
70  iNode = iNode->GetParent();
71  }
72  else if( tok == DSN_LEFT )
73  {
74  tok = lexer.NextTok();
75  str = wxEmptyString;
76  cNode = new XNODE( wxXML_ELEMENT_NODE, wxString( lexer.CurText(), conv ) );
77  iNode->AddChild( cNode );
78  iNode = cNode;
79  }
80  else if( cNode )
81  {
82  str = wxString( lexer.CurText(), conv );
83  if( tok == DSN_STRING )
84  {
85  // update attribute
86  if( iNode->GetAttribute( wxT( "Name" ), &propValue ) )
87  {
88  iNode->DeleteAttribute( wxT( "Name" ) );
89  iNode->AddAttribute( wxT( "Name" ), propValue + wxT( ' ' ) + str );
90  }
91  else
92  iNode->AddAttribute( wxT( "Name" ), str );
93  }
94  else if( str != wxEmptyString )
95  {
96  // update node content
97  content = cNode->GetNodeContent() + wxT( ' ' ) + str;
98 
99  if( cNode->GetChildren() )
100  cNode->GetChildren()->SetContent( content );
101  else
102  cNode->AddChild( new wxXmlNode( wxXML_TEXT_NODE,
103  wxEmptyString,
104  content ) );
105  }
106  }
107  }
108 
109  if( iNode )
110  {
111  aXmlDoc->SetRoot( iNode );
112  //aXmlDoc->Save( wxT( "test.xml" ) );
113  }
114 }
115 
116 } // namespace PCAD2KICAD
void LoadInputFile(wxString aFileName, wxXmlDocument *aXmlDoc)
This file contains miscellaneous commonly used macros and functions.
XNODE * GetParent() const
Definition: xnode.h:73
XNODE * GetChildren() const
Definition: xnode.h:63
static KEYWORD empty_keywords[1]
#define THROW_IO_ERROR(x)
Definition: utf8.cpp:60
Class XNODE holds an XML or S-expression element.
Definition: xnode.h:43
static const char ACCEL_ASCII_KEYWORD[]
int NextTok()
Function NextTok returns the next token found in the input file or DSN_EOF when reaching the end of f...
Definition: dsnlexer.cpp:540
Struct KEYWORD holds a keyword string and its unique integer token.
Definition: dsnlexer.h:40
const char * CurText()
Function CurText returns a pointer to the current token's text.
Definition: dsnlexer.h:479
Class DSNLEXER implements a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:79