Убран лишний case URL_FILE_AND_FILEPATH_FILE

parent 2ab5d5b5
#include <curl/curl.h>
#include <curl/easy.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -13,9 +14,8 @@ void download_file(char *url, char *filepath, CURL *curl);
int file_exists(const char *filename);
enum Download_types{
URL_ONLY,
URL_FILE_ONLY,
URL_FILE_AND_FILEPATHS_FILE
URL_ARG,
URL_FILE,
};
int
......@@ -26,6 +26,7 @@ main(int argc, char** argv)
char *url_file_path = NULL;
char *filepaths_file_path = NULL;
char *place_holder = NULL; // for get_default_filepath func
//get args
......@@ -58,11 +59,10 @@ main(int argc, char** argv)
enum Download_types download_type;
if (url != NULL && filepath != NULL && url_file_path == NULL && filepaths_file_path == NULL){ // only 1 file
download_type = URL_ONLY;
}else if(url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path == NULL){ // -uf
download_type = URL_FILE_ONLY;
}else if(url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path != NULL){ // -uf and -ff
download_type = URL_FILE_AND_FILEPATHS_FILE;
download_type = URL_ARG;
}else if((url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path == NULL) || // -uf
(url == NULL && filepath == NULL && url_file_path != NULL && filepaths_file_path != NULL)){ // -uf and -ff
download_type = URL_FILE;
}else{
puts("Unsupported arguments combination given!");
usage();
......@@ -70,62 +70,54 @@ main(int argc, char** argv)
FILE *url_file = NULL;
FILE *filepaths_file = NULL;
char *curr_url;
char *curr_filepath;
size_t url_len = 0;
size_t filepath_len = 0;
switch(download_type){
case URL_ONLY:
case URL_ARG:
download_file(url, filepath, curl);
break;
//TODO:refactor (merge if possible) 2 cases below
case URL_FILE_ONLY:
case URL_FILE:
url_file = fopen(url_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';
download_file(curr_url, get_default_filepath(curr_url, place_holder), curl);
curr_url[strlen(curr_url) - 1] = '\0';
// 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;
}
}
// 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);
}
free(place_holder);
free(curr_url);
fclose(url_file);
break;
case URL_FILE_AND_FILEPATHS_FILE:
url_file = fopen(url_file_path, "r");
filepaths_file = fopen(filepaths_file_path, "r");
if(!url_file)
die("Could not open given url file %s!", url_file_path);
if(!filepaths_file)
die("Could not open given filepath file %s!", filepaths_file_path);
while(getline(&curr_url, &filepath_len, url_file) != -1){
if(getline(&curr_filepath, &url_len, filepaths_file) == -1)
curr_filepath = get_default_filepath(curr_url, place_holder);
if (curr_filepath[strlen(curr_filepath) - 1] == '\n')
curr_filepath[strlen(curr_filepath) - 1] = '\0';
if (curr_url[strlen(curr_url) - 1] == '\n')
curr_url[strlen(curr_url) - 1] = '\0';
download_file(curr_url, curr_filepath, curl);
if (filepaths_file_path != NULL) {
free(curr_filepath);
fclose(filepaths_file);
}
free(place_holder);
free(curr_url);
free(curr_filepath);
fclose(url_file);
fclose(filepaths_file);
break;
}
curl_easy_cleanup(curl);
curl_easy_cleanup(curl);
return 0;
}
void
......@@ -150,15 +142,16 @@ download_file(char *url, char *filepath, CURL *curl)
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
CURLcode res = curl_easy_perform(curl);
fprintf(stdout, "Starting %s\n", filepath);
fclose(file);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
fprintf(stderr, "Failed: %s\n to download %s", curl_easy_strerror(res), filepath);
else
fprintf(stdout, "curl returned %s; Downloaded %s \n", curl_easy_strerror(res), filepath);
fprintf(stdout, "%s; Downloaded %s \n", curl_easy_strerror(res), filepath);
}
char*
......@@ -199,7 +192,6 @@ usage()
exit(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