Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 17 Jan 2020
- Usage: spacestotabs <number> <file name>
- number: how many spaces will be converted into a tab
- */
- #include <stdio.h>
- #include <string.h>
- int is_string_numerical(char *str)
- {
- int len = strlen(str);
- int i;
- for (i = 0; i < len; ++i) {
- if (!isdigit(str[i])) {
- return 0;
- }
- }
- return 1;
- }
- void backupfile(char *filename)
- {
- FILE *file;
- FILE *backupfile;
- char *backupfilename = strcat(filename, "_backup");
- file = fopen(filename, "r");
- backupfile = fopen(backupfilename, "w");
- char *c = file;
- while (*c != 'EOF') {
- fputc(*c, backupfile);
- ++c;
- }
- fclose(backupfile);
- fclose(file);
- }
- int main(int argc, char *argv[])
- {
- int howmanyspaces; // how many spaces will be converted into a tab
- if (argc < 2) {
- printf("Error: too few arguments. \"spacestotabs -h\" for help.\n");
- }
- if (argc > 3) {
- printf("Warning: too many arguments. Arguments after the second one will be ignored. \"spacestotabs -h\" for help.\n");
- }
- // Handle first argument
- if (argv[1][0] == '-') {
- switch (argv[1][1])
- {
- case 'h': // Help content
- printf("\nUsage: spacestotabs <number> <file name>\n");
- printf("\n<number> : Number of spaces that will be converted into a tab. Number has to be positive.\n\n");
- return 0;
- default: // Parameter not found, wrong parameter
- printf("Error: wrong arguments. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- } else {
- if (!is_string_numerical(argv[1])) {
- printf("Error: wrong parameter. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- howmanyspaces = atoi(argv[1]);
- if (howmanyspaces <= 0) {
- printf("Error: number is too small. \"spacestotabs -h\" for help.\n");
- return 1;
- }
- }
- const int SIZE = strlen(argv[2]) + 10; // TODO: we need only + 7 or 8
- char filename[SIZE];
- strcpy(filename, argv[2]);
- // Backup the file first
- int iscorrectanswer = 0;
- do {
- printf("Would you like to backup your file before the operation? [Y / N] : ");
- char c = getchar();
- switch (c)
- {
- case 'n':
- case 'N':
- iscorrectanswer = 1;
- break;
- case 'y':
- case 'Y':
- backupfile(filename);
- printf("Backup is created in the same directory with the name %s_backup\n", filename);
- iscorrectanswer = 1;
- break;
- default:
- printf("Error: unknown input. Use letters y or n to answer.\n");
- }
- } while (!iscorrectanswer);
- // TODO : Do the operation on the original file, don't create a new one.
- char *outfilename = strcat(filename, "_out");
- FILE *file;
- FILE *out;
- file = fopen(filename, "r");
- out = fopen(outfilename, "w");
- int previousspacecount = 0; // how many spaces has been read in a row
- int writepointercooldown = 4; // TODO: Change this back to 'howmanyspaces' - 1
- char *read = file;
- char *write = read - 4; // TODO: Change this back to 'howmanyspaces' - 1
- while (*read != 'EOF') {
- if (*read == ' ') {
- ++previousspacecount;
- } else {
- previousspacecount = 0;
- }
- if (!writepointercooldown) {
- fputs(*read, out);
- }
- if (previousspacecount == howmanyspaces) {
- fputs('\t', write);
- writepointercooldown = howmanyspaces;
- }
- if (!writepointercooldown) {
- ++write;
- } else {
- --writepointercooldown;
- }
- ++read;
- }
- fclose(file);
- fclose(out);
- printf("Spaces have been converted to tabs.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement