Dependencies: PinDetect TextLCD mbed mRotaryEncoder
FATFileSystem/Interface/FATFileSystem.cpp@0:afb2650fb49a, 2012-02-13 (annotated)
- Committer:
- cicklaus
- Date:
- Mon Feb 13 02:11:20 2012 +0000
- Revision:
- 0:afb2650fb49a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cicklaus | 0:afb2650fb49a | 1 | /* mbed Microcontroller Library - FATFileSystem |
cicklaus | 0:afb2650fb49a | 2 | Copyright (c) 2008, sford */ |
cicklaus | 0:afb2650fb49a | 3 | |
cicklaus | 0:afb2650fb49a | 4 | //Modified by Thomas Hamilton, Copyright 2010 |
cicklaus | 0:afb2650fb49a | 5 | |
cicklaus | 0:afb2650fb49a | 6 | #include "FATFileSystem.h" |
cicklaus | 0:afb2650fb49a | 7 | |
cicklaus | 0:afb2650fb49a | 8 | DWORD get_fattime(void) |
cicklaus | 0:afb2650fb49a | 9 | { |
cicklaus | 0:afb2650fb49a | 10 | return 35719201; |
cicklaus | 0:afb2650fb49a | 11 | } |
cicklaus | 0:afb2650fb49a | 12 | |
cicklaus | 0:afb2650fb49a | 13 | FATFileSystem* FATFileSystem::DriveArray[_DRIVES] = {0}; |
cicklaus | 0:afb2650fb49a | 14 | |
cicklaus | 0:afb2650fb49a | 15 | FATFileSystem::FATFileSystem(const char* SystemName) : FileSystemLike(SystemName) |
cicklaus | 0:afb2650fb49a | 16 | { |
cicklaus | 0:afb2650fb49a | 17 | for (unsigned char i = 0; i < _DRIVES; i++) |
cicklaus | 0:afb2650fb49a | 18 | { |
cicklaus | 0:afb2650fb49a | 19 | if(!DriveArray[i]) |
cicklaus | 0:afb2650fb49a | 20 | { |
cicklaus | 0:afb2650fb49a | 21 | DriveArray[i] = this; |
cicklaus | 0:afb2650fb49a | 22 | Drive = i; |
cicklaus | 0:afb2650fb49a | 23 | f_mount((BYTE)i, &FileSystemObject); |
cicklaus | 0:afb2650fb49a | 24 | return; |
cicklaus | 0:afb2650fb49a | 25 | } |
cicklaus | 0:afb2650fb49a | 26 | } |
cicklaus | 0:afb2650fb49a | 27 | } |
cicklaus | 0:afb2650fb49a | 28 | |
cicklaus | 0:afb2650fb49a | 29 | FATFileSystem::~FATFileSystem() |
cicklaus | 0:afb2650fb49a | 30 | { |
cicklaus | 0:afb2650fb49a | 31 | for (unsigned char i = 0; i < _DRIVES; i++) |
cicklaus | 0:afb2650fb49a | 32 | { |
cicklaus | 0:afb2650fb49a | 33 | if (DriveArray[i] == this) |
cicklaus | 0:afb2650fb49a | 34 | { |
cicklaus | 0:afb2650fb49a | 35 | DriveArray[i] = NULL; |
cicklaus | 0:afb2650fb49a | 36 | f_mount((BYTE)i, NULL); |
cicklaus | 0:afb2650fb49a | 37 | } |
cicklaus | 0:afb2650fb49a | 38 | } |
cicklaus | 0:afb2650fb49a | 39 | delete this; |
cicklaus | 0:afb2650fb49a | 40 | } |
cicklaus | 0:afb2650fb49a | 41 | |
cicklaus | 0:afb2650fb49a | 42 | FileHandle* FATFileSystem::open(const char* filename, int flags) |
cicklaus | 0:afb2650fb49a | 43 | { |
cicklaus | 0:afb2650fb49a | 44 | FAT_FIL FileObject; |
cicklaus | 0:afb2650fb49a | 45 | char FileName[64]; |
cicklaus | 0:afb2650fb49a | 46 | BYTE ModeFlags = 0; |
cicklaus | 0:afb2650fb49a | 47 | |
cicklaus | 0:afb2650fb49a | 48 | sprintf(FileName, "%d:/%s", Drive, filename); |
cicklaus | 0:afb2650fb49a | 49 | switch (flags & 3) |
cicklaus | 0:afb2650fb49a | 50 | { |
cicklaus | 0:afb2650fb49a | 51 | case O_RDONLY: ModeFlags = FA_READ; break; |
cicklaus | 0:afb2650fb49a | 52 | case O_WRONLY: ModeFlags = FA_WRITE; break; |
cicklaus | 0:afb2650fb49a | 53 | case O_RDWR: ModeFlags = FA_READ | FA_WRITE; break; |
cicklaus | 0:afb2650fb49a | 54 | } |
cicklaus | 0:afb2650fb49a | 55 | if(flags & O_CREAT) |
cicklaus | 0:afb2650fb49a | 56 | { |
cicklaus | 0:afb2650fb49a | 57 | if(flags & O_TRUNC) |
cicklaus | 0:afb2650fb49a | 58 | { |
cicklaus | 0:afb2650fb49a | 59 | ModeFlags |= FA_CREATE_ALWAYS; |
cicklaus | 0:afb2650fb49a | 60 | } |
cicklaus | 0:afb2650fb49a | 61 | else |
cicklaus | 0:afb2650fb49a | 62 | { |
cicklaus | 0:afb2650fb49a | 63 | ModeFlags |= FA_OPEN_ALWAYS; |
cicklaus | 0:afb2650fb49a | 64 | } |
cicklaus | 0:afb2650fb49a | 65 | } |
cicklaus | 0:afb2650fb49a | 66 | else |
cicklaus | 0:afb2650fb49a | 67 | { |
cicklaus | 0:afb2650fb49a | 68 | ModeFlags |= FA_OPEN_EXISTING; |
cicklaus | 0:afb2650fb49a | 69 | } |
cicklaus | 0:afb2650fb49a | 70 | if (f_open(&FileObject, (const TCHAR*)FileName, ModeFlags)) |
cicklaus | 0:afb2650fb49a | 71 | { |
cicklaus | 0:afb2650fb49a | 72 | return NULL; |
cicklaus | 0:afb2650fb49a | 73 | } |
cicklaus | 0:afb2650fb49a | 74 | else |
cicklaus | 0:afb2650fb49a | 75 | { |
cicklaus | 0:afb2650fb49a | 76 | if (flags & O_APPEND) |
cicklaus | 0:afb2650fb49a | 77 | { |
cicklaus | 0:afb2650fb49a | 78 | f_lseek(&FileObject, (DWORD)FileObject.fsize); |
cicklaus | 0:afb2650fb49a | 79 | } |
cicklaus | 0:afb2650fb49a | 80 | return new FATFileHandle(FileObject); |
cicklaus | 0:afb2650fb49a | 81 | } |
cicklaus | 0:afb2650fb49a | 82 | } |
cicklaus | 0:afb2650fb49a | 83 | |
cicklaus | 0:afb2650fb49a | 84 | int FATFileSystem::remove(const char* filename) |
cicklaus | 0:afb2650fb49a | 85 | { |
cicklaus | 0:afb2650fb49a | 86 | char FileName[64]; |
cicklaus | 0:afb2650fb49a | 87 | |
cicklaus | 0:afb2650fb49a | 88 | sprintf(FileName, "%d:/%s", Drive, filename); |
cicklaus | 0:afb2650fb49a | 89 | if (f_unlink((const TCHAR*)FileName)) |
cicklaus | 0:afb2650fb49a | 90 | { |
cicklaus | 0:afb2650fb49a | 91 | return -1; |
cicklaus | 0:afb2650fb49a | 92 | } |
cicklaus | 0:afb2650fb49a | 93 | else |
cicklaus | 0:afb2650fb49a | 94 | { |
cicklaus | 0:afb2650fb49a | 95 | return 0; |
cicklaus | 0:afb2650fb49a | 96 | } |
cicklaus | 0:afb2650fb49a | 97 | } |
cicklaus | 0:afb2650fb49a | 98 | |
cicklaus | 0:afb2650fb49a | 99 | int FATFileSystem::rename(const char* oldname, const char* newname) |
cicklaus | 0:afb2650fb49a | 100 | { |
cicklaus | 0:afb2650fb49a | 101 | char OldName[64]; |
cicklaus | 0:afb2650fb49a | 102 | |
cicklaus | 0:afb2650fb49a | 103 | sprintf(OldName, "%d:/%s", Drive, oldname); |
cicklaus | 0:afb2650fb49a | 104 | if (f_rename((const TCHAR*)OldName, (const TCHAR*)newname)) |
cicklaus | 0:afb2650fb49a | 105 | { |
cicklaus | 0:afb2650fb49a | 106 | return -1; |
cicklaus | 0:afb2650fb49a | 107 | } |
cicklaus | 0:afb2650fb49a | 108 | else |
cicklaus | 0:afb2650fb49a | 109 | { |
cicklaus | 0:afb2650fb49a | 110 | return 0; |
cicklaus | 0:afb2650fb49a | 111 | } |
cicklaus | 0:afb2650fb49a | 112 | } |
cicklaus | 0:afb2650fb49a | 113 | |
cicklaus | 0:afb2650fb49a | 114 | DirHandle* FATFileSystem::opendir(const char* name) |
cicklaus | 0:afb2650fb49a | 115 | { |
cicklaus | 0:afb2650fb49a | 116 | FAT_DIR DirectoryObject; |
cicklaus | 0:afb2650fb49a | 117 | char DirectoryName[64]; |
cicklaus | 0:afb2650fb49a | 118 | |
cicklaus | 0:afb2650fb49a | 119 | sprintf(DirectoryName, "%d:%s", Drive, name); |
cicklaus | 0:afb2650fb49a | 120 | if (f_opendir(&DirectoryObject, (const TCHAR*)DirectoryName)) |
cicklaus | 0:afb2650fb49a | 121 | { |
cicklaus | 0:afb2650fb49a | 122 | return NULL; |
cicklaus | 0:afb2650fb49a | 123 | } |
cicklaus | 0:afb2650fb49a | 124 | else |
cicklaus | 0:afb2650fb49a | 125 | { |
cicklaus | 0:afb2650fb49a | 126 | return new FATDirHandle(DirectoryObject); |
cicklaus | 0:afb2650fb49a | 127 | } |
cicklaus | 0:afb2650fb49a | 128 | } |
cicklaus | 0:afb2650fb49a | 129 | |
cicklaus | 0:afb2650fb49a | 130 | int FATFileSystem::mkdir(const char* name, mode_t mode) |
cicklaus | 0:afb2650fb49a | 131 | { |
cicklaus | 0:afb2650fb49a | 132 | char DirectoryName[64]; |
cicklaus | 0:afb2650fb49a | 133 | |
cicklaus | 0:afb2650fb49a | 134 | sprintf(DirectoryName, "%d:%s", Drive, name); |
cicklaus | 0:afb2650fb49a | 135 | if (f_mkdir((const TCHAR*)DirectoryName)) |
cicklaus | 0:afb2650fb49a | 136 | { |
cicklaus | 0:afb2650fb49a | 137 | return -1; |
cicklaus | 0:afb2650fb49a | 138 | } |
cicklaus | 0:afb2650fb49a | 139 | else |
cicklaus | 0:afb2650fb49a | 140 | { |
cicklaus | 0:afb2650fb49a | 141 | return 0; |
cicklaus | 0:afb2650fb49a | 142 | } |
cicklaus | 0:afb2650fb49a | 143 | } |
cicklaus | 0:afb2650fb49a | 144 | |
cicklaus | 0:afb2650fb49a | 145 | int FATFileSystem::format(unsigned int allocationunit) |
cicklaus | 0:afb2650fb49a | 146 | { |
cicklaus | 0:afb2650fb49a | 147 | if (f_mkfs(Drive, 0, allocationunit)) |
cicklaus | 0:afb2650fb49a | 148 | { |
cicklaus | 0:afb2650fb49a | 149 | return -1; |
cicklaus | 0:afb2650fb49a | 150 | } |
cicklaus | 0:afb2650fb49a | 151 | else |
cicklaus | 0:afb2650fb49a | 152 | { |
cicklaus | 0:afb2650fb49a | 153 | return 0; |
cicklaus | 0:afb2650fb49a | 154 | } |
cicklaus | 0:afb2650fb49a | 155 | } |