-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.cpp
67 lines (54 loc) · 1.83 KB
/
filesystem.cpp
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
//
// Created by Harold on 2021/5/6.
//
#include <iostream>
#include <fstream>
#include <cassert>
#define DEBUG
#include "../utils/io/filesystem.h"
using namespace M_FILESYSTEM;
template<typename T>
void print_v(std::vector<T> const& v) {
for (auto const& e : v)
std::cout << e << ' ';
std::cout << std::endl;
}
int main(int argc, char* argv[]) {
std::string filename = argv[0];
if (argc > 1) filename = argv[1];
std::cout << GetFileNameWithoutDir(filename) << std::endl;
std::cout << GetFileExtensionLower(filename) << std::endl;
auto dir = GetFileParaentDir(filename);
std::cout << dir << std::endl;
std::cout << GetRegularizedDir(dir) << std::endl;
assert(DirExists(dir));
auto cwd = GetWorkingDir();
std::cout << cwd << std::endl;
assert(MakeDir("tmp"));
assert(MakeDirFullPath("tmp/tmp1/tmp2"));
assert(RemoveDir("tmp/tmp1/tmp2"));
auto new_cwd = cwd + "/tmp";
assert(ChangeWorkingDir(new_cwd));
std::cout << GetWorkingDir() << std::endl;
std::fstream tmp;
tmp.open("tmp.txt", std::ios::out);
tmp.close();
assert(FileExists(filename));
std::vector<std::string> subdirs, filenames;
assert(ListContentsInDir(new_cwd, subdirs, filenames));
print_v(subdirs);
print_v(filenames);
filenames.clear();
assert(ListFilesInDirWithPredicate(new_cwd, [](std::string const& fn) { return GetFileNameWithoutDir(fn) == "tmp.txt"; }, filenames));
print_v(filenames);
assert(ChangeWorkingDir(cwd));
// fail here, since rmdir requires dir is empty, need to rm all contents first
assert(!RemoveDir("tmp"));
assert(ChangeWorkingDir(new_cwd));
assert(RemoveFile("tmp.txt"));
assert(RemoveDir("tmp1"));
assert(ChangeWorkingDir(cwd));
std::cout << GetWorkingDir() << std::endl;
assert(RemoveDir("tmp"));
return 0;
}