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 <s_expr_loader.h>
30 
31 #include <dsnlexer.h>
32 #include <macros.h>
33 #include <xnode.h>
34 
35 namespace PCAD2KICAD {
36 
37 static KEYWORD empty_keywords[1] = {};
38 static const char ACCEL_ASCII_KEYWORD[] = "ACCEL_ASCII";
39 
40 void LoadInputFile( const wxString& aFileName, wxXmlDocument* aXmlDoc )
41 {
42  char line[sizeof( ACCEL_ASCII_KEYWORD )];
43  int tok;
44  XNODE* iNode = NULL, *cNode = NULL;
45  wxString str, propValue, content;
46  wxCSConv conv( wxT( "windows-1251" ) );
47 
48  FILE* fp = wxFopen( aFileName, wxT( "rt" ) );
49 
50  if( !fp )
51  THROW_IO_ERROR( wxT( "Unable to open file: " ) + aFileName );
52 
53  // check file format
54  if( !fgets( line, sizeof( line ), fp )
55  // first line starts with "ACCEL_ASCII" with optional stuff on same line after that.
56  || memcmp( line, ACCEL_ASCII_KEYWORD, sizeof(ACCEL_ASCII_KEYWORD)-1 ) )
57  THROW_IO_ERROR( "Unknown file type" );
58 
59  // rewind the file
60  fseek( fp, 0, SEEK_SET );
61 
62  // lexer now owns fp, will close on exception or return
63  DSNLEXER lexer( empty_keywords, 0, fp, aFileName );
64 
65  iNode = new XNODE( wxXML_ELEMENT_NODE, wxT( "www.lura.sk" ) );
66 
67  while( ( tok = lexer.NextTok() ) != DSN_EOF )
68  {
69  if( tok == DSN_RIGHT )
70  {
71  iNode = iNode->GetParent();
72  }
73  else if( tok == DSN_LEFT )
74  {
75  tok = lexer.NextTok();
76  str = wxEmptyString;
77  cNode = new XNODE( wxXML_ELEMENT_NODE, wxString( lexer.CurText(), conv ) );
78  iNode->AddChild( cNode );
79  iNode = cNode;
80  }
81  else if( cNode )
82  {
83  str = wxString( lexer.CurText(), conv );
84  if( tok == DSN_STRING )
85  {
86  // update attribute
87  if( iNode->GetAttribute( wxT( "Name" ), &propValue ) )
88  {
89  iNode->DeleteAttribute( wxT( "Name" ) );
90  iNode->AddAttribute( wxT( "Name" ), propValue + wxT( ' ' ) + str );
91  }
92  else
93  iNode->AddAttribute( wxT( "Name" ), str );
94  }
95  else if( str != wxEmptyString )
96  {
97  // update node content
98  content = cNode->GetNodeContent() + wxT( ' ' ) + str;
99 
100  if( cNode->GetChildren() )
101  cNode->GetChildren()->SetContent( content );
102  else
103  cNode->AddChild( new wxXmlNode( wxXML_TEXT_NODE,
104  wxEmptyString,
105  content ) );
106  }
107  }
108  }
109 
110  if( iNode )
111  {
112  aXmlDoc->SetRoot( iNode );
113  //aXmlDoc->Save( wxT( "test.xml" ) );
114  }
115 }
116 
117 } // namespace PCAD2KICAD
void LoadInputFile(const 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]
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
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
Class DSNLEXER implements a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:79