main while loop refactoring

parent 0b730f7f
...@@ -10,8 +10,10 @@ ...@@ -10,8 +10,10 @@
void usage(); void usage();
void die(const char *format, ...); void die(const char *format, ...);
char *get_default_filepath(char *, char *); 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); int file_exists(const char *filename);
void download_result_message(CURLcode res, char *filepath);
void drop_newline(char *);
enum Download_types{ enum Download_types{
URL_ARG, URL_ARG,
...@@ -77,43 +79,47 @@ main(int argc, char** argv) ...@@ -77,43 +79,47 @@ main(int argc, char** argv)
size_t url_len = 0; size_t url_len = 0;
size_t filepath_len = 0; size_t filepath_len = 0;
CURLcode result;
switch(download_type){ switch(download_type){
case URL_ARG: case URL_ARG:
download_file(url, filepath, curl); result = download_file(url, filepath, curl);
download_result_message(result, filepath);
break; break;
case URL_FILE: case URL_FILE:
url_file = fopen(url_file_path, "r"); url_file = fopen(url_file_path, "r");
if(filepaths_file_path)
filepaths_file = fopen(filepaths_file_path, "r");
if(!url_file) if(!url_file)
die("Could not open given url file %s!", url_file_path); die("Could not open given url file %s!", url_file_path);
while(getline(&curr_url, &url_len, url_file) != -1){ if(filepaths_file_path && !filepaths_file)
if (curr_url[strlen(curr_url) - 1] == '\n') die("Could not open given file file %s!", filepaths_file_path);
curr_url[strlen(curr_url) - 1] = '\0';
while(getline(&curr_url, &url_len, url_file) != -1){
drop_newline(curr_url);
// Determine the filepath // Determine the filepath
char *download_filepath = NULL;
if (filepaths_file_path != NULL) { if (filepaths_file_path != NULL) {
// Try to read a filepath from the filepaths file // Try to read a filepath from the filepaths file
if(getline(&curr_filepath, &filepath_len, filepaths_file) != -1) { if(getline(&curr_filepath, &filepath_len, filepaths_file) != -1) {
if (curr_filepath[strlen(curr_filepath) - 1] == '\n') drop_newline(curr_filepath);
curr_filepath[strlen(curr_filepath) - 1] = '\0';
download_filepath = curr_filepath;
}
} }
// If no filepath was read from the filepaths file, use the default logic //fallback to default logic if none filepath file given
if (!download_filepath) { }else{
download_filepath = get_default_filepath(curr_url, place_holder); curr_filepath = get_default_filepath(curr_url, place_holder);
} }
result = download_file(curr_url, curr_filepath, curl);
download_file(curr_url, download_filepath, curl); download_result_message(result, curr_filepath);
} }
free(place_holder);
free(curr_url); free(curr_url);
if (filepaths_file_path != NULL) { if (filepaths_file_path != NULL) {
free(curr_filepath); free(curr_filepath);
fclose(filepaths_file); fclose(filepaths_file);
} }else
free(place_holder);
fclose(url_file); fclose(url_file);
break; break;
} }
...@@ -131,14 +137,22 @@ die(const char *format, ...) { ...@@ -131,14 +137,22 @@ die(const char *format, ...) {
} }
void 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) download_file(char *url, char *filepath, CURL *curl)
{ {
FILE *file; FILE *file;
file = fopen(filepath,"wb"); file = fopen(filepath,"wb");
if(!file){ if(!file){
fprintf(stderr, "Error: Could not open file for writing: %s\n", filepath); die("Error: Could not open file for writing: %s\n", filepath);
exit(1);
} }
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
...@@ -148,10 +162,8 @@ download_file(char *url, char *filepath, CURL *curl) ...@@ -148,10 +162,8 @@ download_file(char *url, char *filepath, CURL *curl)
fprintf(stdout, "Starting %s\n", filepath); fprintf(stdout, "Starting %s\n", filepath);
fclose(file); fclose(file);
if(res != CURLE_OK)
fprintf(stderr, "Failed: %s\n to download %s", curl_easy_strerror(res), filepath); return res;
else
fprintf(stdout, "%s; Downloaded %s \n", curl_easy_strerror(res), filepath);
} }
char* char*
...@@ -192,6 +204,14 @@ usage() ...@@ -192,6 +204,14 @@ usage()
exit(0); 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; 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