Adafruit WaveHC Arduino Library
FatReader.h
1 /* Arduino FatReader Library
2  * Copyright (C) 2009 by William Greiman
3  *
4  * This file is part of the Arduino FatReader Library
5  *
6  * This Library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15 
16  * You should have received a copy of the GNU General Public License
17  * along with the Arduino FatReader Library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 #ifndef FatReader_h
21 #define FatReader_h
22 #include <FatStructs.h>
23 #include <SdReader.h>
24 
25 // flags for ls()
27 #define LS_FLAG_FRAGMENTED 1
28 
29 #define LS_SIZE 2
30 
31 #define LS_R 4
32 
33 // offsets for structures used in volume init
35 #define BPB_OFFSET 11
36 
37 #define BPB_COUNT 37
38 
39 #define PART_OFFSET (512 - 64 - 2)
40 
41 // format dir.name into name[13] as standard 8.3 string
42 void dirName(dir_t &dir, char name[]);
43 // Print name field of dir_t struct in 8.3 format
44 void printEntryName(dir_t &dir);
45 //------------------------------------------------------------------------------
49 class FatVolume {
51  friend class FatReader;
52  uint8_t blocksPerCluster_;
53  uint32_t blocksPerFat_;
54  uint32_t clusterCount_;
55  uint32_t dataStartBlock_;
56  uint8_t fatCount_;
57  uint32_t fatStartBlock_;
58  uint8_t fatType_;
59  SdReader *rawDevice_;
60  uint16_t rootDirEntryCount_;
61  uint32_t rootDirStart_;
62  uint32_t totalBlocks_;
63  uint8_t chainIsContiguous(uint32_t cluster);
64  uint32_t chainSize(uint32_t cluster);
65  uint8_t isEOC(uint32_t cluster) {
66  return cluster >= (fatType_ == 16 ? FAT16EOC_MIN : FAT32EOC_MIN);
67  }
68  uint32_t nextCluster(uint32_t cluster);
69  uint8_t rawRead(uint32_t block, uint16_t offset, uint8_t *dst,
70  uint16_t count) {
71  return rawDevice_->readData(block, offset, dst, count);
72  }
73  uint8_t validCluster(uint32_t cluster) {
74  return (1 < cluster && cluster < (clusterCount_ + 2));
75  }
76 
77 public:
79  FatVolume(void) : fatType_(0) {}
91  uint8_t init(SdReader &dev) { return init(dev, 1) ? 1 : init(dev, 0); }
92  uint8_t init(SdReader &dev, uint8_t part);
93 
94  // inline functions that return volume info
96  uint8_t blocksPerCluster(void) { return blocksPerCluster_; }
98  uint32_t blocksPerFat(void) { return blocksPerFat_; }
100  uint32_t clusterCount(void) { return clusterCount_; }
102  uint32_t dataStartBlock(void) { return dataStartBlock_; }
104  uint8_t fatCount(void) { return fatCount_; }
106  uint32_t fatStartBlock(void) { return fatStartBlock_; }
108  uint8_t fatType(void) { return fatType_; }
109 
114  SdReader *rawDevice(void) { return rawDevice_; }
116  uint32_t rootDirEntryCount(void) { return rootDirEntryCount_; }
119  uint32_t rootDirStart(void) { return rootDirStart_; }
121  uint32_t totalBlocks(void) { return totalBlocks_; }
122 };
123 //------------------------------------------------------------------------------
127 class FatReader {
128 // values for type_
130 #define FILE_IS_CONTIGUOUS 0X08
131 
132 #define FILE_TYPE_MASK 0X07
133 
134 #define FILE_TYPE_CLOSED 0X00
135 
136 #define FILE_TYPE_NORMAL 0X01
137 
138 #define FILE_TYPE_ROOT16 0X02
139 
140 #define FILE_TYPE_ROOT32 0X03
141 
142 #define FILE_TYPE_SUBDIR 0X04
143 
144 #define FILE_TYPE_MIN_DIR FILE_TYPE_ROOT16
145  uint8_t type_;
146  uint32_t fileSize_;
147  uint32_t readCluster_;
148  uint32_t readPosition_;
149  uint32_t firstCluster_;
150  FatVolume *vol_;
151  int16_t readBlockData(uint8_t *dst, uint16_t count);
152  void lsR(dir_t &d, uint8_t flags, uint8_t indent);
153 
154 public:
156  FatReader(void) : type_(FILE_TYPE_CLOSED) {}
157  void ls(uint8_t flags = 0);
158  uint8_t openRoot(FatVolume &vol);
159  uint8_t open(FatVolume &vol, dir_t &dir);
160  uint8_t open(FatReader &dir, char *name);
161  uint8_t open(FatReader &dir, uint16_t index);
162  void optimizeContiguous(void);
163  int16_t read(void *buf, uint16_t count);
164  int8_t readDir(dir_t &dir);
165  void rewind(void);
166  uint8_t seekCur(uint32_t pos);
167  // inline functions
169  void close(void) { type_ = FILE_TYPE_CLOSED; }
171  uint32_t fileSize(void) { return fileSize_; }
178  uint8_t fileType(void) { return type_ & FILE_TYPE_MASK; }
180  uint32_t firstCluster(void) { return firstCluster_; }
184  uint8_t isContiguous(void) { return type_ & FILE_IS_CONTIGUOUS; }
186  uint8_t isDir(void) { return fileType() >= FILE_TYPE_MIN_DIR; }
188  uint8_t isFile(void) { return fileType() == FILE_TYPE_NORMAL; }
190  uint8_t isOpen(void) { return fileType() != FILE_TYPE_CLOSED; }
192  uint32_t readCluster(void) { return readCluster_; }
194  uint32_t readPosition(void) { return readPosition_; }
204  uint8_t seekSet(uint32_t pos) {
205  if (pos >= readPosition_)
206  return seekCur(pos - readPosition_);
207  rewind();
208  return seekCur(pos);
209  }
210 
215  FatVolume *volume(void) { return vol_; }
216 };
217 #endif // FatReader_h
uint8_t fatType(void)
Definition: FatReader.h:108
void close(void)
Definition: FatReader.h:169
SdReader * rawDevice(void)
Raw device for this volume.
Definition: FatReader.h:114
uint8_t isContiguous(void)
Definition: FatReader.h:184
uint32_t fileSize(void)
Definition: FatReader.h:171
uint32_t rootDirEntryCount(void)
Definition: FatReader.h:116
uint32_t blocksPerFat(void)
Definition: FatReader.h:98
uint8_t fileType(void)
Definition: FatReader.h:178
uint32_t clusterCount(void)
Definition: FatReader.h:100
uint32_t readPosition(void)
Definition: FatReader.h:194
FatVolume provides access to FAT volumes.
Definition: FatReader.h:49
Hardware access class for SD flash cards.
Definition: SdReader.h:83
uint8_t fatCount(void)
Definition: FatReader.h:104
uint32_t fatStartBlock(void)
Definition: FatReader.h:106
#define FAT16EOC_MIN
Definition: FatStructs.h:259
uint8_t init(SdReader &dev)
Definition: FatReader.h:91
FatReader implements a minimal FAT16/FAT32 file reader class.
Definition: FatReader.h:127
FatVolume * volume(void)
get the parent volume
Definition: FatReader.h:215
uint32_t totalBlocks(void)
Definition: FatReader.h:121
FatVolume(void)
Definition: FatReader.h:79
uint32_t firstCluster(void)
Definition: FatReader.h:180
uint8_t isFile(void)
Definition: FatReader.h:188
uint8_t readData(uint32_t block, uint16_t offset, uint8_t *dst, uint16_t count)
Definition: SdReader.cpp:235
FatReader(void)
Definition: FatReader.h:156
uint32_t readCluster(void)
Definition: FatReader.h:192
FAT short directory entry.
Definition: FatStructs.h:302
uint8_t isOpen(void)
Definition: FatReader.h:190
uint32_t rootDirStart(void)
Definition: FatReader.h:119
#define FAT32EOC_MIN
Definition: FatStructs.h:261
uint8_t seekSet(uint32_t pos)
Definition: FatReader.h:204
uint8_t blocksPerCluster(void)
Definition: FatReader.h:96
uint32_t dataStartBlock(void)
Definition: FatReader.h:102
uint8_t isDir(void)
Definition: FatReader.h:186