From 497745ad31c90545b288e2845049e0ce474bcbe2 Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: 周六, 19 10月 2019 10:10:45 +0800 Subject: [PATCH] Merge branch 'feature/code_review_and_refactor' into develop --- src/library/base/FileUtils.cpp | 69 ++++++++++++++++++++++++++++++++++ 1 files changed, 69 insertions(+), 0 deletions(-) diff --git a/src/library/base/FileUtils.cpp b/src/library/base/FileUtils.cpp new file mode 100644 index 0000000..f8fcc20 --- /dev/null +++ b/src/library/base/FileUtils.cpp @@ -0,0 +1,69 @@ +/* + * FileUtils.cpp + * + * Created on: Oct 8, 2019 + * Author: devel + */ + +#include <fstream> +#include <string> +#include <cerrno> +#include <iostream> +#include <algorithm> + +#include "FileUtils.hpp" + +namespace license { +using namespace std; + +vector<string> filter_existing_files(const vector<string> &fileList, + EventRegistry& registry,const char* extraData) { + vector<string> existingFiles; + for (auto it = fileList.begin(); it != fileList.end(); it++) { + registry.addEvent(LICENSE_SPECIFIED,it->c_str(), extraData); + ifstream f(it->c_str()); + if (f.good()) { + existingFiles.push_back(*it); + registry.addEvent(LICENSE_FOUND,it->c_str(),extraData); + } else { + registry.addEvent(LICENSE_FILE_NOT_FOUND,it->c_str(), extraData); + } + f.close(); + } + return existingFiles; +} + +string get_file_contents(const char *filename, size_t max_size) { + ifstream in(filename, std::ios::binary); + if (in) { + string contents; + size_t index = in.seekg(0, ios::end).tellg(); + size_t limited_size = min(index, max_size); + contents.resize(limited_size); + in.seekg(0, ios::beg); + in.read(&contents[0], limited_size); + return contents; + } + throw(errno); +} + +string remove_extension(const string& path) { + if (path == "." || path == "..") { + return path; + } + size_t dotpos = path.find_last_of("."); + //no dot + if (dotpos == string::npos) { + return path; + } + //find the last path separator + size_t pathsep_pos = path.find_last_of("\\/"); + if (pathsep_pos == string::npos) { + return (dotpos == 0 ? path : path.substr(0, dotpos)); + } else if(pathsep_pos >= dotpos +1) { + return path; + } + return path.substr(0, dotpos); +} + +} -- Gitblit v1.9.1