Shahar Hadas
2020-05-10 d0b85cf10415a378c11d385066bb27116f2c2df1
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * FileUtils.cpp
 *
 *  Created on: Oct 8, 2019
 *      Author: devel
 */
 
#include <fstream>
#include <string>
#include <cerrno>
#include <iostream>
#include <algorithm>
 
#include "file_utils.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 = (size_t)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);
}
 
}