Repository navigation
Expand file tree
/
Copy pathcity.cpp
More file actions
160 lines (142 loc) · 4.82 KB
/
Copy pathcity.cpp
File metadata and controls
160 lines (142 loc) · 4.82 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
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
159
160
#include <iostream>
#include <unistd.h>
#include <string>
#include <vector>
#include <fcntl.h>
#include <fstream>
#include <algorithm>
#include "logger.hpp"
#include "utils.hpp"
#include "const.hpp"
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;
string total_profit_calculator(vector<vector<string>> &file_data, vector<string> selected_goods)
{
double total_profit = 0;
for (auto g : selected_goods)
{
for (auto &line_data : file_data)
{
if (line_data[0] == g && (line_data[3] == "output\r" || line_data[3] == "output"))
{
for (auto &ld2 : file_data)
{
if (ld2[0] == g && (ld2[3] == "input\r" || ld2[3] == "input") && ld2[2] != "0")
{
if (stoi(line_data[2]) <= stoi(ld2[2]))
{
total_profit += stod(line_data[2]) * (stod(line_data[1]) - stod(ld2[1]));
ld2[2] = to_string(stoi(ld2[2]) - stoi(line_data[2]));
line_data[2] = "0";
break;
}
else
{
total_profit += stoi(ld2[2]) * (10 * stod(line_data[1]) - 10 * stod(ld2[1])) / 10;
line_data[2] = to_string(stoi(line_data[2]) - stoi(ld2[2]));
ld2[2] = "0";
}
}
}
}
}
}
int roundedValue = static_cast<int>(std::round(total_profit));
return to_string(roundedValue);
}
string calculate_for_Item(string item_name, vector<vector<string>> file_data)
{
string ans;
int price = 0, amount = 0;
for (auto ld : file_data)
{
if (ld[0] == item_name && (ld[3] == "input\r" || ld[3] == "input"))
{
amount += stoi(ld[2]);
price += stod(ld[1]) * stoi(ld[2]);
}
}
ans = to_string(amount) + "," + to_string(price) + "\n";
return ans;
}
vector<vector<string>> read_file(string city_file, Logger &log)
{
std::ifstream file(city_file);
if (!file.is_open())
{
log.error("Failed to open the file: " + city_file);
}
std::vector<std::vector<std::string>> data; // 2D vector to store CSV data
std::string line;
while (std::getline(file, line))
{
std::vector<std::string> row;
std::stringstream line_stream(line);
std::string cell;
// Split line into cells using ',' as the delimiter
while (std::getline(line_stream, cell, ','))
{
row.push_back(cell);
}
data.push_back(row); // Add the row to the 2D vector
}
file.close();
return data;
}
void named_pipe_to_goods(vector<string> selected_goods, Logger &log, vector<vector<string>> file_data)
{
for (string item_name : selected_goods)
{
// Open the named pipe for writing
string pipe_path = GOODS_PIPE_PREFIX + item_name;
int fd = open(pipe_path.c_str(), O_WRONLY);
if (fd == -1)
{
log.error("Failed to open pipe for writing");
return;
}
string message = calculate_for_Item(item_name, file_data);
write(fd, message.c_str(), message.size()); // Include null terminator
message.pop_back();
log.cinfo("This Message: " + message + " sent to Item " + item_name);
close(fd); // Close the pipe after writing
}
}
int main(int argc, char *argv[])
{
// // -- startup --
(void)argc; // fuck warnings
int child_read_head = stoi(argv[1]);
string city_name = argv[2];
Logger log("STORE " + city_name);
// // -- read args from main --
char buffer[MAX_BUFFER_SIZE];
ssize_t bytes_read = read(child_read_head, buffer, sizeof(buffer) - 1);
vector<string> args;
if (bytes_read > 0)
{
// buffer example : Mashhad!stores/Mashhad.csv!2!14!shekar,berenj,!manamanmanmanmanmanmanm
args = tokenize(buffer, '!');
}
else
{
log.error("Empry message from mainnnnnnnn");
}
close(child_read_head);
string city_file = args[1]; // Store CSV file path
// // -- read file --
vector<vector<string>> file_data = read_file(city_file, log);
vector<string> selected_goods = tokenize(args[4], ',');
// // -- write final info to main (unnamed pipe) --
string total_profit = total_profit_calculator(file_data, selected_goods);
log.cinfo("total profit sent to main : " + total_profit);
int child_write_head = stoi(args[3]);
write(child_write_head, total_profit.c_str(), total_profit.size() + 1);
close(child_write_head);
// // -- write info to Items (named pipe) --
named_pipe_to_goods(selected_goods, log, file_data);
log.cinfo("City process terminating.");
exit(1);
}