bomToolkit.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. perror("Error opening input file");
  11. break;
  12. case ERROROPENTEMP:
  13. perror("Error opening temp file");
  14. break;
  15. case ERRORINPUT:
  16. perror("Error reading input file");
  17. break;
  18. case ERROROUTPUT:
  19. perror("Error writing temp file");
  20. break;
  21. case ERRORREMOVE:
  22. perror("Error removing input file");
  23. break;
  24. case ERRORRENAME:
  25. perror("Error renaming temp file");
  26. break;
  27. case ERRORCLOSEINPUT:
  28. perror("Error closing input file");
  29. break;
  30. case ERRORCLOSETEMP:
  31. perror("Error closing temp file");
  32. break;
  33. case ERRORSEEK:
  34. perror("Error seeking input file");
  35. break;
  36. case ERRORWRITINGTEMP:
  37. perror("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. int result;
  50. switch(argv[2][0]) {
  51. case 'c':
  52. result = checkBOM(inputFileName);
  53. result == HASBOM ? printf("%s has BOM.\n", inputFileName) :
  54. result == NOBOM ? printf("%s has no BOM.\n", inputFileName) : reportError(result);
  55. break;
  56. case 'r':
  57. result = removeBOM(inputFileName);
  58. result == SUCCESS ? printf("BOM removed from %s.\n", inputFileName) : reportError(result);
  59. break;
  60. case 'a':
  61. result = addBOM(inputFileName);
  62. result == SUCCESS ? printf("BOM added to %s.\n", inputFileName) : reportError(result);
  63. break;
  64. default:
  65. puts("No such option!");
  66. usage(argv[0]);
  67. return 1;
  68. }
  69. return 0;
  70. }