summaryrefslogtreecommitdiff
path: root/arm9/source/types/pathbuf.c
blob: 1a610c86b4cc8cc78a1a9b12d4dbfc1d1b7cf676 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
}