-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwinFiles.cpp
364 lines (305 loc) · 8.04 KB
/
winFiles.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//--------------------------------------
// The main Alarm window
//--------------------------------------
#include "winFiles.h"
#include "gBuffer.h"
#include "dlgMsg.h"
#include "dlgConfirm.h"
#include <SD.h>
#include <string.h>
#include "protectedFS.h"
#define DEBUG_FILES 1
//----------------------
// implementation
//----------------------
winFiles files_win;
void winFiles::fileError(const char *text)
{
setErrMsg(text);
g_debug("FILE ERROR: %s",text);
m_file_error = true;
}
void winFiles::getLine(int line_num, const char **left_side, const char **right_side)
{
char *p = gs_buffer;
while (line_num)
{
while (*p) p++;
p++;
while (*p) p++;
p++;
line_num--;
}
*left_side = p;
while (*p) p++;
p++;
*right_side = p;
}
const char *winFiles::getFileToRun(int i) const
{
int line_num = m_top_num + i;
const char *p = gs_buffer;
while (line_num)
{
while (*p) p++;
p++;
while (*p) p++;
p++;
line_num--;
}
return p;
}
bool winFiles::canGoHome() { return strcmp(m_path,"/"); }
bool winFiles::canGoBack() { return strcmp(m_path,"/"); }
void winFiles::onGoHome() { setPath("/",true); }
void winFiles::onGoBack()
{
char *p = m_path;
char *last = p;
while (*p)
{
if (*p == '/')
{
last = p;
}
p++;
}
*last = 0;
setPath(m_path,true);
}
bool isGcodeFile(const char *new_path)
{
const char *p = new_path;
while (*p)
{
if (*p++ == '.')
new_path = p;
}
return
!strcmp(new_path,"g") ||
!strcmp(new_path,"gc") ||
!strcmp(new_path,"gcode") ||
!strcmp(new_path,"nc") ||
!strcmp(new_path,"ngc") ||
!strcmp(new_path,"G") ||
!strcmp(new_path,"GC") ||
!strcmp(new_path,"GCODE") ||
!strcmp(new_path,"NC") ||
!strcmp(new_path,"NGC");
}
void winFiles::onLineClicked(uiDualElement *dual, int top_num, int rel_line_num)
{
const char *fn = dual->label;
#if DEBUG_FILES
g_debug("onLineClicked(%d,%s)",rel_line_num,fn);
#endif
if (fn[0] == '/')
{
fn++;
setPath(fn,false);
}
else if (isGcodeFile(fn))
{
m_last_path[0] = 0;
dlg_confirm.setConfirm(CONFIRM_COMMAND_RUN_FILE + rel_line_num);
the_app.openWindow(&dlg_confirm);
}
else
{
warningMsg("File is not gcode!");
}
}
void winFiles::setPath(const char *ipath, bool absolute)
{
if (absolute) // ipath includes the slash
{
strcpy(m_path,ipath);
}
else // ipath gets added to current path
{
if (strlen(m_path) + strlen(ipath) + 1 >= MAX_FILE_PATH)
{
errorMsg("path too long!!");
return;
}
if (strcmp(m_path,"/"))
strcat(m_path,"/");
strcat(m_path,ipath);
}
if (!m_path[0])
strcpy(m_path,"/");
m_top_num = 0;
m_last_top_num = -1;
m_last_path[0] = 0;
m_buffer_version = 0;
#if DEBUG_FILES
g_debug("setPath(%s)",m_path);
#endif
}
void winFiles::begin()
{
uiScrollable::begin();
if (m_file_error)
{
m_file_error = false;
setErrMsg(NULL);
setPath("/",true);
}
else
{
m_last_path[0] = 0;
}
m_draw_needed = true;
}
bool winFiles::addFilename(const char *filename, int size)
{
int len_fn = strlen(filename);
#if DEBUG_FILES > 1
g_debug("adding %s %d at %d",filename,size,gs_buf_ptr);
#endif
if (size == -1) // directory
{
if (gs_buf_ptr + len_fn + 3 >= MAX_G_BUFFER)
{
g_debug("too many files at directory '%s'",filename);
return false;
}
gs_buffer[gs_buf_ptr++] = '/';
strcpy(&gs_buffer[gs_buf_ptr],filename);
gs_buf_ptr += len_fn + 1;
gs_buffer[gs_buf_ptr++] = 0;
}
else
{
char tmp_buf[12];
sprintf(tmp_buf,"%d",size);
int len_tmp = strlen(tmp_buf);
if (gs_buf_ptr + len_fn + len_tmp + 2 >= MAX_G_BUFFER)
{
g_debug("too many files at '%s'",filename);
return false;
}
strcpy(&gs_buffer[gs_buf_ptr],filename);
gs_buf_ptr += len_fn + 1;
strcpy(&gs_buffer[gs_buf_ptr],tmp_buf);
gs_buf_ptr += len_tmp + 1;
}
m_num_lines++;
return true;
}
void winFiles::sortFilenames()
// laborious sort in place with no cached pointers
{
#if DEBUG_FILES
g_debug("sort %d filenames",m_num_lines);
#endif
int i = 0;
char *p1 = gs_buffer;
while (i < m_num_lines-1)
{
char *p2 = p1;
while (*p2) p2++;
p2++;
char *psize1 = p2;
while (*p2) p2++;
p2++;
char *psize2 = p2;
while (*psize2) psize2++;
psize2++;
#if DEBUG_FILES > 1
g_debug("compare(%d) %s:%s to %s:%s",i,p1,psize1,p2,psize2);
#endif
char *pcmp1 = p1;
char *pcmp2 = p2;
if (*pcmp1 == '/') pcmp1++;
if (*pcmp2 == '/') pcmp2++;
int cmp = strcasecmp(pcmp1,pcmp2);
if (cmp <= 0)
{
#if DEBUG_FILES > 2
g_debug("skipping(%d) %s:%s",i,p1,psize1);
#endif
i++;
p1 = p2;
}
else
{
#define MAX_FILENAME 128
char filename[MAX_FILENAME];
int copy1_len = strlen(p1) + strlen(psize1) + 2;
int copy2_len = strlen(p2) + strlen(psize2) + 2;
if (copy1_len > MAX_FILENAME)
{
fileError("Cannot sort directory!");
return;
}
char *to2 = &p1[copy2_len];
memcpy(filename,p1,copy1_len);
memcpy(p1,p2,copy2_len);
memcpy(to2,filename,copy1_len);
#if DEBUG_FILES > 2
g_debug("after swapping %s and %s",p1,to2);
#endif
if (i>0)
{
i--;
if (i == 0)
{
p1 = gs_buffer;
}
else
{
p1 -= 2; // skip previous size delimiter
while (*p1) p1--; // skip size if any
p1--; // skip name delimiter
while (*p1) p1--; // until before previous name
p1++; // and then forward one
}
}
}
}
#if DEBUG_FILES > 1
g_debug("sort finished");
#endif
}
uint32_t winFiles::updateBuffer()
{
g_debug("winFiles::updateBuffer(%d,%s)",m_top_num,m_path);
// invalidate the global buffer, take ownership of the version
// and set base number of lines and buffer pointer to zero
gs_buffer_version++;
gs_buf_ptr = 0;
m_num_lines = 0;
// start the SDCard as necessary
// winFiles uses g_status directly
if (g_status.getSDState(true) != SDState::Idle)
{
fileError("Could not get SDCard");
return gs_buffer_version;
}
// iterate over the directory
protectedFS fs(SD); // for leafName();
proFile root = fs.open(m_path);
if (root && root.isDirectory())
{
proFile file = root.openNextFile();
while (file)
{
if (!addFilename(file.leafName(),
file.isDirectory()? -1 : file.size()))
{
break;
}
file = root.openNextFile();
}
}
else
{
fileError("Could not opendir");
}
if (root)
root.close();
// SD.end();
sortFilenames();
return gs_buffer_version;
}