-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActions.cpp
More file actions
129 lines (114 loc) · 3.42 KB
/
Actions.cpp
File metadata and controls
129 lines (114 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Common.h"
void Extension::SetBOMMarkASC(const char * FileToAddTo, int TypeOfBOM, int IgnoreCurrentBOM)
{
// Check parameters
if (!FileToAddTo || // Invalid file name supplied
TypeOfBOM > 2 || TypeOfBOM < 0 || // Invalid BOM choice (0 = none (ANSI), 1 = UTF-8, 2 = UTF-16)
IgnoreCurrentBOM < 0 || IgnoreCurrentBOM > 1) // Invalid RemoveCurrentBOM choice (yes, no)
return;
// Too long or ""
if (strnlen(FileToAddTo, MAX_PATH+1) == MAX_PATH || FileToAddTo[0] == '\0')
return;
// Duplicate file name (MMF2 will run off with orignal char *s)
const char * Copy = _strdup(FileToAddTo);
if (!Copy)
return;
// The entire file will have to stream, Windows doesn't support prepending
BOMThreadData * Data = new BOMThreadData(Copy, TypeOfBOM, IgnoreCurrentBOM == 1);
CreateThread(NULL, NULL, SetBOMMarkASCThread, Data, NULL, NULL);
}
// ?!
DWORD WINAPI SetBOMMarkASCThread(void * Param)
{
// Invalid parameter (We'll assume all the struct variables are valid if Param is valid)
if(!Param)
return 1;
BOMThreadData * Data = (BOMThreadData *)Param;
std::string ReadFromFilePath = Data->FileToAddTo,
WriteToFilePath = Data->FileToAddTo;
WriteToFilePath += ".tmp";
bool IgnoreCurrentBOM = Data->IgnoreCurrentBOM;
delete Param;
// Open file
FILE * ReadFromFile = NULL, * WriteToFile = NULL;
if (fopen_s(&ReadFromFile, ReadFromFilePath.c_str(), "rb") || !ReadFromFile)
return 2;
if (fopen_s(&WriteToFile, WriteToFilePath.c_str(), "ab") || !WriteToFile)
return 3;
fseek(ReadFromFile, 0, SEEK_END);
long FileSize = ftell(ReadFromFile);
if (Data->IgnoreCurrentBOM == false && FileSize >= 3)
{
fseek(ReadFromFile, 0, SEEK_SET);
char Front [3];
// Read 3 bytes (UTF-8 BOM is 3 bytes)
size_t s = fread(Front, sizeof(char), 3, ReadFromFile);
if (s != 3 && s != 2)
{
// Errorz
fclose(ReadFromFile);
fclose(WriteToFile);
return 4;
}
// UTF-16 BOM in original file (0xFF, 0xFE, N/A)
if (s >= 2 && Data->TypeOfBOM != 2 &&
Front[0] == 0xFF && Front[1] == 0xFE)
{
fseek(ReadFromFile, 2, SEEK_SET); // UTF-16 BOM is 2 bytes, not 3
}
else // !UTF-8 (0xEF, 0xBB, 0xBF), so ANSI file
{
if (!(s >= 3 && Data->TypeOfBOM != 1 &&
Front[0] == 0xEF && Front[1] == 0xBB && Front[2] == 0xBF))
{
fseek(ReadFromFile, 0, SEEK_SET); // ANSI doesn't need a BOM, go back
}
}
/* else UTF-8, 3-byte BOM, fread() skipped over it already */
}
fseek(ReadFromFile, -255, SEEK_CUR);
// UTF-8
if (Data->TypeOfBOM == 1)
{
char UTF8_BOM[2] = { 0xFF, 0xFE };
if (fwrite(UTF8_BOM, sizeof(char), 2, WriteToFile) != 2)
{
fclose(ReadFromFile);
fclose(WriteToFile);
remove(WriteToFilePath.c_str());
return 5;
}
}
// UTF-16
else if (Data->TypeOfBOM == 2)
{
char UTF16_BOM[3] = { 0xEF, 0xBB, 0xBF };
if (fwrite(UTF16_BOM, sizeof(char), 3, WriteToFile) != 3)
{
fclose(ReadFromFile);
fclose(WriteToFile);
remove(WriteToFilePath.c_str());
return 6;
}
}
// Stream the original file to end of new file
char Buffer [255];
size_t BytesRead;
while ((BytesRead = fread_s(Buffer, 255, sizeof(char), 255, ReadFromFile)) > 0)
{
//
if (fwrite(Buffer, sizeof(char), BytesRead, WriteToFile) != BytesRead)
{
fclose(ReadFromFile);
fclose(WriteToFile);
remove(WriteToFilePath.c_str());
return 7;
}
}
// Cleanup
fclose(ReadFromFile);
fclose(WriteToFile);
remove(ReadFromFilePath.c_str());
rename(WriteToFilePath.c_str(), ReadFromFilePath.c_str());
return 0;
}