This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.cpp
169 lines (150 loc) · 5.55 KB
/
actions.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
159
160
161
162
163
164
165
166
167
168
169
# include <iostream>
# include <string>
# include <sstream>
# include <stdlib.h>
# include "actions.h"
# include "io.h"
# include "config.h"
# include "connection.h"
# include "predictions_parser.h"
# include "agency_parser.h"
# include "route_list_parser.h"
# include "stop_list_parser.h"
# include "help_info.h"
void action_agency_list(CommandLineAction *clAction, ConfigFile *configFile) {
std::string filepath;
std::string xml_string;
filepath = get_filepath("agencyList.xml");
xml_string = get_file_contents(filepath);
if (xml_string[0] != '<' ||
(configFile->lastUsageExpired() &&
!(clAction->actions & ACTION_CACHED)) ||
clAction->actions & ACTION_ONLINE) {
// The file doesn't exist, so get it from NextBus online API
xml_string = agencyList();
set_file_contents(filepath, xml_string);
}
AgencyParser parser(xml_string);
parser.parse();
std::cout << parser.results() << std::endl;
}
void action_agency_store(CommandLineAction *clAction, ConfigFile *configFile) {
configFile->agency = clAction->agency;
configFile->update();
std::cout << IO_GREEN "Configuration Change:" IO_NORMAL << std::endl
<< " Agency set to '"
<< configFile->agency << "'." << std::endl;
}
void action_route_list(CommandLineAction *clAction, ConfigFile *configFile) {
std::string filepath;
std::string xml_string;
filepath = get_filepath("routeList.xml");
xml_string = get_file_contents(filepath);
if (xml_string[0] != '<' ||
(configFile->lastUsageExpired() &&
!(clAction->actions & ACTION_CACHED)) ||
clAction->actions & ACTION_ONLINE) {
// The file doesn't exist, so get it from NextBus online API
xml_string = routeList(configFile->agency.c_str());
set_file_contents(filepath, xml_string);
}
RouteListParser parser(xml_string);
parser.parse();
std::cout << parser.results() << std::endl;
// TODO: Route Config?
// xml_string = routeConfig(configFile->agency.c_str(), NULL);
}
void action_list_stops(CommandLineAction *clAction, ConfigFile *configFile) {
std::string xml_string;
std::string filepath;
filepath = get_filepath("stopList.xml");
xml_string = stopList(configFile->agency.c_str(), clAction->route.c_str());
set_file_contents(filepath, xml_string);
StopListParser parser(xml_string);
parser.parse();
std::cout << parser.results() << std::endl;
}
void action_schedule_list(CommandLineAction *clAction, ConfigFile *configFile) {
std::string xml_string;
xml_string = schedule(configFile->agency.c_str(), clAction->route.c_str());
// TODO: Find a smart way to cache schedule data
// TODO: parse and display schedule data
std::cout << IO_RED "At this time, the --schedule command is not implemented!" IO_NORMAL << std::endl;
}
void action_predict_stop(CommandLineAction *clAction, ConfigFile *configFile) {
std::string xml_string;
std::string filepath;
filepath = get_filepath("lastPrediction.xml");
xml_string = predictions(configFile->agency.c_str(),
clAction->stop.c_str());
set_file_contents(filepath, xml_string);
PredictionsParser parser(xml_string);
parser.parse();
std::cout << parser.results() << std::endl;
}
void action_predict_route(CommandLineAction *clAction, ConfigFile *configFile) {
std::string xml_string;
std::string filepath;
filepath = get_filepath("lastPrediction.xml");
xml_string = predictions(configFile->agency.c_str(),
clAction->stop.c_str(),
clAction->route.c_str());
set_file_contents(filepath, xml_string);
PredictionsParser parser(xml_string);
parser.parse();
std::cout << parser.results() << std::endl;
}
void action_save_custom(CommandLineAction *clAction, ConfigFile *configFile) {
configFile->savedRouteStops
.push_back(new SavedRouteStop(clAction->save,
clAction->route,
clAction->stop));
configFile->update();
}
void action_use_saved(CommandLineAction *clAction, ConfigFile *configFile,
bool minutesOnly) {
bool saveUseFound;
int i, j;
std::string xml_string;
for (i = 0; i < clAction->saveUses.size(); i++) {
saveUseFound = false;
for (j = 0; j < configFile->savedRouteStops.size()
&& !saveUseFound; j++) {
if (!configFile->savedRouteStops[j]->name.compare(
*clAction->saveUses[i])) {
saveUseFound = true;
xml_string = predictions(configFile->agency.c_str(),
configFile->savedRouteStops[j]->stop.c_str(),
configFile->savedRouteStops[j]->route.c_str());
PredictionsParser parser(xml_string);
parser.parse();
if (minutesOnly) {
std::cout << parser.minutes();
} else {
std::cout << IO_GREEN << *clAction->saveUses[i]
<< IO_NORMAL << std::endl
<< parser.results() << std::endl;
}
}
}
if (!saveUseFound) {
std::cout << IO_RED "No saved route/stop was found for '"
<< *clAction->saveUses[i] << "'." IO_NORMAL << std::endl
<< std::endl;
}
}
}
void action_help() {
std::stringstream ss;
std::string filepath;
std::string readme_string;
// Save the help info to README.txt in the default directory
filepath = get_filepath("README.txt");
set_file_contents(filepath, help_info);
// Call less to try to display it
ss << "less " << filepath;
if (system(ss.str().c_str())) {
// Call to less failed, so dump it to the terminal.
std::cout << help_info << std::endl;
}
}