You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.1 KiB

8 years ago
#include "utils.h"
8 years ago
#include <cassert>
#include <iostream>
8 years ago
#include <fstream>
#include "tinydir.h"
8 years ago
static std::vector<std::string> GetFilesInFolderHelper(std::string folder, std::string output_prefix) {
8 years ago
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;
}
8 years ago
// Skip all dot files.
if (file.name[0] != '.') {
if (file.is_dir) {
// 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, output_prefix + file.name + "/"))
8 years ago
result.push_back(nested_file);
}
8 years ago
else {
result.push_back(output_prefix + file.name);
}
8 years ago
}
if (tinydir_next(&dir) == -1) {
perror("Error getting next file");
goto bail;
}
}
bail:
tinydir_close(&dir);
return result;
}
8 years ago
std::vector<std::string> GetFilesInFolder(std::string folder, bool add_folder_to_path) {
assert(folder.size() > 0);
if (folder[folder.size() - 1] != '/')
folder += '/';
return GetFilesInFolderHelper(folder, add_folder_to_path ? folder : "");
}
8 years ago
std::vector<std::string> ReadLines(std::string filename) {
std::vector<std::string> result;
std::ifstream input(filename);
for (std::string line; getline(input, line); )
result.push_back(line);
return result;
}
8 years ago
void ParseTestExpectation(std::string filename, std::string* expected_output) {
8 years ago
bool in_output = false;
for (std::string line : ReadLines(filename)) {
if (line == "*/")
break;
if (in_output)
8 years ago
*expected_output += line + "\n";
8 years ago
if (line == "OUTPUT:")
in_output = true;
}
}
void Fail(const std::string& message) {
std::cerr << "Fatal error: " << message << std::endl;
std::exit(1);
}
void WriteToFile(const std::string& filename, const std::string& content) {
std::ofstream file(filename);
file << content;
}