@@ -65,11 +65,12 @@ SDClass SD;
6565 */
6666bool SDClass::begin (uint32_t detect, uint32_t level)
6767{
68+ bool status = false ;
6869 /* ##-1- Initializes SD IOs #############################################*/
6970 if (_card.init (detect, level)) {
70- return _fatFs.init ();
71+ status = _fatFs.init ();
7172 }
72- return false ;
73+ return status ;
7374}
7475
7576/* *
@@ -79,11 +80,12 @@ bool SDClass::begin(uint32_t detect, uint32_t level)
7980 */
8081bool SDClass::end (void )
8182{
83+ bool status = false ;
8284 /* ##-1- DeInitializes SD IOs ###########################################*/
8385 if (_fatFs.deinit ()) {
84- return _card.deinit ();
86+ status = _card.deinit ();
8587 }
86- return false ;
88+ return status ;
8789}
8890
8991/* *
@@ -95,11 +97,7 @@ bool SDClass::exists(const char *filepath)
9597{
9698 FILINFO fno;
9799
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 ;
103101}
104102
105103/* *
@@ -110,11 +108,7 @@ bool SDClass::exists(const char *filepath)
110108bool SDClass::mkdir (const char *filepath)
111109{
112110 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 ;
118112}
119113
120114/* *
@@ -124,11 +118,7 @@ bool SDClass::mkdir(const char *filepath)
124118 */
125119bool SDClass::rmdir (const char *filepath)
126120{
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 ;
132122}
133123
134124/* *
@@ -184,11 +174,7 @@ File SDClass::open(const char *filepath, uint8_t mode /* = FA_READ */)
184174 */
185175bool SDClass::remove (const char *filepath)
186176{
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 ;
192178}
193179
194180File SDClass::openRoot (void )
@@ -344,10 +330,7 @@ int File::read()
344330{
345331 UINT byteread;
346332 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 ;
351334}
352335
353336/* *
@@ -359,11 +342,7 @@ int File::read()
359342int File::read (void *buf, size_t len)
360343{
361344 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 ;
367346}
368347
369348/* *
@@ -447,15 +426,11 @@ uint32_t File::position()
447426 */
448427bool File::seek (uint32_t pos)
449428{
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 ;
458432 }
433+ return status;
459434}
460435
461436/* *
@@ -529,10 +504,12 @@ char *File::name()
529504
530505/* *
531506 * @brief Check if the file is directory or normal file
532- * @retval TRUE if directory else FALSE
507+ * @retval true if directory else false
533508 */
534509bool File::isDirectory ()
535510{
511+ // Assume not a directory
512+ bool status = false ;
536513 FILINFO fno;
537514 if (_name == NULL ) {
538515 Error_Handler ();
@@ -542,21 +519,25 @@ bool File::isDirectory()
542519#else
543520 if (_dir.fs != 0 )
544521#endif
545- return true ;
522+ {
523+ status = true ;
524+ }
546525#if (_FATFS == 68300) || (_FATFS == 80286)
547526 else if (_fil->obj .fs != 0 )
548527#else
549528 else if (_fil->fs != 0 )
550529#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+ }
556538 }
557539 }
558- // Assume not a directory
559- return false ;
540+ return status;
560541}
561542
562543File File::openNextFile (uint8_t mode)
@@ -569,35 +550,41 @@ File File::openNextFile(uint8_t mode)
569550 fno.lfname = lfn;
570551 fno.lfsize = sizeof (lfn);
571552#endif
572- while (1 ) {
553+ bool found = false ;
554+ File filtmp = File ();
555+ while (!found) {
573556 res = f_readdir (&_dir, &fno);
574557 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+ }
580564#if _USE_LFN && (_FATFS != 68300 && _FATFS != 80286)
581- fn = *fno.lfname ? fno.lfname : fno.fname ;
565+ fn = *fno.lfname ? fno.lfname : fno.fname ;
582566#else
583- fn = fno.fname ;
567+ fn = fno.fname ;
584568#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 ;
591581 } else {
592- sprintf (fullPath, " %s/%s" , _name, fn);
582+ filtmp._res = FR_NOT_ENOUGH_CORE;
583+ found = true ;
593584 }
594- File filtmp = SD.open (fullPath, mode);
595- free (fullPath);
596- return filtmp;
597- } else {
598- return File (FR_NOT_ENOUGH_CORE);
599585 }
600586 }
587+ return filtmp;
601588}
602589
603590void File::rewindDirectory (void )
0 commit comments