hollyhock
Classes | Functions | Variables
file.hpp File Reference

Functions for interacting with the calculator's file system. More...

#include <stdint.h>
Include dependency graph for file.hpp:

Go to the source code of this file.

Classes

struct  stat
 
struct  findInfo
 

Functions

constexpr uint16_t statDateYear (uint16_t date)
 
constexpr uint16_t statDateMonth (uint16_t date)
 
constexpr uint16_t statDateDay (uint16_t date)
 
constexpr uint16_t statTimeHour (uint16_t time)
 
constexpr uint16_t statTimeMinute (uint16_t time)
 
constexpr uint16_t statTimeSecond (uint16_t time)
 
int close (int fd)
 
int findClose (int findHandle)
 
int findFirst (const wchar_t *path, int *findHandle, wchar_t *name, struct findInfo *findInfoBuf)
 
int findNext (int findHandle, wchar_t *name, struct findInfo *findInfoBuf)
 
int fstat (int fd, struct stat *buf)
 
int getAddr (int fd, int offset, const void **addr)
 
int lseek (int fd, int offset, int whence)
 
int mkdir (const char *path)
 
int open (const char *path, int flags)
 
int read (int fd, void *buf, int count)
 
int remove (const char *path)
 
int rename (const char *oldPath, const char *newPath)
 
int stat (const char *path, struct stat *buf)
 
int write (int fd, const void *buf, int count)
 

Variables

const int SEEK_SET = 0
 Set the file offset to offset.
 
const int SEEK_CUR = 1
 Set the file offset to the current position, plus offset bytes.
 
const int SEEK_END = 2
 Set the file offset to the end of the file, plus offset bytes.
 
const int OPEN_READ = 1 << 0
 Open the file as readable.
 
const int OPEN_WRITE = 1 << 1
 Open the file as writable.
 
const int OPEN_CREATE = 1 << 2
 Create the file, if it does not already exist.
 
const int OPEN_APPEND = 1 << 4
 Opens the file with the file offset set to the end of the file.
 
Errors

Errors returned by file system functions. All negative numbers.

const int ENOMEM = -1
 
const int EINVAL = -2
 
const int EDEVFAIL = -3
 
const int EMOUNTED = -4
 
const int EACCES = -5
 
const int EBADFSID = -6
 
const int ENOVOLUME = -7
 
const int ENOPATH = -8
 
const int EEXIST = -9
 
const int ENAMETOOLONG = -10
 
const int EOUTOFBOUND = -11
 
const int EUNFORMAT = -12
 
const int ENOSPC = -13
 
const int ENOENT = -14
 
const int EISDIRECTORY = -15
 
const int ESHARE = -16
 
const int EMFILE = -17
 
const int EBADF = -18
 
const int EEOF = -19
 
const int ENOTEMPTY = -20
 
const int ECLUSTERSIZEMISMATCH = -40
 
const int ESYSTEM = -99
 

Detailed Description

Functions for interacting with the calculator's file system.

Provides an API similar to that of Unix.

The storage which appears when the calculator is attached to a PC is found under the path \\fls0\ (remember to escape the \ in strings!).

Example: reading 256 bytes from a file called test.txt from the USB flash

int fd = open("\\\\fls0\\test.txt", OPEN_READ);
if (fd < 0) {
// An error occurred calling open
goto exit;
}
uint8_t buf[256];
int ret = read(fd, buf, sizeof(buf));
if (ret < 0) {
// An error occurred calling read
close(fd);
goto exit;
}
ret = close(fd);
if (ret < 0) {
// An error occurred calling close
}
exit:
int close(int fd)
int open(const char *path, int flags)
int read(int fd, void *buf, int count)
const int OPEN_READ
Open the file as readable.
Definition: file.hpp:111

Example: writing 16 bytes to a non-existant file called f.bin in a folder test in the USB flash

int fd = open("\\\\fls0\\test\\f.bin", OPEN_WRITE | OPEN_CREATE);
if (fd < 0) {
// An error occurred calling open
goto exit;
}
uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
int ret = write(fd, buf, sizeof(buf));
if (ret < 0) {
// An error occurred calling write
close(fd);
goto exit;
}
ret = close(fd);
if (ret < 0) {
// An error occurred calling close
}
exit:
int write(int fd, const void *buf, int count)
const int OPEN_CREATE
Create the file, if it does not already exist.
Definition: file.hpp:115
const int OPEN_WRITE
Open the file as writable.
Definition: file.hpp:113

Function Documentation

◆ close()

int close ( int  fd)

Closes an open file.

Parameters
fdThe file descriptor for the open file.
Returns
0 on success, or a negative error code on failure.

◆ findClose()

int findClose ( int  findHandle)

Closes a find handle.

Very, very, very bad things happen if a find handle is not closed.

Parameters
findHandleThe find handle to close.
Returns
0 on success, or a negative error code on failure.

◆ findFirst()

int findFirst ( const wchar_t *  path,
int *  findHandle,
wchar_t *  name,
struct findInfo findInfoBuf 
)

Starts a find operation, locating files matching a specific path.

Can be used to list the contents of a directory by using a wildcard. For example, passing the path L"\\fls0\\*" or L"\\fls0\\*.*" matches all files and directories on the calculator's flash (not recursive, though).

