123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include "bom.h"
- #define BOMSIZE 3
- #define CHUNKSIZE 1024
- unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
- int checkBOM(const char *filePath) {
- FILE *inputFile = fopen(filePath, "r");
- 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 ERRORINPUT;
- }
- if (fclose(inputFile)) return ERRORCLOSEINPUT;
- if (!memcmp(&bom, check, BOMSIZE)) return HASBOM;
- return NOBOM;
- }
- const char *tempFileName = "tempFile";
- int removeBOM(const char *filePath) {
- if (NOBOM == checkBOM(filePath)) return SUCCESS;
- FILE *inputFile = fopen(filePath, "r");
- if (inputFile == NULL) return ERROROPENINPUT;
- if (fseek(inputFile, BOMSIZE, SEEK_SET)) return ERRORSEEK;
- FILE *tempFile = fopen(tempFileName, "w");
- if (tempFile == NULL) return ERROROPENTEMP;
- char buffer[CHUNKSIZE];
- do {
- size_t read = fread(buffer, 1, CHUNKSIZE, inputFile);
- size_t written = fwrite(buffer, 1, read, tempFile);
- if (written != read) {
- fclose(tempFile);
- fclose(inputFile);
- remove(tempFileName);
- return ERRORWRITINGTEMP;
- }
- } while (!feof(inputFile));
- if (fclose(tempFile)) return ERRORCLOSETEMP;
- if (fclose(inputFile)) return ERRORCLOSEINPUT;
- if (remove(filePath)) return ERRORREMOVE;
- if (rename(tempFileName, filePath)) return ERRORRENAME;
- return SUCCESS;
- }
- int addBOM(const char *filePath) {
- if (HASBOM == checkBOM(filePath)) return SUCCESS;
- FILE *inputFile = fopen(filePath, "r");
- if (inputFile == NULL) return ERROROPENINPUT;
- FILE *tempFile = fopen(tempFileName, "w");
- if (tempFile== NULL) return ERROROPENTEMP;
- size_t written = fwrite(bom, 1, BOMSIZE, tempFile);
- if (written != BOMSIZE) return ERROROUTPUT;
- char buffer[CHUNKSIZE];
- while(1) {
- size_t read = fread(buffer, 1, CHUNKSIZE, inputFile);
- written = fwrite(buffer, 1, read, tempFile);
- if (written != read) {
- fclose(tempFile);
- fclose(inputFile);
- remove(tempFileName);
- return ERRORWRITINGTEMP;
- }
- if (feof(inputFile)) break;
- }
- if (fclose(tempFile)) return ERRORCLOSETEMP;
- if (fclose(inputFile)) return ERRORCLOSEINPUT;
- if (remove(filePath)) return ERRORREMOVE;
- if (rename(tempFileName, filePath)) return ERRORRENAME;
- return SUCCESS;
- }
|