Skip to content

Instantly share code, notes, and snippets.

@nabijaczleweli
Last active September 16, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabijaczleweli/72b3ec88ad4bf39c27b5 to your computer and use it in GitHub Desktop.
Save nabijaczleweli/72b3ec88ad4bf39c27b5 to your computer and use it in GitHub Desktop.
Directive store
// The MIT License (MIT)
// Copyright (c) 2015 nabijaczleweli
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#pragma once
#include "settings.hpp"
#include <functional>
#include <iosfwd>
#include <string>
#include <regex>
#include <vector>
extern std::vector<
std::pair<const std::regex, std::function<int(const std::smatch &, std::ostream & to, const std::string & root_directory, const settings_t &)>>> directives;
// The MIT License (MIT)
// Copyright (c) 2015 nabijaczleweli
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include "directives.hpp"
#include "transform.hpp"
#include "util/error.hpp"
#include "util/file.hpp"
#include "util/glob.hpp"
#include "util/line.hpp"
#include <iostream>
#include <fstream>
using namespace std;
static inline regex operator"" _r(const char * str, unsigned int) {
return regex(str, regex_constants::optimize);
}
static int include_file(const smatch & match, ostream & to, const string & rootdir, const settings_t & settings);
std::vector<std::pair<const std::regex, std::function<int(const std::smatch &, std::ostream & to, const std::string & root_directory, const settings_t &)>>>
directives({{"[s-]?include([[:space:]]*((?:[^[:space:]]+[[:space:]]?)*))?"_r, include_file}});
static int do_include_file(const vector<string> & files, ostream & to, const string & rootdir, const settings_t & settings);
static int include_file(const smatch & match, ostream & to, const string & rootdir, const settings_t & settings) {
const auto & includepaths = match.str(2);
if(includepaths.empty()) {
if(settings.verbose)
clog << "v: empty include directive, skipping\n";
return 0;
} else
// GNU make doesn't permit spaces in paths, see http://git.savannah.gnu.org/cgit/make.git/tree/read.c#n3102
return do_include_file(glob(tokenize(includepaths)), to, rootdir, settings);
}
static int do_include_file(const vector<string> & files, ostream & to, const string & rootdir, const settings_t & settings) {
for(const auto & file : files) {
if(settings.verbose)
clog << "v: including file \"" << file << "\"\n";
ifstream includefile(rootdir + '/' + file);
if(!includefile)
return error(file, 2, settings);
if(int ret = transform_makefile(includefile, to, path_nolastnode(rootdir + '/' + file), settings))
return ret;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment