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-2021 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 
33 
35 {
36  m_groupActive = false;
37  m_mode = IOM_WRITE;
38  m_file = stdout;
39 }
40 
41 
42 SHAPE_FILE_IO::SHAPE_FILE_IO( const std::string& aFilename, SHAPE_FILE_IO::IO_MODE aMode )
43 {
44  m_groupActive = false;
45 
46  if( aFilename.length() )
47  {
48  switch( aMode )
49  {
50  case IOM_READ: m_file = fopen( aFilename.c_str(), "rb" ); break;
51  case IOM_WRITE: m_file = fopen( aFilename.c_str(), "wb" ); break;
52  case IOM_APPEND: m_file = fopen( aFilename.c_str(), "ab" ); break;
53  default:
54  return;
55  }
56  }
57  else
58  {
59  m_file = nullptr;
60  }
61 
62  m_mode = aMode;
63  // fixme: exceptions
64 }
65 
66 
68 {
69  if( !m_file )
70  return;
71 
72  if( m_groupActive && m_mode != IOM_READ )
73  fprintf( m_file, "endgroup\n" );
74 
75  if ( m_file != stdout )
76  {
77  fclose( m_file );
78  }
79 }
80 
81 
83 {
84  /* char tmp[1024];
85 
86  do {
87 
88  if (fscanf(m_file, "%s", tmp) != 1)
89  return nullptr;
90 
91  if( !strcmp( tmp, "shape" )
92  break;
93  }
94 
95  int type;
96 
97  SHAPE *rv = nullptr;
98 
99  fscanf(m_file,"%d %s", &type, tmp);
100 
101  switch(type)
102  {
103  case SHAPE::LINE_CHAIN:
104  rv = new SHAPE_LINE_CHAIN;
105  break;
106  }
107 
108  if(!rv)
109  return nullptr;
110 
111  rv.Parse ( )
112 
113  fprintf(m_file,"shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
114 */
115  assert( false );
116  return nullptr;
117 }
118 
119 
120 void SHAPE_FILE_IO::BeginGroup( const std::string& aName )
121 {
122  assert( m_mode != IOM_READ );
123 
124  if( !m_file )
125  return;
126 
127  fprintf( m_file, "group %s\n", aName.c_str() );
128  m_groupActive = true;
129 }
130 
131 
133 {
134  assert( m_mode != IOM_READ );
135 
136  if( !m_file || !m_groupActive )
137  return;
138 
139  fprintf( m_file, "endgroup\n" );
140  m_groupActive = false;
141 }
142 
143 
144 void SHAPE_FILE_IO::Write( const SHAPE* aShape, const std::string& aName )
145 {
146  assert( m_mode != IOM_READ );
147 
148  if( !m_file )
149  return;
150 
151  if( !m_groupActive )
152  fprintf( m_file,"group default\n" );
153 
154  std::string sh = aShape->Format();
155 
156  fprintf( m_file, "shape %d %s %s\n", aShape->Type(), aName.c_str(), sh.c_str() );
157  fflush( m_file );
158 }
virtual const std::string Format() const
Definition: shape.cpp:35
An abstract shape on 2D plane.
Definition: shape.h:116
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
Return the type of the shape.
Definition: shape.h:94