Developer World
Spresense Arduino Library v3.2.0-77d75a4
File.h
Go to the documentation of this file.
1/*
2 * File.h - Spresense Arduino File library
3 * Copyright 2019 Sony Semiconductor Solutions Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef __FILE_H__
21#define __FILE_H__
22
23#ifdef SUBCORE
24#error "File library is NOT supported by SubCore."
25#endif
26
41#include <Arduino.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <fcntl.h>
45#include <stdio.h>
46
47#define FILE_READ O_RDONLY
48#define FILE_WRITE (O_RDONLY | O_WRONLY | O_CREAT)
55class File : public Stream {
56
57private:
58 char* _name;
59 int _fd;
60 unsigned long _size;
61 unsigned long _curpos;
62 void* _dir;
64public:
71 File(const char *name, uint8_t mode = FILE_READ);
72
77
82
89 virtual size_t write(uint8_t data);
90
98 virtual size_t write(const uint8_t *buf, size_t size);
99
105 virtual int read();
106
112 virtual int peek();
113
119 virtual int available();
120
126 virtual void flush();
127
135 int read(void *buf, size_t nbyte);
136
143 boolean seek(uint32_t pos);
144
150 uint32_t position();
151
157 uint32_t size();
158
164 void close();
165
169 operator bool();
170
176 char *name();
177
183 boolean isDirectory(void);
184
191 File openNextFile(uint8_t mode = O_RDONLY);
192
196 void rewindDirectory(void);
197
198 using Print::write;
199};
200
203#endif
The File class allows for reading from and writing to individual files on the File System.
Definition: File.h:55
virtual int available()
Check if there are any bytes available for reading from the file.
void rewindDirectory(void)
Bring you back to the first file in the directory.
File openNextFile(uint8_t mode=O_RDONLY)
Reports the next file or folder in a directory.
File(const char *name, uint8_t mode=FILE_READ)
Construct a new File object.
boolean seek(uint32_t pos)
Seek to a new position in the file.
boolean isDirectory(void)
Check if the current file is a directory or not.
uint32_t size()
Get the size of the file.
char * name()
Returns the file name.
virtual int peek()
Read a byte from the file without advancing to the next one.
void close()
Close the file.
virtual void flush()
Ensures that any bytes written to the file are physically saved to the File System.
int read(void *buf, size_t nbyte)
Read from the file.
uint32_t position()
Get the current position within the file.
virtual int read()
Read from the file.
~File()
Destroy File object.
virtual size_t write(const uint8_t *buf, size_t size)
Write data to the file.
virtual size_t write(uint8_t data)
Write data to the file.
File()
Construct a new File object.
#define FILE_READ
Definition: File.h:47