-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmdDir.cpp
149 lines (118 loc) · 3.06 KB
/
cmdDir.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
/*
Module: cmdDir.cpp
Function:
Print directory of SD card
Copyright and License:
This file copyright (C) 2021 by
MCCI Corporation
3520 Krums Corners Road
Ithaca, NY 14850
See accompanying LICENSE file for copyright and license information.
Author:
Terry Moore, MCCI Corporation May 2021
*/
#include "Catena4430_cmd.h"
#include "Catena4430_Sensor.h"
using namespace McciCatena;
/****************************************************************************\
|
| Manifest declarations
|
\****************************************************************************/
static void
printDirectory(
cCommandStream *pThis,
File &dir,
unsigned level,
bool fRecurse
);
/*
Name: ::cmdDir()
Function:
Command dispatcher for "dir" command.
Definition:
McciCatena::cCommandStream::CommandFn cmdDir;
McciCatena::cCommandStream::CommandStatus cmdDir(
cCommandStream *pThis,
void *pContext,
int argc,
char **argv
);
Description:
The "dir" command has the following syntax:
dir
Display the root directory.
dir {path}
Display a directory of path
If "tree" is used instead of "dir", then a recursive
directory listing is produced.
Returns:
cCommandStream::CommandStatus::kSuccess if successful.
Some other value for failure.
*/
// argv[0] is "dir"
// argv[1] is path; if omitted, root is listed
cCommandStream::CommandStatus cmdDir(
cCommandStream *pThis,
void *pContext,
int argc,
char **argv
)
{
const char *sFile;
cCommandStream::CommandStatus result;
if (argc > 2)
return cCommandStream::CommandStatus::kInvalidParameter;
if (argc == 1)
sFile = "/";
else
sFile = argv[1];
bool fHaveCard = gpMeasurementLoopConcrete->checkSdCard();
if (! fHaveCard)
{
pThis->printf("%s: no SD card found\n", argv[0]);
return cCommandStream::CommandStatus::kIoError;
}
auto dir = gSD.open(sFile);
if (! dir)
{
pThis->printf("%s: not found: %s\n", argv[0], sFile);
result = cCommandStream::CommandStatus::kReadError;
}
else
{
printDirectory(pThis, dir, 0, argv[0][0] == 't');
dir.close();
result = cCommandStream::CommandStatus::kSuccess;
}
gpMeasurementLoopConcrete->sdFinish();
return result;
}
static void
printDirectory(
cCommandStream *pThis,
File &dir,
unsigned level,
bool fRecurse
)
{
while (true)
{
File entry = dir.openNextFile();
if (! entry)
return;
pThis->printf("%*s", 4 * level, "");
pThis->printf("%s", entry.name());
if (entry.isDirectory())
{
pThis->printf("/\n");
if (fRecurse)
printDirectory(pThis, entry, level + 1, fRecurse);
}
else
{
pThis->printf("%*s%10u\n", 16 - strlen(entry.name()), "", entry.size());
}
entry.close();
}
}