From 6d713f064beac912d8a1acb286988630f09ede4d Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Wed, 23 Mar 2011 19:15:33 -0500 Subject: [PATCH] gear up for major use of ReadDelimitedText() by providing a better version --- common/string.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++ include/kicad_string.h | 11 +++++++++++ include/macros.h | 3 ++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/common/string.cpp b/common/string.cpp index 8c84061480..af65f7edfa 100644 --- a/common/string.cpp +++ b/common/string.cpp @@ -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 != '\\' ) diff --git a/include/kicad_string.h b/include/kicad_string.h index 8e90fb5e04..70d5e7fb13 100644 --- a/include/kicad_string.h +++ b/include/kicad_string.h @@ -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 diff --git a/include/macros.h b/include/macros.h index 4da71df155..dc435b0662 100644 --- a/include/macros.h +++ b/include/macros.h @@ -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