KiCad PCB EDA Suite
xnode.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) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 1992-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 
25 
26 #include <xnode.h>
27 #include <macros.h>
28 
29 typedef wxXmlAttribute XATTR;
30 
31 
32 void XNODE::Format( OUTPUTFORMATTER* out, int nestLevel )
33 {
34  switch( GetType() )
35  {
36  case wxXML_ELEMENT_NODE:
37  out->Print( nestLevel, "(%s", out->Quotew( GetName() ).c_str() );
38  FormatContents( out, nestLevel );
39  if( GetNext() )
40  out->Print( 0, ")\n" );
41  else
42  out->Print( 0, ")" );
43  break;
44 
45  default:
46  FormatContents( out, nestLevel );
47  }
48 }
49 
50 
51 void XNODE::FormatContents( OUTPUTFORMATTER* out, int nestLevel )
52 {
53  // output attributes first if they exist
54  for( XATTR* attr = (XATTR*) GetAttributes(); attr; attr = (XATTR*) attr->GetNext() )
55  {
56  out->Print( 0, " (%s %s)",
57  // attr names should never need quoting, no spaces, we designed the file.
58  out->Quotew( attr->GetName() ).c_str(),
59  out->Quotew( attr->GetValue() ).c_str()
60  );
61  }
62 
63  // we only expect to have used one of two types here:
64  switch( GetType() )
65  {
66  case wxXML_ELEMENT_NODE:
67 
68  // output children if they exist.
69  for( XNODE* kid = (XNODE*) GetChildren(); kid; kid = (XNODE*) kid->GetNext() )
70  {
71  if( kid->GetType() != wxXML_TEXT_NODE )
72  {
73  if( kid == GetChildren() )
74  out->Print( 0, "\n" );
75  kid->Format( out, nestLevel+1 );
76  }
77  else
78  {
79  kid->Format( out, 0 );
80  }
81  }
82  break;
83 
84  case wxXML_TEXT_NODE:
85  out->Print( 0, " %s", out->Quotew( GetContent() ).c_str() );
86  break;
87 
88  default:
89  ; // not supported
90  }
91 }
92 
93 // EOF
virtual void FormatContents(OUTPUTFORMATTER *out, int nestLevel)
Function FormatContents writes the contents of object as UTF8 out to an OUTPUTFORMATTER as an S-expre...
Definition: xnode.cpp:51
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
This file contains miscellaneous commonly used macros and functions.
XNODE * GetChildren() const
Definition: xnode.h:63
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:486
virtual void Format(OUTPUTFORMATTER *out, int nestLevel)
Function Format writes this object as UTF8 out to an OUTPUTFORMATTER as an S-expression.
Definition: xnode.cpp:32
Class XNODE holds an XML or S-expression element.
Definition: xnode.h:43
wxXmlAttribute XATTR
Definition: xnode.cpp:29
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:408
XNODE * GetNext() const
Definition: xnode.h:68