gcontini
2019-10-12 dda16735b94661b798d6c0fd3e41af944de0a1fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * 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) {
    vector<string> existingFiles;
    for (auto it = fileList.begin(); it != fileList.end(); it++) {
        ifstream f(it->c_str());
        if (f.good()) {
            existingFiles.push_back(*it);
        }
        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);
}
 
}