C++-ify output using ToString

This commit is contained in:
Seth Hillbrand 2024-06-25 18:35:12 -07:00
parent 0a55ca5e96
commit f97ac5ce73
2 changed files with 44 additions and 52 deletions

View File

@ -238,7 +238,6 @@ bool SPECCTRA_DB::BuiltBoardOutlines( BOARD* aBoard )
PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad ) PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
{ {
char name[256]; // padstack name builder
std::string uniqifier; std::string uniqifier;
// caller must do these checks before calling here. // caller must do these checks before calling here.
@ -286,16 +285,15 @@ PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
if( aPad->GetOffset().x || aPad->GetOffset().y ) if( aPad->GetOffset().x || aPad->GetOffset().y )
{ {
char offsetTxt[64];
VECTOR2I offset( aPad->GetOffset().x, aPad->GetOffset().y ); VECTOR2I offset( aPad->GetOffset().x, aPad->GetOffset().y );
dsnOffset = mapPt( offset ); dsnOffset = mapPt( offset );
// Using () would cause padstack name to be quoted, and {} locks freerouter, so use [].
// using () would cause padstack name to be quoted, and {} locks freerouter, so use []. std::ostringstream oss;
std::snprintf( offsetTxt, sizeof( offsetTxt ), "[%.6g,%.6g]", dsnOffset.x, dsnOffset.y ); oss.imbue( std::locale::classic() );
oss << std::fixed << std::setprecision( 6 )
uniqifier += offsetTxt; << '[' << dsnOffset.x << ',' << dsnOffset.y << ']';
uniqifier += oss.str();
} }
switch( aPad->GetShape() ) switch( aPad->GetShape() )
@ -319,12 +317,11 @@ PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
circle->SetVertex( dsnOffset ); circle->SetVertex( dsnOffset );
} }
snprintf( name, sizeof(name), "Round%sPad_%.6g_um", std::ostringstream oss;
uniqifier.c_str(), IU2um( aPad->GetSize().x ) ); oss << "Round" << uniqifier << "Pad_" << std::fixed << std::setprecision(6)
<< IU2um(aPad->GetSize().x) << "_um";
name[ sizeof(name) - 1 ] = 0; padstack->SetPadstackId( oss.str().c_str() );
padstack->SetPadstackId( name );
break; break;
} }
@ -353,12 +350,11 @@ PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
rect->SetCorners( lowerLeft, upperRight ); rect->SetCorners( lowerLeft, upperRight );
} }
snprintf( name, sizeof( name ), "Rect%sPad_%.6gx%.6g_um", uniqifier.c_str(), std::ostringstream oss;
IU2um( aPad->GetSize().x ), IU2um( aPad->GetSize().y ) ); oss << "Rect" << uniqifier << "Pad_" << std::fixed << std::setprecision(6)
<< IU2um(aPad->GetSize().x) << "x" << IU2um(aPad->GetSize().y) << "_um";
name[sizeof( name ) - 1] = 0; padstack->SetPadstackId( oss.str().c_str() );
padstack->SetPadstackId( name );
break; break;
} }
@ -404,11 +400,11 @@ PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
path->aperture_width = 2.0 * radius; path->aperture_width = 2.0 * radius;
} }
snprintf( name, sizeof( name ), "Oval%sPad_%.6gx%.6g_um", uniqifier.c_str(), std::ostringstream oss;
IU2um( aPad->GetSize().x ), IU2um( aPad->GetSize().y ) ); oss << "Oval" << uniqifier << "Pad_" << std::fixed << std::setprecision(6)
name[sizeof( name ) - 1] = 0; << IU2um(aPad->GetSize().x) << "x" << IU2um(aPad->GetSize().y) << "_um";
padstack->SetPadstackId( name ); padstack->SetPadstackId( oss.str().c_str() );
break; break;
} }
@ -451,13 +447,13 @@ PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
} }
// this string _must_ be unique for a given physical shape // this string _must_ be unique for a given physical shape
snprintf( name, sizeof( name ), "Trapz%sPad_%.6gx%.6g_%c%.6gx%c%.6g_um", uniqifier.c_str(), std::ostringstream oss;
IU2um( aPad->GetSize().x ), IU2um( aPad->GetSize().y ), oss << "Trapz" << uniqifier << "Pad_" << std::fixed << std::setprecision(6)
aPad->GetDelta().x < 0 ? 'n' : 'p', std::abs( IU2um( aPad->GetDelta().x ) ), << IU2um(aPad->GetSize().x) << "x" << IU2um(aPad->GetSize().y) << "_"
aPad->GetDelta().y < 0 ? 'n' : 'p', std::abs( IU2um( aPad->GetDelta().y ) ) ); << (aPad->GetDelta().x < 0 ? "n" : "p") << std::abs(IU2um(aPad->GetDelta().x)) << "x"
name[sizeof( name ) - 1] = 0; << (aPad->GetDelta().y < 0 ? "n" : "p") << std::abs(IU2um(aPad->GetDelta().y)) << "_um";
padstack->SetPadstackId( name ); padstack->SetPadstackId( oss.str().c_str() );
break; break;
} }
@ -524,14 +520,17 @@ PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
} }
// this string _must_ be unique for a given physical shape // this string _must_ be unique for a given physical shape
snprintf( name, sizeof( name ), "RoundRect%sPad_%.6gx%.6g_%.6g_um_%f_%X", uniqifier.c_str(), std::ostringstream oss;
IU2um( aPad->GetSize().x ), IU2um( aPad->GetSize().y ), IU2um( rradius ), oss << "RoundRect" << uniqifier << "Pad_"
doChamfer ? aPad->GetChamferRectRatio() : 0.0, << std::fixed << std::setprecision(6)
doChamfer ? aPad->GetChamferPositions() : 0 ); << IU2um( aPad->GetSize().x ) << 'x'
<< IU2um( aPad->GetSize().y ) << '_'
<< IU2um( rradius ) << "_um_"
<< ( doChamfer ? aPad->GetChamferRectRatio() : 0.0 ) << '_'
<< std::hex << std::uppercase
<< ( doChamfer ? aPad->GetChamferPositions() : 0 );
name[sizeof( name ) - 1] = 0; padstack->SetPadstackId( oss.str().c_str() );
padstack->SetPadstackId( name );
break; break;
} }
@ -577,18 +576,17 @@ PADSTACK* SPECCTRA_DB::makePADSTACK( BOARD* aBoard, PAD* aPad )
// this string _must_ be unique for a given physical shape, so try to make it unique // this string _must_ be unique for a given physical shape, so try to make it unique
const HASH_128 hash = pad_shape.GetHash(); const HASH_128 hash = pad_shape.GetHash();
char hashStr[33] = {}; const BOX2I rect = aPad->GetBoundingBox();
snprintf( hashStr, sizeof( hashStr ), "%016llX%016llX", hash.Value64[0], hash.Value64[1] ); std::ostringstream oss;
oss << "Cust" << uniqifier << "Pad_"
<< std::fixed << std::setprecision(6)
<< IU2um( aPad->GetSize().x ) << 'x' << IU2um( aPad->GetSize().y ) << '_'
<< IU2um( rect.GetWidth() ) << 'x' << IU2um( rect.GetHeight() ) << '_'
<< polygonal_shape.size() << "_um_"
<< hash.ToString();
BOX2I rect = aPad->GetBoundingBox(); padstack->SetPadstackId( oss.str().c_str() );
snprintf( name, sizeof( name ), "Cust%sPad_%.6gx%.6g_%.6gx_%.6g_%d_um_%s",
uniqifier.c_str(), IU2um( aPad->GetSize().x ), IU2um( aPad->GetSize().y ),
IU2um( rect.GetWidth() ), IU2um( rect.GetHeight() ), (int) polygonal_shape.size(),
hashStr );
name[sizeof( name ) - 1] = 0;
padstack->SetPadstackId( name );
break; break;
} }
} }

View File

@ -345,13 +345,7 @@ BOOST_AUTO_TEST_CASE( TestHash128 )
BOOST_CHECK( b == h ); BOOST_CHECK( b == h );
BOOST_CHECK( h == b ); BOOST_CHECK( h == b );
std::stringstream ss; BOOST_CHECK_EQUAL( h.ToString(), std::string( "00CDEF012345678956789ABCDEF01234" ) );
ss << std::hex << std::uppercase << std::setfill('0')
<< std::setw(16) << h.Value64[0]
<< std::setw(16) << h.Value64[1];
std::string hashStr = ss.str();
BOOST_CHECK( std::string( hashStr ) == std::string( "00CDEF012345678956789ABCDEF01234" ) );
} }