gear up for major use of ReadDelimitedText() by providing a better version

This commit is contained in:
Dick Hollenbeck 2011-03-23 19:15:33 -05:00
parent 62c9e7e0c2
commit 6d713f064b
3 changed files with 57 additions and 1 deletions

View File

@ -8,6 +8,48 @@
#include "kicad_string.h"
int ReadDelimitedText( wxString* aDest, const char* aSource )
{
std::string utf8; // utf8 but without escapes and quotes.
bool inside = false;
const char* start = aSource;
char cc;
while( (cc = *aSource++) != 0 )
{
if( cc == '"' )
{
if( inside )
break; // 2nd double quote is end of delimited text
inside = true; // first delimiter found, make note, do not copy
}
else if( inside )
{
if( cc == '\\' )
{
cc = *aSource++;
if( !cc )
break;
// do no copy the escape byte if it is followed by \ or "
if( cc != '"' && cc != '\\' )
utf8 += '\\';
utf8 += cc;
}
else
utf8 += cc;
}
}
*aDest = FROM_UTF8( utf8.c_str() );
return aSource - start;
}
int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
{
if( aDestSize <= 0 )
@ -33,6 +75,8 @@ int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize )
if( cc == '\\' )
{
cc = *aSource++;
if( !cc )
break;
// do no copy the escape byte if it is followed by \ or "
if( cc != '"' && cc != '\\' )

View File

@ -26,9 +26,20 @@ char* strlower( char* Text );
* @param aDestSize is the size of the destination byte buffer.
* @return int - the number of bytes read from source, which may be more than
* the number copied, due to escaping of double quotes and the escape byte itself.
* @deprecated should use the one which fetches a wxString, below.
*/
int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
/**
* Function ReadDelimitedText
* copies bytes from @a aSource delimited string segment to @a aDest wxString.
*
* @param aDest is the destination wxString
* @param aSource is the source C string holding utf8 encoded bytes.
* @return int - the number of bytes read from source, which may be more than
* the number copied, due to escaping of double quotes and the escape byte itself.
*/
int ReadDelimitedText( wxString* aDest, const char* aSource );
/**
* Function EscapedUTF8

View File

@ -20,13 +20,14 @@
* converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
*/
//#define FROM_UTF8( cstring ) wxString::FromUTF8( cstring )
inline wxString FROM_UTF8( const char* cstring )
static inline wxString FROM_UTF8( const char* cstring )
{
wxString line = wxString::FromUTF8( cstring );
if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
return line;
}
/**
* Function GetChars
* returns a wxChar* to the actual character data within a wxString, and is