bomToolkit.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "bom.h"
  2. void usage(char *executable) {
  3. printf("Usage:\n%s <filePath> c - to check for BOM.\n", executable);
  4. printf("%s <filePath> r - to remove BOM.\n", executable);
  5. printf("%s <filePath> a - to add BOM.\n", executable);
  6. }
  7. void reportError(int errorType) {
  8. switch(errorType) {
  9. case ERROROPENINPUT:
  10. puts("Error opening input file!");
  11. break;
  12. case ERROROPENTEMP:
  13. puts("Error opening temp file!");
  14. break;
  15. case ERRORINPUT:
  16. puts("Error reading input file!");
  17. break;
  18. case ERROROUTPUT:
  19. puts("Error writing temp file!");
  20. break;
  21. case ERRORREMOVE:
  22. puts("Error removing input file!");
  23. break;
  24. case ERRORRENAME:
  25. puts("Error renaming temp file!");
  26. break;
  27. case ERRORCLOSEINPUT:
  28. puts("Error closing input file!");
  29. break;
  30. case ERRORCLOSETEMP:
  31. puts("Error closing temp file!");
  32. break;
  33. case ERRORSEEK:
  34. puts("Error seeking input file!");
  35. break;
  36. case ERRORWRITINGTEMP:
  37. puts("Error writing to temp file!");
  38. break;
  39. default:
  40. printf("Unrecognized error: %d\n", errorType);
  41. }
  42. }
  43. int main(int argc, char **argv) {
  44. if (argc < 3) {
  45. usage(argv[0]);
  46. return 1;
  47. }
  48. char *inputFileName = argv[1];
  49. if (strcmp(argv[2], "c") == 0) {
  50. int result = checkBOM(inputFileName);
  51. result == HASBOM ? printf("%s has BOM.\n", inputFileName) :
  52. result == NOBOM ? printf("%s has no BOM.\n", inputFileName) : reportError(result);
  53. }
  54. if (strcmp(argv[2], "r") == 0) {
  55. int result = removeBOM(inputFileName);
  56. result == SUCCESS ? printf("BOM removed from %s.\n", inputFileName) : reportError(result);
  57. }
  58. if (strcmp(argv[2], "a") == 0) {
  59. int result = addBOM(inputFileName);
  60. result == SUCCESS ? printf("BOM added to %s.\n", inputFileName) : reportError(result);
  61. }
  62. return 0;
  63. }