Browse Source

Detailed error reporting

Piotr Czajkowski 4 years ago
parent
commit
87abff0455
1 changed files with 41 additions and 3 deletions
  1. 41 3
      bomToolkit.c

+ 41 - 3
bomToolkit.c

@@ -6,6 +6,40 @@ void usage(char *executable) {
 	printf("%s <filePath> a - to add BOM.\n", executable);
 }
 
+void reportError(int errorType) {
+	switch(errorType) {
+		case ERROROPENINPUT:
+			puts("Error opening input file!");
+			break;
+		case ERROROPENTEMP:
+			puts("Error opening temp file!");
+			break;
+		case ERRORINPUT:
+			puts("Error reading input file!");
+			break;
+		case ERROROUTPUT:
+			puts("Error writing temp file!");
+			break;
+		case ERRORREMOVE:
+			puts("Error removing input file!");
+			break;
+		case ERRORRENAME:
+			puts("Error renaming temp file!");
+			break;
+		case ERRORCLOSEINPUT:
+			puts("Error closing input file!");
+			break;
+		case ERRORCLOSETEMP:
+			puts("Error closing temp file!");
+			break;
+		case ERRORSEEK:
+			puts("Error seeking input file!");
+			break;
+		default:
+			printf("Unrecognized error: %d\n", errorType);
+	}
+}
+
 int main(int argc, char **argv) {
 	if (argc < 3) {
 		usage(argv[0]);
@@ -15,13 +49,17 @@ int main(int argc, char **argv) {
 	char *inputFileName = argv[1];
 
 	if (strcmp(argv[2], "c") == 0) {
-		checkBOM(inputFileName) == HASBOM ? printf("%s has BOM.\n", inputFileName) : printf("%s has no BOM.\n", inputFileName);
+		int result = checkBOM(inputFileName);
+		result == HASBOM ? printf("%s has BOM.\n", inputFileName) :
+			result == NOBOM ? printf("%s has no BOM.\n", inputFileName) : reportError(result);
 	}
 	if (strcmp(argv[2], "r") == 0) {
-		removeBOM(inputFileName) == SUCCESS ? printf("BOM removed from %s.\n", inputFileName) : printf("Error removing BOM from %s!\n", inputFileName);
+		int result = removeBOM(inputFileName);
+		result == SUCCESS ? printf("BOM removed from %s.\n", inputFileName) : reportError(result);
 	}
 	if (strcmp(argv[2], "a") == 0) {
-		addBOM(inputFileName) == SUCCESS ? printf("BOM added to %s.\n", inputFileName) : printf("Error adding BOM to %s!\n", inputFileName);
+		int result = addBOM(inputFileName);
+		result == SUCCESS ? printf("BOM added to %s.\n", inputFileName) : reportError(result);
 	}
 
 	return 0;