diff options
author | Ben Bridle <ben@derelict.engineering> | 2024-11-22 16:04:20 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2024-11-22 16:15:11 +1300 |
commit | 4f8805869c469cb1b3685e03c3ea34d7654b5cb7 (patch) | |
tree | efe555c1064b7a69738e3cf7fd107af7bdc0155f /arm9/source/types | |
parent | fcbc3968bd95e4d19b37d9fa4bca51b1db8596ff (diff) | |
download | bedrock-nds-4f8805869c469cb1b3685e03c3ea34d7654b5cb7.zip |
Implement file device
There is still a small amount of work to be done on the file device:
- Read file size only when requested
- Hide '.' and '..' directories
- Resize files
Diffstat (limited to 'arm9/source/types')
-rw-r--r-- | arm9/source/types/pathbuf.c | 52 | ||||
-rw-r--r-- | arm9/source/types/pathbuf.h | 18 |
2 files changed, 70 insertions, 0 deletions
diff --git a/arm9/source/types/pathbuf.c b/arm9/source/types/pathbuf.c new file mode 100644 index 0000000..1a610c8 --- /dev/null +++ b/arm9/source/types/pathbuf.c @@ -0,0 +1,52 @@ +#include <nds.h> +#include "pathbuf.h" + +u8 pb_read(PathBuf *buf) { + u8 output = buf->mem[buf->p]; + if (output) buf->p++; + return output; +} + +void pb_clear(PathBuf *buf) { + memset(&buf->mem, 0, 256); +} + +void pb_reset(PathBuf *buf, bool to_name) { + buf->p = 0; + if (to_name) for (int i=0; i<255; i++) { + if (buf->mem[i] == '/') { + buf->p = i+1; + } else if (buf->mem[i] == 0) { + break; + } + } +} + +// Push a byte to a PathBuf, return true and reset the pointer if byte is null. +bool pb_push_byte(PathBuf *buf, u8 byte) { + // Stop before overwriting the pointer. + if (buf->p != 0xff) { + buf->mem[buf->p++] = byte; + } + if (byte == 0) { + buf->p = 0; + return true; + } else { + return false; + } +} + +bool pb_is_terminated(PathBuf *buf) { + for (int i=0; i<255; i++) { + if (buf->mem[i] == 0) { + return true; + } + } + return false; +} + +void pb_populate(PathBuf *buf, u8 *path) { + strncpy((char*) &buf->mem[0], (char*) path, 254); + buf->p = 0; +} + diff --git a/arm9/source/types/pathbuf.h b/arm9/source/types/pathbuf.h new file mode 100644 index 0000000..7fd7251 --- /dev/null +++ b/arm9/source/types/pathbuf.h @@ -0,0 +1,18 @@ +#include <nds.h> + +#ifndef PATHBUF_H_ + #define PATHBUF_H_ + + typedef struct { + u8 mem[255]; + u8 p; + } PathBuf; + + u8 pb_read(PathBuf *buf); + void pb_clear(PathBuf *buf); + void pb_reset(PathBuf *buf, bool to_name); + bool pb_push_byte(PathBuf *buf, u8 byte); + bool pb_is_terminated(PathBuf *buf); + void pb_populate(PathBuf *buf, u8 *path); + +#endif |