bomToolkit.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. default:
  37. printf("Unrecognized error: %d\n", errorType);
  38. }
  39. }
  40. int main(int argc, char **argv) {
  41. if (argc < 3) {
  42. usage(argv[0]);
  43. return 1;
  44. }
  45. char *inputFileName = argv[1];
  46. if (strcmp(argv[2], "c") == 0) {
  47. int result = checkBOM(inputFileName);
  48. result == HASBOM ? printf("%s has BOM.\n", inputFileName) :
  49. result == NOBOM ? printf("%s has no BOM.\n", inputFileName) : reportError(result);
  50. }
  51. if (strcmp(argv[2], "r") == 0) {
  52. int result = removeBOM(inputFileName);
  53. result == SUCCESS ? printf("BOM removed from %s.\n", inputFileName) : reportError(result);
  54. }
  55. if (strcmp(argv[2], "a") == 0) {
  56. int result = addBOM(inputFileName);
  57. result == SUCCESS ? printf("BOM added to %s.\n", inputFileName) : reportError(result);
  58. }
  59. return 0;
  60. }