SdBaseFile.h

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
parent 0
2c8ba1964db7
permissions
-rw-r--r--

added missing sanguino files

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 /* Arduino SdFat Library
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 * Copyright (C) 2009 by William Greiman
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4 * This file is part of the Arduino SdFat Library
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6 * This Library is free software: you can redistribute it and/or modify
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 * the Free Software Foundation, either version 3 of the License, or
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9 * (at your option) any later version.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11 * This Library is distributed in the hope that it will be useful,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14 * GNU General Public License for more details.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17 * along with the Arduino SdFat Library. If not, see
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18 * <http://www.gnu.org/licenses/>.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21 #ifdef SDSUPPORT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23 #ifndef SdBaseFile_h
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24 #define SdBaseFile_h
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25 /**
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26 * \file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27 * \brief SdBaseFile class
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30 #include "SdFatConfig.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 #include "SdVolume.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 /**
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34 * \struct fpos_t
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35 * \brief internal type for istream
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36 * do not use in user apps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 struct fpos_t {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39 /** stream position */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40 uint32_t position;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41 /** cluster for position */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42 uint32_t cluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43 fpos_t() : position(0), cluster(0) {}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44 };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46 // use the gnu style oflag in open()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47 /** open() oflag for reading */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 uint8_t const O_READ = 0X01;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 /** open() oflag - same as O_IN */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50 uint8_t const O_RDONLY = O_READ;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51 /** open() oflag for write */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52 uint8_t const O_WRITE = 0X02;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53 /** open() oflag - same as O_WRITE */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54 uint8_t const O_WRONLY = O_WRITE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55 /** open() oflag for reading and writing */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56 uint8_t const O_RDWR = (O_READ | O_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57 /** open() oflag mask for access modes */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58 uint8_t const O_ACCMODE = (O_READ | O_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59 /** The file offset shall be set to the end of the file prior to each write. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60 uint8_t const O_APPEND = 0X04;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61 /** synchronous writes - call sync() after each write */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62 uint8_t const O_SYNC = 0X08;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63 /** truncate the file to zero length */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 uint8_t const O_TRUNC = 0X10;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65 /** set the initial position at the end of the file */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66 uint8_t const O_AT_END = 0X20;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67 /** create the file if nonexistent */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68 uint8_t const O_CREAT = 0X40;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 /** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 uint8_t const O_EXCL = 0X80;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 // SdBaseFile class static and const definitions
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73 // flags for ls()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74 /** ls() flag to print modify date */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 uint8_t const LS_DATE = 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76 /** ls() flag to print file size */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 uint8_t const LS_SIZE = 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78 /** ls() flag for recursive list of subdirectories */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79 uint8_t const LS_R = 4;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 // flags for timestamp
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83 /** set the file's last access date */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 uint8_t const T_ACCESS = 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85 /** set the file's creation date and time */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86 uint8_t const T_CREATE = 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 /** Set the file's write date and time */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 uint8_t const T_WRITE = 4;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89 // values for type_
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90 /** This file has not been opened. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 uint8_t const FAT_FILE_TYPE_CLOSED = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92 /** A normal file */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 uint8_t const FAT_FILE_TYPE_NORMAL = 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94 /** A FAT12 or FAT16 root directory */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95 uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 /** A FAT32 root directory */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97 uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98 /** A subdirectory file*/
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99 uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100 /** Test value for directory type */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101 uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103 /** date field for FAT directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 * \param[in] year [1980,2107]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105 * \param[in] month [1,12]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106 * \param[in] day [1,31]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 * \return Packed date for dir_t entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111 return (year - 1980) << 9 | month << 5 | day;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113 /** year part of FAT directory date field
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 * \param[in] fatDate Date in packed dir format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116 * \return Extracted year [1980,2107]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118 static inline uint16_t FAT_YEAR(uint16_t fatDate) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119 return 1980 + (fatDate >> 9);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121 /** month part of FAT directory date field
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122 * \param[in] fatDate Date in packed dir format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 * \return Extracted month [1,12]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 static inline uint8_t FAT_MONTH(uint16_t fatDate) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127 return (fatDate >> 5) & 0XF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
128 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
129 /** day part of FAT directory date field
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
130 * \param[in] fatDate Date in packed dir format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
131 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
132 * \return Extracted day [1,31]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
133 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
134 static inline uint8_t FAT_DAY(uint16_t fatDate) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
135 return fatDate & 0X1F;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
136 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
137 /** time field for FAT directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
138 * \param[in] hour [0,23]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
139 * \param[in] minute [0,59]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
140 * \param[in] second [0,59]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
141 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
142 * \return Packed time for dir_t entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
143 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
144 static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
145 return hour << 11 | minute << 5 | second >> 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
146 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
147 /** hour part of FAT directory time field
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
148 * \param[in] fatTime Time in packed dir format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
149 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
150 * \return Extracted hour [0,23]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
151 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
152 static inline uint8_t FAT_HOUR(uint16_t fatTime) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
153 return fatTime >> 11;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
154 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
155 /** minute part of FAT directory time field
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
156 * \param[in] fatTime Time in packed dir format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
157 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
158 * \return Extracted minute [0,59]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
159 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
160 static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
161 return(fatTime >> 5) & 0X3F;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
162 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
163 /** second part of FAT directory time field
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
164 * Note second/2 is stored in packed time.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
165 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
166 * \param[in] fatTime Time in packed dir format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
167 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
168 * \return Extracted second [0,58]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
169 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
170 static inline uint8_t FAT_SECOND(uint16_t fatTime) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
171 return 2*(fatTime & 0X1F);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
172 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
173 /** Default date for file timestamps is 1 Jan 2000 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
174 uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
175 /** Default time for file timestamp is 1 am */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
176 uint16_t const FAT_DEFAULT_TIME = (1 << 11);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
177 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
178 /**
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
179 * \class SdBaseFile
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
180 * \brief Base class for SdFile with Print and C++ streams.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
181 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
182 class SdBaseFile {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
183 public:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
184 /** Create an instance. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
185 SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
186 SdBaseFile(const char* path, uint8_t oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
187 ~SdBaseFile() {if(isOpen()) close();}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
188 /**
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
189 * writeError is set to true if an error occurs during a write().
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
190 * Set writeError to false before calling print() and/or write() and check
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
191 * for true after calls to print() and/or write().
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
192 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
193 bool writeError;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
194 //----------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
195 // helpers for stream classes
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
196 /** get position for streams
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
197 * \param[out] pos struct to receive position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
198 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
199 void getpos(fpos_t* pos);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
200 /** set position for streams
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
201 * \param[out] pos struct with value for new position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
202 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
203 void setpos(fpos_t* pos);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
204 //----------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
205 bool close();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
206 bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
207 bool createContiguous(SdBaseFile* dirFile,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
208 const char* path, uint32_t size);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
209 /** \return The current cluster number for a file or directory. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
210 uint32_t curCluster() const {return curCluster_;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
211 /** \return The current position for a file or directory. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
212 uint32_t curPosition() const {return curPosition_;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
213 /** \return Current working directory */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
214 static SdBaseFile* cwd() {return cwd_;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
215 /** Set the date/time callback function
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
216 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
217 * \param[in] dateTime The user's call back function. The callback
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
218 * function is of the form:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
219 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
220 * \code
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
221 * void dateTime(uint16_t* date, uint16_t* time) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
222 * uint16_t year;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
223 * uint8_t month, day, hour, minute, second;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
224 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
225 * // User gets date and time from GPS or real-time clock here
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
226 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
227 * // return date using FAT_DATE macro to format fields
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
228 * *date = FAT_DATE(year, month, day);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
229 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
230 * // return time using FAT_TIME macro to format fields
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
231 * *time = FAT_TIME(hour, minute, second);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
232 * }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
233 * \endcode
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
234 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
235 * Sets the function that is called when a file is created or when
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
236 * a file's directory entry is modified by sync(). All timestamps,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
237 * access, creation, and modify, are set when a file is created.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
238 * sync() maintains the last access date and last modify date/time.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
239 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
240 * See the timestamp() function.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
241 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
242 static void dateTimeCallback(
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
243 void (*dateTime)(uint16_t* date, uint16_t* time)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
244 dateTime_ = dateTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
245 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
246 /** Cancel the date/time callback function. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
247 static void dateTimeCallbackCancel() {dateTime_ = 0;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
248 bool dirEntry(dir_t* dir);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
249 static void dirName(const dir_t& dir, char* name);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
250 bool exists(const char* name);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
251 int16_t fgets(char* str, int16_t num, char* delim = 0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
252 /** \return The total number of bytes in a file or directory. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
253 uint32_t fileSize() const {return fileSize_;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
254 /** \return The first cluster number for a file or directory. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
255 uint32_t firstCluster() const {return firstCluster_;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
256 bool getFilename(char* name);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
257 /** \return True if this is a directory else false. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
258 bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
259 /** \return True if this is a normal file else false. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
260 bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
261 /** \return True if this is an open file/directory else false. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
262 bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
263 /** \return True if this is a subdirectory else false. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
264 bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
265 /** \return True if this is the root directory. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
266 bool isRoot() const {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
267 return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
268 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
269 void ls( uint8_t flags = 0, uint8_t indent = 0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
270 bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
271 // alias for backward compactability
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
272 bool makeDir(SdBaseFile* dir, const char* path) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
273 return mkdir(dir, path, false);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
274 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
275 bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
276 bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
277 bool open(const char* path, uint8_t oflag = O_READ);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
278 bool openNext(SdBaseFile* dirFile, uint8_t oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
279 bool openRoot(SdVolume* vol);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
280 int peek();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
281 static void printFatDate(uint16_t fatDate);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
282 static void printFatTime( uint16_t fatTime);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
283 bool printName();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
284 int16_t read();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
285 int16_t read(void* buf, uint16_t nbyte);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
286 int8_t readDir(dir_t* dir);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
287 static bool remove(SdBaseFile* dirFile, const char* path);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
288 bool remove();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
289 /** Set the file's current position to zero. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
290 void rewind() {seekSet(0);}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
291 bool rename(SdBaseFile* dirFile, const char* newPath);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
292 bool rmdir();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
293 // for backward compatibility
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
294 bool rmDir() {return rmdir();}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
295 bool rmRfStar();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
296 /** Set the files position to current position + \a pos. See seekSet().
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
297 * \param[in] offset The new position in bytes from the current position.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
298 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
299 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
300 bool seekCur(int32_t offset) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
301 return seekSet(curPosition_ + offset);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
302 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
303 /** Set the files position to end-of-file + \a offset. See seekSet().
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
304 * \param[in] offset The new position in bytes from end-of-file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
305 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
306 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
307 bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
308 bool seekSet(uint32_t pos);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
309 bool sync();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
310 bool timestamp(SdBaseFile* file);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
311 bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
312 uint8_t hour, uint8_t minute, uint8_t second);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
313 /** Type of file. You should use isFile() or isDir() instead of type()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
314 * if possible.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
315 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
316 * \return The file or directory type.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
317 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
318 uint8_t type() const {return type_;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
319 bool truncate(uint32_t size);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
320 /** \return SdVolume that contains this file. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
321 SdVolume* volume() const {return vol_;}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
322 int16_t write(const void* buf, uint16_t nbyte);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
323 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
324 private:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
325 // allow SdFat to set cwd_
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
326 friend class SdFat;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
327 // global pointer to cwd dir
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
328 static SdBaseFile* cwd_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
329 // data time callback function
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
330 static void (*dateTime_)(uint16_t* date, uint16_t* time);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
331 // bits defined in flags_
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
332 // should be 0X0F
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
333 static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
334 // sync of directory entry required
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
335 static uint8_t const F_FILE_DIR_DIRTY = 0X80;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
336
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
337 // private data
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
338 uint8_t flags_; // See above for definition of flags_ bits
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
339 uint8_t fstate_; // error and eof indicator
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
340 uint8_t type_; // type of file see above for values
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
341 uint32_t curCluster_; // cluster for current file position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
342 uint32_t curPosition_; // current file position in bytes from beginning
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
343 uint32_t dirBlock_; // block for this files directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
344 uint8_t dirIndex_; // index of directory entry in dirBlock
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
345 uint32_t fileSize_; // file size in bytes
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
346 uint32_t firstCluster_; // first cluster of file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
347 SdVolume* vol_; // volume where file is located
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
348
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
349 /** experimental don't use */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
350 bool openParent(SdBaseFile* dir);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
351 // private functions
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
352 bool addCluster();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
353 bool addDirCluster();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
354 dir_t* cacheDirEntry(uint8_t action);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
355 int8_t lsPrintNext( uint8_t flags, uint8_t indent);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
356 static bool make83Name(const char* str, uint8_t* name, const char** ptr);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
357 bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
358 bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
359 bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
360 dir_t* readDirCache();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
361 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
362 // to be deleted
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
363 static void printDirName( const dir_t& dir,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
364 uint8_t width, bool printSlash);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
365 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
366 // Deprecated functions - suppress cpplint warnings with NOLINT comment
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
367 #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
368 public:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
369 /** \deprecated Use:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
370 * bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
371 * \param[out] bgnBlock the first block address for the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
372 * \param[out] endBlock the last block address for the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
373 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
374 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
375 bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
376 return contiguousRange(&bgnBlock, &endBlock);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
377 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
378 /** \deprecated Use:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
379 * bool createContiguous(SdBaseFile* dirFile,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
380 * const char* path, uint32_t size)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
381 * \param[in] dirFile The directory where the file will be created.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
382 * \param[in] path A path with a valid DOS 8.3 file name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
383 * \param[in] size The desired file size.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
384 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
385 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
386 bool createContiguous(SdBaseFile& dirFile, // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
387 const char* path, uint32_t size) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
388 return createContiguous(&dirFile, path, size);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
389 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
390 /** \deprecated Use:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
391 * static void dateTimeCallback(
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
392 * void (*dateTime)(uint16_t* date, uint16_t* time));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
393 * \param[in] dateTime The user's call back function.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
394 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
395 static void dateTimeCallback(
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
396 void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
397 oldDateTime_ = dateTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
398 dateTime_ = dateTime ? oldToNew : 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
399 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
400 /** \deprecated Use: bool dirEntry(dir_t* dir);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
401 * \param[out] dir Location for return of the file's directory entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
402 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
403 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
404 bool dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
405 /** \deprecated Use:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
406 * bool mkdir(SdBaseFile* dir, const char* path);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
407 * \param[in] dir An open SdFat instance for the directory that will contain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
408 * the new directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
409 * \param[in] path A path with a valid 8.3 DOS name for the new directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
410 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
411 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
412 bool mkdir(SdBaseFile& dir, const char* path) { // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
413 return mkdir(&dir, path);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
414 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
415 /** \deprecated Use:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
416 * bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
417 * \param[in] dirFile An open SdFat instance for the directory containing the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
418 * file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
419 * \param[in] path A path with a valid 8.3 DOS name for the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
420 * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
421 * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
422 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
423 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
424 bool open(SdBaseFile& dirFile, // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
425 const char* path, uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
426 return open(&dirFile, path, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
427 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
428 /** \deprecated Do not use in new apps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
429 * \param[in] dirFile An open SdFat instance for the directory containing the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
430 * file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
431 * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
432 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
433 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
434 bool open(SdBaseFile& dirFile, const char* path) { // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
435 return open(dirFile, path, O_RDWR);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
436 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
437 /** \deprecated Use:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
438 * bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
439 * \param[in] dirFile An open SdFat instance for the directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
440 * \param[in] index The \a index of the directory entry for the file to be
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
441 * opened. The value for \a index is (directory file position)/32.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
442 * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
443 * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
444 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
445 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
446 bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
447 return open(&dirFile, index, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
448 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
449 /** \deprecated Use: bool openRoot(SdVolume* vol);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
450 * \param[in] vol The FAT volume containing the root directory to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
451 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
452 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
453 bool openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
454 /** \deprecated Use: int8_t readDir(dir_t* dir);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
455 * \param[out] dir The dir_t struct that will receive the data.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
456 * \return bytes read for success zero for eof or -1 for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
457 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
458 int8_t readDir(dir_t& dir) {return readDir(&dir);} // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
459 /** \deprecated Use:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
460 * static uint8_t remove(SdBaseFile* dirFile, const char* path);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
461 * \param[in] dirFile The directory that contains the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
462 * \param[in] path The name of the file to be removed.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
463 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
464 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
465 static bool remove(SdBaseFile& dirFile, const char* path) { // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
466 return remove(&dirFile, path);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
467 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
468 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
469 // rest are private
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
470 private:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
471 static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
472 static void oldToNew(uint16_t* date, uint16_t* time) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
473 uint16_t d;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
474 uint16_t t;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
475 oldDateTime_(d, t);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
476 *date = d;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
477 *time = t;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
478 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
479 #endif // ALLOW_DEPRECATED_FUNCTIONS
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
480 };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
481
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
482 #endif // SdBaseFile_h
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
483 #endif

mercurial