2022-05-28 00:30:18 +00:00
|
|
|
/**
|
|
|
|
This program inverts the caracters in a
|
|
|
|
given text file, using command line arguments
|
|
|
|
or by prompting the question to the user.
|
|
|
|
|
|
|
|
By Joni Silva, 27/05/2022.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <main.h>
|
|
|
|
|
|
|
|
|
|
|
|
// Necessary to initalize my struct with all the info
|
|
|
|
struct parameters info = {
|
2022-05-30 19:52:15 +00:00
|
|
|
.filename = "EMPTY", // Here we store the file name
|
|
|
|
.fd = NULL, // File descriptor
|
|
|
|
.version = "1.0", // Software version
|
|
|
|
.fileNameSize = 80, // How many characters has the filename, including extention
|
|
|
|
// Below is the variable that holds the text to dislpay for help
|
2022-05-28 00:30:18 +00:00
|
|
|
.help = "\n\
|
|
|
|
------------------------------------------------\n\
|
2022-05-28 00:40:51 +00:00
|
|
|
Usage: ./inverT -i [<some.text.file>]\n\
|
2022-05-28 00:30:18 +00:00
|
|
|
-help Print help and exit.\n\
|
|
|
|
-v Print application version and exit.\n\
|
2022-05-28 01:12:34 +00:00
|
|
|
-i Invert text characters.\n\
|
2022-05-28 00:40:51 +00:00
|
|
|
Example: ./inverT -i test.txt\n",
|
2022-05-28 00:30:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]) {
|
|
|
|
|
|
|
|
|
2022-05-30 19:52:15 +00:00
|
|
|
if (argc == 1) { // If only one argument
|
|
|
|
ask_file(); // Ask user for the file name and location
|
|
|
|
invert_file(); // and invert the file content
|
2022-05-28 00:30:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 19:52:15 +00:00
|
|
|
else if (argc == 2) { // If two arguments
|
|
|
|
if (strncmp(argv[1], "-help", 5) == 0) { // Check if the flag is '-help'
|
|
|
|
printf("%s\n", info.help); // and print help info
|
2022-05-28 00:30:18 +00:00
|
|
|
}
|
2022-05-30 19:52:15 +00:00
|
|
|
else if (strncmp(argv[1], "-v", 2) == 0) { // Check if the flag is '-v'
|
|
|
|
printf("Version %s\n", info.version); // and print version info
|
2022-05-28 00:30:18 +00:00
|
|
|
}
|
2022-05-30 19:52:15 +00:00
|
|
|
else { // Otherwise
|
|
|
|
strcpy(info.filename, argv[1]); // save the second argument into info.filename
|
|
|
|
invert_file(); // and invert the file content
|
2022-05-28 00:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 19:52:15 +00:00
|
|
|
else if (argc == 3) { // If three arguments
|
|
|
|
if(strncmp(argv[1], "-i", 2) == 0) { // Check if the flag is '-i'
|
|
|
|
strcpy(info.filename, argv[2]); // save the third argument into info.filename
|
|
|
|
invert_file(); // and invert the file content
|
2022-05-28 00:30:18 +00:00
|
|
|
}
|
2022-05-30 19:52:15 +00:00
|
|
|
else { // Otherwise
|
|
|
|
printf("\nInvalid flag!!!"); // print this
|
|
|
|
printf("%s\n", info.help); // and then print help info
|
2022-05-28 00:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 19:52:15 +00:00
|
|
|
else { // Otherwise if more then three arguments
|
|
|
|
printf("\nTo many arguments!!!"); // print this
|
|
|
|
printf("%s\n", info.help); // and then print help info
|
2022-05-28 00:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|