Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
C programming 2nd term
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ильин Владимир Александрович
C programming 2nd term
Commits
69b53949
Commit
69b53949
authored
May 03, 2024
by
Ильин Владимир Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
main while loop refactoring
parent
0b730f7f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
25 deletions
+45
-25
main.c
avtomat/main.c
+45
-25
No files found.
avtomat/main.c
View file @
69b53949
...
...
@@ -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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment