1/* 2 * Copyright (c) 2011-2014, Intel Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation and/or 13 * other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors 16 * may be used to endorse or promote products derived from this software without 17 * specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30#include "PathNavigator.h" 31#include "Tokenizer.h" 32 33CPathNavigator::CPathNavigator(const std::string &strPath) 34{ 35 init(strPath); 36} 37 38void CPathNavigator::init(const std::string &strPath) 39{ 40 Tokenizer tokenizer(strPath, "/"); 41 42 _astrItems = tokenizer.split(); 43 44 _bValid = checkPathFormat(strPath); 45} 46 47bool CPathNavigator::isPathValid() const 48{ 49 return _bValid; 50} 51 52// Navigate through 53bool CPathNavigator::navigateThrough(const std::string &strItemName, std::string &strError) 54{ 55 if (!_bValid) { 56 57 strError = "Path not well formed: " + getCurrentPath(); 58 59 return false; 60 } 61 62 std::string *pStrChildName = next(); 63 64 if (!pStrChildName) { 65 66 strError = 67 "Path not complete: " + getCurrentPath() + ", trying to access to " + strItemName; 68 69 return false; 70 } 71 72 if (*pStrChildName != strItemName) { 73 74 strError = "Path not found: " + getCurrentPath() + ", expected: " + strItemName + 75 " but found: " + *pStrChildName; 76 77 return false; 78 } 79 80 return true; 81} 82 83std::string *CPathNavigator::next() 84{ 85 if (_currentIndex < _astrItems.size()) { 86 87 return &_astrItems[_currentIndex++]; 88 } 89 90 return NULL; 91} 92 93std::string CPathNavigator::getCurrentPath() const 94{ 95 std::string strPath = "/"; 96 97 if (!_currentIndex) { 98 99 return strPath; 100 } 101 102 size_t item; 103 for (item = 0; item < _currentIndex - 1; item++) { 104 105 strPath += _astrItems[item] + "/"; 106 } 107 108 strPath += _astrItems[item]; 109 110 return strPath; 111} 112 113bool CPathNavigator::checkPathFormat(const std::string &strUpl) 114{ 115 return strUpl[0] == '/'; 116} 117