KiCad PCB EDA Suite
SHAPE_FILE_IO Class Reference

Class SHAPE_FILE_IO. More...

#include <shape_file_io.h>

Public Types

enum  IO_MODE { IOM_READ = 0, IOM_APPEND, IOM_WRITE }
 

Public Member Functions

 SHAPE_FILE_IO ()
 
 SHAPE_FILE_IO (const std::string &aFilename, IO_MODE aMode=IOM_READ)
 
 ~SHAPE_FILE_IO ()
 
void BeginGroup (const std::string aName="<noname>")
 
void EndGroup ()
 
SHAPERead ()
 
void Write (const SHAPE *aShape, const std::string aName="<noname>")
 
void Write (const SHAPE &aShape, const std::string aName="<noname>")
 

Private Attributes

FILE * m_file
 
bool m_groupActive
 
IO_MODE m_mode
 

Detailed Description

Class SHAPE_FILE_IO.

Helper class for saving/loading shapes from a file.

Definition at line 38 of file shape_file_io.h.

Member Enumeration Documentation

Enumerator
IOM_READ 
IOM_APPEND 
IOM_WRITE 

Definition at line 41 of file shape_file_io.h.

Constructor & Destructor Documentation

SHAPE_FILE_IO::SHAPE_FILE_IO ( )

Definition at line 31 of file shape_file_io.cpp.

References IOM_WRITE, m_file, m_groupActive, and m_mode.

32 {
33  m_groupActive = false;
34  m_mode = IOM_WRITE;
35  m_file = stdout;
36 }
IO_MODE m_mode
Definition: shape_file_io.h:67
SHAPE_FILE_IO::SHAPE_FILE_IO ( const std::string &  aFilename,
SHAPE_FILE_IO::IO_MODE  aMode = IOM_READ 
)

Definition at line 38 of file shape_file_io.cpp.

References IOM_APPEND, IOM_READ, IOM_WRITE, m_file, m_groupActive, and m_mode.

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 }
IO_MODE m_mode
Definition: shape_file_io.h:67
SHAPE_FILE_IO::~SHAPE_FILE_IO ( )

Definition at line 63 of file shape_file_io.cpp.

References IOM_READ, m_file, m_groupActive, and m_mode.

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 }
IO_MODE m_mode
Definition: shape_file_io.h:67

Member Function Documentation

void SHAPE_FILE_IO::BeginGroup ( const std::string  aName = "<noname>")

Definition at line 118 of file shape_file_io.cpp.

References IOM_READ, m_file, m_groupActive, and m_mode.

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 }
IO_MODE m_mode
Definition: shape_file_io.h:67
void SHAPE_FILE_IO::EndGroup ( )

Definition at line 130 of file shape_file_io.cpp.

References IOM_READ, m_file, m_groupActive, and m_mode.

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 }
IO_MODE m_mode
Definition: shape_file_io.h:67
SHAPE * SHAPE_FILE_IO::Read ( )

Definition at line 78 of file shape_file_io.cpp.

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 }
void SHAPE_FILE_IO::Write ( const SHAPE aShape,
const std::string  aName = "<noname>" 
)

Definition at line 142 of file shape_file_io.cpp.

References SHAPE::Format(), IOM_READ, m_file, m_groupActive, m_mode, and SHAPE::Type().

Referenced by Write().

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
IO_MODE m_mode
Definition: shape_file_io.h:67
SHAPE_TYPE Type() const
Function Type()
Definition: shape.h:82
void SHAPE_FILE_IO::Write ( const SHAPE aShape,
const std::string  aName = "<noname>" 
)
inline

Definition at line 59 of file shape_file_io.h.

References Write().

60  {
61  Write( &aShape, aName );
62  }
void Write(const SHAPE *aShape, const std::string aName="<noname>")

Member Data Documentation

FILE* SHAPE_FILE_IO::m_file
private

Definition at line 65 of file shape_file_io.h.

Referenced by BeginGroup(), EndGroup(), SHAPE_FILE_IO(), Write(), and ~SHAPE_FILE_IO().

bool SHAPE_FILE_IO::m_groupActive
private

Definition at line 66 of file shape_file_io.h.

Referenced by BeginGroup(), EndGroup(), SHAPE_FILE_IO(), Write(), and ~SHAPE_FILE_IO().

IO_MODE SHAPE_FILE_IO::m_mode
private

Definition at line 67 of file shape_file_io.h.

Referenced by BeginGroup(), EndGroup(), SHAPE_FILE_IO(), Write(), and ~SHAPE_FILE_IO().


The documentation for this class was generated from the following files: