Commit 0093f4f6 authored by Dick Hollenbeck's avatar Dick Hollenbeck

++common:

  * macros.h now has TO_UTF8() and FROM_UTF8() which are working converters
    to and from UTF-8 encoding for any wxWidgets build mode.  We can switch to
    them at any time.  I am using them now for specctra conversions and
    elsewhere where I wanted gauranteed UTF8 encoding.
  * added OUTPUTFORMATTER::Quoted( const wxString& ) to simplify converting
    to UTF8 encoded s-expression atoms.  The recommended technique is now simply:
        out->Quoted( wxString ).c_str()
parent 740c03e7
......@@ -4,6 +4,17 @@ KiCad ChangeLog 2010
Please add newer entries at the top, list the date and your name with
email address.
2011-Feb-2 UPDATE Dick Hollenbeck <dick@softplc.com>
================================================================================
++common:
* macros.h now has TO_UTF8() and FROM_UTF8() which are working converters
to and from UTF-8 encoding for any wxWidgets build mode. We can switch to
them at any time. I am using them now for specctra conversions and
elsewhere where I wanted gauranteed UTF8 encoding.
* added OUTPUTFORMATTER::Quoted( const wxString& ) to simplify converting
to UTF8 encoded s-expression atoms. The recommended technique is now simply:
out->Quoted( wxString ).c_str()
2011-Jan-30 UPDATE Dick Hollenbeck <dick@softplc.com>
================================================================================
++all:
......
This diff is collapsed.
......@@ -2,7 +2,7 @@
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2007-2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2007-2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2007 Kicad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
......
......@@ -34,7 +34,7 @@ void XNODE::Format( OUTPUTFORMATTER* out, int nestLevel ) throw( IO_ERROR )
switch( GetType() )
{
case wxXML_ELEMENT_NODE:
out->Print( nestLevel, "(%s", CONV_TO_UTF8( GetName() ) );
out->Print( nestLevel, "(%s", out->Quoted( GetName() ).c_str() );
FormatContents( out, nestLevel );
if( GetNext() )
out->Print( 0, ")\n" );
......@@ -55,8 +55,8 @@ void XNODE::FormatContents( OUTPUTFORMATTER* out, int nestLevel ) throw( IO_ERRO
{
out->Print( 0, " (%s %s)",
// attr names should never need quoting, no spaces, we designed the file.
CONV_TO_UTF8( attr->GetName() ),
out->Quoted( CONV_TO_UTF8( attr->GetValue() ) ).c_str()
out->Quoted( attr->GetName() ).c_str(),
out->Quoted( attr->GetValue() ).c_str()
);
}
......@@ -82,9 +82,7 @@ void XNODE::FormatContents( OUTPUTFORMATTER* out, int nestLevel ) throw( IO_ERRO
break;
case wxXML_TEXT_NODE:
out->Print( 0, " %s",
out->Quoted( CONV_TO_UTF8( GetContent() ) ).c_str()
);
out->Print( 0, " %s", out->Quoted( GetContent() ).c_str() );
break;
default:
......
......@@ -32,12 +32,10 @@ wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx )
void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const throw( IO_ERROR )
{
out->Print( nestLevel, "(field (name %s)",
out->Quoted( CONV_TO_UTF8(m_Name) ).c_str() );
out->Print( nestLevel, "(field (name %s)", out->Quoted( m_Name ).c_str() );
if( !m_Value.IsEmpty() )
out->Print( 0, "(value %s)",
out->Quoted( CONV_TO_UTF8(m_Value) ).c_str() );
out->Print( 0, "(value %s)", out->Quoted( m_Value ).c_str() );
if( m_Visible )
out->Print( 0, " visible" );
......@@ -57,7 +55,7 @@ void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in ) throw( IO_ERROR
in->NeedSYMBOLorNUMBER();
m_Name = CONV_FROM_UTF8( in->CurText() );
m_Name = FROM_UTF8( in->CurText() );
in->NeedRIGHT(); // end (name ...)
......@@ -71,7 +69,7 @@ void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in ) throw( IO_ERROR
{
case T_value:
in->NeedSYMBOLorNUMBER();
m_Value = CONV_FROM_UTF8( in->CurText() );
m_Value = FROM_UTF8( in->CurText() );
in->NeedRIGHT();
break;
......@@ -156,14 +154,14 @@ int TEMPLATES::AddTemplateFieldName( const TEMPLATE_FIELDNAME& aFieldName )
if( m_Fields[i].m_Name == aFieldName.m_Name )
{
D(printf("inserting template fieldname:'%s' at %d\n",
CONV_TO_UTF8(aFieldName.m_Name), i );)
aFieldName.m_Name.utf8_str(), i );)
m_Fields[i] = aFieldName;
return i; // return the container index
}
}
// D(printf("appending template fieldname:'%s'\n", CONV_TO_UTF8(aFieldName.m_Name) );)
// D(printf("appending template fieldname:'%s'\n", aFieldName.m_Name.utf8_str() );)
// the name is legal and not previously added to the config container, append
// it and return its index within the container.
......
......@@ -11,7 +11,7 @@
// in order to use UTF8 in Kicad files.
// But this change break compatibility with older files under Windows,
// if some non ASCII characters are found in strings.
// So this is a TODO change.
// So this is a TODO change. Simply switch to TO_UTF8() and FROM_UTF() then.
#if wxUSE_UNICODE
#define CONV_TO_UTF8( wxstring ) ( (const char*) wxConvCurrent->cWX2MB( wxstring ) )
#define CONV_FROM_UTF8( utf8string ) ( wxConvCurrent->cMB2WC( utf8string ) )
......@@ -21,6 +21,19 @@
#define CONV_FROM_UTF8( utf8string ) (utf8string)
#endif
/**
* Macro TO_UTF8
* converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
* wxstring is a wxString, not a wxT() or _(). The scope of the return value
* is very limited and volatile, but can be used with printf() style functions well.
*/
#define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
/**
* Macro FROM_UTF8
* converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
*/
#define FROM_UTF8( cstring ) wxString::FromUTF8( cstring )
/**
* Function GetChars
......
......@@ -489,20 +489,25 @@ public:
* Function Quoted
* checks \a aWrapee input string for a need to be quoted
* (e.g. contains a ')' character or a space), and for \" double quotes
* within the string that need to be doubled up such that the DSNLEXER
* within the string that need to be escaped such that the DSNLEXER
* will correctly parse the string from a file later.
*
* @param aWrapee is a string that might need wraping in double quotes,
* and it might need to have its internal quotes doubled up, or not.
* and it might need to have its internal content escaped, or not.
*
* @return std::string - whose c_str() function can be called for passing
* to printf() style functions that must output utf8 encoded s-expression streams.
* @throw IO_ERROR, if aWrapee has any \r or \n bytes in it which is
* illegal according to the DSNLEXER who does not ever want them
* within a string.
* to printf() style functions that output UTF8 encoded s-expression streams.
*
* @throw IO_ERROR, if there is any kind of problem with the input string.
*/
virtual std::string Quoted( const std::string& aWrapee ) throw( IO_ERROR );
std::string Quoted( const wxString& aWrapee ) throw( IO_ERROR )
{
// s-expressions atoms are always encoded as UTF-8
return Quoted( (const char*) aWrapee.utf8_str() );
}
//-----</interface functions>-----------------------------------------
};
......
......@@ -88,7 +88,7 @@ void SPECCTRA_DB::buildLayerMaps( BOARD* aBoard )
kicadLayer2pcb[kilayer] = pcbNdx;
// save the specctra layer name in SPECCTRA_DB::layerIds for later.
layerIds.push_back( CONV_TO_UTF8( aBoard->GetLayerName( kilayer ) ) );
layerIds.push_back( TO_UTF8( aBoard->GetLayerName( kilayer ) ) );
}
}
......@@ -3404,7 +3404,7 @@ void SPECCTRA_DB::ExportPCB( wxString filename, bool aNameChange ) throw( IO_ERR
STREAM_OUTPUTFORMATTER outputFormatter( os, quote_char[0] );
if( aNameChange )
pcb->pcbname = CONV_TO_UTF8(filename);
pcb->pcbname = TO_UTF8(filename);
pcb->Format( &outputFormatter, 0 );
}
......@@ -3600,7 +3600,7 @@ PARSER::PARSER( ELEM* aParent ) :
host_cad = "Kicad's PCBNEW";
wxString msg = GetBuildVersion();
host_version = CONV_TO_UTF8(msg);
host_version = TO_UTF8(msg);
}
......
......@@ -221,7 +221,7 @@ static DRAWSEGMENT* findPoint( const wxPoint& aPoint, TYPE_COLLECTOR* items )
DRAWSEGMENT* graphic = (DRAWSEGMENT*) (*items)[i];
printf( "type=%s, GetStart()=%d,%d GetEnd()=%d,%d\n",
CONV_TO_UTF8( BOARD_ITEM::ShowShape( (Track_Shapes)graphic->m_Shape ) ),
TO_UTF8( BOARD_ITEM::ShowShape( (Track_Shapes)graphic->m_Shape ) ),
graphic->GetStart().x,
graphic->GetStart().y,
graphic->GetEnd().x,
......@@ -509,7 +509,7 @@ IMAGE* SPECCTRA_DB::makeIMAGE( BOARD* aBoard, MODULE* aModule )
IMAGE* image = new IMAGE(0);
image->image_id = CONV_TO_UTF8( aModule->m_LibRef );
image->image_id = TO_UTF8( aModule->m_LibRef );
// from the pads, and make an IMAGE using collated padstacks.
for( int p=0; p<moduleItems.GetCount(); ++p )
......@@ -558,7 +558,7 @@ IMAGE* SPECCTRA_DB::makeIMAGE( BOARD* aBoard, MODULE* aModule )
PIN* pin = new PIN(image);
padName = pad->ReturnStringPadName();
pin->pin_id = CONV_TO_UTF8( padName );
pin->pin_id = TO_UTF8( padName );
if( padName!=wxEmptyString && pinmap.find( padName )==pinmap.end() )
{
......@@ -659,7 +659,7 @@ IMAGE* SPECCTRA_DB::makeIMAGE( BOARD* aBoard, MODULE* aModule )
case S_ARC:
default:
D( printf("makeIMAGE(): unsupported shape %s\n",
CONV_TO_UTF8( BOARD_ITEM::ShowShape( (Track_Shapes)graphic->m_Shape)) );)
TO_UTF8( BOARD_ITEM::ShowShape( (Track_Shapes)graphic->m_Shape)) );)
continue;
}
}
......@@ -932,7 +932,7 @@ void SPECCTRA_DB::FromBOARD( BOARD* aBoard ) throw( IO_ERROR )
}
// if we cannot insert OK, that means the reference has been seen before.
STRINGSET_PAIR refpair = refs.insert( CONV_TO_UTF8( module->GetReference() ) );
STRINGSET_PAIR refpair = refs.insert( TO_UTF8( module->GetReference() ) );
if( !refpair.second ) // insert failed
{
ThrowIOError( _("Multiple components have identical reference IDs of \"%s\"."),
......@@ -1096,7 +1096,7 @@ void SPECCTRA_DB::FromBOARD( BOARD* aBoard ) throw( IO_ERROR )
PATH* mainPolygon = new PATH( plane, T_polygon );
plane->SetShape( mainPolygon );
plane->name = CONV_TO_UTF8( item->m_Netname );
plane->name = TO_UTF8( item->m_Netname );
if( plane->name.size() == 0 )
{
......@@ -1181,7 +1181,7 @@ void SPECCTRA_DB::FromBOARD( BOARD* aBoard ) throw( IO_ERROR )
NETINFO_ITEM* net = aBoard->m_NetInfo->GetNetItem(ii);
int netcode = net->GetNet();
if( netcode > 0 )
nets[ netcode ]->net_id = CONV_TO_UTF8( net->GetNetname() );
nets[ netcode ]->net_id = TO_UTF8( net->GetNetname() );
}
items.Collect( aBoard, scanMODULEs );
......@@ -1194,7 +1194,7 @@ void SPECCTRA_DB::FromBOARD( BOARD* aBoard ) throw( IO_ERROR )
IMAGE* image = makeIMAGE( aBoard, module );
componentId = CONV_TO_UTF8( module->GetReference() );
componentId = TO_UTF8( module->GetReference() );
// create a net list entry for all the actual pins in the image
// for the current module. location of this code is critical
......@@ -1241,7 +1241,7 @@ void SPECCTRA_DB::FromBOARD( BOARD* aBoard ) throw( IO_ERROR )
place->SetRotation( module->m_Orient/10.0 );
place->SetVertex( mapPt( module->m_Pos ) );
place->component_id = componentId;
place->part_number = CONV_TO_UTF8( module->GetValue() );
place->part_number = TO_UTF8( module->GetValue() );
// module is flipped from bottom side, set side to T_back
if( module->flag )
......@@ -1373,7 +1373,7 @@ void SPECCTRA_DB::FromBOARD( BOARD* aBoard ) throw( IO_ERROR )
old_netcode = netcode;
NETINFO_ITEM* net = aBoard->FindNet( netcode );
wxASSERT( net );
netname = CONV_TO_UTF8( net->GetNetname() );
netname = TO_UTF8( net->GetNetname() );
}
WIRE* wire = new WIRE( wiring );
......@@ -1435,7 +1435,7 @@ void SPECCTRA_DB::FromBOARD( BOARD* aBoard ) throw( IO_ERROR )
NETINFO_ITEM* net = aBoard->FindNet( netcode );
wxASSERT( net );
dsnVia->net_id = CONV_TO_UTF8( net->GetNetname() );
dsnVia->net_id = TO_UTF8( net->GetNetname() );
dsnVia->via_type = T_protect; // @todo, this should be configurable
}
......@@ -1512,10 +1512,10 @@ void SPECCTRA_DB::exportNETCLASS( NETCLASS* aNetClass, BOARD* aBoard )
// freerouter creates a class named 'default' anyway, and if we
// try and use that, we end up with two 'default' via rules so use
// something else as the name of our default class.
clazz->class_id = CONV_TO_UTF8( aNetClass->GetName() );
clazz->class_id = TO_UTF8( aNetClass->GetName() );
for( NETCLASS::iterator net = aNetClass->begin(); net != aNetClass->end(); ++net )
clazz->net_ids.push_back( CONV_TO_UTF8( *net ) );
clazz->net_ids.push_back( TO_UTF8( *net ) );
clazz->rules = new RULE( clazz, T_rule );
......
......@@ -200,7 +200,7 @@ TRACK* SPECCTRA_DB::makeTRACK( PATH* aPath, int aPointIndex, int aNetcode ) thro
if( layerNdx == -1 )
{
wxString layerName = CONV_FROM_UTF8( aPath->layer_id.c_str() );
wxString layerName = FROM_UTF8( aPath->layer_id.c_str() );
ThrowIOError( _("Session file uses invalid layer id \"%s\""),
GetChars(layerName) );
}
......@@ -308,7 +308,7 @@ SEGVIA* SPECCTRA_DB::makeVIA( PADSTACK* aPadstack, const POINT& aPoint, int aNet
int layerNdx = findLayerName( circle->layer_id );
if( layerNdx == -1 )
{
wxString layerName = CONV_FROM_UTF8( circle->layer_id.c_str() );
wxString layerName = FROM_UTF8( circle->layer_id.c_str() );
ThrowIOError( _("Session file uses invalid layer id \"%s\""),
GetChars( layerName ) );
}
......@@ -387,7 +387,7 @@ void SPECCTRA_DB::FromSESSION( BOARD* aBoard ) throw( IO_ERROR )
{
PLACE* place = &places[i]; // '&' even though places[] holds a pointer!
wxString reference = CONV_FROM_UTF8( place->component_id.c_str() );
wxString reference = FROM_UTF8( place->component_id.c_str() );
MODULE* module = aBoard->FindModuleByReference( reference );
if( !module )
{
......@@ -446,7 +446,7 @@ void SPECCTRA_DB::FromSESSION( BOARD* aBoard ) throw( IO_ERROR )
// page 143 of spec says wire's net_id is optional
if( net->net_id.size() )
{
wxString netName = CONV_FROM_UTF8( net->net_id.c_str() );
wxString netName = FROM_UTF8( net->net_id.c_str() );
NETINFO_ITEM* net = aBoard->FindNet( netName );
if( net )
......@@ -471,7 +471,7 @@ void SPECCTRA_DB::FromSESSION( BOARD* aBoard ) throw( IO_ERROR )
We kept our own zones in the BOARD, so ignore this so called
'wire'.
wxString netId = CONV_FROM_UTF8( wire->net_id.c_str() );
wxString netId = FROM_UTF8( wire->net_id.c_str() );
ThrowIOError(
_("Unsupported wire shape: \"%s\" for net: \"%s\""),
DLEX::GetTokenString(shape).GetData(),
......@@ -507,7 +507,7 @@ void SPECCTRA_DB::FromSESSION( BOARD* aBoard ) throw( IO_ERROR )
// page 144 of spec says wire_via's net_id is optional
if( net->net_id.size() )
{
wxString netName = CONV_FROM_UTF8( net->net_id.c_str() );
wxString netName = FROM_UTF8( net->net_id.c_str() );
NETINFO_ITEM* net = aBoard->FindNet( netName );
if( net )
......@@ -534,7 +534,7 @@ void SPECCTRA_DB::FromSESSION( BOARD* aBoard ) throw( IO_ERROR )
// Could use a STRING_FORMATTER here and convert the entire
// wire_via to text and put that text into the exception.
wxString psid( CONV_FROM_UTF8( wire_via->GetPadstackId().c_str() ) );
wxString psid( FROM_UTF8( wire_via->GetPadstackId().c_str() ) );
ThrowIOError( _("A wire_via references a missing padstack \"%s\""),
GetChars( psid ) );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment