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

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