KiCad PCB EDA Suite
SHAPE_FILE_IO Class Reference

Helper class for saving/loading shapes from a file. 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

Helper class for saving/loading shapes from a file.

Definition at line 39 of file shape_file_io.h.

Member Enumeration Documentation

◆ IO_MODE

Enumerator
IOM_READ 
IOM_APPEND 
IOM_WRITE 

Definition at line 42 of file shape_file_io.h.

Constructor & Destructor Documentation

◆ SHAPE_FILE_IO() [1/2]

SHAPE_FILE_IO::SHAPE_FILE_IO ( )

Definition at line 34 of file shape_file_io.cpp.

35 {
36  m_groupActive = false;
37  m_mode = IOM_WRITE;
38  m_file = stdout;
39 }
IO_MODE m_mode
Definition: shape_file_io.h:68

References IOM_WRITE, m_file, m_groupActive, and m_mode.

◆ SHAPE_FILE_IO() [2/2]

SHAPE_FILE_IO::SHAPE_FILE_IO ( const std::string &  aFilename,
SHAPE_FILE_IO::IO_MODE  aMode = IOM_READ 
)

Definition at line 42 of file shape_file_io.cpp.

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

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

◆ ~SHAPE_FILE_IO()

SHAPE_FILE_IO::~SHAPE_FILE_IO ( )

Definition at line 67 of file shape_file_io.cpp.

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

References IOM_READ, m_file, m_groupActive, and m_mode.

Member Function Documentation

◆ BeginGroup()

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

Definition at line 120 of file shape_file_io.cpp.

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

References IOM_READ, m_file, m_groupActive, and m_mode.

◆ EndGroup()

void SHAPE_FILE_IO::EndGroup ( )

Definition at line 132 of file shape_file_io.cpp.

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

References IOM_READ, m_file, m_groupActive, and m_mode.

◆ Read()

SHAPE * SHAPE_FILE_IO::Read ( )

Definition at line 82 of file shape_file_io.cpp.

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 }

◆ Write() [1/2]

void SHAPE_FILE_IO::Write ( const SHAPE aShape,
const std::string &  aName = "<noname>" 
)

Definition at line 144 of file shape_file_io.cpp.

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
IO_MODE m_mode
Definition: shape_file_io.h:68
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:94

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

Referenced by process(), and Write().

◆ Write() [2/2]

void SHAPE_FILE_IO::Write ( const SHAPE aShape,
const std::string &  aName = "<noname>" 
)
inline

Definition at line 60 of file shape_file_io.h.

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

References Write().

Member Data Documentation

◆ m_file

FILE* SHAPE_FILE_IO::m_file
private

Definition at line 66 of file shape_file_io.h.

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

◆ m_groupActive

bool SHAPE_FILE_IO::m_groupActive
private

Definition at line 67 of file shape_file_io.h.

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

◆ m_mode

IO_MODE SHAPE_FILE_IO::m_mode
private

Definition at line 68 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: