2012-04-02 18:11:00 +00:00
|
|
|
/**
|
|
|
|
* @file datafile_read_write.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-05-11 08:51:07 +00:00
|
|
|
* Copyright (C) 2016 Jean-Pierre Charras
|
2021-07-18 14:06:48 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-04-02 18:11:00 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2019-05-01 18:57:59 +00:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
2012-04-02 18:11:00 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2019-05-01 18:57:59 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-04-02 18:11:00 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-03 00:39:08 +00:00
|
|
|
#include <wx/app.h>
|
|
|
|
#include <wx/msgdlg.h>
|
|
|
|
|
2020-10-13 01:01:25 +00:00
|
|
|
#include <build_version.h>
|
2021-10-08 14:44:44 +00:00
|
|
|
#include <calculator_panels/panel_regulator.h>
|
2020-10-13 01:01:25 +00:00
|
|
|
#include <class_regulator_data.h>
|
|
|
|
#include <datafile_read_write.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2020-10-13 01:01:25 +00:00
|
|
|
#include <macros.h>
|
2012-04-02 18:11:00 +00:00
|
|
|
#include <pcb_calculator_datafile_lexer.h>
|
2020-10-13 01:01:25 +00:00
|
|
|
#include <pcb_calculator_frame.h>
|
|
|
|
#include <pgm_base.h>
|
2012-04-02 18:11:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
using namespace PCBCALC_DATA_T;
|
|
|
|
|
|
|
|
|
|
|
|
static const char* getTokenName( T aTok )
|
|
|
|
{
|
|
|
|
return PCB_CALCULATOR_DATAFILE_LEXER::TokenName( aTok );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-03 16:01:40 +00:00
|
|
|
bool PANEL_REGULATOR::ReadDataFile()
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
|
|
|
FILE* file = wxFopen( GetDataFilename(), wxT( "rt" ) );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( file == nullptr )
|
2012-04-02 18:11:00 +00:00
|
|
|
return false;
|
|
|
|
|
2016-05-11 08:51:07 +00:00
|
|
|
// Switch the locale to standard C (needed to read/write floating point numbers)
|
2021-10-10 15:03:24 +00:00
|
|
|
LOCALE_IO toggle;
|
2024-03-20 12:59:36 +00:00
|
|
|
m_RegulatorList.Clear();
|
2012-04-02 18:11:00 +00:00
|
|
|
|
2021-10-10 15:03:24 +00:00
|
|
|
PCB_CALCULATOR_DATAFILE* datafile = new PCB_CALCULATOR_DATAFILE( &m_RegulatorList );
|
2012-04-02 18:11:00 +00:00
|
|
|
|
|
|
|
// dataReader dtor will close file
|
|
|
|
FILE_LINE_READER dataReader( file, GetDataFilename() );
|
|
|
|
PCB_CALCULATOR_DATAFILE_PARSER datafile_parser( &dataReader );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
datafile_parser.Parse( datafile );
|
|
|
|
}
|
2014-04-09 13:33:04 +00:00
|
|
|
catch( const IO_ERROR& ioe )
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
|
|
|
delete datafile;
|
|
|
|
|
2016-09-14 22:36:45 +00:00
|
|
|
wxString msg = ioe.What();
|
2014-04-09 13:33:04 +00:00
|
|
|
|
|
|
|
msg += wxChar('\n');
|
|
|
|
msg += _("Data file error.");
|
|
|
|
|
|
|
|
wxMessageBox( msg );
|
2012-04-02 18:11:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-03 16:01:40 +00:00
|
|
|
m_choiceRegulatorSelector->Clear();
|
|
|
|
m_choiceRegulatorSelector->Append( m_RegulatorList.GetRegList() );
|
|
|
|
SelectLastSelectedRegulator();
|
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
delete datafile;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2021-10-03 16:01:40 +00:00
|
|
|
bool PANEL_REGULATOR::WriteDataFile()
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
2016-05-11 08:51:07 +00:00
|
|
|
// Switch the locale to standard C (needed to read/write floating point numbers)
|
2021-10-10 15:03:24 +00:00
|
|
|
LOCALE_IO toggle;
|
2012-04-02 18:11:00 +00:00
|
|
|
|
2020-10-26 23:49:11 +00:00
|
|
|
auto datafile = std::make_unique<PCB_CALCULATOR_DATAFILE>( &m_RegulatorList );
|
2012-04-02 18:11:00 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-10-03 16:01:40 +00:00
|
|
|
FILE_OUTPUTFORMATTER formatter( GetDataFilename() );
|
2012-10-25 15:41:20 +00:00
|
|
|
|
|
|
|
int nestlevel = datafile->WriteHeader( &formatter );
|
|
|
|
|
|
|
|
datafile->Format( &formatter, nestlevel );
|
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
while( nestlevel-- )
|
2012-10-25 15:41:20 +00:00
|
|
|
formatter.Print( nestlevel, ")\n" );
|
2012-04-02 18:11:00 +00:00
|
|
|
}
|
2016-10-10 15:04:48 +00:00
|
|
|
catch( const IO_ERROR& )
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_RegulatorListChanged = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PCB_CALCULATOR_DATAFILE::PCB_CALCULATOR_DATAFILE( REGULATOR_LIST * aList )
|
|
|
|
{
|
|
|
|
m_list = aList;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
static const char* regtype_str[] =
|
|
|
|
{
|
|
|
|
"normal", "3terminal"
|
|
|
|
};
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
int PCB_CALCULATOR_DATAFILE::WriteHeader( OUTPUTFORMATTER* aFormatter ) const
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
|
|
|
int nestlevel = 0;
|
|
|
|
aFormatter->Print( nestlevel++, "(datafile\n");
|
2024-03-20 12:59:36 +00:00
|
|
|
aFormatter->Print( nestlevel++, "(version 2)\n" );
|
2012-04-02 18:11:00 +00:00
|
|
|
aFormatter->Print( nestlevel++, "(date %s)\n",
|
2023-07-15 03:23:42 +00:00
|
|
|
aFormatter->Quotew( GetISO8601CurrentDateTime() ).c_str() );
|
2012-04-02 18:11:00 +00:00
|
|
|
aFormatter->Print( nestlevel++, "(tool %s)\n",
|
* KIWAY Milestone A): Make major modules into DLL/DSOs.
! The initial testing of this commit should be done using a Debug build so that
all the wxASSERT()s are enabled. Also, be sure and keep enabled the
USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it
off is senseless anyways. If you want stable code, go back to a prior version,
the one tagged with "stable".
* Relocate all functionality out of the wxApp derivative into more finely
targeted purposes:
a) DLL/DSO specific
b) PROJECT specific
c) EXE or process specific
d) configuration file specific data
e) configuration file manipulations functions.
All of this functionality was blended into an extremely large wxApp derivative
and that was incompatible with the desire to support multiple concurrently
loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects.
An amazing amount of organization come from simply sorting each bit of
functionality into the proper box.
* Switch to wxConfigBase from wxConfig everywhere except instantiation.
* Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD,
PGM_SINGLE_TOP,
* Remove "Return" prefix on many function names.
* Remove obvious comments from CMakeLists.txt files, and from else() and endif()s.
* Fix building boost for use in a DSO on linux.
* Remove some of the assumptions in the CMakeLists.txt files that windows had
to be the host platform when building windows binaries.
* Reduce the number of wxStrings being constructed at program load time via
static construction.
* Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that
these functions are useful even when the wxConfigBase comes from another
source, as is the case in the KICAD_MANAGER_FRAME.
* Move the setting of the KIPRJMOD environment variable into class PROJECT,
so that it can be moved into a project variable soon, and out of FP_LIB_TABLE.
* Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all
its child wxFrames and wxDialogs now have a Kiway() member function which
returns a KIWAY& that that window tree branch is in support of. This is like
wxWindows DNA in that child windows get this member with proper value at time
of construction.
* Anticipate some of the needs for milestones B) and C) and make code
adjustments now in an effort to reduce work in those milestones.
* No testing has been done for python scripting, since milestone C) has that
being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
|
|
|
aFormatter->Quotew( Pgm().App().GetAppName() +
|
2012-04-02 18:11:00 +00:00
|
|
|
wxChar(' ') + GetBuildVersion() ).c_str() );
|
|
|
|
|
|
|
|
return nestlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCB_CALCULATOR_DATAFILE::Format( OUTPUTFORMATTER* aFormatter,
|
2017-06-08 21:47:21 +00:00
|
|
|
int aNestLevel ) const
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
|
|
|
// Write regulators list:
|
|
|
|
aFormatter->Print( aNestLevel++, "(%s\n", getTokenName( T_regulators ) );
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2021-10-10 15:03:24 +00:00
|
|
|
for( REGULATOR_DATA* item : m_list->m_List )
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
|
|
|
aFormatter->Print( aNestLevel, "(%s %s\n", getTokenName( T_regulator ),
|
2024-03-20 12:59:36 +00:00
|
|
|
aFormatter->Quotew( item->m_Name ).c_str() );
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2024-03-20 12:59:36 +00:00
|
|
|
aFormatter->Print( aNestLevel + 1, "(%s %g)\n", getTokenName( T_reg_vref_min ),
|
|
|
|
item->m_VrefMin );
|
|
|
|
aFormatter->Print( aNestLevel + 1, "(%s %g)\n", getTokenName( T_reg_vref_typ ),
|
|
|
|
item->m_VrefTyp );
|
|
|
|
aFormatter->Print( aNestLevel + 1, "(%s %g)\n", getTokenName( T_reg_vref_max ),
|
|
|
|
item->m_VrefMax );
|
|
|
|
|
|
|
|
if( item->m_Type == 1 )
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
2024-03-20 12:59:36 +00:00
|
|
|
aFormatter->Print( aNestLevel + 1, "(%s %g)\n", getTokenName( T_reg_iadj_typ ),
|
|
|
|
item->m_IadjTyp );
|
|
|
|
aFormatter->Print( aNestLevel + 1, "(%s %g)\n", getTokenName( T_reg_iadj_max ),
|
|
|
|
item->m_IadjMax );
|
2012-04-02 18:11:00 +00:00
|
|
|
}
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_reg_type ),
|
|
|
|
regtype_str[item->m_Type] );
|
|
|
|
aFormatter->Print( aNestLevel, ")\n" );
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
aFormatter->Print( --aNestLevel, ")\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PCB_CALCULATOR_DATAFILE::Parse( PCB_CALCULATOR_DATAFILE_PARSER* aParser )
|
|
|
|
{
|
|
|
|
aParser->Parse( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PCB_CALCULATOR_DATAFILE_PARSER::PCB_CALCULATOR_DATAFILE_PARSER( LINE_READER* aReader ) :
|
|
|
|
PCB_CALCULATOR_DATAFILE_LEXER( aReader )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
PCB_CALCULATOR_DATAFILE_PARSER::PCB_CALCULATOR_DATAFILE_PARSER( char* aLine,
|
|
|
|
const wxString& aSource ) :
|
2012-04-02 18:11:00 +00:00
|
|
|
PCB_CALCULATOR_DATAFILE_LEXER( aLine, aSource )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void PCB_CALCULATOR_DATAFILE_PARSER::Parse( PCB_CALCULATOR_DATAFILE* aDataList )
|
2012-04-02 18:11:00 +00:00
|
|
|
{
|
|
|
|
T token;
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
while( ( token = NextTok() ) != T_EOF)
|
|
|
|
{
|
|
|
|
if( token == T_LEFT )
|
|
|
|
{
|
|
|
|
token = NextTok();
|
|
|
|
|
|
|
|
if( token == T_regulators )
|
|
|
|
{
|
|
|
|
ParseRegulatorDescr( aDataList );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
void PCB_CALCULATOR_DATAFILE_PARSER::ParseRegulatorDescr( PCB_CALCULATOR_DATAFILE* aDataList )
|
|
|
|
{
|
|
|
|
T token;
|
|
|
|
wxString name;
|
2024-03-20 12:59:36 +00:00
|
|
|
double vrefmin, vreftyp, vrefmax = 0.0;
|
|
|
|
double iadjtyp, iadjmax = 0.0;
|
|
|
|
|
|
|
|
auto parseToken = [&]()
|
|
|
|
{
|
|
|
|
double val;
|
|
|
|
token = NextTok();
|
|
|
|
|
|
|
|
if( token != T_NUMBER )
|
|
|
|
Expecting( T_NUMBER );
|
|
|
|
|
|
|
|
sscanf( CurText(), "%lf", &val );
|
|
|
|
NeedRIGHT();
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
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;
|
2024-03-20 12:59:36 +00:00
|
|
|
vrefmin = 0.0;
|
|
|
|
vreftyp = 0.0;
|
|
|
|
vrefmax = 0.0;
|
|
|
|
iadjtyp = 0.0;
|
|
|
|
iadjmax = 0.0;
|
2012-04-02 18:11:00 +00:00
|
|
|
|
|
|
|
// Read name
|
|
|
|
token = NextTok();
|
2023-09-09 04:10:57 +00:00
|
|
|
name = From_UTF8( CurText() );
|
2012-04-02 18:11:00 +00:00
|
|
|
|
|
|
|
while( ( token = NextTok() ) != T_RIGHT )
|
|
|
|
{
|
|
|
|
if( token == T_EOF)
|
|
|
|
Unexpected( T_EOF );
|
|
|
|
|
|
|
|
if( token == T_LEFT )
|
|
|
|
token = NextTok();
|
|
|
|
|
|
|
|
switch( token )
|
|
|
|
{
|
2024-03-20 12:59:36 +00:00
|
|
|
// Parse legacy entry
|
|
|
|
case T_reg_vref:
|
|
|
|
vreftyp = parseToken();
|
|
|
|
vrefmin = vreftyp;
|
|
|
|
vrefmax = vreftyp;
|
2012-04-02 18:11:00 +00:00
|
|
|
break;
|
|
|
|
|
2024-03-20 12:59:36 +00:00
|
|
|
case T_reg_vref_min: vrefmin = parseToken(); break;
|
|
|
|
case T_reg_vref_typ: vreftyp = parseToken(); break;
|
|
|
|
case T_reg_vref_max: vrefmax = parseToken(); break;
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2024-03-20 12:59:36 +00:00
|
|
|
// Parse legacy entry
|
|
|
|
case T_reg_iadj:
|
|
|
|
iadjtyp = parseToken();
|
|
|
|
iadjmax = iadjtyp;
|
2012-04-02 18:11:00 +00:00
|
|
|
break;
|
|
|
|
|
2024-03-20 12:59:36 +00:00
|
|
|
case T_reg_iadj_typ: iadjtyp = parseToken(); break;
|
|
|
|
case T_reg_iadj_max: iadjmax = parseToken(); break;
|
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
case T_reg_type: // type: normal or 3 terminal reg
|
|
|
|
token = NextTok();
|
2021-07-18 14:06:48 +00:00
|
|
|
|
|
|
|
if( strcasecmp( CurText(), regtype_str[0] ) == 0 )
|
2012-04-02 18:11:00 +00:00
|
|
|
type = 0;
|
2016-08-16 08:27:09 +00:00
|
|
|
else if( strcasecmp( CurText(), regtype_str[1] ) == 0 )
|
2012-04-02 18:11:00 +00:00
|
|
|
type = 1;
|
|
|
|
else
|
|
|
|
Unexpected( CurText() );
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2012-04-02 18:11:00 +00:00
|
|
|
NeedRIGHT();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Unexpected( CurText() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! name.IsEmpty() )
|
|
|
|
{
|
2024-03-20 12:59:36 +00:00
|
|
|
REGULATOR_DATA* new_item = new REGULATOR_DATA( name, vrefmin, vreftyp, vrefmax,
|
|
|
|
type, iadjtyp, iadjmax );
|
2012-04-02 18:11:00 +00:00
|
|
|
aDataList->m_list->Add( new_item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|