1240116Smarcel// 2240116Smarcel// Automated Testing Framework (atf) 3240116Smarcel// 4240116Smarcel// Copyright (c) 2007 The NetBSD Foundation, Inc. 5240116Smarcel// All rights reserved. 6240116Smarcel// 7240116Smarcel// Redistribution and use in source and binary forms, with or without 8240116Smarcel// modification, are permitted provided that the following conditions 9240116Smarcel// are met: 10240116Smarcel// 1. Redistributions of source code must retain the above copyright 11240116Smarcel// notice, this list of conditions and the following disclaimer. 12240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright 13240116Smarcel// notice, this list of conditions and the following disclaimer in the 14240116Smarcel// documentation and/or other materials provided with the distribution. 15240116Smarcel// 16240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28240116Smarcel// 29240116Smarcel 30240116Smarcel#include <stdexcept> 31240116Smarcel 32240116Smarcel#include "expand.hpp" 33240116Smarcel#include "text.hpp" 34240116Smarcel 35240116Smarcelnamespace impl = atf::expand; 36240116Smarcel#define IMPL_NAME "atf::expand" 37240116Smarcel 38240116Smarcel// ------------------------------------------------------------------------ 39240116Smarcel// Auxiliary functions. 40240116Smarcel// ------------------------------------------------------------------------ 41240116Smarcel 42240116Smarcelnamespace { 43240116Smarcel 44240116Smarcelstd::string 45240116Smarcelglob_to_regex(const std::string& glob) 46240116Smarcel{ 47240116Smarcel std::string regex; 48240116Smarcel regex.reserve(glob.length() * 2); 49240116Smarcel 50240116Smarcel regex += '^'; 51240116Smarcel for (std::string::const_iterator iter = glob.begin(); iter != glob.end(); 52240116Smarcel iter++) { 53240116Smarcel switch (*iter) { 54240116Smarcel case '*': regex += ".*"; break; 55240116Smarcel case '?': regex += "."; break; 56240116Smarcel default: regex += *iter; 57240116Smarcel } 58240116Smarcel } 59240116Smarcel regex += '$'; 60240116Smarcel 61240116Smarcel return regex; 62240116Smarcel} 63240116Smarcel 64240116Smarcel} // anonymous namespace 65240116Smarcel 66240116Smarcel// ------------------------------------------------------------------------ 67240116Smarcel// Free functions. 68240116Smarcel// ------------------------------------------------------------------------ 69240116Smarcel 70240116Smarcelbool 71240116Smarcelimpl::is_glob(const std::string& glob) 72240116Smarcel{ 73240116Smarcel // NOTE: Keep this in sync with glob_to_regex! 74240116Smarcel return glob.find_first_of("*?") != std::string::npos; 75240116Smarcel} 76240116Smarcel 77240116Smarcelbool 78240116Smarcelimpl::matches_glob(const std::string& glob, const std::string& candidate) 79240116Smarcel{ 80240116Smarcel return atf::text::match(candidate, glob_to_regex(glob)); 81240116Smarcel} 82