Browse Source

First version

Piotr Czajkowski 6 years ago
parent
commit
daf7382543
7 changed files with 140 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 72 0
      bom.c
  3. 11 0
      bom.h
  4. 16 0
      makefile
  5. 1 0
      testBOM
  6. 1 0
      testNoBOM
  7. 38 0
      test_bom.c

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.dSYM/

+ 72 - 0
bom.c

@@ -0,0 +1,72 @@
+#include "bom.h"
+
+#define BOMSIZE 3
+unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
+
+int checkBOM(char *filePath) {
+	FILE *inputFile = fopen(filePath, "r");
+	if (inputFile == NULL) return ERROR;
+
+    unsigned char check[BOMSIZE];
+    if (BOMSIZE != fread(check, BOMSIZE, 1, inputFile)) {
+        if (feof(inputFile)) return NOBOM;
+        if (ferror(inputFile)) return ERROR;
+    }
+	if (fclose(inputFile)) return ERROR;
+
+    if (!memcmp(&bom, check, BOMSIZE)) return HASBOM;
+	return NOBOM;
+}
+
+char *tempFileName = "tempFile";
+
+int removeBOM(char *filePath) {
+    if (NOBOM == checkBOM(filePath)) return SUCCESS;
+
+	FILE *inputFile = fopen(filePath, "r");
+    if (inputFile == NULL) return ERROR;
+
+	if (fseek(inputFile, BOMSIZE, SEEK_SET)) return ERROR;
+
+	FILE *tempFile = fopen(tempFileName, "w");
+    if (tempFile== NULL) return ERROR;
+
+	int c = fgetc(inputFile);
+	while (c != EOF) {
+		if (EOF == fputc(c, tempFile)) return ERROR;
+		c = fgetc(inputFile);
+	}
+    if (fclose(tempFile)) return ERROR;
+    if (fclose(inputFile)) return ERROR;
+
+    if (remove(filePath)) return ERROR;
+    if (rename(tempFileName, filePath)) return ERROR;
+
+    return SUCCESS;
+}
+
+int addBOM(char *filePath) {
+    if (HASBOM == checkBOM(filePath)) return SUCCESS;
+
+    FILE *inputFile = fopen(filePath, "r");
+    if (inputFile == NULL) return ERROR;
+
+    FILE *tempFile = fopen(tempFileName, "w");
+    if (tempFile== NULL) return ERROR;
+
+    for (int i = 0; i < BOMSIZE; i++)
+        if (EOF == fputc(bom[i], tempFile)) return ERROR;
+
+    int c = fgetc(inputFile);
+    while (c != EOF) {
+        if (EOF == fputc(c, tempFile)) return ERROR;
+        c = fgetc(inputFile);
+    }
+    if (fclose(tempFile)) return ERROR;
+    if (fclose(inputFile)) return ERROR;
+
+    if (remove(filePath)) return ERROR;
+    if (rename(tempFileName, filePath)) return ERROR;
+
+    return SUCCESS;
+}

+ 11 - 0
bom.h

@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <string.h>
+
+#define NOBOM 0
+#define HASBOM 1
+#define ERROR 2
+#define SUCCESS 3
+
+int checkBOM(char *filePath);
+int removeBOM(char *filePath);
+int addBOM(char *filePath);

+ 16 - 0
makefile

@@ -0,0 +1,16 @@
+CFLAGS=-g -Wall -Wextra -O3 -std=gnu99
+objects=bom.o
+
+test_bom: $(objects)
+
+clean:
+	@rm *.o
+
+test: test_bom
+	@rm *.o
+	@cp testBOM testRemove
+	@cp testNoBOM testAdd
+	@./test_bom
+	@rm test_bom
+	@rm testRemove
+	@rm testAdd

+ 1 - 0
testBOM

@@ -0,0 +1 @@
+something

+ 1 - 0
testNoBOM

@@ -0,0 +1 @@
+something

+ 38 - 0
test_bom.c

@@ -0,0 +1,38 @@
+#include <assert.h>
+#include <stdio.h>
+#include "bom.h"
+
+char *fileWithBOM = "./testBOM";
+char *fileWithoutBOM = "./testNoBOM";
+
+void testCheckBOM(void) {
+	assert(HASBOM == checkBOM(fileWithBOM));
+	assert(NOBOM == checkBOM(fileWithoutBOM));
+}
+
+char *fileBOMRemoval = "./testRemove";
+
+void testRemoveBOM(void) {
+    int result = removeBOM(fileBOMRemoval);
+	assert(ERROR != result);
+    assert(SUCCESS == result);
+    assert(NOBOM == checkBOM(fileBOMRemoval));
+}
+
+char *fileBOMAddition = "./testAdd";
+
+void testAddBOM(void) {
+    int result = addBOM(fileBOMAddition);
+    assert(ERROR != result);
+    assert(SUCCESS == result);
+    assert(HASBOM == checkBOM(fileBOMAddition));
+}
+
+int main(int argc, char **argv) {
+	(void)argc;
+	(void)argv;
+	testCheckBOM();
+	testRemoveBOM();
+    testAddBOM();
+	printf("OK\n");
+}