-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnativefs.cpp
264 lines (202 loc) · 5.51 KB
/
nativefs.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "nativefs.hpp"
#include "logger.hpp"
#include <QDir>
#include <math.h>
using namespace Logging;
namespace {
const QString strDir("DIR");
const QString strPrg("PRG");
}
NativeFS::NativeFS()
: m_listDirectories(false)
{
} // ctor
void NativeFS::setListingFilters(const QString &filters, bool listDirectories)
{
m_filters = filters;
m_listDirectories = listDirectories;
} // setListingFilters
bool NativeFS::openHostFile(const QString& fileName)
{
return fopen(fileName);
} // openHostFile
void NativeFS::closeHostFile()
{
if(!m_hostFile.fileName().isEmpty() and m_hostFile.isOpen())
m_hostFile.close();
m_status = NOT_READY;
} // closeHostFile
bool NativeFS::fopen(const QString& fileName)
{
closeHostFile();
m_hostFile.setFileName(fileName);
bool isOpen = m_hostFile.open(QIODevice::ReadOnly);
m_status = isOpen ? FILE_OPEN : NOT_READY;
return isOpen;
} // fopen
bool NativeFS::newFile(const QString& fileName)
{
Q_UNUSED(fileName);
// TODO: Implement.
return false;
} // newFile
/* TODO: Move this to PI NativeFS and recode, create new file.
byte fat_newfile(char *filename)
{
// Remove and create file
fatRemove(filename);
return fatFcreate(filename);
} // fat_newfile
*/
char NativeFS::getc()
{
char theByte;
qint64 numRead(m_hostFile.read(&theByte, 1));
if(numRead < 1) // shouldn't happen?
m_status = FILE_EOF;
return theByte;
} // getc
QString NativeFS::openedFileName() const
{
return m_hostFile.fileName();
} // openedFileName
ushort NativeFS::openedFileSize() const
{
return m_hostFile.size();
} // openedFileSize
bool NativeFS::isEOF() const
{
return m_hostFile.atEnd();
} // isEOF
bool NativeFS::putc(char c)
{
qint64 written = m_hostFile.write(&c, 1);
// TODO: Implement.
return 1 == written;
} // putc
bool NativeFS::close()
{
closeHostFile();
return true;
} // close
bool NativeFS::sendListing(ISendLine& cb)
{
QDir dir(QDir::current());
QString dirName(dir.dirName().toUpper());
dirName.truncate(23);
dirName = dirName.leftJustified(23);
QStringList filters = m_filters.split(',', QString::SkipEmptyParts);
QString line("\x12\""); // Invert face, "
QString diskLabel("NATIVE FS");
uchar i;
for(i = 2; i < 25; i++) {
uchar c = dirName.at(0).toLatin1();
dirName.remove(0, 1);
if(0xA0 == c) // Convert padding A0 to spaces
c = ' ';
if(18 == i) // Ending "
c = '"';
line += c;
}
// Ending name with dbl quotes
line[i] = QChar('"').toLatin1();
cb.send(0, line);
QFileInfoList list(dir.entryInfoList(filters, QDir::NoDot bitor QDir::Files
bitor (m_listDirectories ? QDir::AllDirs : QDir::Files), QDir::Name bitor QDir::DirsFirst));
Log("NATIVEFS", QString("Listing %1 entrie(s) to CBM.").arg(QString::number(list.count())), info);
while(!list.isEmpty()) {
QFileInfo entry = list.first();
list.removeFirst();
line = " \"";
line.append(entry.fileName().toUpper());
line.append("\" ");
int spaceFill = 16 - entry.fileName().length();
if(spaceFill > 0) {
QString spaceAdd(spaceFill, ' ');
line += spaceAdd;
}
if(entry.isDir())
line.append(strDir);
else
line.append(strPrg);
ushort fileSize = entry.size() / 1024;
// Send initial spaces (offset) according to file size
cb.send(fileSize, line.mid((int)log10(fileSize)));
}
return true;
} // sendListing
////////////////////////////////////////////////////////////////////////////////
//
// Send SD info function
bool NativeFS::sendMediaInfo(ISendLine &cb)
{
// TODO: Improve this with information about the file system type AND, usage and free data.
Log("NATIVEFS", "sendMediaInfo.", info);
cb.send(0, QString("NATIVE FS ACTIVE -> XXX.")); // TODO: File system type instead of xxx.
cb.send(1, QString("CURRENT DIR: %1").arg(QDir::currentPath().toUpper()));
cb.send(2, "HELLO FROM ARDUINO UNO!");
return true;
} // sendMediaInfo
FileDriverBase::FSStatus NativeFS::status() const
{
return FileDriverBase::status();
} // status
bool NativeFS::setCurrentDirectory(const QString& dir)
{
bool wasSuccess = QDir::setCurrent(dir);
if(wasSuccess)
Log("NATIVEFS", QString("Changing current directory to: %1").arg(QDir::currentPath()), success);
else
Log("NATIVEFS", QString("Failed changing current directory to: %1 (this may be just OK)").arg(dir), warning);
return wasSuccess;
} // setCurrentDirectory
// Command to the command channel.
IOErrorMessage NativeFS::cmdChannel(const QString& cmd)
{
Q_UNUSED(cmd);
return ErrDriveNotReady;
} // cmdChannel
// Fat parsing command channel
//
/* TODO: Move this to PI, Native file system command.
unsigned char fat_cmd(char *c)
{
// Get command letter and argument
char cmd_letter;
char *arg, *p;
char ret = ERR_OK;
cmd_letter = cmd.str[0];
arg = strchr(cmd.str,':');
if(arg not_eq NULL) {
arg++;
if(cmd_letter == 'S') {
// Scratch a file
if(!fatRemove(arg))
ret = ERR_FILE_NOT_FOUND;
}
else if(cmd_letter == 'R') {
// Rename, find = and replace with 0 to get cstr
p = strchr(arg, '=');
if(p not_eq NULL) {
*p = 0;
p++;
if(!fatRename(p, arg)) {
ret = ERR_FILE_NOT_FOUND;
}
}
}
else if(cmd_letter == 'N') {
// Format new disk creates an M2I file
ret = M2I_newdisk(arg);
if(ret == ERR_OK) {
// It worked, go to M2I state
interface_state = I_M2I;
}
}
else {
ret = ErrSyntaxError;
}
}
return ret;
}
*/