forked from videoP/jaPRO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTxtFile.cpp
197 lines (175 loc) · 3.71 KB
/
TxtFile.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// TxtFile.cpp
#include <afxwin.h>
#include "TxtFile.h"
CTxtFile::CTxtFile()
{
}
CTxtFile::~CTxtFile()
{
}
CTxtFile* CTxtFile::Create(CFile* file)
{
CTxtFile* newFile = new CTxtFile();
newFile->Init(file);
return newFile;
}
CTxtFile* CTxtFile::Create(LPCTSTR filename)
{
CTxtFile* newFile = new CTxtFile();
newFile->Init(filename);
return newFile;
}
void CTxtFile::Delete()
{
if (m_ownsFile && (m_file != NULL))
{
delete m_file;
m_file = NULL;
}
delete this;
}
void CTxtFile::Init(LPCTSTR filename)
{
try
{
m_file = new CFile(filename, CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive);
m_ownsFile = true;
}
catch(CException* e)
{
m_file = NULL;
e->Delete();
}
}
void CTxtFile::Init(CFile* file)
{
m_file = file;
m_ownsFile = false;
}
void CTxtFile::Write(LPCTSTR string)
{
ASSERT(string);
m_file->Write(string, strlen(string));
}
void CTxtFile::Writeln(LPCTSTR string1, LPCTSTR string2, LPCTSTR string3)
{
Write(string1);
Write(string2);
Writeln(string3);
}
void CTxtFile::Writeln(LPCTSTR string1, LPCTSTR string2)
{
Write(string1);
Writeln(string2);
}
void CTxtFile::Write(LPCTSTR string1, LPCTSTR string2, LPCTSTR string3)
{
Write(string1);
Write(string2);
Write(string3);
}
void CTxtFile::Write(LPCTSTR string1, LPCTSTR string2)
{
Write(string1);
Write(string2);
}
void CTxtFile::Writeln(LPCTSTR string)
{
Write(string);
Writeln();
}
void CTxtFile::Writeln()
{
Write("\r\n");
}
void CTxtFile::Write(int value)
{
CString temp;
temp.Format("%d", value);
Write(temp);
}
void CTxtFile::Write(float value, int fractSize)
{
CString temp;
switch (fractSize)
{
case 1:
temp.Format("%.1f", value);
break;
case 2:
temp.Format("%.2f", value);
break;
case 3:
temp.Format("%.3f", value);
break;
case 4:
temp.Format("%.4f", value);
break;
case 5:
temp.Format("%.5f", value);
break;
case 6:
temp.Format("%.6f", value);
break;
default:
temp.Format("%g", value);
break;
}
Write(temp);
}
void CTxtFile::WriteString(LPCTSTR string)
{
CString temp;
temp.Format("\"%s\"", string);
Write(temp);
}
// there is a slight problem with writing comments at the moment to do with spurious 0x0D bytes occasionally
// being introduced. This may be due to designers sometimes using text editors that use CR-only line ends
// rather than CR/LF pairs, and the tokenizer reader not stripping them. I could update the tokenizer source
// member "GetStringValue()" but that's shadowed in a frightening number of programs, and I don't want to break
// something miles away, so I'll just check for them here. Basically, you should never get 2 0x0D bytes next
// to each other, so each time I find one a pair I'll remove the first one.
//
void CTxtFile::WriteComment(LPCTSTR string, int indent)
{
const char sFind [3]={0x0D,0x0D,0}; // a bit horrible really, but needs to be exact bytes
const char sReplace[2]={0x0D,0};
CString temp = string;
while (temp.Replace(sFind,sReplace)); // after this we're down to 1 0x0D max
int loc = temp.Find('\n');
if (loc == -1)
{
// now we know there's no 0x0D/0x0A pairs then any remaining 0x0D bytes are orphans and should be disposed
// of (don't you just love it when text files are read in as binary?)
//
while (temp.Replace(sReplace,NULL));
Space(indent);
Write("//", temp);
Writeln();
return;
}
Space(indent);
Writeln("/* ");
while (loc > -1)
{
CString left = temp.Left(loc);
temp = temp.Right(temp.GetLength() - loc - 1);
Space(indent);
Writeln(left);
loc = temp.Find('\n');
}
Space(indent);
Writeln(temp);
Space(indent);
Writeln("*/");
}
void CTxtFile::Space(int value)
{
char* temp;
temp = (char*)malloc(value + 1);
ASSERT(temp);
memset(temp, ' ', value);
temp[value] = '\0';
Write(temp);
free(temp);
}