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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 #include <string>
26 #include <cassert>
27 
28 #include <geometry/shape.h>
29 #include <geometry/shape_file_io.h>
30 
32 {
33  m_groupActive = false;
34  m_mode = IOM_WRITE;
35  m_file = stdout;
36 }
37 
38 SHAPE_FILE_IO::SHAPE_FILE_IO( const std::string& aFilename, SHAPE_FILE_IO::IO_MODE aMode )
39 {
40  m_groupActive = false;
41 
42  if( aFilename.length() )
43  {
44  switch( aMode )
45  {
46  case IOM_READ: m_file = fopen( aFilename.c_str(), "rb" ); break;
47  case IOM_WRITE: m_file = fopen( aFilename.c_str(), "wb" ); break;
48  case IOM_APPEND: m_file = fopen( aFilename.c_str(), "ab" ); break;
49  default:
50  return;
51  }
52  }
53  else
54  {
55  m_file = NULL;
56  }
57 
58  m_mode = aMode;
59  // fixme: exceptions
60 }
61 
62 
64 {
65  if( !m_file )
66  return;
67 
68  if( m_groupActive && m_mode != IOM_READ )
69  fprintf( m_file, "endgroup\n" );
70 
71  if ( m_file != stdout )
72  {
73  fclose( m_file );
74  }
75 }
76 
77 
79 {
80  /* char tmp[1024];
81 
82  do {
83 
84  if (fscanf(m_file, "%s", tmp) != 1)
85  return NULL;
86 
87  if( !strcmp( tmp, "shape" )
88  break;
89  }
90 
91  int type;
92 
93  SHAPE *rv = NULL;
94 
95  fscanf(m_file,"%d %s", &type, tmp);
96 
97  printf("create shape %d\n", type);
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 }
void BeginGroup(const std::string aName="<noname>")
Class SHAPE.
Definition: shape.h:57
virtual const std::string Format() const
Definition: shape.cpp:35
IO_MODE m_mode
Definition: shape_file_io.h:67
SHAPE_TYPE Type() const
Function Type()
Definition: shape.h:82
void Write(const SHAPE *aShape, const std::string aName="<noname>")
SHAPE * Read()