KiCad PCB EDA Suite
shape_file_io.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) 2013 CERN
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <cassert>
28 #include <string>
29 
30 #include <geometry/shape.h>
31 #include <geometry/shape_file_io.h>
32 
34 {
35  m_groupActive = false;
36  m_mode = IOM_WRITE;
37  m_file = stdout;
38 }
39 
40 SHAPE_FILE_IO::SHAPE_FILE_IO( const std::string& aFilename, SHAPE_FILE_IO::IO_MODE aMode )
41 {
42  m_groupActive = false;
43 
44  if( aFilename.length() )
45  {
46  switch( aMode )
47  {
48  case IOM_READ: m_file = fopen( aFilename.c_str(), "rb" ); break;
49  case IOM_WRITE: m_file = fopen( aFilename.c_str(), "wb" ); break;
50  case IOM_APPEND: m_file = fopen( aFilename.c_str(), "ab" ); break;
51  default:
52  return;
53  }
54  }
55  else
56  {
57  m_file = NULL;
58  }
59 
60  m_mode = aMode;
61  // fixme: exceptions
62 }
63 
64 
66 {
67  if( !m_file )
68  return;
69 
70  if( m_groupActive && m_mode != IOM_READ )
71  fprintf( m_file, "endgroup\n" );
72 
73  if ( m_file != stdout )
74  {
75  fclose( m_file );
76  }
77 }
78 
79 
81 {
82  /* char tmp[1024];
83 
84  do {
85 
86  if (fscanf(m_file, "%s", tmp) != 1)
87  return NULL;
88 
89  if( !strcmp( tmp, "shape" )
90  break;
91  }
92 
93  int type;
94 
95  SHAPE *rv = NULL;
96 
97  fscanf(m_file,"%d %s", &type, tmp);
98 
99  switch(type)
100  {
101  case SHAPE::LINE_CHAIN:
102  rv = new SHAPE_LINE_CHAIN;
103  break;
104  }
105 
106  if(!rv)
107  return NULL;
108 
109  rv.Parse ( )
110 
111  fprintf(m_file,"shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
112 */
113  assert( false );
114  return NULL;
115 }
116 
117 
118 void SHAPE_FILE_IO::BeginGroup( const std::string& aName )
119 {
120  assert( m_mode != IOM_READ );
121 
122  if( !m_file )
123  return;
124 
125  fprintf( m_file, "group %s\n", aName.c_str() );
126  m_groupActive = true;
127 }
128 
129 
131 {
132  assert( m_mode != IOM_READ );
133 
134  if( !m_file || !m_groupActive )
135  return;
136 
137  fprintf( m_file, "endgroup\n" );
138  m_groupActive = false;
139 }
140 
141 
142 void SHAPE_FILE_IO::Write( const SHAPE* aShape, const std::string& aName )
143 {
144  assert( m_mode != IOM_READ );
145 
146  if( !m_file )
147  return;
148 
149  if( !m_groupActive )
150  fprintf( m_file,"group default\n" );
151 
152  std::string sh = aShape->Format();
153 
154  fprintf( m_file, "shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
155  fflush( m_file );
156 }
virtual const std::string Format() const
Definition: shape.cpp:35
#define NULL
SHAPE.
Definition: shape.h:120
IO_MODE m_mode
Definition: shape_file_io.h:68
void Write(const SHAPE *aShape, const std::string &aName="<noname>")
void BeginGroup(const std::string &aName="<noname>")
SHAPE * Read()
SHAPE_TYPE Type() const
Function Type()
Definition: shape.h:96