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 (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 ( const std::string &  aFilename,
SHAPE_FILE_IO::IO_MODE  aMode = IOM_READ 
)

Definition at line 31 of file shape_file_io.cpp.

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

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

Definition at line 56 of file shape_file_io.cpp.

References IOM_READ, m_file, m_groupActive, and m_mode.

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

Member Function Documentation

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

Definition at line 108 of file shape_file_io.cpp.

References IOM_READ, m_file, m_groupActive, and m_mode.

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

Definition at line 120 of file shape_file_io.cpp.

References IOM_READ, m_file, m_groupActive, and m_mode.

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

Definition at line 68 of file shape_file_io.cpp.

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

Definition at line 132 of file shape_file_io.cpp.

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

Referenced by Write().

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

Definition at line 58 of file shape_file_io.h.

References Write().

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

Member Data Documentation

FILE* SHAPE_FILE_IO::m_file
private

Definition at line 64 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 65 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 66 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: