Browse Source

Better errors, I hope

Piotr Czajkowski 4 years ago
parent
commit
7eabf2a642
3 changed files with 18 additions and 20 deletions
  1. 17 17
      bom.c
  2. 1 1
      bom.h
  3. 0 2
      test_bom.c

+ 17 - 17
bom.c

@@ -6,14 +6,14 @@ unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
 
 int checkBOM(const char *filePath) {
 	FILE *inputFile = fopen(filePath, "r");
-	if (inputFile == NULL) return ERROR;
+	if (inputFile == NULL) return ERROROPENINPUT;
 
 	unsigned char check[BOMSIZE];
 	if (BOMSIZE != fread(check, BOMSIZE, 1, inputFile)) {
 		if (feof(inputFile)) return NOBOM;
-		if (ferror(inputFile)) return ERROR;
+		if (ferror(inputFile)) return ERRORINPUT;
 	}
-	if (fclose(inputFile)) return ERROR;
+	if (fclose(inputFile)) return ERRORCLOSEINPUT;
 
 	if (!memcmp(&bom, check, BOMSIZE)) return HASBOM;
 	return NOBOM;
@@ -25,12 +25,12 @@ int removeBOM(const char *filePath) {
 	if (NOBOM == checkBOM(filePath)) return SUCCESS;
 
 	FILE *inputFile = fopen(filePath, "r");
-	if (inputFile == NULL) return ERROR;
+	if (inputFile == NULL) return ERROROPENINPUT;
 
-	if (fseek(inputFile, BOMSIZE, SEEK_SET)) return ERROR;
+	if (fseek(inputFile, BOMSIZE, SEEK_SET)) return ERRORSEEK;
 
 	FILE *tempFile = fopen(tempFileName, "w");
-	if (tempFile == NULL) return ERROR;
+	if (tempFile == NULL) return ERROROPENTEMP;
 
 	char buffer[CHUNKSIZE];
 	do {
@@ -38,11 +38,11 @@ int removeBOM(const char *filePath) {
 		fwrite(buffer, 1, read, tempFile);
 	} while (!feof(inputFile));
 	
-	if (fclose(tempFile)) return ERROR;
-	if (fclose(inputFile)) return ERROR;
+	if (fclose(tempFile)) return ERRORCLOSETEMP;
+	if (fclose(inputFile)) return ERRORCLOSEINPUT;
 
-	if (remove(filePath)) return ERROR;
-	if (rename(tempFileName, filePath)) return ERROR;
+	if (remove(filePath)) return ERRORREMOVE;
+	if (rename(tempFileName, filePath)) return ERRORRENAME;
 
 	return SUCCESS;
 }
@@ -51,13 +51,13 @@ int addBOM(const char *filePath) {
 	if (HASBOM == checkBOM(filePath)) return SUCCESS;
 
 	FILE *inputFile = fopen(filePath, "r");
-	if (inputFile == NULL) return ERROR;
+	if (inputFile == NULL) return ERROROPENINPUT;
 
 	FILE *tempFile = fopen(tempFileName, "w");
-	if (tempFile== NULL) return ERROR;
+	if (tempFile== NULL) return ERROROPENTEMP;
 
 	int written = fwrite(bom, 1, BOMSIZE, tempFile);
-	if (written != BOMSIZE) return ERROR;
+	if (written != BOMSIZE) return ERROROUTPUT;
 
 	char buffer[CHUNKSIZE];
 	do {
@@ -65,11 +65,11 @@ int addBOM(const char *filePath) {
 		fwrite(buffer, 1, read, tempFile);
 	} while (!feof(inputFile));
 
-	if (fclose(tempFile)) return ERROR;
-	if (fclose(inputFile)) return ERROR;
+	if (fclose(tempFile)) return ERRORCLOSETEMP;
+	if (fclose(inputFile)) return ERRORCLOSEINPUT;
 
-	if (remove(filePath)) return ERROR;
-	if (rename(tempFileName, filePath)) return ERROR;
+	if (remove(filePath)) return ERRORREMOVE;
+	if (rename(tempFileName, filePath)) return ERRORRENAME;
 
 	return SUCCESS;
 }

+ 1 - 1
bom.h

@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include <string.h>
 
-enum { NOBOM, HASBOM, ERROR, SUCCESS };
+enum { NOBOM, HASBOM, SUCCESS, ERROROPENINPUT, ERROROPENTEMP, ERRORINPUT, ERROROUTPUT, ERRORREMOVE, ERRORRENAME, ERRORCLOSEINPUT, ERRORCLOSETEMP, ERRORSEEK};
 
 int checkBOM(const char *filePath);
 int removeBOM(const char *filePath);

+ 0 - 2
test_bom.c

@@ -14,7 +14,6 @@ char *fileBOMRemoval = "./testRemove";
 
 void testRemoveBOM(void) {
 	int result = removeBOM(fileBOMRemoval);
-	assert(ERROR != result);
 	assert(SUCCESS == result);
 	assert(NOBOM == checkBOM(fileBOMRemoval));
 }
@@ -23,7 +22,6 @@ char *fileBOMAddition = "./testAdd";
 
 void testAddBOM(void) {
 	int result = addBOM(fileBOMAddition);
-	assert(ERROR != result);
 	assert(SUCCESS == result);
 	assert(HASBOM == checkBOM(fileBOMAddition));
 }