2017-02-17 09:57:44 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2017-03-06 07:19:00 +00:00
|
|
|
#include <cassert>
|
2017-02-23 08:18:54 +00:00
|
|
|
#include <iostream>
|
2017-02-17 09:57:44 +00:00
|
|
|
#include <fstream>
|
2017-04-08 22:54:36 +00:00
|
|
|
#include <unordered_map>
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-03-25 20:24:42 +00:00
|
|
|
#include <tinydir.h>
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-03-31 04:15:42 +00:00
|
|
|
// See http://stackoverflow.com/a/2072890
|
|
|
|
bool EndsWith(const std::string& value, const std::string& ending) {
|
|
|
|
if (ending.size() > value.size())
|
|
|
|
return false;
|
|
|
|
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StartsWith(const std::string& value, const std::string& start) {
|
|
|
|
if (start.size() > value.size())
|
|
|
|
return false;
|
|
|
|
return std::equal(start.begin(), start.end(), value.begin());
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:24:51 +00:00
|
|
|
static std::vector<std::string> GetFilesInFolderHelper(std::string folder, bool recursive, std::string output_prefix) {
|
2017-02-17 09:57:44 +00:00
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
tinydir_dir dir;
|
|
|
|
if (tinydir_open(&dir, folder.c_str()) == -1) {
|
|
|
|
perror("Error opening file");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (dir.has_next) {
|
|
|
|
tinydir_file file;
|
|
|
|
if (tinydir_readfile(&dir, &file) == -1) {
|
|
|
|
perror("Error getting file");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2017-03-06 07:19:00 +00:00
|
|
|
// Skip all dot files.
|
|
|
|
if (file.name[0] != '.') {
|
|
|
|
if (file.is_dir) {
|
2017-03-11 02:24:51 +00:00
|
|
|
if (recursive) {
|
|
|
|
// Note that we must always ignore the '.' and '..' directories, otherwise
|
|
|
|
// this will loop infinitely. The above check handles that for us.
|
|
|
|
for (std::string nested_file : GetFilesInFolderHelper(file.path, true /*recursive*/, output_prefix + file.name + "/"))
|
|
|
|
result.push_back(nested_file);
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-03-06 07:19:00 +00:00
|
|
|
else {
|
|
|
|
result.push_back(output_prefix + file.name);
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tinydir_next(&dir) == -1) {
|
|
|
|
perror("Error getting next file");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail:
|
|
|
|
tinydir_close(&dir);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:24:51 +00:00
|
|
|
std::vector<std::string> GetFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path) {
|
2017-03-06 07:19:00 +00:00
|
|
|
assert(folder.size() > 0);
|
|
|
|
if (folder[folder.size() - 1] != '/')
|
|
|
|
folder += '/';
|
|
|
|
|
2017-03-11 02:24:51 +00:00
|
|
|
return GetFilesInFolderHelper(folder, recursive, add_folder_to_path ? folder : "");
|
2017-03-06 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:02:55 +00:00
|
|
|
// http://stackoverflow.com/a/6089413
|
|
|
|
std::istream& SafeGetline(std::istream& is, std::string& t) {
|
|
|
|
t.clear();
|
|
|
|
|
|
|
|
// The characters in the stream are read one-by-one using a std::streambuf.
|
|
|
|
// That is faster than reading them one-by-one using the std::istream.
|
|
|
|
// Code that uses streambuf this way must be guarded by a sentry object.
|
|
|
|
// The sentry object performs various tasks,
|
|
|
|
// such as thread synchronization and updating the stream state.
|
|
|
|
|
|
|
|
std::istream::sentry se(is, true);
|
|
|
|
std::streambuf* sb = is.rdbuf();
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
int c = sb->sbumpc();
|
|
|
|
switch (c) {
|
|
|
|
case '\n':
|
|
|
|
return is;
|
|
|
|
case '\r':
|
|
|
|
if(sb->sgetc() == '\n')
|
|
|
|
sb->sbumpc();
|
|
|
|
return is;
|
|
|
|
case EOF:
|
|
|
|
// Also handle the case when the last line has no line ending
|
|
|
|
if(t.empty())
|
|
|
|
is.setstate(std::ios::eofbit);
|
|
|
|
return is;
|
|
|
|
default:
|
|
|
|
t += (char)c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
std::vector<std::string> ReadLines(std::string filename) {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
std::ifstream input(filename);
|
2017-03-09 18:02:55 +00:00
|
|
|
for (std::string line; SafeGetline(input, line); )
|
2017-02-17 09:57:44 +00:00
|
|
|
result.push_back(line);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
std::unordered_map<std::string, std::string> ParseTestExpectation(std::string filename) {
|
2017-02-17 09:57:44 +00:00
|
|
|
bool in_output = false;
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
#if false
|
|
|
|
#include "bar.h"
|
|
|
|
|
|
|
|
void foo();
|
|
|
|
|
|
|
|
/*
|
|
|
|
// if no name is given assume to be this file name
|
|
|
|
// no output section means we don't check that index.
|
|
|
|
OUTPUT: bar.cc
|
|
|
|
{}
|
|
|
|
|
|
|
|
OUTPUT: bar.h
|
|
|
|
{}
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::string> result;
|
|
|
|
|
|
|
|
std::string active_output_filename;
|
|
|
|
std::string active_output_contents;
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
for (std::string line : ReadLines(filename)) {
|
|
|
|
if (line == "*/")
|
|
|
|
break;
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
if (StartsWith(line, "OUTPUT:")) {
|
|
|
|
if (in_output) {
|
|
|
|
result[active_output_filename] = active_output_contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.size() > 7)
|
|
|
|
active_output_filename = line.substr(8);
|
|
|
|
else
|
|
|
|
active_output_filename = filename;
|
|
|
|
active_output_contents = "";
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
in_output = true;
|
2017-04-08 22:54:36 +00:00
|
|
|
}
|
|
|
|
else if (in_output)
|
|
|
|
active_output_contents += line + "\n";
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-04-08 22:54:36 +00:00
|
|
|
|
|
|
|
if (in_output)
|
|
|
|
result[active_output_filename] = active_output_contents;
|
|
|
|
return result;
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-02-23 08:18:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Fail(const std::string& message) {
|
|
|
|
std::cerr << "Fatal error: " << message << std::endl;
|
|
|
|
std::exit(1);
|
2017-03-01 08:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteToFile(const std::string& filename, const std::string& content) {
|
|
|
|
std::ofstream file(filename);
|
|
|
|
file << content;
|
2017-03-09 18:02:55 +00:00
|
|
|
}
|