main while loop refactoring

parent 0b730f7f
......@@ -10,8 +10,10 @@
void usage();
void die(const char *format, ...);
char *get_default_filepath(char *, char *);
void download_file(char *url, char *filepath, CURL *curl);
CURLcode download_file(char *url, char *filepath, CURL *curl);
int file_exists(const char *filename);
void download_result_message(CURLcode res, char *filepath);
void drop_newline(char *);
enum Download_types{
URL_ARG,
......@@ -76,44 +78,48 @@ main(int argc, char** argv)
size_t url_len = 0;
size_t filepath_len = 0;
CURLcode result;
switch(download_type){
case URL_ARG:
download_file(url, filepath, curl);
result = download_file(url, filepath, curl);
download_result_message(result, filepath);
break;
case URL_FILE:
url_file = fopen(url_file_path, "r");
if(filepaths_file_path)
filepaths_file = fopen(filepaths_file_path, "r");
if(!url_file)
die("Could not open given url file %s!", url_file_path);
while(getline(&curr_url, &url_len, url_file) != -1){
if (curr_url[strlen(curr_url) - 1] == '\n')
curr_url[strlen(curr_url) - 1] = '\0';
if(filepaths_file_path && !filepaths_file)
die("Could not open given file file %s!", filepaths_file_path);
while(getline(&curr_url, &url_len, url_file) != -1){
drop_newline(curr_url);
// Determine the filepath
char *download_filepath = NULL;
if (filepaths_file_path != NULL) {
// Try to read a filepath from the filepaths file
if(getline(&curr_filepath, &filepath_len, filepaths_file) != -1) {
if (curr_filepath[strlen(curr_filepath) - 1] == '\n')
curr_filepath[strlen(curr_filepath) - 1] = '\0';
download_filepath = curr_filepath;
drop_newline(curr_filepath);
}
//fallback to default logic if none filepath file given
}else{
curr_filepath = get_default_filepath(curr_url, place_holder);
}
// If no filepath was read from the filepaths file, use the default logic
if (!download_filepath) {
download_filepath = get_default_filepath(curr_url, place_holder);
}
download_file(curr_url, download_filepath, curl);
result = download_file(curr_url, curr_filepath, curl);
download_result_message(result, curr_filepath);
}
free(place_holder);
free(curr_url);
if (filepaths_file_path != NULL) {
free(curr_filepath);
fclose(filepaths_file);
}
}else
free(place_holder);
fclose(url_file);
break;
}
......@@ -131,14 +137,22 @@ die(const char *format, ...) {
}
void
download_result_message(CURLcode res, char *filepath)
{
if(res != CURLE_OK)
fprintf(stderr, "Failed: %s\n to download %s", curl_easy_strerror(res), filepath);
else
fprintf(stdout, "%s; Downloaded %s \n", curl_easy_strerror(res), filepath);
}
CURLcode
download_file(char *url, char *filepath, CURL *curl)
{
FILE *file;
file = fopen(filepath,"wb");
if(!file){
fprintf(stderr, "Error: Could not open file for writing: %s\n", filepath);
exit(1);
die("Error: Could not open file for writing: %s\n", filepath);
}
curl_easy_setopt(curl, CURLOPT_URL, url);
......@@ -148,10 +162,8 @@ download_file(char *url, char *filepath, CURL *curl)
fprintf(stdout, "Starting %s\n", filepath);
fclose(file);
if(res != CURLE_OK)
fprintf(stderr, "Failed: %s\n to download %s", curl_easy_strerror(res), filepath);
else
fprintf(stdout, "%s; Downloaded %s \n", curl_easy_strerror(res), filepath);
return res;
}
char*
......@@ -192,6 +204,14 @@ usage()
exit(0);
}
int file_exists(const char *filename) {
void
drop_newline(char *str)
{
if (str[strlen(str) - 1] == '\n')
str[strlen(str) - 1] = '\0';
}
int
file_exists(const char *filename) {
return access(filename, F_OK) != -1;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment