strat des robots
Fork of CRAC-Strat_2017 by
mbed/LocalFileSystem.h@17:d1594579eec6, 2017-05-19 (annotated)
- Committer:
- ClementBreteau
- Date:
- Fri May 19 17:14:07 2017 +0000
- Revision:
- 17:d1594579eec6
- Parent:
- 0:ad97421fb1fb
strat du robot, 19-05-2017, 19h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
antbig | 0:ad97421fb1fb | 1 | /* mbed Microcontroller Library - LocalFileSystem |
antbig | 0:ad97421fb1fb | 2 | * Copyright (c) 2008-2009 ARM Limited. All rights reserved. |
antbig | 0:ad97421fb1fb | 3 | */ |
antbig | 0:ad97421fb1fb | 4 | |
antbig | 0:ad97421fb1fb | 5 | #ifndef MBED_LOCALFILESYSTEM_H |
antbig | 0:ad97421fb1fb | 6 | #define MBED_LOCALFILESYSTEM_H |
antbig | 0:ad97421fb1fb | 7 | |
antbig | 0:ad97421fb1fb | 8 | #include "FileSystemLike.h" |
antbig | 0:ad97421fb1fb | 9 | |
antbig | 0:ad97421fb1fb | 10 | namespace mbed { |
antbig | 0:ad97421fb1fb | 11 | |
antbig | 0:ad97421fb1fb | 12 | FILEHANDLE local_file_open(const char* name, int flags); |
antbig | 0:ad97421fb1fb | 13 | |
antbig | 0:ad97421fb1fb | 14 | class LocalFileHandle : public FileHandle { |
antbig | 0:ad97421fb1fb | 15 | |
antbig | 0:ad97421fb1fb | 16 | public: |
antbig | 0:ad97421fb1fb | 17 | LocalFileHandle(FILEHANDLE fh); |
antbig | 0:ad97421fb1fb | 18 | |
antbig | 0:ad97421fb1fb | 19 | virtual int close(); |
antbig | 0:ad97421fb1fb | 20 | |
antbig | 0:ad97421fb1fb | 21 | virtual ssize_t write(const void *buffer, size_t length); |
antbig | 0:ad97421fb1fb | 22 | |
antbig | 0:ad97421fb1fb | 23 | virtual ssize_t read(void *buffer, size_t length); |
antbig | 0:ad97421fb1fb | 24 | |
antbig | 0:ad97421fb1fb | 25 | virtual int isatty(); |
antbig | 0:ad97421fb1fb | 26 | |
antbig | 0:ad97421fb1fb | 27 | virtual off_t lseek(off_t position, int whence); |
antbig | 0:ad97421fb1fb | 28 | |
antbig | 0:ad97421fb1fb | 29 | virtual int fsync(); |
antbig | 0:ad97421fb1fb | 30 | |
antbig | 0:ad97421fb1fb | 31 | virtual off_t flen(); |
antbig | 0:ad97421fb1fb | 32 | |
antbig | 0:ad97421fb1fb | 33 | protected: |
antbig | 0:ad97421fb1fb | 34 | FILEHANDLE _fh; |
antbig | 0:ad97421fb1fb | 35 | int pos; |
antbig | 0:ad97421fb1fb | 36 | }; |
antbig | 0:ad97421fb1fb | 37 | |
antbig | 0:ad97421fb1fb | 38 | /* Class: LocalFileSystem |
antbig | 0:ad97421fb1fb | 39 | * A filesystem for accessing the local mbed Microcontroller USB disk drive |
antbig | 0:ad97421fb1fb | 40 | * |
antbig | 0:ad97421fb1fb | 41 | * This allows programs to read and write files on the same disk drive that is used to program the |
antbig | 0:ad97421fb1fb | 42 | * mbed Microcontroller. Once created, the standard C file access functions are used to open, |
antbig | 0:ad97421fb1fb | 43 | * read and write files. |
antbig | 0:ad97421fb1fb | 44 | * |
antbig | 0:ad97421fb1fb | 45 | * Example: |
antbig | 0:ad97421fb1fb | 46 | * > #include "mbed.h" |
antbig | 0:ad97421fb1fb | 47 | * > |
antbig | 0:ad97421fb1fb | 48 | * > LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
antbig | 0:ad97421fb1fb | 49 | * > |
antbig | 0:ad97421fb1fb | 50 | * > int main() { |
antbig | 0:ad97421fb1fb | 51 | * > FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing |
antbig | 0:ad97421fb1fb | 52 | * > fprintf(fp, "Hello World!"); |
antbig | 0:ad97421fb1fb | 53 | * > fclose(fp); |
antbig | 0:ad97421fb1fb | 54 | * > remove("/local/out.txt"); // Removes the file "out.txt" from the local file system |
antbig | 0:ad97421fb1fb | 55 | * > |
antbig | 0:ad97421fb1fb | 56 | * > DIR *d = opendir("/local"); // Opens the root directory of the local file system |
antbig | 0:ad97421fb1fb | 57 | * > struct dirent *p; |
antbig | 0:ad97421fb1fb | 58 | * > while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system |
antbig | 0:ad97421fb1fb | 59 | * > printf("%s\n", p->d_name); // to stdout. |
antbig | 0:ad97421fb1fb | 60 | * > } |
antbig | 0:ad97421fb1fb | 61 | * > closedir(d); |
antbig | 0:ad97421fb1fb | 62 | * > } |
antbig | 0:ad97421fb1fb | 63 | * |
antbig | 0:ad97421fb1fb | 64 | * Implementation Notes: |
antbig | 0:ad97421fb1fb | 65 | * If the microcontroller program makes an access to the local drive, it will be marked as "removed" |
antbig | 0:ad97421fb1fb | 66 | * on the Host computer. This means it is no longer accessible from the Host Computer. |
antbig | 0:ad97421fb1fb | 67 | * |
antbig | 0:ad97421fb1fb | 68 | * The drive will only re-appear when the microcontroller program exists. Note that if the program does |
antbig | 0:ad97421fb1fb | 69 | * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again! |
antbig | 0:ad97421fb1fb | 70 | */ |
antbig | 0:ad97421fb1fb | 71 | class LocalFileSystem : public FileSystemLike { |
antbig | 0:ad97421fb1fb | 72 | |
antbig | 0:ad97421fb1fb | 73 | public: |
antbig | 0:ad97421fb1fb | 74 | |
antbig | 0:ad97421fb1fb | 75 | LocalFileSystem(const char* n) : FileSystemLike(n) { |
antbig | 0:ad97421fb1fb | 76 | |
antbig | 0:ad97421fb1fb | 77 | } |
antbig | 0:ad97421fb1fb | 78 | |
antbig | 0:ad97421fb1fb | 79 | virtual FileHandle *open(const char* name, int flags); |
antbig | 0:ad97421fb1fb | 80 | virtual int remove(const char *filename); |
antbig | 0:ad97421fb1fb | 81 | virtual DirHandle *opendir(const char *name); |
antbig | 0:ad97421fb1fb | 82 | }; |
antbig | 0:ad97421fb1fb | 83 | |
antbig | 0:ad97421fb1fb | 84 | } // namespace mbed |
antbig | 0:ad97421fb1fb | 85 | |
antbig | 0:ad97421fb1fb | 86 | #endif |