@@ -65,11 +65,12 @@ SDClass SD;
65
65
*/
66
66
bool SDClass::begin (uint32_t detect, uint32_t level)
67
67
{
68
+ bool status = false ;
68
69
/* ##-1- Initializes SD IOs #############################################*/
69
70
if (_card.init (detect, level)) {
70
- return _fatFs.init ();
71
+ status = _fatFs.init ();
71
72
}
72
- return false ;
73
+ return status ;
73
74
}
74
75
75
76
/* *
@@ -79,11 +80,12 @@ bool SDClass::begin(uint32_t detect, uint32_t level)
79
80
*/
80
81
bool SDClass::end (void )
81
82
{
83
+ bool status = false ;
82
84
/* ##-1- DeInitializes SD IOs ###########################################*/
83
85
if (_fatFs.deinit ()) {
84
- return _card.deinit ();
86
+ status = _card.deinit ();
85
87
}
86
- return false ;
88
+ return status ;
87
89
}
88
90
89
91
/* *
@@ -95,11 +97,7 @@ bool SDClass::exists(const char *filepath)
95
97
{
96
98
FILINFO fno;
97
99
98
- if (f_stat (filepath, &fno) != FR_OK) {
99
- return false ;
100
- } else {
101
- return true ;
102
- }
100
+ return (f_stat (filepath, &fno) != FR_OK) ? false : true ;
103
101
}
104
102
105
103
/* *
@@ -110,11 +108,7 @@ bool SDClass::exists(const char *filepath)
110
108
bool SDClass::mkdir (const char *filepath)
111
109
{
112
110
FRESULT res = f_mkdir (filepath);
113
- if ((res != FR_OK) && (res != FR_EXIST)) {
114
- return false ;
115
- } else {
116
- return true ;
117
- }
111
+ return ((res != FR_OK) && (res != FR_EXIST)) ? false : true ;
118
112
}
119
113
120
114
/* *
@@ -124,11 +118,7 @@ bool SDClass::mkdir(const char *filepath)
124
118
*/
125
119
bool SDClass::rmdir (const char *filepath)
126
120
{
127
- if (f_unlink (filepath) != FR_OK) {
128
- return false ;
129
- } else {
130
- return true ;
131
- }
121
+ return (f_unlink (filepath) != FR_OK) ? false : true ;
132
122
}
133
123
134
124
/* *
@@ -184,11 +174,7 @@ File SDClass::open(const char *filepath, uint8_t mode /* = FA_READ */)
184
174
*/
185
175
bool SDClass::remove (const char *filepath)
186
176
{
187
- if (f_unlink (filepath) != FR_OK) {
188
- return false ;
189
- } else {
190
- return true ;
191
- }
177
+ return (f_unlink (filepath) != FR_OK) ? false : true ;
192
178
}
193
179
194
180
File SDClass::openRoot (void )
@@ -344,10 +330,7 @@ int File::read()
344
330
{
345
331
UINT byteread;
346
332
int8_t data;
347
- if (f_read (_fil, (void *)&data, 1 , (UINT *)&byteread) == FR_OK) {
348
- return data;
349
- }
350
- return -1 ;
333
+ return (f_read (_fil, (void *)&data, 1 , (UINT *)&byteread) == FR_OK) ? data : -1 ;
351
334
}
352
335
353
336
/* *
@@ -359,11 +342,7 @@ int File::read()
359
342
int File::read (void *buf, size_t len)
360
343
{
361
344
UINT bytesread;
362
-
363
- if (f_read (_fil, buf, len, (UINT *)&bytesread) == FR_OK) {
364
- return bytesread;
365
- }
366
- return -1 ;
345
+ return (f_read (_fil, buf, len, (UINT *)&bytesread) == FR_OK) ? bytesread : -1 ;
367
346
}
368
347
369
348
/* *
@@ -447,15 +426,11 @@ uint32_t File::position()
447
426
*/
448
427
bool File::seek (uint32_t pos)
449
428
{
450
- if (pos > size ()) {
451
- return false ;
452
- } else {
453
- if (f_lseek (_fil, pos) != FR_OK) {
454
- return false ;
455
- } else {
456
- return true ;
457
- }
429
+ bool status = false ;
430
+ if (pos <= size ()) {
431
+ status = (f_lseek (_fil, pos) != FR_OK) ? false : true ;
458
432
}
433
+ return status;
459
434
}
460
435
461
436
/* *
@@ -529,10 +504,12 @@ char *File::name()
529
504
530
505
/* *
531
506
* @brief Check if the file is directory or normal file
532
- * @retval TRUE if directory else FALSE
507
+ * @retval true if directory else false
533
508
*/
534
509
bool File::isDirectory ()
535
510
{
511
+ // Assume not a directory
512
+ bool status = false ;
536
513
FILINFO fno;
537
514
if (_name == NULL ) {
538
515
Error_Handler ();
@@ -542,21 +519,25 @@ bool File::isDirectory()
542
519
#else
543
520
if (_dir.fs != 0 )
544
521
#endif
545
- return true ;
522
+ {
523
+ status = true ;
524
+ }
546
525
#if (_FATFS == 68300) || (_FATFS == 80286)
547
526
else if (_fil->obj .fs != 0 )
548
527
#else
549
528
else if (_fil->fs != 0 )
550
529
#endif
551
- return false ;
552
- // if not init get info
553
- if (f_stat (_name, &fno) == FR_OK) {
554
- if (fno.fattrib & AM_DIR) {
555
- return true ;
530
+ {
531
+ status = false ;
532
+ } else {
533
+ // if not init get info
534
+ if (f_stat (_name, &fno) == FR_OK) {
535
+ if (fno.fattrib & AM_DIR) {
536
+ status = true ;
537
+ }
556
538
}
557
539
}
558
- // Assume not a directory
559
- return false ;
540
+ return status;
560
541
}
561
542
562
543
File File::openNextFile (uint8_t mode)
@@ -569,35 +550,41 @@ File File::openNextFile(uint8_t mode)
569
550
fno.lfname = lfn;
570
551
fno.lfsize = sizeof (lfn);
571
552
#endif
572
- while (1 ) {
553
+ bool found = false ;
554
+ File filtmp = File ();
555
+ while (!found) {
573
556
res = f_readdir (&_dir, &fno);
574
557
if (res != FR_OK || fno.fname [0 ] == 0 ) {
575
- return File (res);
576
- }
577
- if (fno.fname [0 ] == ' .' ) {
578
- continue ;
579
- }
558
+ filtmp._res = res;
559
+ found = true ;
560
+ } else {
561
+ if (fno.fname [0 ] == ' .' ) {
562
+ continue ;
563
+ }
580
564
#if _USE_LFN && (_FATFS != 68300 && _FATFS != 80286)
581
- fn = *fno.lfname ? fno.lfname : fno.fname ;
565
+ fn = *fno.lfname ? fno.lfname : fno.fname ;
582
566
#else
583
- fn = fno.fname ;
567
+ fn = fno.fname ;
584
568
#endif
585
- size_t name_len = strlen (_name);
586
- char *fullPath = (char *)malloc (name_len + strlen (fn) + 2 );
587
- if (fullPath != NULL ) {
588
- // Avoid twice '/'
589
- if ((name_len > 0 ) && (_name[name_len - 1 ] == ' /' )) {
590
- sprintf (fullPath, " %s%s" , _name, fn);
569
+ size_t name_len = strlen (_name);
570
+ char *fullPath = (char *)malloc (name_len + strlen (fn) + 2 );
571
+ if (fullPath != NULL ) {
572
+ // Avoid twice '/'
573
+ if ((name_len > 0 ) && (_name[name_len - 1 ] == ' /' )) {
574
+ sprintf (fullPath, " %s%s" , _name, fn);
575
+ } else {
576
+ sprintf (fullPath, " %s/%s" , _name, fn);
577
+ }
578
+ filtmp = SD.open (fullPath, mode);
579
+ free (fullPath);
580
+ found = true ;
591
581
} else {
592
- sprintf (fullPath, " %s/%s" , _name, fn);
582
+ filtmp._res = FR_NOT_ENOUGH_CORE;
583
+ found = true ;
593
584
}
594
- File filtmp = SD.open (fullPath, mode);
595
- free (fullPath);
596
- return filtmp;
597
- } else {
598
- return File (FR_NOT_ENOUGH_CORE);
599
585
}
600
586
}
587
+ return filtmp;
601
588
}
602
589
603
590
void File::rewindDirectory (void )
0 commit comments