-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.cpp
More file actions
115 lines (109 loc) · 3.47 KB
/
utils.cpp
File metadata and controls
115 lines (109 loc) · 3.47 KB
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
109
110
111
112
113
114
115
//
// Created by 贵阳 on 7/26/16.
//
#include "utils.h"
void ListAllFile(const char *path) {
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
while (pDir != NULL && NULL != (ent=readdir(pDir))) {
switch (ent->d_type) {
case DT_REG:
if(strncmp(ent->d_name, ".", 1) == 0)
continue;
cout << ent->d_name << endl;
break;
case DT_DIR:
{
if(strncmp(ent->d_name, ".", 1) == 0)
continue;
cout << ent->d_name << endl;
string absPath(path);
if(absPath[absPath.size() - 1] == '/')
absPath += string(ent->d_name);
else
absPath = absPath + "/" + string(ent->d_name);
ListAllFile(absPath.c_str());
break;
}
default:
cout << ent->d_name << endl;
}
}
}
void getDirent(const char* topPath, vector<string> &dirs) {
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(topPath);
while (pDir != NULL && NULL != (ent=readdir(pDir))) {
switch (ent->d_type) {
case DT_DIR:
{
if(strncmp(ent->d_name, ".", 1) == 0)
continue;
// cout << ent->d_name << endl;
string absPath(topPath);
if(absPath[absPath.size() - 1] == '/')
absPath += string(ent->d_name);
else
absPath = absPath + "/" + string(ent->d_name);
dirs.push_back(absPath);
break;
}
}
}
}
void getThisDirentAllFiles(const char* curPath, vector<string> &absFilePath) {
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(curPath);
while (pDir != NULL && NULL != (ent=readdir(pDir))) {
switch (ent->d_type) {
case DT_REG:
{
if(strncmp(ent->d_name, ".", 1) == 0)
continue;
// cout << ent->d_name << endl;
string abspath(curPath);
if(abspath[abspath.size()-1] == '/')
abspath += string(ent->d_name);
else
abspath = abspath + "/" + string(ent->d_name);
absFilePath.push_back(abspath);
if(abspath.find(".csv") != string::npos) {
absFilePath.clear();
return ;
}
break;
}
}
}
}
void readOneFileData(string filepath, vector<vector<double >>& data, bool skipHeader) {
ifstream in(filepath, std::ifstream::in);
string strValue;
int index = 0;
while(getline(in,strValue)) {
index++;
if(skipHeader == true && index == 1)
continue;
vector<double > tmp;
splitStr(strValue, tmp, ',');
data.push_back(tmp);
}
in.close();
}
void splitStr(string str, vector<double >& res, char ch) {
//trim the free space from the head and tail
str.erase(0,str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ") + 1);
if(str.size() == 0) return ;
int start=0, end=0;
while(start <= end && end < str.size()) {
while(end<str.size() && str[end]!=ch) end++;
string sub = str.substr(start, end-start);
res.push_back(stoi(sub));
while(end<str.size() && str[end]==ch) end++;
start = end;
}
}