forked from DediProgSW/SF100Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileIO.cpp
189 lines (156 loc) · 4.11 KB
/
FileIO.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
#include "stdafx.h"
#include "FileIO.h"
#include "IntelHexFile.h"
#include "MotorolaFile.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <iterator>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
CFileIO::FILE_FORMAT CFileIO::GetFileFormatFromExt(const wstring& csPath)
{
//int pos = csPath.ReverseFind('.') ;
//if(pos < 0)
// return UNKNOWN ;
//CString fmt = csPath.Right(csPath.GetLength() - pos - 1) ;
//fmt.MakeUpper() ;
boost::filesystem::wpath p(csPath);
wstring fmt(p.extension());
boost::to_upper(fmt);
if(fmt == L".HEX")
return HEX ;
else if(fmt == L".S19" || fmt==L".MOT")
return S19 ;
else
return BIN ;
}
// read file
bool CFileIO::Read(const wstring& csPath, std::vector<unsigned char>& buffer,unsigned char PaddingByte)
{
switch(GetFileFormatFromExt(csPath))
{
case HEX :
return ReadHEXFile(csPath, buffer,PaddingByte) ;
case S19 :
return ReadS19File(csPath, buffer,PaddingByte) ;
default :
return ReadBINFile(csPath, buffer) ;
}
}
// write file
bool CFileIO::Write(const std::vector<unsigned char>& buffer, const wstring& csPath)
{
switch(GetFileFormatFromExt(csPath))
{
case HEX :
return WriteHEXFile(buffer, csPath) ;
case S19 :
return WriteS19File(buffer, csPath) ;
default :
return WriteBINFile(buffer, csPath) ;
}
}
// read bin
bool CFileIO::ReadBINFile(const wstring& csPath, std::vector<unsigned char>& buffer)
{
//ANSI only
std::ifstream in(csPath.c_str(), std::ios::binary) ;
if(in == 0L)
return false ;
// get file buffer
std::stringstream ss ;
ss << in.rdbuf() ;
in.close() ;
// get length
std::string strBuff(ss.str()) ;
buffer.resize(strBuff.length()) ;
std::copy(strBuff.c_str(), strBuff.c_str() + strBuff.length(), buffer.begin()) ;
return !buffer.empty() ;
}
// write bin
bool CFileIO::WriteBINFile(const std::vector<unsigned char>& buffer, const wstring& csPath)
{
//ANSI only
std::ofstream out(csPath.c_str(), std::ios::binary) ;
if(out == 0L)
return false ;
std::copy(buffer.begin(), buffer.end(), std::ostream_iterator<unsigned char>(out,"")) ;
out.close() ;
return true ;
}
// read hex
bool CFileIO::ReadHEXFile(const wstring& csPath, std::vector<unsigned char>& buffer,unsigned char PaddingByte)
{
//ANSI only
char szPath[MAX_PATH] = {0};
WideCharToMultiByte(
CP_ACP,
WC_COMPOSITECHECK | WC_DEFAULTCHAR,
csPath.c_str(),
csPath.size(),
szPath,
MAX_PATH,
0,
0
);
buffer.clear();
return (HexFileToBin(szPath, buffer,PaddingByte)) ;
}
//write hex
bool CFileIO::WriteHEXFile(const std::vector<unsigned char>& buffer, const wstring& csPath)
{
//FIXME
//ANSI only
//ANSI only
//char szPath[MAX_PATH] = {0};
//WideCharToMultiByte(
// CP_ACP,
// WC_COMPOSITECHECK | WC_DEFAULTCHAR,
// csPath.GetString(),
// csPath.GetLength(),
// szPath,
// MAX_PATH,
// 0,
// 0
// );
//return (BinToHexFile(szPath, buffer)) ;
return (BinToHexFile(csPath, buffer)) ;
}
// read s19
bool CFileIO::ReadS19File(const wstring& csPath, std::vector<unsigned char>& buffer,unsigned char PaddingByte)
{
//ANSI only
char szPath[MAX_PATH] = {0};
WideCharToMultiByte(
CP_ACP,
WC_COMPOSITECHECK | WC_DEFAULTCHAR,
csPath.c_str(),
csPath.size(),
szPath,
MAX_PATH,
0,
0
);
buffer.clear();
return (S19FileToBin(szPath, buffer,PaddingByte)) ;
}
// write s19
bool CFileIO::WriteS19File(const std::vector<unsigned char>& buffer, const wstring& csPath)
{
//FIXME
//ANSI only
//ANSI only
char szPath[MAX_PATH] = {0};
WideCharToMultiByte(
CP_ACP,
WC_COMPOSITECHECK | WC_DEFAULTCHAR,
csPath.c_str(),
csPath.size(),
szPath,
MAX_PATH,
0,
0
);
return (BinToS19File(szPath, buffer)) ;
}