To find the next file/directory which matches the path, call findNext, passing in the find handle returned by this function. Ensure the find handle is closed using findClose when the find operation is finished. Bad things happen if the handle is not closed.

Parameters
[in]pathThe path to search. May contain wildcards.
[out]findHandleThe find handle created. Must be closed when the find operation is finished.
[out]nameThe name of the file/directory found.
[out]findInfoBufInformation about the found file.
Returns
0 on success, or a negative error code on failure.

◆ findNext()

int findNext ( int  findHandle,
wchar_t *  name,
struct findInfo findInfoBuf 
)

Returns information about the next matching file/directory in a find operation.

Parameters
findHandleThe find handle returned from findFirst.
[out]nameThe name of the file/directory found.
[out]findInfoBufInformation about the found file.
Returns
0 on success, or a negative error code on failure.

◆ fstat()

int fstat ( int  fd,
struct stat buf 
)

Retrieves information about an open file.

Parameters
fdThe file descriptor of an open file to retrieve the information of.
[out]bufThe retrieved information about the file.
Returns
0 on success, or a negative error code on failure.

◆ getAddr()

int getAddr ( int  fd,
int  offset,
const void **  addr 
)

Retrieves the memory address of a file.

If the file is empty or the offset would point outside of the file, EINVAL is returned.

Parameters
fdThe file descriptor of an open file.
offsetAn offset to apply to the pointer to the file's data.
[out]addrThe address of the file's data.
Returns
0 on success, or a negative error code on failure.

◆ lseek()

int lseek ( int  fd,
int  offset,
int  whence 
)

Repositions the file offset of the file descriptor. The new position depends on the value of both offset and whence. See lseek whence Values.

Parameters
fdThe file descriptor of an open file to change the file offset of.
offsetThe new offset, relative to some point.
whenceWhere offset is relative to.
Returns
The new file offset on success, or a negative error code on failure.

◆ mkdir()

int mkdir ( const char *  path)

Creates a directory.

Parameters
[in]pathThe path to the directory to be created.
Returns
0 on success, or a negative error code on failure.

◆ open()

int open ( const char *  path,
int  flags 
)

Opens a file on the file system.

The argument flags is fed a bitwise OR'd combination of flags. See open flags Values.

Parameters
[in]pathThe path to the file to open.
flagsA bitfield describing the mode in which to open the file.
Returns
A file descriptor on success, or a negative error code on failure.

◆ read()

int read ( int  fd,
void *  buf,
int  count 
)

Reads up to count bytes from a file, and stores them in buf.

If count bytes cannot be read from the file, as many bytes as possible are read (i.e. between 0 and count - 1 bytes are read and stored in buf).

Parameters
fdThe file descriptor of an open file to read from.
[out]bufA buffer to place the read bytes into.
countThe maximum number of bytes to read.
Returns
The number of bytes read on success, or a negative error code on failure.

◆ remove()

int remove ( const char *  path)

Deletes a file or directory.

Parameters
[in]pathThe path to the file or directory to be deleted.
Returns
0 on success, or a negative error code on failure.

◆ rename()

int rename ( const char *  oldPath,
const char *  newPath 
)

Renames a file or directory.

Parameters
[in]oldPathThe path to the file or directory to be renamed.
[in]newPathThe path to the new name for the file or directory.
Returns
0 on success, or a negative error code on failure.

◆ stat()

int stat ( const char *  path,
struct stat buf 
)

Retrieves information about a file.

Parameters
[in]pathThe path to the file to retrieve the information of.
[out]bufThe retrieved information about the file.
Returns
0 on success, or a negative error code on failure.

◆ statDateDay()

constexpr uint16_t statDateDay ( uint16_t  date)
constexpr

Retrieves the day from a struct stat date field.

Parameters
dateThe date field from a struct stat.
Returns
The day encoded in the date field.

◆ statDateMonth()

constexpr uint16_t statDateMonth ( uint16_t  date)
constexpr

Retrieves the month from a struct stat date field.

Parameters
dateThe date field from a struct stat.
Returns
The month encoded in the date field.

◆ statDateYear()

constexpr uint16_t statDateYear ( uint16_t  date)
constexpr

Retrieves the year from a struct stat date field.

Parameters
dateThe date field from a struct stat.
Returns
The year encoded in the date field.

◆ statTimeHour()

constexpr uint16_t statTimeHour ( uint16_t  time)
constexpr

Retrieves the hour from a struct stat time field.

Parameters
timeThe time field from a struct stat.
Returns
The hour encoded in the time field.

◆ statTimeMinute()

constexpr uint16_t statTimeMinute ( uint16_t  time)
constexpr

Retrieves the minute from a struct stat time field.

Parameters
timeThe time field from a struct stat.
Returns
The minute encoded in the time field.

◆ statTimeSecond()

constexpr uint16_t statTimeSecond ( uint16_t  time)
constexpr

Retrieves the second from a struct stat time field.

Has a maximum resolution of 2 seconds.

Parameters
timeThe time field from a struct stat.
Returns
The second encoded in the time field.

◆ write()

int write ( int  fd,
const void *  buf,
int  count 
)

Writes count bytes from buf to a file.

Parameters
fdThe file descriptor of an open file to write to.
[in]bufA buffer of bytes to write to the file.
countThe number of bytes from buf to write to the file.
Returns
The number of bytes written on success, or a negative error code on failure.