delete-trailing-ws.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* This file deletes trailing white space in a file */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <argp.h>
  5. #include <string.h>
  6. enum boolean { NO, YES };
  7. enum boolean i = NO;
  8. enum boolean o = NO;
  9. char fileName [128];
  10. char outputFileName [128];
  11. const char * argp_program_version = "0.1";
  12. /* mail bug reports to */
  13. const char * argp_program_bug_address = "jbranso@fastmail.com";
  14. static const struct argp_option options [] =
  15. {
  16. {"input", 'i', "FILE", 0, "delete the trailing whitespace in the file" },
  17. {"output", 'o', "FILE", 0, "output to save the file"},
  18. { 0 }
  19. };
  20. //parse options
  21. error_t argp_parser (int opt, char *arg, struct argp_state *state) {
  22. extern enum boolean bool;
  23. switch (opt)
  24. {
  25. // if this parser function is called on an option that it doesn't recognize, then don't do anything.
  26. default:
  27. return ARGP_ERR_UNKNOWN;
  28. case 'i':
  29. {
  30. i = YES;
  31. memcpy (fileName, arg, strlen (arg));
  32. break;
  33. }
  34. case 'o':
  35. {
  36. o = YES;
  37. memcpy (outputFileName, arg, strlen (arg));
  38. break;
  39. }
  40. }
  41. return 0;
  42. }
  43. struct argp argp =
  44. { options, argp_parser, 0, "Deletes trailing whitespace." };
  45. int main (int argc, char **argv) {
  46. argp_parse (&argp, argc, argv, 0, 0, 0);
  47. if (fileName == 0) // make this check to see if the array has a string...
  48. return 0;
  49. FILE * inputFile = fopen(fileName, "r+");
  50. if (outputFileName == 0)
  51. return 0;
  52. FILE * outputFile = fopen(outputFileName, "w");
  53. //get getline ready
  54. const int MAX_LINE = 512;
  55. // this counts the number of white space in a file
  56. // if the file contents are " a", then when getc returns 'a'
  57. // then whiteSpaceString is " \0"
  58. char whiteSpaceString [MAX_LINE];
  59. whiteSpaceString[0] = '\0';
  60. // the length of each line
  61. size_t lengthOfLine;
  62. char c;
  63. while ( (c = getc(inputFile)) != EOF ) //get each character in file
  64. {
  65. // if c is non-white space, then print it
  66. if (ispunct (c) || isalnum (c))
  67. {
  68. // if current char in the string was preceeded by whitespace,
  69. // then print the white space, then the string
  70. // ie: " e" => getc () => 'e' --> print " e"
  71. if (whiteSpaceString[0] != '\0') {
  72. fputs(whiteSpaceString, outputFile);
  73. whiteSpaceString[0] = '\0';
  74. }
  75. putc(c, outputFile);
  76. }
  77. else if (isblank (c))
  78. { //tab or space
  79. strcat (whiteSpaceString, &c); //use a custom function or macro
  80. }
  81. else //if (c == '\n')
  82. {
  83. whiteSpaceString[0] = '\0';
  84. putc(c, outputFile);
  85. }
  86. }
  87. fclose (inputFile);
  88. fclose (outputFile);
  89. // free the memory the compiled memory the regexp was using
  90. //regfree (&regexp);
  91. return 0;
  92. }