-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedreader.cpp
More file actions
189 lines (159 loc) · 4.58 KB
/
feedreader.cpp
File metadata and controls
189 lines (159 loc) · 4.58 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "feedreader.hpp"
#define DOWNLOAD_DIR "temp/"
/**
* @brief prints the usage of the feedreader
*
*/
void print_usage(){
fprintf(stderr, "Usage: feedreader <URL | -f <feedfile>> [-c <certfile>] [-C <certaddr>] [-T] [-a] [-u]\n");
exit(1);
}
/**
* @brief prints a message to stderr
*
*/
void debug_print(const char *msg){
fprintf(stderr, "DEBUG: %s", msg);
}
/**
* @brief prints an error message to stderr
*
*/
void error_print(string msg){
fprintf(stderr, "Error: %s\n", msg.c_str());
}
/**
* @brief Downloads and then processes the provided XML resource
*
* @param url_add string of the url
* @param certfile
* @param certaddr
* @param showTime
* @param showAuthor
* @param showUrls
*/
void read_from_url(const char* url_add, std::string certfile, std::string certaddr, struct parse_config config){
struct url url_location = parse_url(url_add);
if (!url_location.is_valid){
string msg = "Invalid URL: " + string(url_add);
error_print(msg);
return;
}
string filename = DOWNLOAD_DIR;
filename.append("temp.xml");
// fprintf(stderr, "Downloading %s to %s\n", url_add, filename.c_str());
bool download_success = false;
if(url_location.is_https)
download_success = download_https_feed(url_location, filename, certfile, certaddr);
else
download_success = download_http_feed(url_location, filename);
if(download_success){
parse_news_feed_file(filename, config);
}else{
string msg = "Failed to download: " + string(url_add);
error_print(msg);
}
}
int main(int argc, char *argv[])
{
/*
Parsing command line arguments
*/
struct parse_config config = {
.show_time = false,
.show_author = false,
.show_urls = false
};
bool is_single_url = false;
string location = "";
string certfile = "";
string certaddr = "";
if(argc < 2 ){
fprintf(stderr, "Error: No URL or file specified.\n");
print_usage();
}
int c;
int option_index = 0;
while ((c = getopt_long(argc, argv, ":auTf:c:C:h", {}, &option_index)) != -1)
{
switch(c)
{
case 'T':
config.show_time = true;
break;
case 'a':
config.show_author = true;
break;
case 'u':
config.show_urls = true;
break;
case 'f':
location = optarg;
break;
case 'c':
certfile = optarg;
break;
case 'C':
certaddr = optarg;
break;
case 'h':
print_usage();
break;
case '?':
break;
default :
print_usage();
break;
}
}
for(; optind < argc; optind++){
location = argv[optind];
is_single_url = true;
fprintf(stderr, "Assuming %s is a URL\n", location.c_str());
}
/*
Preparing the download directory
*/
struct stat info;
if( stat( DOWNLOAD_DIR, &info ) != 0 ){
int check = mkdir(DOWNLOAD_DIR,0777);
// check == 0 if directory is created
if(check){
printf("Unable to create directory\n");
exit(2);
}
}
else if( info.st_mode & S_IFDIR ){
//dir already exists
}
else{
printf( "%s is no directory\n", DOWNLOAD_DIR );
exit(2);
}
/*
Starting to parse the feed(s)
*/
if(is_single_url){
read_from_url(location.c_str(), certfile, certaddr, config);
}else{
fstream newfile;
newfile.open(location.c_str(),ios::in);
if (newfile.is_open()){
string tp;
while(getline(newfile, tp)){
if(tp[0] == '#')
continue;
if (!tp.empty() && tp[tp.size() - 1] == '\r')
tp.erase(tp.size() - 1);
if(tp.empty() || tp.find_first_not_of(' ') == string::npos)
continue;
read_from_url(tp.c_str(), certfile, certaddr, config);
}
newfile.close();
}else{
fprintf(stderr, "Error: Could not open file %s\n", location.c_str());
exit(1);
}
}
return 0;
}