-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfile-manager.cc
108 lines (89 loc) · 2.5 KB
/
file-manager.cc
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "file-manager.h"
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
using std::cout;
using std::endl;
FileManager::FileManager() { Initialize(); }
FileManager::~FileManager() {}
bool FileManager::Initialize() {
// Get the user directory. Without this we can't find where to save our data.
char* home_dir = getenv("HOME");
home_dir_ = string(home_dir);
if (home_dir_.empty()) {
cout << "Error: Unable to determine home directory." << endl;
return false;
}
home_dir_ += "/";
// Try and make the .todo directory. This is also how we determine if it
// already exists.
data_dir_ = home_dir_ + ".todo";
if (!CheckDir(data_dir_)) {
return false;
}
data_dir_ += "/";
// Make (or check for the existence of) the Projects directory.
project_dir_ = data_dir_ + "Projects";
if (!CheckDir(project_dir_)) {
return false;
}
project_dir_ += "/";
// See how many projects there are in the project directory
vector<string> projects;
if (!DirectoryContents(project_dir_, &projects)) {
cout << "Error getting contents of project directory." << endl;
return false;
}
// Check for a config file.
config_file_path_ = data_dir_ + "config";
if (!FileExists(config_file_path_)) {
config_file_path_ = "";
}
return true;
}
bool FileManager::DirectoryContents(const string& dir,
vector<string>* contents) {
contents->clear();
DIR* dir_ptr = opendir(dir.c_str());
if (dir_ptr == NULL) {
return false;
}
dirent* dr;
while ((dr = readdir(dir_ptr))) {
if (strcmp(dr->d_name, ".") != 0 && strcmp(dr->d_name, "..") != 0 &&
dr->d_name[0] != '.') {
contents->push_back(dr->d_name);
}
}
closedir(dir_ptr);
return true;
}
int FileManager::NumSavedProjects() {
vector<string> projects;
DirectoryContents(ProjectDir(), &projects);
return projects.size();
}
vector<string> FileManager::SavedProjectNames() {
vector<string> projects;
DirectoryContents(ProjectDir(), &projects);
return projects;
}
bool FileManager::CheckDir(const string& dir) {
int edir = mkdir(dir.c_str(), 0755);
if (edir != 0 && errno != EEXIST) {
cout << "Error creating or reading directory \"" << dir << "\"" << endl;
return false;
}
return true;
}
bool FileManager::FileExists(const string& file_path) {
struct stat file_stat;
if (stat(file_path.c_str(), &file_stat) != 0) {
return false;
}
return true;
}