diff options
author | Alex Zolotarev <alex@mapswithme.com> | 2013-06-25 19:47:48 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:56:57 +0300 |
commit | b20c78f4a846dc21deafb208fedb288e4bd544e6 (patch) | |
tree | 0978b55769bc9992fde2563327e4ae0634db7290 /map | |
parent | 8a9eacc353413332e150b93ae879d95232c171fe (diff) |
[api] Refactored and deleted duplicate ApiPoint
Diffstat (limited to 'map')
-rw-r--r-- | map/framework.cpp | 45 | ||||
-rw-r--r-- | map/framework.hpp | 4 | ||||
-rw-r--r-- | map/ge0_parser.cpp | 48 | ||||
-rw-r--r-- | map/ge0_parser.hpp | 6 | ||||
-rw-r--r-- | map/map.pro | 1 | ||||
-rw-r--r-- | map/map_tests/ge0_parser_tests.cpp | 31 | ||||
-rw-r--r-- | map/map_tests/mwm_url_tests.cpp | 22 | ||||
-rw-r--r-- | map/mwm_url.cpp | 2 | ||||
-rw-r--r-- | map/mwm_url.hpp | 2 | ||||
-rw-r--r-- | map/url_api.hpp | 28 |
10 files changed, 70 insertions, 119 deletions
diff --git a/map/framework.cpp b/map/framework.cpp index aae7f51a2f..9768bad793 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -1412,7 +1412,7 @@ void Framework::AddBookmarkAndSetViewport(Bookmark & bm, m2::RectD const & viewP ShowRectExVisibleScale(viewPort); } -bool Framework::SetViewportByURL(string const & url, url_api::Request & request) +bool Framework::SetViewportByURL(string const & url, url_scheme::ApiPoint & balloonPoint) { if (strings::StartsWith(url, "geo")) { @@ -1423,29 +1423,24 @@ bool Framework::SetViewportByURL(string const & url, url_api::Request & request) if (info.IsValid()) { - // @TODO this is hack to kick-off release - request.m_points.push_back(url_api::Point()); - url_api::Point & newPoint = request.m_points.back(); - - newPoint.m_name = m_stringsBundle.GetString("dropped_pin"); - request.m_viewportLat = newPoint.m_lat = info.m_lat; - request.m_viewportLon = newPoint.m_lon = info.m_lon; - request.m_viewportZoomLevel = info.m_zoom; + balloonPoint.m_name = m_stringsBundle.GetString("dropped_pin"); + balloonPoint.m_lat = info.m_lat; + balloonPoint.m_lon = info.m_lon; SetViewPortSync(info.GetViewport()); return true; } } else if (strings::StartsWith(url, "ge0")) { - url_api::Ge0Parser parser; - if (parser.Parse(url, request)) + url_scheme::Ge0Parser parser; + double zoomLevel; + if (parser.Parse(url, balloonPoint, zoomLevel)) { - url_api::Point & point = request.m_points.front(); - if (point.m_name.empty()) - point.m_name = m_stringsBundle.GetString("dropped_pin"); + if (balloonPoint.m_name.empty()) + balloonPoint.m_name = m_stringsBundle.GetString("dropped_pin"); - m2::PointD const center(MercatorBounds::LonToX(request.m_viewportLon), MercatorBounds::LatToY(request.m_viewportLat)); - SetViewPortSync(scales::GetRectForLevel(request.m_viewportZoomLevel, center, 1)); + m2::PointD const center(MercatorBounds::LonToX(balloonPoint.m_lon), MercatorBounds::LatToY(balloonPoint.m_lat)); + SetViewPortSync(scales::GetRectForLevel(zoomLevel, center, 1)); return true; } } @@ -1456,23 +1451,11 @@ bool Framework::SetViewportByURL(string const & url, url_api::Request & request) // Can do better consider nav bar size SetViewPortSync(MercatorBounds::FromLatLonRect(m_ParsedMapApi.GetLatLonRect())); - // Populate request if request has only one point - // with this one point to show balloon. - // @todo: refactor to more general model, Point and ApiPoint must not exist together. - if (m_ParsedMapApi.GetPoints().size() == 1) + if (!m_ParsedMapApi.GetPoints().empty()) { - url_scheme::ApiPoint const apiPoint = m_ParsedMapApi.GetPoints().front(); - url_api::Point point; - - request.m_viewportLat = point.m_lat = apiPoint.m_lat; - request.m_viewportLon = point.m_lon = apiPoint.m_lon; - point.m_name = apiPoint.m_title; - point.m_id = apiPoint.m_id; - - request.m_points.push_back(point); + balloonPoint = m_ParsedMapApi.GetPoints().front(); + return true; } - - return true; } } return false; diff --git a/map/framework.hpp b/map/framework.hpp index 158ad8b831..f914d7dfdf 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -10,7 +10,6 @@ #include "feature_vec_model.hpp" #include "bookmark.hpp" #include "bookmark_manager.hpp" -#include "url_api.hpp" #include "mwm_url.hpp" #include "../defines.hpp" @@ -306,7 +305,8 @@ public: m2::PointD GetViewportCenter() const; void SetViewportCenter(m2::PointD const & pt); - bool SetViewportByURL(string const & url, url_api::Request & request); + /// @param[out] outPoint is a point in url, where we need to show balloon + bool SetViewportByURL(string const & url, url_scheme::ApiPoint & balloonPoint); bool NeedRedraw() const; void SetNeedRedraw(bool flag); diff --git a/map/ge0_parser.cpp b/map/ge0_parser.cpp index d37be6d971..b1ef7b3d3e 100644 --- a/map/ge0_parser.cpp +++ b/map/ge0_parser.cpp @@ -1,5 +1,5 @@ #include "ge0_parser.hpp" -#include "url_api.hpp" +#include "mwm_url.hpp" #include "../api/internal/c/api-client-internals.h" @@ -10,8 +10,10 @@ #include "../base/math.hpp" #include "../base/string_utils.hpp" +namespace url_scheme +{ -url_api::Ge0Parser::Ge0Parser() +Ge0Parser::Ge0Parser() { for (size_t i = 0; i < 256; ++i) m_base64ReverseCharTable[i] = 255; @@ -22,7 +24,7 @@ url_api::Ge0Parser::Ge0Parser() } } -bool url_api::Ge0Parser::Parse(string const & url, Request & request) +bool Ge0Parser::Parse(string const & url, url_scheme::ApiPoint & outPoint, double & outZoomLevel) { // URL format: // @@ -38,44 +40,36 @@ bool url_api::Ge0Parser::Parse(string const & url, Request & request) const int LATLON_LENGTH = NAME_POSITON_IN_URL - LATLON_POSITION - 1; const size_t MAX_NAME_LENGTH = 256; - request.Clear(); if (url.size() < 16 || !strings::StartsWith(url, "ge0://")) return false; uint8_t const zoomI = DecodeBase64Char(url[ZOOM_POSITION]); if (zoomI > 63) return false; - request.m_viewportZoomLevel = DecodeZoom(zoomI); - - request.m_points.push_back(url_api::Point()); - url_api::Point & newPt = request.m_points.back(); + outZoomLevel = DecodeZoom(zoomI); - DecodeLatLon(url.substr(LATLON_POSITION, LATLON_LENGTH), newPt.m_lat, newPt.m_lon); + DecodeLatLon(url.substr(LATLON_POSITION, LATLON_LENGTH), outPoint.m_lat, outPoint.m_lon); - ASSERT(MercatorBounds::ValidLon(newPt.m_lon), (newPt.m_lon)); - ASSERT(MercatorBounds::ValidLat(newPt.m_lat), (newPt.m_lat)); - - request.m_viewportLat = newPt.m_lat; - request.m_viewportLon = newPt.m_lon; + ASSERT(MercatorBounds::ValidLon(outPoint.m_lon), (outPoint.m_lon)); + ASSERT(MercatorBounds::ValidLat(outPoint.m_lat), (outPoint.m_lat)); if (url.size() >= NAME_POSITON_IN_URL) - newPt.m_name = DecodeName(url.substr(NAME_POSITON_IN_URL, - min(url.size() - NAME_POSITON_IN_URL, MAX_NAME_LENGTH))); + outPoint.m_name = DecodeName(url.substr(NAME_POSITON_IN_URL, min(url.size() - NAME_POSITON_IN_URL, MAX_NAME_LENGTH))); return true; } -uint8_t url_api::Ge0Parser::DecodeBase64Char(char const c) +uint8_t Ge0Parser::DecodeBase64Char(char const c) { return m_base64ReverseCharTable[static_cast<uint8_t>(c)]; } -double url_api::Ge0Parser::DecodeZoom(uint8_t const zoomByte) +double Ge0Parser::DecodeZoom(uint8_t const zoomByte) { //Coding zoom - int newZoom = ((oldZoom - 4) * 4) return static_cast<double>(zoomByte) / 4 + 4; } -void url_api::Ge0Parser::DecodeLatLon(string const & url, double & lat, double & lon) +void Ge0Parser::DecodeLatLon(string const & url, double & lat, double & lon) { int latInt = 0, lonInt = 0; DecodeLatLonToInt(url, latInt, lonInt, url.size()); @@ -83,7 +77,7 @@ void url_api::Ge0Parser::DecodeLatLon(string const & url, double & lat, double & lon = DecodeLonFromInt(lonInt, (1 << MAPSWITHME_MAX_COORD_BITS) - 1); } -void url_api::Ge0Parser::DecodeLatLonToInt(string const & url, int & lat, int & lon, int const bytes) +void Ge0Parser::DecodeLatLonToInt(string const & url, int & lat, int & lon, int const bytes) { for(int i = 0, shift = MAPSWITHME_MAX_COORD_BITS - 3; i < bytes; ++i, shift -= 3) { @@ -102,17 +96,17 @@ void url_api::Ge0Parser::DecodeLatLonToInt(string const & url, int & lat, int & lon += middleOfSquare; } -double url_api::Ge0Parser::DecodeLatFromInt(int const lat, int const maxValue) +double Ge0Parser::DecodeLatFromInt(int const lat, int const maxValue) { return static_cast<double>(lat) / maxValue * 180 - 90; } -double url_api::Ge0Parser::DecodeLonFromInt(int const lon, int const maxValue) +double Ge0Parser::DecodeLonFromInt(int const lon, int const maxValue) { return static_cast<double>(lon) / (maxValue + 1.0) * 360.0 - 180; } -string url_api::Ge0Parser::DecodeName(string name) +string Ge0Parser::DecodeName(string name) { ValidateName(name); name = UrlDecode(name); @@ -120,7 +114,7 @@ string url_api::Ge0Parser::DecodeName(string name) return name; } -void url_api::Ge0Parser::SpacesToUnderscore(string & name) +void Ge0Parser::SpacesToUnderscore(string & name) { for (size_t i = 0; i < name.size(); ++i) if (name[i] == ' ') @@ -129,7 +123,7 @@ void url_api::Ge0Parser::SpacesToUnderscore(string & name) name[i] = ' '; } -void url_api::Ge0Parser::ValidateName(string & name) +void Ge0Parser::ValidateName(string & name) { if (name.empty()) return; @@ -147,7 +141,9 @@ void url_api::Ge0Parser::ValidateName(string & name) name.resize(name.size() - 2); } -bool url_api::Ge0Parser::IsHexChar(char const a) +bool Ge0Parser::IsHexChar(char const a) { return ((a >= '0' && a <= '9') || (a >= 'A' && a <= 'F') || (a >= 'a' && a <= 'f')); } + +} // namespace url_scheme diff --git a/map/ge0_parser.hpp b/map/ge0_parser.hpp index 09bc6d9370..16fc269bcc 100644 --- a/map/ge0_parser.hpp +++ b/map/ge0_parser.hpp @@ -3,17 +3,17 @@ #include "../std/string.hpp" -namespace url_api +namespace url_scheme { -struct Request; +struct ApiPoint; class Ge0Parser { public: Ge0Parser(); - bool Parse(string const & url, Request & request); + bool Parse(string const & url, url_scheme::ApiPoint & outPoint, double & outZoomLevel); protected: uint8_t DecodeBase64Char(char const c); diff --git a/map/map.pro b/map/map.pro index 25e5cf213c..54ad4e6300 100644 --- a/map/map.pro +++ b/map/map.pro @@ -56,7 +56,6 @@ HEADERS += \ geometry_processors.hpp \ bookmark_manager.hpp \ ge0_parser.hpp \ - url_api.hpp \ SOURCES += \ feature_vec_model.cpp \ diff --git a/map/map_tests/ge0_parser_tests.cpp b/map/map_tests/ge0_parser_tests.cpp index 7d097ee7bd..3cb3e2a2c2 100644 --- a/map/map_tests/ge0_parser_tests.cpp +++ b/map/map_tests/ge0_parser_tests.cpp @@ -1,7 +1,7 @@ #include "../../testing/testing.hpp" #include "../ge0_parser.hpp" -#include "../url_api.hpp" +#include "../mwm_url.hpp" #include "../../api/internal/c/api-client-internals.h" #include "../../api/src/c/api-client.h" @@ -9,8 +9,8 @@ #include "../../base/macros.hpp" -using url_api::Ge0Parser; -using url_api::Request; +using url_scheme::Ge0Parser; +using url_scheme::ApiPoint; namespace { @@ -40,29 +40,30 @@ double GetLonEpsilon(int coordBytes) void TestSuccess(char const * s, double lat, double lon, double zoom, char const * name) { Ge0Parser parser; - Request request; - bool const result = parser.Parse(s, request); + ApiPoint apiPoint; + double parsedZoomLevel; + bool const result = parser.Parse(s, apiPoint, parsedZoomLevel); TEST(result, (s, zoom, lat, lon, name)); - TEST_EQUAL(request.m_points.size(), 1, (s, zoom, lat, lon, name)); - TEST_EQUAL(request.m_points[0].m_name, string(name), (s)); - TEST_EQUAL(request.m_points[0].m_id, string(), (s)); + TEST_EQUAL(apiPoint.m_name, string(name), (s)); + TEST_EQUAL(apiPoint.m_id, string(), (s)); double const latEps = GetLatEpsilon(9); double const lonEps = GetLonEpsilon(9); - TEST(fabs(request.m_points[0].m_lat - lat) <= latEps, (s, zoom, lat, lon, name)); - TEST(fabs(request.m_points[0].m_lon - lon) <= lonEps, (s, zoom, lat, lon, name)); + TEST(fabs(apiPoint.m_lat - lat) <= latEps, (s, zoom, lat, lon, name)); + TEST(fabs(apiPoint.m_lon - lon) <= lonEps, (s, zoom, lat, lon, name)); - TEST(fabs(request.m_viewportLat - lat) <= latEps, (s, zoom, lat, lon, name)); - TEST(fabs(request.m_viewportLon - lon) <= lonEps, (s, zoom, lat, lon, name)); - TEST_ALMOST_EQUAL(request.m_viewportZoomLevel, zoom, (s, zoom, lat, lon, name)); + TEST(fabs(apiPoint.m_lat - lat) <= latEps, (s, zoom, lat, lon, name)); + TEST(fabs(apiPoint.m_lon - lon) <= lonEps, (s, zoom, lat, lon, name)); + TEST_ALMOST_EQUAL(parsedZoomLevel, zoom, (s, zoom, lat, lon, name)); } void TestFailure(char const * s) { Ge0Parser parser; - Request request; - bool const result = parser.Parse(s, request); + ApiPoint apiPoint; + double zoomLevel; + bool const result = parser.Parse(s, apiPoint, zoomLevel); TEST_EQUAL(result, false, (s)); } diff --git a/map/map_tests/mwm_url_tests.cpp b/map/map_tests/mwm_url_tests.cpp index dc8c9008e6..0bc8c1a9f7 100644 --- a/map/map_tests/mwm_url_tests.cpp +++ b/map/map_tests/mwm_url_tests.cpp @@ -19,7 +19,7 @@ UNIT_TEST(MapApiSmoke) TEST_EQUAL(api.GetPoints().size(), 1, ()); TEST_EQUAL(api.GetPoints()[0].m_lat, 38.970559, ()); TEST_EQUAL(api.GetPoints()[0].m_lon, -9.419289, ()); - TEST_EQUAL(api.GetPoints()[0].m_title, "Point Name", ()); + TEST_EQUAL(api.GetPoints()[0].m_name, "Point Name", ()); TEST_EQUAL(api.GetPoints()[0].m_id, "", ()); TEST_EQUAL(api.GetGlobalBackUrl(), "", ()); } @@ -47,7 +47,7 @@ UNIT_TEST(MapApiPointNameBeforeLatLon) ParsedMapApi api(Uri("mapswithme://map?n=Name&ll=1,2")); TEST(api.IsValid(), ()); TEST_EQUAL(api.GetPoints().size(), 1, ()); - TEST_EQUAL(api.GetPoints()[0].m_title, "", ()); + TEST_EQUAL(api.GetPoints()[0].m_name, "", ()); } UNIT_TEST(MapApiPointNameOverwritten) @@ -55,7 +55,7 @@ UNIT_TEST(MapApiPointNameOverwritten) ParsedMapApi api(Uri("mapswithme://map?ll=1,2&n=A&N=B")); TEST(api.IsValid(), ()); TEST_EQUAL(api.GetPoints().size(), 1, ()); - TEST_EQUAL(api.GetPoints()[0].m_title, "B", ()); + TEST_EQUAL(api.GetPoints()[0].m_name, "B", ()); } UNIT_TEST(MapApiMultiplePoints) @@ -65,11 +65,11 @@ UNIT_TEST(MapApiMultiplePoints) TEST_EQUAL(api.GetPoints().size(), 3, ()); TEST_EQUAL(api.GetPoints()[0].m_lat, 1.1, ()); TEST_EQUAL(api.GetPoints()[0].m_lon, 1.2, ()); - TEST_EQUAL(api.GetPoints()[0].m_title, "A", ()); - TEST_EQUAL(api.GetPoints()[1].m_title, "", ()); + TEST_EQUAL(api.GetPoints()[0].m_name, "A", ()); + TEST_EQUAL(api.GetPoints()[1].m_name, "", ()); TEST_EQUAL(api.GetPoints()[1].m_lat, 2.1, ()); TEST_EQUAL(api.GetPoints()[1].m_lon, 2.2, ()); - TEST_EQUAL(api.GetPoints()[2].m_title, "C", ()); + TEST_EQUAL(api.GetPoints()[2].m_name, "C", ()); TEST_EQUAL(api.GetPoints()[2].m_lat, -3.1, ()); TEST_EQUAL(api.GetPoints()[2].m_lon, -3.2, ()); } @@ -81,7 +81,7 @@ UNIT_TEST(MapApiInvalidPointLatLonButValidOtherParts) TEST_EQUAL(api.GetPoints().size(), 1, ()); TEST_EQUAL(api.GetPoints()[0].m_lat, 2, ()); TEST_EQUAL(api.GetPoints()[0].m_lon, 2, ()); - TEST_EQUAL(api.GetPoints()[0].m_title, "B", ()); + TEST_EQUAL(api.GetPoints()[0].m_name, "B", ()); } UNIT_TEST(MapApiPointURLEncoded) @@ -89,7 +89,7 @@ UNIT_TEST(MapApiPointURLEncoded) ParsedMapApi api(Uri("mwm://map?ll=1,2&n=%D0%9C%D0%B8%D0%BD%D1%81%D0%BA&id=http%3A%2F%2Fmap%3Fll%3D1%2C2%26n%3Dtest")); TEST(api.IsValid(), ()); TEST_EQUAL(api.GetPoints().size(), 1, ()); - TEST_EQUAL(api.GetPoints()[0].m_title, "\xd0\x9c\xd0\xb8\xd0\xbd\xd1\x81\xd0\xba", ()); + TEST_EQUAL(api.GetPoints()[0].m_name, "\xd0\x9c\xd0\xb8\xd0\xbd\xd1\x81\xd0\xba", ()); TEST_EQUAL(api.GetPoints()[0].m_id, "http://map?ll=1,2&n=test", ()); } @@ -216,7 +216,7 @@ string generatePartOfUrl(url_scheme::ApiPoint const & point) { stringstream stream; stream << "&ll=" << strings::ToString(point.m_lat) << "," << strings::ToString(point.m_lon) - << "&n=" << point.m_title + << "&n=" << point.m_name << "&id=" << point.m_id; return stream.str(); } @@ -241,7 +241,7 @@ void generateRandomTest(size_t numberOfPoints, size_t stringLength) point.m_lat *= random.Generate() % 2 == 0 ? 1 : -1; point.m_lon = random.Generate() % 180; point.m_lon *= random.Generate() % 2 == 0 ? 1 : -1; - point.m_title = randomString(stringLength, i); + point.m_name = randomString(stringLength, i); point.m_id = randomString(stringLength, i); vect[i] = point; } @@ -256,7 +256,7 @@ void generateRandomTest(size_t numberOfPoints, size_t stringLength) { TEST_EQUAL(points[i].m_lat, vect[i].m_lat, ()); TEST_EQUAL(points[i].m_lon, vect[i].m_lon, ()); - TEST_EQUAL(points[i].m_title, vect[i].m_title, ()); + TEST_EQUAL(points[i].m_name, vect[i].m_name, ()); TEST_EQUAL(points[i].m_id, vect[i].m_id, ()); } TEST_EQUAL(api.GetApiVersion(), 1, ()); diff --git a/map/mwm_url.cpp b/map/mwm_url.cpp index 5bc66520a3..220555374b 100644 --- a/map/mwm_url.cpp +++ b/map/mwm_url.cpp @@ -103,7 +103,7 @@ void ParsedMapApi::AddKeyValue(string key, string const & value) else if (key == "n") { if (!m_points.empty()) - m_points.back().m_title = value; + m_points.back().m_name = value; else LOG(LWARNING, ("Map API: Point name with no point. 'll' should come first!")); } diff --git a/map/mwm_url.hpp b/map/mwm_url.hpp index 556292d229..ff71b71c27 100644 --- a/map/mwm_url.hpp +++ b/map/mwm_url.hpp @@ -10,7 +10,7 @@ struct ApiPoint { double m_lat; double m_lon; - string m_title; + string m_name; string m_id; }; diff --git a/map/url_api.hpp b/map/url_api.hpp deleted file mode 100644 index 176c382dea..0000000000 --- a/map/url_api.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "../std/vector.hpp" - -namespace url_api -{ - -struct Point -{ - Point() : m_lat(0), m_lon(0) {} - - double m_lat; - double m_lon; - string m_name; - string m_id; -}; - -struct Request -{ - vector<Point> m_points; - double m_viewportLat, m_viewportLon, m_viewportZoomLevel; - - void Clear() - { - m_points.clear(); - m_viewportLat = m_viewportLon = m_viewportZoomLevel = 0; - } -}; - -} // namespace url_api |