Files
inverT/main.c

69 lines
2.1 KiB
C
Raw Normal View History

/**
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 = {
.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
.help = "\n\
------------------------------------------------\n\
2022-05-28 00:40:51 +00:00
Usage: ./inverT -i [<some.text.file>]\n\
-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",
};
int main (int argc, char *argv[]) {
if (argc == 1) { // If only one argument
ask_file(); // Ask user for the file name and location
invert_file(); // and invert the file content
}
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
}
else if (strncmp(argv[1], "-v", 2) == 0) { // Check if the flag is '-v'
printf("Version %s\n", info.version); // and print version info
}
else { // Otherwise
strcpy(info.filename, argv[1]); // save the second argument into info.filename
invert_file(); // and invert the file content
}
}
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
}
else { // Otherwise
printf("\nInvalid flag!!!"); // print this
printf("%s\n", info.help); // and then print help info
}
}
else { // Otherwise if more then three arguments
printf("\nTo many arguments!!!"); // print this
printf("%s\n", info.help); // and then print help info
}
return 0;
}