ccls/utils.cc

86 lines
1.9 KiB
C++
Raw Normal View History

2017-02-17 09:57:44 +00:00
#include "utils.h"
2017-02-23 08:18:54 +00:00
#include <iostream>
2017-02-17 09:57:44 +00:00
#include <fstream>
#include "tinydir.h"
2017-03-05 19:48:05 +00:00
std::vector<std::string> GetFilesInFolder(std::string folder, bool add_folder_to_path) {
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-05 19:48:05 +00:00
std::string full_path;
if (add_folder_to_path)
full_path = folder + "/";
full_path += file.name;
2017-02-17 09:57:44 +00:00
if (file.is_dir) {
2017-03-05 19:48:05 +00:00
// Ignore all dot directories.
// Note that we must always ignore the '.' and '..' directories, otherwise
// this will loop infinitely.
if (file.name[0] != '.') {
for (std::string nested_file : GetFilesInFolder(full_path, true /*add_folder_to_path*/))
2017-02-17 09:57:44 +00:00
result.push_back(nested_file);
}
}
else {
result.push_back(full_path);
}
if (tinydir_next(&dir) == -1) {
perror("Error getting next file");
goto bail;
}
}
bail:
tinydir_close(&dir);
return result;
}
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;
}
2017-02-18 19:37:24 +00:00
void ParseTestExpectation(std::string filename, std::string* expected_output) {
2017-02-17 09:57:44 +00:00
bool in_output = false;
for (std::string line : ReadLines(filename)) {
if (line == "*/")
break;
if (in_output)
2017-02-18 19:37:24 +00:00
*expected_output += line + "\n";
2017-02-17 09:57:44 +00:00
if (line == "OUTPUT:")
in_output = true;
}
}
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-02-23 08:18:54 +00:00
}