00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "SD.h"
00054
00055
00056 #define MAX_COMPONENT_LEN 12 // What is max length?
00057 #define PATH_COMPONENT_BUFFER_LEN MAX_COMPONENT_LEN+1
00058
00059 bool getNextPathComponent(char *path, unsigned int *p_offset,
00060 char *buffer) {
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 int bufferOffset = 0;
00088
00089 int offset = *p_offset;
00090
00091
00092 if (path[offset] == '/') {
00093 offset++;
00094 }
00095
00096
00097 while (bufferOffset < MAX_COMPONENT_LEN
00098 && (path[offset] != '/')
00099 && (path[offset] != '\0')) {
00100 buffer[bufferOffset++] = path[offset++];
00101 }
00102
00103 buffer[bufferOffset] = '\0';
00104
00105
00106
00107 if (path[offset] == '/') {
00108 offset++;
00109 }
00110
00111 *p_offset = offset;
00112
00113 return (path[offset] != '\0');
00114 }
00115
00116
00117
00118 boolean walkPath(char *filepath, SdFile& parentDir,
00119 boolean (*callback)(SdFile& parentDir,
00120 char *filePathComponent,
00121 boolean isLastComponent,
00122 void *object),
00123 void *object = NULL) {
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 SdFile subfile1;
00154 SdFile subfile2;
00155
00156 char buffer[PATH_COMPONENT_BUFFER_LEN];
00157
00158 unsigned int offset = 0;
00159
00160 SdFile *p_parent;
00161 SdFile *p_child;
00162
00163 SdFile *p_tmp_sdfile;
00164
00165 p_child = &subfile1;
00166
00167 p_parent = &parentDir;
00168
00169 while (true) {
00170
00171 boolean moreComponents = getNextPathComponent(filepath, &offset, buffer);
00172
00173 boolean shouldContinue = callback((*p_parent), buffer, !moreComponents, object);
00174
00175 if (!shouldContinue) {
00176
00177
00178
00179 if (p_parent != &parentDir) {
00180 (*p_parent).close();
00181 }
00182 return false;
00183 }
00184
00185 if (!moreComponents) {
00186 break;
00187 }
00188
00189 boolean exists = (*p_child).open(*p_parent, buffer, O_RDONLY);
00190
00191
00192
00193 if (p_parent != &parentDir) {
00194 (*p_parent).close();
00195 }
00196
00197
00198 if (exists) {
00199
00200
00201 if (p_parent == &parentDir) {
00202 p_parent = &subfile2;
00203 }
00204
00205 p_tmp_sdfile = p_parent;
00206 p_parent = p_child;
00207 p_child = p_tmp_sdfile;
00208 } else {
00209 return false;
00210 }
00211 }
00212
00213 if (p_parent != &parentDir) {
00214 (*p_parent).close();
00215 }
00216
00217 return true;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 boolean callback_pathExists(SdFile& parentDir, char *filePathComponent,
00234 boolean isLastComponent, void *object) {
00235
00236
00237
00238
00239
00240
00241
00242
00243 SdFile child;
00244
00245 boolean exists = child.open(parentDir, filePathComponent, O_RDONLY);
00246
00247 if (exists) {
00248 child.close();
00249 }
00250
00251 return exists;
00252 }
00253
00254
00255
00256 boolean callback_makeDirPath(SdFile& parentDir, char *filePathComponent,
00257 boolean isLastComponent, void *object) {
00258
00259
00260
00261
00262
00263
00264
00265
00266 boolean result = false;
00267 SdFile child;
00268
00269 result = callback_pathExists(parentDir, filePathComponent, isLastComponent, object);
00270 if (!result) {
00271 result = child.makeDir(parentDir, filePathComponent);
00272 }
00273
00274 return result;
00275 }
00276
00277
00278
00279 boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
00280 boolean isLastComponent, void *object) {
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 if (isLastComponent) {
00298 SDClass *p_SD = static_cast<SDClass*>(object);
00299 p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
00300 if (p_SD->fileOpenMode == FILE_WRITE) {
00301 p_SD->file.seekSet(p_SD->file.fileSize());
00302 }
00303
00304 return false;
00305 }
00306 return true;
00307 }
00308
00309
00310 boolean callback_remove(SdFile& parentDir, char *filePathComponent,
00311 boolean isLastComponent, void *object) {
00312 if (isLastComponent) {
00313 return SdFile::remove(parentDir, filePathComponent);
00314 }
00315 return true;
00316 }
00317
00318 boolean callback_rmdir(SdFile& parentDir, char *filePathComponent,
00319 boolean isLastComponent, void *object) {
00320 if (isLastComponent) {
00321 SdFile f;
00322 if (!f.open(parentDir, filePathComponent, O_READ)) return false;
00323 return f.rmDir();
00324 }
00325 return true;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334 boolean SDClass::begin(uint8_t csPin) {
00335
00336
00337
00338
00339
00340
00341
00342 return card.init(SPI_HALF_SPEED, csPin) &&
00343 volume.init(card) &&
00344 root.openRoot(volume);
00345 }
00346
00347
00348 File SDClass::open(char *filepath, uint8_t mode) {
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 fileOpenMode = mode;
00375 walkPath(filepath, root, callback_openPath, this);
00376
00377 return File();
00378
00379 }
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 boolean SDClass::exists(char *filepath) {
00393
00394
00395
00396
00397
00398 return walkPath(filepath, root, callback_pathExists);
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 boolean SDClass::mkdir(char *filepath) {
00414
00415
00416
00417
00418
00419
00420
00421 return walkPath(filepath, root, callback_makeDirPath);
00422 }
00423
00424 boolean SDClass::rmdir(char *filepath) {
00425
00426
00427
00428
00429
00430
00431
00432 return walkPath(filepath, root, callback_rmdir);
00433 }
00434
00435 boolean SDClass::remove(char *filepath) {
00436 return walkPath(filepath, root, callback_remove);
00437 }
00438
00439 SDClass SD;