1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @file datafile_read_write.cpp
*/
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 Jean-Pierre Charras
* Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <macros.h>
#include <common.h>
#include <kicad_string.h>
#include <pgm_base.h>
#include <pcb_calculator.h>
#include <pcb_calculator_datafile_lexer.h>
#include <class_regulator_data.h>
#include <datafile_read_write.h>
#include <build_version.h>
using namespace PCBCALC_DATA_T;
static const char* getTokenName( T aTok )
{
return PCB_CALCULATOR_DATAFILE_LEXER::TokenName( aTok );
}
bool PCB_CALCULATOR_FRAME::ReadDataFile()
{
FILE* file = wxFopen( GetDataFilename(), wxT( "rt" ) );
if( file == NULL )
return false;
// Switch the locale to standard C (needed to read/write floating point numbers
LOCALE_IO toggle;
PCB_CALCULATOR_DATAFILE * datafile = new PCB_CALCULATOR_DATAFILE( &m_RegulatorList );
// dataReader dtor will close file
FILE_LINE_READER dataReader( file, GetDataFilename() );
PCB_CALCULATOR_DATAFILE_PARSER datafile_parser( &dataReader );
try
{
datafile_parser.Parse( datafile );
}
catch( const IO_ERROR& ioe )
{
delete datafile;
wxString msg = ioe.errorText;
msg += wxChar('\n');
msg += _("Data file error.");
wxMessageBox( msg );
return false;
}
delete datafile;
return true;
}
bool PCB_CALCULATOR_FRAME::WriteDataFile()
{
// Switch the locale to standard C (needed to read/write floating point numbers
LOCALE_IO toggle;
std::auto_ptr<PCB_CALCULATOR_DATAFILE> datafile( new PCB_CALCULATOR_DATAFILE( &m_RegulatorList ) );
try
{
FILE_OUTPUTFORMATTER formatter( GetDataFilename() );
int nestlevel = datafile->WriteHeader( &formatter );
datafile->Format( &formatter, nestlevel );
while( nestlevel-- )
formatter.Print( nestlevel, ")\n" );
}
catch( const IO_ERROR& ioe )
{
return false;
}
m_RegulatorListChanged = false;
return true;
}
PCB_CALCULATOR_DATAFILE::PCB_CALCULATOR_DATAFILE( REGULATOR_LIST * aList )
{
m_list = aList;
}
static const char* regtype_str[] =
{
"normal", "3terminal"
};
int PCB_CALCULATOR_DATAFILE::WriteHeader( OUTPUTFORMATTER* aFormatter ) const throw( IO_ERROR )
{
int nestlevel = 0;
aFormatter->Print( nestlevel++, "(datafile\n");
aFormatter->Print( nestlevel++, "(version 1)\n" );
aFormatter->Print( nestlevel++, "(date %s)\n",
aFormatter->Quotew( DateAndTime() ).c_str() );
aFormatter->Print( nestlevel++, "(tool %s)\n",
aFormatter->Quotew( Pgm().App().GetAppName() +
wxChar(' ') + GetBuildVersion() ).c_str() );
return nestlevel;
}
void PCB_CALCULATOR_DATAFILE::Format( OUTPUTFORMATTER* aFormatter,
int aNestLevel ) const throw( IO_ERROR )
{
// Write regulators list:
aFormatter->Print( aNestLevel++, "(%s\n", getTokenName( T_regulators ) );
for( unsigned ii = 0; ii < m_list->m_List.size(); ii++ )
{
REGULATOR_DATA * item = m_list->m_List[ii];
aFormatter->Print( aNestLevel, "(%s %s\n", getTokenName( T_regulator ),
aFormatter->Quotew(item->m_Name ).c_str() );
aFormatter->Print( aNestLevel+1, "(%s %g)\n", getTokenName( T_reg_vref ),
item->m_Vref );
if( item->m_Iadj != 0 && item->m_Type == 1)
{
aFormatter->Print( aNestLevel+1, "(%s %g)\n", getTokenName( T_reg_iadj ),
item->m_Iadj );
}
aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_reg_type ),
regtype_str[item->m_Type] );
aFormatter->Print( aNestLevel, ")\n" );
}
aFormatter->Print( --aNestLevel, ")\n" );
}
void PCB_CALCULATOR_DATAFILE::Parse( PCB_CALCULATOR_DATAFILE_PARSER* aParser )
throw( IO_ERROR, PARSE_ERROR )
{
aParser->Parse( this );
}
// PCB_CALCULATOR_DATAFILE_PARSER
PCB_CALCULATOR_DATAFILE_PARSER::PCB_CALCULATOR_DATAFILE_PARSER( LINE_READER* aReader ) :
PCB_CALCULATOR_DATAFILE_LEXER( aReader )
{
}
PCB_CALCULATOR_DATAFILE_PARSER::PCB_CALCULATOR_DATAFILE_PARSER( char* aLine, wxString aSource ) :
PCB_CALCULATOR_DATAFILE_LEXER( aLine, aSource )
{
}
void PCB_CALCULATOR_DATAFILE_PARSER::Parse( PCB_CALCULATOR_DATAFILE* aDataList ) throw( IO_ERROR, PARSE_ERROR )
{
T token;
while( ( token = NextTok() ) != T_EOF)
{
if( token == T_LEFT )
{
token = NextTok();
if( token == T_regulators )
{
ParseRegulatorDescr( aDataList );
continue;
}
}
}
}
void PCB_CALCULATOR_DATAFILE_PARSER::ParseRegulatorDescr( PCB_CALCULATOR_DATAFILE* aDataList )
throw( IO_ERROR, PARSE_ERROR )
{
T token;
wxString name;
double vref, iadj;
int type;
while( ( token = NextTok() ) != T_RIGHT )
{
if( token == T_EOF)
Unexpected( T_EOF );
if( token == T_LEFT )
token = NextTok();
if( token == T_regulator )
{
type = 0;
vref = 0.0;
// Read name
token = NextTok();
name = FROM_UTF8( CurText() );
while( ( token = NextTok() ) != T_RIGHT )
{
if( token == T_EOF)
Unexpected( T_EOF );
if( token == T_LEFT )
token = NextTok();
switch( token )
{
case T_reg_vref: // the voltage reference value
token = NextTok();
if( token != T_NUMBER )
Expecting( T_NUMBER );
sscanf( CurText(), "%lf" , &vref);
NeedRIGHT();
break;
case T_reg_iadj: // the Iadj reference value
token = NextTok();
if( token != T_NUMBER )
Expecting( T_NUMBER );
sscanf( CurText(), "%lf" , &iadj);
NeedRIGHT();
break;
case T_reg_type: // type: normal or 3 terminal reg
token = NextTok();
if( stricmp( CurText(), regtype_str[0] ) == 0 )
type = 0;
else if( stricmp( CurText(), regtype_str[1] ) == 0 )
type = 1;
else
Unexpected( CurText() );
NeedRIGHT();
break;
default:
Unexpected( CurText() );
break;
}
}
if( ! name.IsEmpty() )
{
if( type != 1 )
iadj = 0.0;
REGULATOR_DATA * new_item = new REGULATOR_DATA(name, vref, type, iadj );
aDataList->m_list->Add( new_item );
}
}
}
}