32
32
33
33
using namespace LittleFilesystem ;
34
34
35
-
36
35
File::File (Adafruit_LittleFS &fs)
37
36
{
38
37
_fs = &fs;
39
- _file = NULL ;
40
- _dir = NULL ;
41
- _path = NULL ;
42
38
_is_dir = false ;
39
+ _opened = false ;
40
+ _name[0 ] = 0 ;
41
+ _dir_path = NULL ;
42
+
43
+ varclr (&_dir);
44
+ varclr (&_file);
43
45
}
44
46
45
47
File::File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
48
+ : File(fs)
46
49
{
47
- _fs = &fs;
48
- _file = NULL ;
49
- _dir = NULL ;
50
- _path = NULL ;
51
- _is_dir = false ;
52
-
53
50
this ->open (filename, mode);
54
51
}
55
52
56
- File& File::operator = (const File &rhs)
57
- {
58
- // close if currently opened
59
- if ( _file ) close ();
60
- memcpy (this , &rhs, sizeof (File));
61
- return *this ;
62
- }
63
-
64
53
File::~File ()
65
54
{
66
-
55
+ if ( _dir_path ) rtos_free (_dir_path);
67
56
}
68
57
69
58
bool File::_open_file (char const *filepath, uint8_t mode)
@@ -73,53 +62,41 @@ bool File::_open_file (char const *filepath, uint8_t mode)
73
62
74
63
if ( flags )
75
64
{
76
- lfs_file_t * fhdl = (lfs_file_t *) rtos_malloc (sizeof (lfs_file_t ));
77
- if (!fhdl) return false ;
78
-
79
- int rc = lfs_file_open (_fs->getFS (), fhdl, filepath, flags);
65
+ int rc = lfs_file_open (_fs->getFS (), &_file, filepath, flags);
80
66
81
67
if ( rc )
82
68
{
83
69
// failed to open
84
- rtos_free (fhdl);
85
70
PRINT_LFS_ERR (rc);
86
-
87
71
return false ;
88
72
}
89
73
90
74
// move to end of file
91
- if ( mode == FILE_WRITE ) lfs_file_seek (_fs->getFS (), fhdl , 0 , LFS_SEEK_END);
75
+ if ( mode == FILE_WRITE ) lfs_file_seek (_fs->getFS (), &_file , 0 , LFS_SEEK_END);
92
76
93
- _file = fhdl ;
77
+ _opened = true ;
94
78
_is_dir = false ;
95
-
96
- _path = (char *) rtos_malloc (strlen (filepath) + 1 );
97
- strcpy (_path, filepath);
98
79
}
99
80
100
81
return true ;
101
82
}
102
83
103
84
bool File::_open_dir (char const *filepath)
104
85
{
105
- File file (*_fs);
106
-
107
- lfs_dir_t * fhdl = (lfs_dir_t *) rtos_malloc (sizeof (lfs_dir_t ));
108
- int rc = lfs_dir_open (_fs->getFS (), fhdl, filepath);
86
+ int rc = lfs_dir_open (_fs->getFS (), &_dir, filepath);
109
87
110
88
if ( rc )
111
89
{
112
90
// failed to open
113
- rtos_free (fhdl);
114
91
PRINT_LFS_ERR (rc);
115
92
return false ;
116
93
}
117
94
118
- _dir = fhdl ;
95
+ _opened = true ;
119
96
_is_dir = true ;
120
97
121
- _path = (char *) rtos_malloc (strlen (filepath) + 1 );
122
- strcpy (_path , filepath);
98
+ _dir_path = (char *) rtos_malloc (strlen (filepath) + 1 );
99
+ strcpy (_dir_path , filepath);
123
100
124
101
return true ;
125
102
}
@@ -129,11 +106,11 @@ bool File::open (char const *filepath, uint8_t mode)
129
106
bool ret = false ;
130
107
131
108
// close if currently opened
132
- if ( _file ) close ();
109
+ if ( _opened ) close ();
133
110
134
111
struct lfs_info info;
135
-
136
112
int rc = lfs_stat (_fs->getFS (), filepath, &info);
113
+
137
114
if ( LFS_ERR_OK == rc )
138
115
{
139
116
// file existed, open file or directory accordingly
@@ -149,6 +126,13 @@ bool File::open (char const *filepath, uint8_t mode)
149
126
PRINT_LFS_ERR (rc);
150
127
}
151
128
129
+ // save bare file name
130
+ if (ret)
131
+ {
132
+ char const * splash = strrchr (filepath, ' /' );
133
+ strncpy (_name, splash ? (splash + 1 ) : filepath, LFS_NAME_MAX);
134
+ }
135
+
152
136
return ret;
153
137
}
154
138
@@ -162,7 +146,7 @@ size_t File::write (uint8_t const *buf, size_t size)
162
146
{
163
147
VERIFY (!_is_dir, 0 );
164
148
165
- lfs_ssize_t wrcount = lfs_file_write (_fs->getFS (), _file, buf, size);
149
+ lfs_ssize_t wrcount = lfs_file_write (_fs->getFS (), & _file, buf, size);
166
150
VERIFY (wrcount > 0 , 0 );
167
151
return wrcount;
168
152
}
@@ -177,7 +161,7 @@ int File::read (void)
177
161
int File::read (void *buf, uint16_t nbyte)
178
162
{
179
163
VERIFY (!_is_dir, 0 );
180
- return lfs_file_read (_fs->getFS (), _file, buf, nbyte);
164
+ return lfs_file_read (_fs->getFS (), & _file, buf, nbyte);
181
165
}
182
166
183
167
int File::peek (void )
@@ -198,64 +182,55 @@ int File::available (void)
198
182
bool File::seek (uint32_t pos)
199
183
{
200
184
VERIFY (!_is_dir, false );
201
- return lfs_file_seek (_fs->getFS (), _file, pos, LFS_SEEK_SET) >= 0 ;
185
+ return lfs_file_seek (_fs->getFS (), & _file, pos, LFS_SEEK_SET) >= 0 ;
202
186
}
203
187
204
188
uint32_t File::position (void )
205
189
{
206
190
VERIFY (!_is_dir, 0 );
207
- return lfs_file_tell (_fs->getFS (), _file);
191
+ return lfs_file_tell (_fs->getFS (), & _file);
208
192
}
209
193
210
194
uint32_t File::size (void )
211
195
{
212
196
VERIFY (!_is_dir, 0 );
213
- return lfs_file_size (_fs->getFS (), _file);
197
+ return lfs_file_size (_fs->getFS (), & _file);
214
198
}
215
199
216
200
void File::flush (void )
217
201
{
218
202
VERIFY (!_is_dir,);
219
- lfs_file_sync (_fs->getFS (), _file);
203
+ lfs_file_sync (_fs->getFS (), & _file);
220
204
}
221
205
222
206
void File::close (void )
223
207
{
224
- if ( _file )
208
+ if ( _opened )
225
209
{
226
210
if ( _is_dir )
227
211
{
228
- lfs_dir_close (_fs->getFS (), _dir);
212
+ lfs_dir_close (_fs->getFS (), & _dir);
229
213
}
230
214
else
231
215
{
232
- lfs_file_close (_fs->getFS (), _file);
216
+ lfs_file_close (_fs->getFS (), & _file);
233
217
}
234
-
235
- rtos_free (_file);
236
218
}
237
219
238
- if ( _path ) rtos_free (_path) ;
220
+ _opened = false ;
239
221
240
- _file = NULL ;
241
- _path = NULL ;
222
+ if ( _dir_path ) rtos_free (_dir_path) ;
223
+ _dir_path = NULL ;
242
224
}
243
225
244
226
File::operator bool (void )
245
227
{
246
- return _file != NULL ;
228
+ return _opened ;
247
229
}
248
230
249
231
char const * File::name (void )
250
232
{
251
- // return barename only
252
- char * barename = strrchr (_path, ' /' );
253
- return barename ? (barename + 1 ) : _path;
254
- }
255
-
256
- char const * File::path (void )
257
- {
258
- return _path;
233
+ return _name;
259
234
}
260
235
261
236
bool File::isDirectory (void )
@@ -277,16 +252,16 @@ File File::openNextFile (uint8_t mode)
277
252
// skip "." and ".." entries
278
253
do
279
254
{
280
- rc = lfs_dir_read (_fs->getFS (), _dir, &info);
255
+ rc = lfs_dir_read (_fs->getFS (), & _dir, &info);
281
256
} while ( rc == 1 && (!strcmp (" ." , info.name ) || !strcmp (" .." , info.name )) );
282
257
283
258
if ( rc == 1 )
284
259
{
285
260
// string cat name with current folder
286
- char filepath[strlen (_path ) + 1 + strlen (info.name ) + 1 ];
261
+ char filepath[strlen (_dir_path ) + 1 + strlen (info.name ) + 1 ];
287
262
288
- strcpy (filepath, _path );
289
- if ( !(_path [0 ] == ' /' && _path [1 ] == 0 ) ) strcat (filepath, " /" ); // only add '/' if cwd is not root
263
+ strcpy (filepath, _dir_path );
264
+ if ( !(_dir_path [0 ] == ' /' && _dir_path [1 ] == 0 ) ) strcat (filepath, " /" ); // only add '/' if cwd is not root
290
265
strcat (filepath, info.name );
291
266
292
267
file.open (filepath, mode);
@@ -301,5 +276,5 @@ File File::openNextFile (uint8_t mode)
301
276
302
277
void File::rewindDirectory (void )
303
278
{
304
- VERIFY_LFS (lfs_dir_rewind (_fs->getFS (), _dir),);
279
+ VERIFY_LFS (lfs_dir_rewind (_fs->getFS (), & _dir),);
305
280
}
0 commit comments