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