@@ -66,42 +66,41 @@ File::~File ()
66
66
67
67
}
68
68
69
- File File::_open_file (char const *filepath, uint8_t mode)
69
+ bool File::_open_file (char const *filepath, uint8_t mode)
70
70
{
71
- File file (*_fs);
72
-
73
71
int flags = (mode == FILE_READ) ? LFS_O_RDONLY :
74
72
(mode == FILE_WRITE) ? (LFS_O_RDWR | LFS_O_CREAT) : 0 ;
75
73
76
74
if ( flags )
77
75
{
78
76
lfs_file_t * fhdl = (lfs_file_t *) rtos_malloc (sizeof (lfs_file_t ));
79
- VERIFY ( fhdl, file) ;
77
+ if (! fhdl) return false ;
80
78
81
79
int rc = lfs_file_open (_fs->getFS (), fhdl, filepath, flags);
82
80
83
81
if ( rc )
84
82
{
83
+ // failed to open
85
84
rtos_free (fhdl);
86
85
PRINT_LFS_ERR (rc);
86
+
87
+ return false ;
87
88
}
88
- else
89
- {
90
- // move to end of file
91
- if ( mode == FILE_WRITE ) lfs_file_seek (_fs->getFS (), fhdl, 0 , LFS_SEEK_END);
92
89
93
- file. _file = fhdl;
94
- file. _is_dir = false ;
90
+ // move to end of file
91
+ if ( mode == FILE_WRITE ) lfs_file_seek (_fs-> getFS (), fhdl, 0 , LFS_SEEK_END) ;
95
92
96
- file._path = (char *) rtos_malloc (strlen (filepath) + 1 );
97
- strcpy (file._path , filepath);
98
- }
93
+ _file = fhdl;
94
+ _is_dir = false ;
95
+
96
+ _path = (char *) rtos_malloc (strlen (filepath) + 1 );
97
+ strcpy (_path, filepath);
99
98
}
100
99
101
- return file ;
100
+ return true ;
102
101
}
103
102
104
- File File::_open_dir (char const *filepath)
103
+ bool File::_open_dir (char const *filepath)
105
104
{
106
105
File file (*_fs);
107
106
@@ -110,23 +109,25 @@ File File::_open_dir (char const *filepath)
110
109
111
110
if ( rc )
112
111
{
112
+ // failed to open
113
113
rtos_free (fhdl);
114
114
PRINT_LFS_ERR (rc);
115
+ return false ;
115
116
}
116
- else
117
- {
118
- file._dir = fhdl;
119
- file._is_dir = true ;
120
117
121
- file._path = (char *) rtos_malloc (strlen (filepath) + 1 );
122
- strcpy (file._path , filepath);
123
- }
118
+ _dir = fhdl;
119
+ _is_dir = true ;
124
120
125
- return file;
121
+ _path = (char *) rtos_malloc (strlen (filepath) + 1 );
122
+ strcpy (_path, filepath);
123
+
124
+ return true ;
126
125
}
127
126
128
127
bool File::open (char const *filepath, uint8_t mode)
129
128
{
129
+ bool ret = false ;
130
+
130
131
// close if currently opened
131
132
if ( _file ) close ();
132
133
@@ -136,19 +137,19 @@ bool File::open (char const *filepath, uint8_t mode)
136
137
if ( LFS_ERR_OK == rc )
137
138
{
138
139
// file existed, open file or directory accordingly
139
- * this = (info.type == LFS_TYPE_REG) ? _open_file (filepath, mode) : _open_dir (filepath);
140
+ ret = (info.type == LFS_TYPE_REG) ? _open_file (filepath, mode) : _open_dir (filepath);
140
141
}
141
142
else if ( LFS_ERR_NOENT == rc )
142
143
{
143
144
// file not existed, only proceed with FILE_WRITE mode
144
- if ( mode == FILE_WRITE ) * this = _open_file (filepath, mode);
145
+ if ( mode == FILE_WRITE ) ret = _open_file (filepath, mode);
145
146
}
146
147
else
147
148
{
148
149
PRINT_LFS_ERR (rc);
149
150
}
150
151
151
- return _file != NULL ;
152
+ return ret ;
152
153
}
153
154
154
155
size_t File::write (uint8_t ch)
0 commit comments