Kaydet (Commit) 061ebc34 authored tarafından Caolán McNamara's avatar Caolán McNamara

add a getToken wrapper for extracting a single token painlessly

üst 90fe8dad
...@@ -134,6 +134,34 @@ COMPHELPER_DLLPUBLIC rtl::OString replace(const rtl::OString &rIn, ...@@ -134,6 +134,34 @@ COMPHELPER_DLLPUBLIC rtl::OString replace(const rtl::OString &rIn,
COMPHELPER_DLLPUBLIC rtl::OUString replace(const rtl::OUString &rIn, COMPHELPER_DLLPUBLIC rtl::OUString replace(const rtl::OUString &rIn,
const rtl::OUString &rSearch, const rtl::OUString &rReplace); const rtl::OUString &rSearch, const rtl::OUString &rReplace);
/** Returns a token in the OString
@param token the number of the token to return
@param cTok the character which seperate the tokens.
@return the token if token is negative or doesn't exist an empty token
is returned
*/
COMPHELPER_DLLPUBLIC inline rtl::OString getToken(const rtl::OString &rIn,
sal_Int32 nToken, sal_Char cTok) SAL_THROW(())
{
sal_Int32 nIndex = 0;
return rIn.getToken(nToken, cTok, nIndex);
}
/** Returns a token in the OUString
@param token the number of the token to return
@param cTok the character which seperate the tokens.
@return the token if token is negative or doesn't exist an empty token
is returned
*/
COMPHELPER_DLLPUBLIC inline rtl::OUString getToken(const rtl::OUString &rIn,
sal_Int32 nToken, sal_Unicode cTok) SAL_THROW(())
{
sal_Int32 nIndex = 0;
return rIn.getToken(nToken, cTok, nIndex);
}
/** Convert a sequence of strings to a single comma separated string. /** Convert a sequence of strings to a single comma separated string.
Note that no escaping of commas or anything fancy is done. Note that no escaping of commas or anything fancy is done.
......
...@@ -44,12 +44,14 @@ public: ...@@ -44,12 +44,14 @@ public:
void test(); void test();
void testNatural(); void testNatural();
void testReplace(); void testReplace();
void testToken();
void testDecimalStringToNumber(); void testDecimalStringToNumber();
CPPUNIT_TEST_SUITE(TestString); CPPUNIT_TEST_SUITE(TestString);
CPPUNIT_TEST(test); CPPUNIT_TEST(test);
CPPUNIT_TEST(testNatural); CPPUNIT_TEST(testNatural);
CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testReplace);
CPPUNIT_TEST(testToken);
CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testDecimalStringToNumber);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
}; };
...@@ -311,11 +313,39 @@ void TestString::testReplace() ...@@ -311,11 +313,39 @@ void TestString::testReplace()
CPPUNIT_ASSERT(aOut.isEmpty()); CPPUNIT_ASSERT(aOut.isEmpty());
aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa foo aaa foo bbb")); aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa foo aaa foo bbb"));
aOut = ::comphelper::string::replace(aIn, aOut = ::comphelper::string::replace(aIn,
rtl::OString(RTL_CONSTASCII_STRINGPARAM("foo")), rtl::OString(RTL_CONSTASCII_STRINGPARAM("foo")),
rtl::OString(RTL_CONSTASCII_STRINGPARAM("bar"))); rtl::OString(RTL_CONSTASCII_STRINGPARAM("bar")));
CPPUNIT_ASSERT(aOut.equalsL( CPPUNIT_ASSERT(aOut.equalsL(
RTL_CONSTASCII_STRINGPARAM("aaa bar aaa bar bbb"))); RTL_CONSTASCII_STRINGPARAM("aaa bar aaa bar bbb")));
aOut = ::comphelper::string::replace(aIn,
rtl::OString(' '),
rtl::OString());
CPPUNIT_ASSERT(aOut.equalsL(
RTL_CONSTASCII_STRINGPARAM("aaafooaaafoobbb")));
}
void TestString::testToken()
{
::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("10.11.12"));
::rtl::OString aOut;
aOut = ::comphelper::string::getToken(aIn, -1, '.');
CPPUNIT_ASSERT(aOut.isEmpty());
aOut = ::comphelper::string::getToken(aIn, 0, '.');
CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("10")));
aOut = ::comphelper::string::getToken(aIn, 1, '.');
CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("11")));
aOut = ::comphelper::string::getToken(aIn, 2, '.');
CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("12")));
aOut = ::comphelper::string::getToken(aIn, 3, '.');
CPPUNIT_ASSERT(aOut.isEmpty());
} }
CPPUNIT_TEST_SUITE_REGISTRATION(TestString); CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
......
...@@ -53,6 +53,7 @@ ...@@ -53,6 +53,7 @@
#include <osl/process.h> #include <osl/process.h>
#include <connectivity/dbexception.hxx> #include <connectivity/dbexception.hxx>
#include <comphelper/namedvaluecollection.hxx> #include <comphelper/namedvaluecollection.hxx>
#include <comphelper/string.hxx>
#include <unotools/confignode.hxx> #include <unotools/confignode.hxx>
#include <unotools/ucbstreamhelper.hxx> #include <unotools/ucbstreamhelper.hxx>
#include "resource/hsqldb_res.hrc" #include "resource/hsqldb_res.hrc"
...@@ -278,7 +279,7 @@ namespace connectivity ...@@ -278,7 +279,7 @@ namespace connectivity
if ( pStream.get() ) if ( pStream.get() )
{ {
ByteString sLine; ByteString sLine;
ByteString sVersionString; rtl::OString sVersionString;
while ( pStream->ReadLine(sLine) ) while ( pStream->ReadLine(sLine) )
{ {
if ( sLine.Len() == 0 ) if ( sLine.Len() == 0 )
...@@ -292,18 +293,19 @@ namespace connectivity ...@@ -292,18 +293,19 @@ namespace connectivity
else else
{ {
if ( sIniKey.Equals( "version" ) if ( sIniKey.Equals( "version" )
&& ( sVersionString.Len() == 0 ) && ( sVersionString.isEmpty() )
) )
{ {
sVersionString = sValue; sVersionString = sValue;
} }
} }
} }
if ( sVersionString.Len() ) if (!sVersionString.isEmpty())
{ {
const sal_Int32 nMajor = sVersionString.GetToken(0,'.').ToInt32(); using comphelper::string::getToken;
const sal_Int32 nMinor = sVersionString.GetToken(1,'.').ToInt32(); const sal_Int32 nMajor = getToken(sVersionString, 0, '.').toInt32();
const sal_Int32 nMicro = sVersionString.GetToken(2,'.').ToInt32(); const sal_Int32 nMinor = getToken(sVersionString, 1, '.').toInt32();
const sal_Int32 nMicro = getToken(sVersionString, 2, '.').toInt32();
if ( nMajor > 1 if ( nMajor > 1
|| ( nMajor == 1 && nMinor > 8 ) || ( nMajor == 1 && nMinor > 8 )
|| ( nMajor == 1 && nMinor == 8 && nMicro > 0 ) ) || ( nMajor == 1 && nMinor == 8 && nMicro > 0 ) )
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <rtl/strbuf.hxx> #include <rtl/strbuf.hxx>
#include <comphelper/string.hxx>
extern "C" { int yyerror( const char * ); } extern "C" { int yyerror( const char * ); }
extern "C" { int YYWarning( const char * ); } extern "C" { int YYWarning( const char * ); }
...@@ -1032,23 +1033,29 @@ int Export::Execute( int nToken, const char * pToken ) ...@@ -1032,23 +1033,29 @@ int Export::Execute( int nToken, const char * pToken )
bDontWriteOutput = sal_True; bDontWriteOutput = sal_True;
} }
break; break;
case APPFONTMAPPING: { case APPFONTMAPPING:
{
using comphelper::string::replace;
using comphelper::string::getToken;
bDontWriteOutput = sal_False; bDontWriteOutput = sal_False;
// this is a AppfontMapping, so look if its a definition // this is a AppfontMapping, so look if its a definition
// of field size // of field size
ByteString sKey = sToken.GetToken( 0, '=' ); ByteString sKey = sToken.GetToken( 0, '=' );
sKey.EraseAllChars( ' ' ); sKey.EraseAllChars( ' ' );
sKey.EraseAllChars( '\t' ); sKey.EraseAllChars( '\t' );
ByteString sMapping = sToken.GetToken( 1, '=' ); rtl::OString sMapping = sToken.GetToken( 1, '=' );
sMapping = sMapping.GetToken( 1, '(' ); sMapping = getToken(sMapping, 1, '(');
sMapping = sMapping.GetToken( 0, ')' ); sMapping = getToken(sMapping, 0, ')');
sMapping.EraseAllChars( ' ' ); sMapping = replace(sMapping, rtl::OString(' '), rtl::OString());
sMapping.EraseAllChars( '\t' ); sMapping = replace(sMapping, rtl::OString('\t'), rtl::OString());
if ( sKey.ToUpperAscii() == "SIZE" ) { if ( sKey.ToUpperAscii() == "SIZE" )
pResData->nWidth = ( sal_uInt16 ) sMapping.GetToken( 0, ',' ).ToInt32(); {
} pResData->nWidth = ( sal_uInt16 ) getToken(sMapping, 0, ',').toInt32();
else if ( sKey == "POSSIZE" ) { }
pResData->nWidth = ( sal_uInt16 ) sMapping.GetToken( 2, ',' ).ToInt32(); else if ( sKey == "POSSIZE" )
{
pResData->nWidth = ( sal_uInt16 ) getToken(sMapping, 2, ',').toInt32();
} }
} }
break; break;
......
...@@ -77,6 +77,7 @@ APP1TARGET= transex3 ...@@ -77,6 +77,7 @@ APP1TARGET= transex3
APP1OBJS= $(OBJ)$/src_yy_wrapper.obj APP1OBJS= $(OBJ)$/src_yy_wrapper.obj
APP1STDLIBS+= \ APP1STDLIBS+= \
$(TOOLSLIB) \ $(TOOLSLIB) \
$(COMPHELPERLIB) \
$(SALLIB) $(SALLIB)
APP1LIBS+= $(LB)$/$(TARGET).lib APP1LIBS+= $(LB)$/$(TARGET).lib
APP1DEPN= $(OBJ)$/src_yy_wrapper.obj $(LB)$/$(TARGET).lib APP1DEPN= $(OBJ)$/src_yy_wrapper.obj $(LB)$/$(TARGET).lib
......
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