-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
158 lines (131 loc) · 3.7 KB
/
utils.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include"utils.hpp"
using namespace std;
void create_process(string province_name,string pipe_name, const char* argv){
int fd[2]; // 0 for read, 1 for write
if (pipe(fd) < 0 ){
cout << "error occured in opening pipe" << endl;
}
int pid = fork();
if (pid < 0){
cout << "error occured in creating new thread" << endl;
}
if (pid == 0){
//child
close(fd[1]);
string fd_char = to_string(fd[0]);
char *pchar = &fd_char[0];
char* arg[] = {pchar,NULL};
execv(argv,arg);
}
else{
//parrent
close(fd[0]);
string buf = "#" + province_name + "#" + pipe_name;
char x[SIZE];
strcpy(x,&buf[0]);
mkfifo(pipe_name.c_str(), 0666);
write(fd[1],&x, SIZE);
}
}
stringvec parse_input(char buf[SIZE]){
char dir_name_c[SIZE],pipe_name_c[SIZE],command_c[SIZE];
sscanf(buf,"#%[^#]#%[^#]#%[^#]#",dir_name_c,pipe_name_c,command_c);
string dir_name(dir_name_c);
string pipe_name(pipe_name_c);
string command(command_c);
stringvec vec;
vec.push_back(dir_name);
vec.push_back(pipe_name);
vec.push_back(command);
return vec;
}
map<string, int> map_words(stringvec words) {
map<string, int> result;
for (int i = 0; i < words.size(); i++) {
if (result.find(words[i]) == result.end()) {
result[words[i]] = 1;
} else {
result[words[i]] = result[words[i]] + 1;
}
}
return result;
}
stringvec read_csv(string filename){
stringvec result;
ifstream myFile(filename);
if(!myFile.is_open()) throw runtime_error("Could not open file");
std::string line;
while(getline(myFile, line)){
std::stringstream test(line);
std::string segment;
while(std::getline(test, segment, ','))
{
result.push_back(segment);
}
}
myFile.close();
return result;
}
int return_max(vector<int> vec){
int price = -1;
for (int i = 0; i < vec.size(); i++){
price = max(price,vec[i]);
}
return price;
}
int return_min(vector<int> vec){
int price = std::numeric_limits<int>::max();
for (int i = 0; i < vec.size(); i++){
if (vec[i] != -1){
price = min(price,vec[i]);
}
}
return (price == std::numeric_limits<int>::max())? -1:price;
}
int read_from_fifo(string fifo_path){
int fifo;
const char *pchar = fifo_path.c_str();
if ((fifo = open(pchar, O_RDONLY)) < 0){
printf("error in opening read fifo ");
return 0;
}
char price_c[SIZE];
int flag;
if((flag = read(fifo, price_c, SIZE) )< 0){
printf("error in reading fifo ");
exit(1);
}
close(fifo);
unlink(pchar);
int price = stoi(price_c);
return price;
}
void write_to_fifo(int price_i, string fifo_path){
int fifo;
const char *fifochar = fifo_path.c_str();
if ((fifo = open(fifochar,O_WRONLY)) < 0){
printf("error in opening write fifo\n");
return;
}
string s = to_string(price_i);
const char *pchar = s.c_str();
char kh[100];
strcpy(kh, pchar);
int flag;
if ((flag = write(fifo, kh, SIZE)) < 0){
printf("error in writing to pipe\n");
exit(1);
}
close(fifo);
}
stringvec read_directory(const std::string& name){
stringvec v;
DIR* dirp = opendir(name.c_str());
struct dirent * dp;
while ((dp = readdir(dirp)) != NULL) {
if (strcmp(dp->d_name,".") != 0 & strcmp(dp->d_name,"..") != 0)
v.push_back(dp->d_name);
}
closedir(dirp);
return v;
}