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