@@ -134,7 +134,7 @@ File SDClass::open(const char *filepath)
134
134
{
135
135
File file = File (filepath);
136
136
137
- if (f_open (& file._fil , filepath, FA_READ) != FR_OK)
137
+ if (f_open (file._fil , filepath, FA_READ) != FR_OK)
138
138
{
139
139
f_opendir (&file._dir , filepath);
140
140
}
@@ -156,7 +156,7 @@ File SDClass::open(const char *filepath, uint8_t mode)
156
156
mode = mode | FA_CREATE_ALWAYS;
157
157
}
158
158
159
- if (f_open (& file._fil , filepath, mode) != FR_OK)
159
+ if (f_open (file._fil , filepath, mode) != FR_OK)
160
160
{
161
161
f_opendir (&file._dir , filepath);
162
162
}
@@ -194,16 +194,20 @@ File SDClass::openRoot(void)
194
194
File::File ()
195
195
{
196
196
_name = NULL ;
197
- _fil.fs = 0 ;
198
- _dir.fs = 0 ;
197
+ _fil = (FIL*)malloc (sizeof (FIL));
198
+ assert (_fil != NULL );
199
+ _fil->fs = 0 ;
200
+ _dir.fs = 0 ;
199
201
}
200
202
201
203
File::File (const char * name)
202
204
{
203
205
_name = (char *)malloc (strlen (name) +1 );
204
206
assert (_name != NULL );
205
207
sprintf (_name, " %s" , name);
206
- _fil.fs = 0 ;
208
+ _fil = (FIL*)malloc (sizeof (FIL));
209
+ assert (_fil != NULL );
210
+ _fil->fs = 0 ;
207
211
_dir.fs = 0 ;
208
212
}
209
213
@@ -345,7 +349,7 @@ int File::read()
345
349
{
346
350
uint8_t byteread;
347
351
int8_t data;
348
- f_read (& _fil, (void *)&data, 1 , (UINT *)&byteread);
352
+ f_read (_fil, (void *)&data, 1 , (UINT *)&byteread);
349
353
return data;
350
354
}
351
355
@@ -359,7 +363,7 @@ int File::read(void* buf, size_t len)
359
363
{
360
364
uint8_t bytesread;
361
365
362
- f_read (& _fil, buf, len, (UINT *)&bytesread);
366
+ f_read (_fil, buf, len, (UINT *)&bytesread);
363
367
return bytesread;
364
368
365
369
}
@@ -373,12 +377,13 @@ void File::close()
373
377
{
374
378
if (_name)
375
379
{
376
- if (_fil. fs != 0 ) {
380
+ if (_fil && _fil-> fs != 0 ) {
377
381
/* Flush the file before close */
378
- f_sync (& _fil);
382
+ f_sync (_fil);
379
383
380
384
/* Close the file */
381
- f_close (&_fil);
385
+ f_close (_fil);
386
+ free (_fil);
382
387
}
383
388
384
389
if (_dir.fs != 0 ) {
@@ -397,7 +402,7 @@ void File::close()
397
402
*/
398
403
void File::flush ()
399
404
{
400
- f_sync (& _fil);
405
+ f_sync (_fil);
401
406
}
402
407
403
408
/* *
@@ -421,7 +426,7 @@ int File::peek()
421
426
uint32_t File::position ()
422
427
{
423
428
uint32_t filepos = 0 ;
424
- filepos = f_tell (& _fil);
429
+ filepos = f_tell (_fil);
425
430
return filepos;
426
431
}
427
432
@@ -438,7 +443,7 @@ uint8_t File::seek(uint32_t pos)
438
443
}
439
444
else
440
445
{
441
- if (f_lseek (& _fil, pos) != FR_OK)
446
+ if (f_lseek (_fil, pos) != FR_OK)
442
447
{
443
448
return FALSE ;
444
449
}
@@ -458,12 +463,12 @@ uint32_t File::size()
458
463
{
459
464
uint32_t file_size = 0 ;
460
465
461
- file_size = f_size (& _fil);
466
+ file_size = f_size (_fil);
462
467
return (file_size);
463
468
}
464
469
465
470
File::operator bool () {
466
- return ((_name == NULL ) || ((_fil. fs == 0 ) && (_dir.fs == 0 ))) ? FALSE : TRUE ;
471
+ return ((_name == NULL ) || ((_fil == NULL ) && (_dir. fs == 0 )) || ((_fil-> fs == 0 ) && (_dir.fs == 0 ))) ? FALSE : TRUE ;
467
472
}
468
473
/* *
469
474
* @brief Write data to the file
@@ -484,7 +489,7 @@ size_t File::write(uint8_t data)
484
489
size_t File::write (const char *buf, size_t size)
485
490
{
486
491
size_t byteswritten;
487
- f_write (& _fil, (const void *)buf, size, (UINT *)&byteswritten);
492
+ f_write (_fil, (const void *)buf, size, (UINT *)&byteswritten);
488
493
return byteswritten;
489
494
}
490
495
@@ -563,7 +568,7 @@ uint8_t File::isDirectory()
563
568
assert (_name != NULL );
564
569
if (_dir.fs != 0 )
565
570
return TRUE ;
566
- else if (_fil. fs != 0 )
571
+ else if (_fil-> fs != 0 )
567
572
return FALSE ;
568
573
// if not init get info
569
574
if (f_stat (_name, &fno) == FR_OK)
0 commit comments