summaryrefslogtreecommitdiff
path: root/tools/filesystem.c
blob: 0f04100409128269193b5ac12a506de7138f29ea (plain)
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
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <time.h>
#include "filesystem.h"

bool FileExists(const string &fullpath) {
    struct stat buffer;
    return (stat (fullpath.c_str(), &buffer) == 0); 
}

bool FileExists(const string &path, const string &name, const string &ext) {
    stringstream fileName;
    fileName << path << name << "." << ext;
    struct stat buffer;
    return (stat (fileName.str().c_str(), &buffer) == 0); 
}

string ReadFromFile(const string &path) {
    string line;
    stringstream content;
    ifstream myfile (path.c_str());
    if (myfile.is_open()) {
        while ( getline (myfile,line) ) {
            content << line << '\n';
        }
        myfile.close();
    }
    return content.str();
}

bool WriteIntoFile(const string &path, const string &content) {
    if (content.size() < 5)
        return false;
    ofstream myfile;
    myfile.open(path.c_str());
    if (myfile.is_open()) {
        myfile << content;
        myfile.close();
        return true;
    }
    return false;
}

time_t FileCreationTime(const string &path) {
    struct stat t_stat;
    stat(path.c_str(), &t_stat);
    struct tm * timeinfo = localtime(&t_stat.st_ctime);
    time_t creationTime = mktime(timeinfo);
    return creationTime;
}