123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /* This file deletes trailing white space in a file */
- #include <stdio.h>
- #include <stdlib.h>
- #include <argp.h>
- #include <string.h>
- enum boolean { NO, YES };
- enum boolean i = NO;
- enum boolean o = NO;
- char fileName [128];
- char outputFileName [128];
- const char * argp_program_version = "0.1";
- /* mail bug reports to */
- const char * argp_program_bug_address = "jbranso@fastmail.com";
- static const struct argp_option options [] =
- {
- {"input", 'i', "FILE", 0, "delete the trailing whitespace in the file" },
- {"output", 'o', "FILE", 0, "output to save the file"},
- { 0 }
- };
- //parse options
- error_t argp_parser (int opt, char *arg, struct argp_state *state) {
- extern enum boolean bool;
- switch (opt)
- {
- // if this parser function is called on an option that it doesn't recognize, then don't do anything.
- default:
- return ARGP_ERR_UNKNOWN;
- case 'i':
- {
- i = YES;
- memcpy (fileName, arg, strlen (arg));
- break;
- }
- case 'o':
- {
- o = YES;
- memcpy (outputFileName, arg, strlen (arg));
- break;
- }
- }
- return 0;
- }
- struct argp argp =
- { options, argp_parser, 0, "Deletes trailing whitespace." };
- int main (int argc, char **argv) {
- argp_parse (&argp, argc, argv, 0, 0, 0);
- if (fileName == 0) // make this check to see if the array has a string...
- return 0;
- FILE * inputFile = fopen(fileName, "r+");
- if (outputFileName == 0)
- return 0;
- FILE * outputFile = fopen(outputFileName, "w");
- //get getline ready
- const int MAX_LINE = 512;
- // this counts the number of white space in a file
- // if the file contents are " a", then when getc returns 'a'
- // then whiteSpaceString is " \0"
- char whiteSpaceString [MAX_LINE];
- whiteSpaceString[0] = '\0';
- // the length of each line
- size_t lengthOfLine;
-
- char c;
-
- while ( (c = getc(inputFile)) != EOF ) //get each character in file
- {
- // if c is non-white space, then print it
- if (ispunct (c) || isalnum (c))
- {
- // if current char in the string was preceeded by whitespace,
- // then print the white space, then the string
- // ie: " e" => getc () => 'e' --> print " e"
- if (whiteSpaceString[0] != '\0') {
- fputs(whiteSpaceString, outputFile);
- whiteSpaceString[0] = '\0';
- }
- putc(c, outputFile);
- }
- else if (isblank (c))
- { //tab or space
- strcat (whiteSpaceString, &c); //use a custom function or macro
- }
- else //if (c == '\n')
- {
- whiteSpaceString[0] = '\0';
- putc(c, outputFile);
- }
- }
- fclose (inputFile);
- fclose (outputFile);
- // free the memory the compiled memory the regexp was using
- //regfree (®exp);
- return 0;
- }
|