Wildcards: use vectors instead of varargs + tests

Varargs make it very hard to pass strings in flexibly
when they (and the number of them) are not known
at compile time.

For example, this allows up to now amalgamate the "single
ext" and "multiple ext" unit tests.
This commit is contained in:
John Beard 2019-01-05 23:55:14 +00:00 committed by Wayne Stambaugh
parent f86d953c80
commit dd313d4d47
3 changed files with 72 additions and 81 deletions

View File

@ -69,37 +69,30 @@ static wxString formatWildcardExt( const wxString& aWildcard )
} }
wxString AddFileExtListToFilter( int aArgCnt, ... ) wxString AddFileExtListToFilter( const std::vector<std::string>& aExts )
{ {
wxString files_filter = " ("; wxString files_filter = " (";
va_list args;
va_start( args, aArgCnt );
// Add extensions to the info message: // Add extensions to the info message:
for( int ii = 0; ii < aArgCnt; ii++) for( const auto& ext : aExts )
{ {
const char* ext = va_arg(args, char*);
files_filter << " *." << ext; files_filter << " *." << ext;
} }
files_filter << ")|*."; files_filter << ")|*.";
va_start( args, aArgCnt );
// Add extensions to the filter list, using a formated string (GTK specific): // Add extensions to the filter list, using a formated string (GTK specific):
for( int ii = 0; ii < aArgCnt; ii++) bool first = true;
for( const auto& ext : aExts )
{ {
const char* ext = va_arg( args, const char* ); if( !first )
if( ii > 0 )
files_filter << ";*."; files_filter << ";*.";
first = false;
files_filter << formatWildcardExt( ext ); files_filter << formatWildcardExt( ext );
} }
va_end( args );
return files_filter; return files_filter;
} }
@ -148,271 +141,271 @@ const wxString AllFilesWildcard( _( "All files (*)|*" ) );
wxString SchematicSymbolFileWildcard() wxString SchematicSymbolFileWildcard()
{ {
return _( "KiCad drawing symbol files" ) + AddFileExtListToFilter( 1, "sym" ); return _( "KiCad drawing symbol files" ) + AddFileExtListToFilter( { "sym" } );
} }
wxString SchematicLibraryFileWildcard() wxString SchematicLibraryFileWildcard()
{ {
return _( "KiCad symbol library files" ) + AddFileExtListToFilter( 1, "lib" ); return _( "KiCad symbol library files" ) + AddFileExtListToFilter( { "lib" } );
} }
wxString ProjectFileWildcard() wxString ProjectFileWildcard()
{ {
return _( "KiCad project files" ) + AddFileExtListToFilter( 1, "pro" ); return _( "KiCad project files" ) + AddFileExtListToFilter( { "pro" } );
} }
wxString SchematicFileWildcard() wxString SchematicFileWildcard()
{ {
return _( "KiCad schematic files" ) + AddFileExtListToFilter( 1, "sch" ); return _( "KiCad schematic files" ) + AddFileExtListToFilter( { "sch" } );
} }
wxString EagleSchematicFileWildcard() wxString EagleSchematicFileWildcard()
{ {
return _( "Eagle XML schematic files" ) + AddFileExtListToFilter( 1, "sch" ); return _( "Eagle XML schematic files" ) + AddFileExtListToFilter( { "sch" } );
} }
wxString EagleFilesWildcard() wxString EagleFilesWildcard()
{ {
return _( "Eagle XML files" ) + AddFileExtListToFilter( 2, "sch", "brd" ); return _( "Eagle XML files" ) + AddFileExtListToFilter( { "sch", "brd" } );
} }
wxString NetlistFileWildcard() wxString NetlistFileWildcard()
{ {
return _( "KiCad netlist files" ) + AddFileExtListToFilter( 1, "net" ); return _( "KiCad netlist files" ) + AddFileExtListToFilter( { "net" } );
} }
wxString GerberFileWildcard() wxString GerberFileWildcard()
{ {
return _( "Gerber files" ) + AddFileExtListToFilter( 1, "pho" ); return _( "Gerber files" ) + AddFileExtListToFilter( { "pho" } );
} }
wxString LegacyPcbFileWildcard() wxString LegacyPcbFileWildcard()
{ {
return _( "KiCad printed circuit board files" ) + AddFileExtListToFilter( 1, "brd" ); return _( "KiCad printed circuit board files" ) + AddFileExtListToFilter( { "brd" } );
} }
wxString EaglePcbFileWildcard() wxString EaglePcbFileWildcard()
{ {
return _( "Eagle ver. 6.x XML PCB files" ) + AddFileExtListToFilter( 1, "brd" ); return _( "Eagle ver. 6.x XML PCB files" ) + AddFileExtListToFilter( { "brd" } );
} }
wxString PCadPcbFileWildcard() wxString PCadPcbFileWildcard()
{ {
return _( "P-Cad 200x ASCII PCB files" ) + AddFileExtListToFilter( 1, "pcb" ); return _( "P-Cad 200x ASCII PCB files" ) + AddFileExtListToFilter( { "pcb" } );
} }
wxString PcbFileWildcard() wxString PcbFileWildcard()
{ {
return _( "KiCad printed circuit board files" ) + AddFileExtListToFilter( 1, "kicad_pcb" ); return _( "KiCad printed circuit board files" ) + AddFileExtListToFilter( { "kicad_pcb" } );
} }
wxString KiCadFootprintLibFileWildcard() wxString KiCadFootprintLibFileWildcard()
{ {
return _( "KiCad footprint files" ) + AddFileExtListToFilter( 1, "kicad_mod" ); return _( "KiCad footprint files" ) + AddFileExtListToFilter( { "kicad_mod" } );
} }
wxString KiCadFootprintLibPathWildcard() wxString KiCadFootprintLibPathWildcard()
{ {
return _( "KiCad footprint library paths" ) + AddFileExtListToFilter( 1, "pretty" ); return _( "KiCad footprint library paths" ) + AddFileExtListToFilter( { "pretty" } );
} }
wxString LegacyFootprintLibPathWildcard() wxString LegacyFootprintLibPathWildcard()
{ {
return _( "Legacy footprint library files" ) + AddFileExtListToFilter( 1, "mod" ); return _( "Legacy footprint library files" ) + AddFileExtListToFilter( { "mod" } );
} }
wxString EagleFootprintLibPathWildcard() wxString EagleFootprintLibPathWildcard()
{ {
return _( "Eagle ver. 6.x XML library files" ) + AddFileExtListToFilter( 1, "lbr" ); return _( "Eagle ver. 6.x XML library files" ) + AddFileExtListToFilter( { "lbr" } );
} }
wxString GedaPcbFootprintLibFileWildcard() wxString GedaPcbFootprintLibFileWildcard()
{ {
return _( "Geda PCB footprint library files" ) + AddFileExtListToFilter( 1, "fp" ); return _( "Geda PCB footprint library files" ) + AddFileExtListToFilter( { "fp" } );
} }
wxString PageLayoutDescrFileWildcard() wxString PageLayoutDescrFileWildcard()
{ {
return _( "Page layout design files" ) + AddFileExtListToFilter( 1, "kicad_wks" ); return _( "Page layout design files" ) + AddFileExtListToFilter( { "kicad_wks" } );
} }
// Wildcard for cvpcb component to footprint link file // Wildcard for cvpcb component to footprint link file
wxString ComponentFileWildcard() wxString ComponentFileWildcard()
{ {
return _( "KiCad symbol footprint link files" ) + AddFileExtListToFilter( 1, "cmp" ); return _( "KiCad symbol footprint link files" ) + AddFileExtListToFilter( { "cmp" } );
} }
// Wildcard for reports and fabrication documents // Wildcard for reports and fabrication documents
wxString DrillFileWildcard() wxString DrillFileWildcard()
{ {
return _( "Drill files" ) + AddFileExtListToFilter( 3, "drl", "nc", "xnc" ); return _( "Drill files" ) + AddFileExtListToFilter( { "drl", "nc", "xnc" } );
} }
wxString SVGFileWildcard() wxString SVGFileWildcard()
{ {
return _( "SVG files" ) + AddFileExtListToFilter( 1, "svg" ); return _( "SVG files" ) + AddFileExtListToFilter( { "svg" } );
} }
wxString HtmlFileWildcard() wxString HtmlFileWildcard()
{ {
return _( "HTML files" ) + AddFileExtListToFilter( 2, "htm" , "html" ); return _( "HTML files" ) + AddFileExtListToFilter( { "htm", "html" } );
} }
wxString CsvFileWildcard() wxString CsvFileWildcard()
{ {
return _( "CSV Files" ) + AddFileExtListToFilter( 1, "csv" ); return _( "CSV Files" ) + AddFileExtListToFilter( { "csv" } );
} }
wxString PdfFileWildcard() wxString PdfFileWildcard()
{ {
return _( "Portable document format files" ) + AddFileExtListToFilter( 1, "pdf" ); return _( "Portable document format files" ) + AddFileExtListToFilter( { "pdf" } );
} }
wxString PSFileWildcard() wxString PSFileWildcard()
{ {
return _( "PostScript files" ) + AddFileExtListToFilter( 1, "ps" ); return _( "PostScript files" ) + AddFileExtListToFilter( { "ps" } );
} }
wxString ReportFileWildcard() wxString ReportFileWildcard()
{ {
return _( "Report files" ) + AddFileExtListToFilter( 1, "rpt" ); return _( "Report files" ) + AddFileExtListToFilter( { "rpt" } );
} }
wxString FootprintPlaceFileWildcard() wxString FootprintPlaceFileWildcard()
{ {
return _( "Footprint place files" ) + AddFileExtListToFilter( 1, "pos" ); return _( "Footprint place files" ) + AddFileExtListToFilter( { "pos" } );
} }
wxString Shapes3DFileWildcard() wxString Shapes3DFileWildcard()
{ {
return _( "VRML and X3D files" ) + AddFileExtListToFilter( 2, "wrl", "x3d" ); return _( "VRML and X3D files" ) + AddFileExtListToFilter( { "wrl", "x3d" } );
} }
wxString IDF3DFileWildcard() wxString IDF3DFileWildcard()
{ {
return _( "IDFv3 footprint files" ) + AddFileExtListToFilter( 1, "idf" ); return _( "IDFv3 footprint files" ) + AddFileExtListToFilter( { "idf" } );
} }
wxString TextFileWildcard() wxString TextFileWildcard()
{ {
return _( "Text files" ) + AddFileExtListToFilter( 1, "txt" ); return _( "Text files" ) + AddFileExtListToFilter( { "txt" } );
} }
wxString ModLegacyExportFileWildcard() wxString ModLegacyExportFileWildcard()
{ {
return _( "Legacy footprint export files" ) + AddFileExtListToFilter( 1, "emp" ); return _( "Legacy footprint export files" ) + AddFileExtListToFilter( { "emp" } );
} }
wxString ErcFileWildcard() wxString ErcFileWildcard()
{ {
return _( "Electronic rule check file" ) + AddFileExtListToFilter( 1, "erc" ); return _( "Electronic rule check file" ) + AddFileExtListToFilter( { "erc" } );
} }
wxString SpiceLibraryFileWildcard() wxString SpiceLibraryFileWildcard()
{ {
return _( "Spice library file" ) + AddFileExtListToFilter( 1, "lib" ); return _( "Spice library file" ) + AddFileExtListToFilter( { "lib" } );
} }
wxString SpiceNetlistFileWildcard() wxString SpiceNetlistFileWildcard()
{ {
return _( "SPICE netlist file" ) + AddFileExtListToFilter( 1, "cir" ); return _( "SPICE netlist file" ) + AddFileExtListToFilter( { "cir" } );
} }
wxString CadstarNetlistFileWildcard() wxString CadstarNetlistFileWildcard()
{ {
return _( "CadStar netlist file" ) + AddFileExtListToFilter( 1, "frp" ); return _( "CadStar netlist file" ) + AddFileExtListToFilter( { "frp" } );
} }
wxString EquFileWildcard() wxString EquFileWildcard()
{ {
return _( "Symbol footprint association files" ) + AddFileExtListToFilter( 1, "equ" ); return _( "Symbol footprint association files" ) + AddFileExtListToFilter( { "equ" } );
} }
wxString ZipFileWildcard() wxString ZipFileWildcard()
{ {
return _( "Zip file" ) + AddFileExtListToFilter( 1, "zip" ); return _( "Zip file" ) + AddFileExtListToFilter( { "zip" } );
} }
wxString GencadFileWildcard() wxString GencadFileWildcard()
{ {
return _( "GenCAD 1.4 board files" ) + AddFileExtListToFilter( 1, "cad" ); return _( "GenCAD 1.4 board files" ) + AddFileExtListToFilter( { "cad" } );
} }
wxString DxfFileWildcard() wxString DxfFileWildcard()
{ {
return _( "DXF Files" ) + AddFileExtListToFilter( 1, "dxf" ); return _( "DXF Files" ) + AddFileExtListToFilter( { "dxf" } );
} }
wxString GerberJobFileWildcard() wxString GerberJobFileWildcard()
{ {
return _( "Gerber job file" ) + AddFileExtListToFilter( 1, "gbrjob" ); return _( "Gerber job file" ) + AddFileExtListToFilter( { "gbrjob" } );
} }
wxString SpecctraDsnFileWildcard() wxString SpecctraDsnFileWildcard()
{ {
return _( "Specctra DSN file" ) + AddFileExtListToFilter( 1, "dsn" ); return _( "Specctra DSN file" ) + AddFileExtListToFilter( { "dsn" } );
} }
wxString IpcD356FileWildcard() wxString IpcD356FileWildcard()
{ {
return _( "IPC-D-356 Test Files" ) + AddFileExtListToFilter( 1, "d356" ); return _( "IPC-D-356 Test Files" ) + AddFileExtListToFilter( { "d356" } );
} }
wxString WorkbookFileWildcard() wxString WorkbookFileWildcard()
{ {
return _( "Workbook file" ) + AddFileExtListToFilter( 1, "wbk" ); return _( "Workbook file" ) + AddFileExtListToFilter( { "wbk" } );
} }
wxString PngFileWildcard() wxString PngFileWildcard()
{ {
return _( "PNG file" ) + AddFileExtListToFilter( 1, "png" ); return _( "PNG file" ) + AddFileExtListToFilter( { "png" } );
} }
wxString JpegFileWildcard() wxString JpegFileWildcard()
{ {
return _( "Jpeg file" ) + AddFileExtListToFilter( 2, "jpg", "jpeg" ); return _( "Jpeg file" ) + AddFileExtListToFilter( { "jpg", "jpeg" } );
} }

View File

@ -34,6 +34,9 @@
#include <wx/wx.h> #include <wx/wx.h>
#include <string>
#include <vector>
/** /**
* \defgroup file_extensions File Extension Definitions * \defgroup file_extensions File Extension Definitions
* *
@ -47,20 +50,24 @@
*/ */
/** /**
* build the wildcard extension file dialog wildcard filter to add to the base message dialog. * Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
* for instance, to open .txt files in a file dialog: *
* For instance, to open .txt files in a file dialog:
* the base message is for instance "Text files" * the base message is for instance "Text files"
* the ext list is " (*.txt)|*.txt" * the ext list is " (*.txt)|*.txt"
* and the returned string to add to the base message is " (*.txt)|*.txt" * and the returned string to add to the base message is " (*.txt)|*.txt"
* the message to display in the dialog is "Text files (*.txt)|*.txt" * the message to display in the dialog is "Text files (*.txt)|*.txt"
* *
* @param aArgCnt is the count of file ext to add to the filter * This function produces a case-insensitive filter (so .txt, .TXT and .tXT
* other params are the const char* file ext to add to the filter * are all match if you pass "txt" into the function).
*
* @param aExts is the list of exts to add to the filter. Do not include the
* leading dot.
* *
* @return the appropriate file dialog wildcard filter list. * @return the appropriate file dialog wildcard filter list.
*/ */
wxString AddFileExtListToFilter( int aArgCnt, ... ); wxString AddFileExtListToFilter( const std::vector<std::string>& aExts );
// Do NOT use wxString for these. wxStrings are not thread-safe, even when const. (For the // Do NOT use wxString for these. wxStrings are not thread-safe, even when const. (For the

View File

@ -73,26 +73,17 @@ static constexpr bool should_use_regex_filters()
/** /**
* Check correct handling of filter strings (as used by WX) * Check correct handling of filter strings (as used by WX)
*/ */
BOOST_AUTO_TEST_CASE( SingleFilter ) BOOST_AUTO_TEST_CASE( BasicFilter )
{ {
const auto& c = ext_wildcard_cases[0]; for( const auto& c : ext_wildcard_cases )
const std::string exp_filter = {
should_use_regex_filters() ? c.m_re_filter : c.m_filter_case_insenstive; const std::string exp_filter =
should_use_regex_filters() ? c.m_re_filter : c.m_filter_case_insenstive;
const auto resp = AddFileExtListToFilter( 1, "png" ); const auto resp = AddFileExtListToFilter( c.m_exts );
BOOST_CHECK_EQUAL( resp, exp_filter ); BOOST_CHECK_EQUAL( resp, exp_filter );
} }
BOOST_AUTO_TEST_CASE( MultipleFilter )
{
const auto& c = ext_wildcard_cases[1];
const std::string exp_filter =
should_use_regex_filters() ? c.m_re_filter : c.m_filter_case_insenstive;
const auto resp = AddFileExtListToFilter( 2, "png", "gif" );
BOOST_CHECK_EQUAL( resp, exp_filter );
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()