From kde-commits Mon Nov 14 12:47:31 2011 From: Bernhard Beschow Date: Mon, 14 Nov 2011 12:47:31 +0000 To: kde-commits Subject: [marble] src/lib/geodata/data: GeoDataCoordinates: compile on Maemo Message-Id: <20111114124731.922C7A60A6 () git ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=132127491702901 Git commit 1289f324cbcacc316358b08b97daaff22d0c1bbb by Bernhard Beschow. Committed on 14/11/2011 at 13:11. Pushed by beschow into branch 'master'. GeoDataCoordinates: compile on Maemo * only tested for decimal case, DM and DMS test cases don't terminate M +125 -70 src/lib/geodata/data/GeoDataCoordinates.cpp http://commits.kde.org/marble/1289f324cbcacc316358b08b97daaff22d0c1bbb diff --git a/src/lib/geodata/data/GeoDataCoordinates.cpp b/src/lib/geodata/= data/GeoDataCoordinates.cpp index 5cbe132..8a44c5b 100644 --- a/src/lib/geodata/data/GeoDataCoordinates.cpp +++ b/src/lib/geodata/data/GeoDataCoordinates.cpp @@ -9,6 +9,7 @@ // Copyright 2007-2008 Inge Wallin // Copyright 2008 Patrick Spendrin // Copyright 2011 Friedrich W. H. Kossebau +// Copyright 2011 Bernhard Beschow // = = @@ -62,7 +63,6 @@ private: * @param isPosHemisphere if the texts of the degree value are relativ= e to the pos hemisphere * @return the calculated degree value */ - typedef qreal (°reeValueFromXFunction)( const QRegExp& regex, int c= , bool isPosHemisphere ); static qreal degreeValueFromDMS( const QRegExp& regex, int c, bool isP= osHemisphere ); static qreal degreeValueFromDM( const QRegExp& regex, int c, bool isPo= sHemisphere ); static qreal degreeValueFromD( const QRegExp& regex, int c, bool isPos= Hemisphere ); @@ -87,21 +87,13 @@ public: private: /** * @brief tries to parse the input with the given reg expression and g= et the lon and lat values - * @param expTemplate the regular expression to use for matching - * (should contain placeholders %1 and %2 for non-capturing decimalPoi= nt expression - * and resp. the capturing direction expression) * @param input the string to parse, must not have other content than = the coordinates - * @param degreeValueFromX function pointer of the function to use for= calculating - * the degree value from the captured texts - * @param c the index in the list of captured texts which has the dire= ction text - * (assumes that the direction text is the last captured text per coor= dinate, - * and that for both coordinates the order and the number of the texts= are the same) * @param dirPosition position of the dir in the list of captured texts * @return @c true on successful parsing, @c false otherwise. */ - bool tryMatch( const char* expTemplate, const QString& input, - degreeValueFromXFunction degreeValueFromX, - int c, DirPosition dirPosition ); + bool tryMatchFromDms( const QString& input, DirPosition dirPosition ); + bool tryMatchFromDm( const QString& input, DirPosition dirPosition ); + bool tryMatchFromD( const QString& input, DirPosition dirPosition ); = /** * @brief initializes also all properties which only need to be lazily = initialized @@ -269,104 +261,167 @@ bool LonLatParser::parse( const QString& string ) = initAll(); = - // #2: Two numbers with directions - // direction as postfix, e.g. 74.2245 N 32.2434 W - const char lonLatDCapExp[] =3D - "([-+]?\\d{1,3}%1?\\d*)(?:%3)?(?:\\s*)%2(?:,|;|\\s)\\s*" - "([-+]?\\d{1,3}%1?\\d*)(?:%3)?(?:\\s*)%2"; + if ( tryMatchFromD( input, PostfixDir ) ) { + return true; + } = - if (tryMatch(lonLatDCapExp, input, degreeValueFromD, 2, PostfixDir)) { + if ( tryMatchFromD( input, PrefixDir ) ) { return true; } = - // direction as prefix, e.g. N 74.2245 W 32.2434 - const char lonLatDCapExp2[] =3D - "%2\\s*([-+]?\\d{1,3}%1?\\d*)(?:%3)?\\s*(?:,|;|\\s)\\s*" - "%2\\s*([-+]?\\d{1,3}%1?\\d*)(?:%3)?"; + if ( tryMatchFromDms( input, PostfixDir ) ) { + return true; + } + + if ( tryMatchFromDms( input, PrefixDir ) ) { + return true; + } + + if ( tryMatchFromDm( input, PostfixDir ) ) { + return true; + } = - if (tryMatch(lonLatDCapExp2, input, degreeValueFromD, 2, PrefixDir)) { + if ( tryMatchFromDm( input, PrefixDir ) ) { return true; } = - // #3: Sexagesimal + return false; +} + +// #3: Sexagesimal +bool LonLatParser::tryMatchFromDms( const QString& input, DirPosition dirP= osition ) +{ // direction as postfix - const char lonLatDMSCapExp[] =3D + const char *postfixCapExp =3D "([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2})(?:%4|\\s)\\s*" "(\\d{1,2}%1?\\d*)(?:%5)?\\s*%2[,;]?\\s*" "([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2})(?:%4|\\s)\\s*" "(\\d{1,2}%1?\\d*)(?:%5)?\\s*%2"; = - if (tryMatch(lonLatDMSCapExp, input, degreeValueFromDMS, 5, PostfixDir= )) { - return true; - } - // direction as prefix - const char lonLatDMSCapExp2[] =3D + const char *prefixCapExp =3D "%2\\s*([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2})(?:%4|\\s)\\s*" "(\\d{1,2}%1?\\d*)(?:%5)?\\s*(?:,|;|\\s)\\s*" "%2\\s*([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2})(?:%4|\\s)\\s*" "(\\d{1,2}%1?\\d*)(?:%5)?"; = - if (tryMatch(lonLatDMSCapExp2, input, degreeValueFromDMS, 5, PrefixDir= )) { - return true; + const char *expTemplate =3D ( dirPosition =3D=3D PostfixDir ) ? postfi= xCapExp + : prefixCapExp; + + const QString numberCapExp =3D + QString::fromLatin1( expTemplate ).arg( m_decimalPointExp, m_dirCa= pExp, + m_degreeExp, m_minutesExp,= m_secondsExp); + + const QRegExp regex =3D QRegExp( numberCapExp ); + if( !regex.exactMatch( input ) ) { + return false; + } + + bool isDir1LonDir; + bool isLonDirPosHemisphere; + bool isLatDirPosHemisphere; + const QString dir1 =3D regex.cap( dirPosition =3D=3D PostfixDir ? 5 : = 1 ); + const QString dir2 =3D regex.cap( dirPosition =3D=3D PostfixDir ? 10 := 6 ); + if ( !isCorrectDirections( dir1, dir2, isDir1LonDir, + isLonDirPosHemisphere, isLatDirPosHemispher= e ) ) { + return false; } = - // #4: Sexagesimal with minute precision + const int valueStartIndex1 =3D (dirPosition =3D=3D PostfixDir ? 1 : 2); + const int valueStartIndex2 =3D (dirPosition =3D=3D PostfixDir ? 6 : 7); + m_lon =3D degreeValueFromDMS( regex, isDir1LonDir ? valueStartIndex1 := valueStartIndex2, + isLonDirPosHemisphere ); + m_lat =3D degreeValueFromDMS( regex, isDir1LonDir ? valueStartIndex2 := valueStartIndex1, + isLatDirPosHemisphere ); + + return true; +} + +// #4: Sexagesimal with minute precision +bool LonLatParser::tryMatchFromDm( const QString& input, DirPosition dirPo= sition ) +{ // direction as postfix - const char lonLatDMCapExp[] =3D + const char *postfixCapExp =3D "([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2}%1?\\d*)(?:%4)?\\s*%2[,;]= ?\\s*" "([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2}%1?\\d*)(?:%4)?\\s*%2"; = - if (tryMatch(lonLatDMCapExp, input, degreeValueFromDM, 4, PostfixDir))= { - return true; - } - // direction as prefix - const char lonLatDMCapExp2[] =3D + const char *prefixCapExp =3D "%2\\s*([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2}%1?\\d*)(?:%4)?\\s*= (?:,|;|\\s)\\s*" "%2\\s*([-+]?)(\\d{1,3})(?:%3|\\s)\\s*(\\d{1,2}%1?\\d*)(?:%4)?"; = - if (tryMatch(lonLatDMCapExp2, input, degreeValueFromDM, 4, PrefixDir))= { - return true; + const char *expTemplate =3D ( dirPosition =3D=3D PostfixDir ) ? postfi= xCapExp + : prefixCapExp; + + const QString numberCapExp =3D + QString::fromLatin1( expTemplate ).arg( m_decimalPointExp, m_dirCa= pExp, + m_degreeExp, m_minutesExp = ); + const QRegExp regex =3D QRegExp( numberCapExp ); + if( !regex.exactMatch(input) ) { + return false; } = - return false; + bool isDir1LonDir; + bool isLonDirPosHemisphere; + bool isLatDirPosHemisphere; + const QString dir1 =3D regex.cap( dirPosition =3D=3D PostfixDir ? 4 : = 1 ); + const QString dir2 =3D regex.cap( dirPosition =3D=3D PostfixDir ? 8 : = 5 ); + if ( !isCorrectDirections( dir1, dir2, isDir1LonDir, + isLonDirPosHemisphere, isLatDirPosHemispher= e ) ) { + return false; + } + + const int valueStartIndex1 =3D ( dirPosition =3D=3D PostfixDir ? 1 : 2= ); + const int valueStartIndex2 =3D ( dirPosition =3D=3D PostfixDir ? 5 : 6= ); + m_lon =3D degreeValueFromDM( regex, isDir1LonDir ? valueStartIndex1 : = valueStartIndex2, + isLonDirPosHemisphere ); + m_lat =3D degreeValueFromDM( regex, isDir1LonDir ? valueStartIndex2 : = valueStartIndex1, + isLatDirPosHemisphere ); + + return true; } = -bool LonLatParser::tryMatch(const char* expTemplate, const QString& input, - degreeValueFromXFunction degreeValueFromX, - int c, DirPosition dirPosition ) +// #2: Two numbers with directions +bool LonLatParser::tryMatchFromD( const QString& input, DirPosition dirPos= ition ) { - bool successful =3D false; + // direction as postfix, e.g. 74.2245 N 32.2434 W + const char *postfixCapExp =3D + "([-+]?\\d{1,3}%1?\\d*)(?:%3)?(?:\\s*)%2(?:,|;|\\s)\\s*" + "([-+]?\\d{1,3}%1?\\d*)(?:%3)?(?:\\s*)%2"; + + // direction as prefix, e.g. N 74.2245 W 32.2434 + const char *prefixCapExp =3D + "%2\\s*([-+]?\\d{1,3}%1?\\d*)(?:%3)?\\s*(?:,|;|\\s)\\s*" + "%2\\s*([-+]?\\d{1,3}%1?\\d*)(?:%3)?"; + + const char *expTemplate =3D ( dirPosition =3D=3D PostfixDir ) ? postfi= xCapExp + : prefixCapExp; = const QString numberCapExp =3D - (c =3D=3D 5) ? QString::fromLatin1(expTemplate).arg(m_decimalPoint= Exp, m_dirCapExp, - m_degreeExp, m_min= utesExp, m_secondsExp) : - (c =3D=3D 4) ? QString::fromLatin1(expTemplate).arg(m_decimalPoint= Exp, m_dirCapExp, - m_degreeExp, m_min= utesExp) : - /* else */ QString::fromLatin1(expTemplate).arg(m_decimalPointExp,= m_dirCapExp, - m_degreeExp); + QString::fromLatin1( expTemplate ).arg( m_decimalPointExp, m_dirCa= pExp, m_degreeExp ); const QRegExp regex =3D QRegExp( numberCapExp ); - if( regex.exactMatch(input) ) { - bool isDir1LonDir; - bool isLonDirPosHemisphere; - bool isLatDirPosHemisphere; - const QString dir1 =3D regex.cap( dirPosition =3D=3D PostfixDir ? = c : 1 ); - const QString dir2 =3D regex.cap( dirPosition =3D=3D PostfixDir ? = 2*c : c+1 ); - if (isCorrectDirections(dir1, dir2, isDir1LonDir, - isLonDirPosHemisphere, isLatDirPosHemisphe= re)) { - const int valueStartIndex1 =3D (dirPosition =3D=3D PostfixDir = ? 1 : 2); - const int valueStartIndex2 =3D c + (dirPosition =3D=3D Postfix= Dir ? 1 : 2); - m_lon =3D degreeValueFromX(regex, isDir1LonDir ? valueStartInd= ex1 : valueStartIndex2, - isLonDirPosHemisphere); - m_lat =3D degreeValueFromX(regex, isDir1LonDir ? valueStartInd= ex2 : valueStartIndex1, - isLatDirPosHemisphere); - - successful =3D true; - } + if( !regex.exactMatch( input ) ) { + return false; } = - return successful; + bool isDir1LonDir; + bool isLonDirPosHemisphere; + bool isLatDirPosHemisphere; + const QString dir1 =3D regex.cap( dirPosition =3D=3D PostfixDir ? 2 : = 1 ); + const QString dir2 =3D regex.cap( dirPosition =3D=3D PostfixDir ? 4 : = 3 ); + if ( !isCorrectDirections( dir1, dir2, isDir1LonDir, + isLonDirPosHemisphere, isLatDirPosHemispher= e ) ) { + return false; + } + + const int valueStartIndex1 =3D ( dirPosition =3D=3D PostfixDir ? 1 : 2= ); + const int valueStartIndex2 =3D ( dirPosition =3D=3D PostfixDir ? 3 : 4= ); + m_lon =3D degreeValueFromD( regex, isDir1LonDir ? valueStartIndex1 : v= alueStartIndex2, + isLonDirPosHemisphere ); + m_lat =3D degreeValueFromD( regex, isDir1LonDir ? valueStartIndex2 : v= alueStartIndex1, + isLatDirPosHemisphere ); + + return true; } = QString LonLatParser::createDecimalPointExp()