~mrms/tarum

4439a41806cb4a54d0918fb7b1a3938891d2196a — Marek Maškarinec 11 months ago 6e57732
readBin, readStr -> read
3 files changed, 13 insertions(+), 51 deletions(-)

M pak.json
M tar.c
M tar.um
M pak.json => pak.json +1 -1
@@ 1,6 1,6 @@
{
    "name": "tar",
    "version": "v0.1.0",
    "version": "v0.1.1",
    "author": "Marek Maškarinec <marek@mrms.cz>",
    "license": "Unlicense/MIT",
    "description": "A tar library",

M tar.c => tar.c +3 -31
@@ 95,42 95,14 @@ umc__tar_get_files(UmkaStackSlot *p, UmkaStackSlot *r)
	}
}

// fn umc__tar_read_str(tar: ^struct{}, file: str, out: ^str): int
void
umc__tar_read_str(UmkaStackSlot *p, UmkaStackSlot *r)
{
	mtar_t *tar = (mtar_t *)p[2].ptrVal;
	char *file = p[1].ptrVal;
	char **out = p[0].ptrVal;
	void *umka = r->ptrVal;
	r->intVal = 0;

	mtar_header_t h;
	int ret = mtar_find(tar, file, &h);
	if (ret) {
		r->intVal = ret;
		return;
	}

	char *buf = malloc(h.size + 1);

	ret = mtar_read_data(tar, buf, h.size);
	if (ret) {
		r->intVal = ret;
		return;
	}

	buf[h.size] = '\0';
	*out = umkaMakeStr(umka, buf);
}
static void *byteArrType = NULL;

// fn umc__tar_read_bin(tar: ^struct{}, file: str, out: ^[]byte): int
void
umc__tar_read_bin(UmkaStackSlot *p, UmkaStackSlot *r)
umc__tar_read(UmkaStackSlot *p, UmkaStackSlot *r)
{
	static void *byteArrType = NULL;
	if (byteArrType == NULL) {
		byteArrType = umkaGetType(r->ptrVal, "tar.um", "ByteArr");
		byteArrType = umkaGetType(r->ptrVal, "tar.um", "__Bytes");
	}

	mtar_t *tar = (mtar_t *)p[2].ptrVal;

M tar.um => tar.um +9 -19
@@ 70,26 70,15 @@ fn (t: ^Tar) getFiles*(): ([]File, Errno) {
    return files, err
}

fn umc__tar_read_str(tar: ^struct{}, path: str, out: ^str): Errno

//~~fn readStr
// Reads the given file from the tar archive as a string.
fn (t: ^Tar) readStr*(path: str): (str, Errno) {
//~~
    var s: str
    err := umc__tar_read_str(t._, path, &s)
    return s, err
}

type ByteArr* = []uint8
fn umc__tar_read_bin(tar: ^struct{}, path: str, out: ^[]uint8): Errno
type __Bytes* = []uint8
fn umc__tar_read(tar: ^struct{}, path: str, out: ^[]uint8): Errno

//~~fn readBin
// Reads the given file from the tar archive as a byte array.
fn (t: ^Tar) readBin*(path: str): ([]uint8, Errno) {
fn (t: ^Tar) read*(path: str): ([]uint8, Errno) {
//~~
    var s: []uint8
    err := umc__tar_read_bin(t._, path, &s)
    err := umc__tar_read(t._, path, &s)
    return s, err
}



@@ 108,7 97,7 @@ fn (t: ^Tar) extract*(dir: str): Errno {
        path := filepath.join(dir, files[i].name)
        os.mkdirp(filepath.dir(path))

        dat, err := t.readBin(files[i].name)
        dat, err := t.read(files[i].name)
        if (err != 0) {
            return err
        }


@@ 187,13 176,14 @@ fn main() {
    }
    
    printf("Reading pak.json:\n")
    var pakJson: str
    pakJson, err = tar.readStr("pak.json")
    var pakJson: []uint8
    pakJson, err = tar.read("pak.json")
    printf("%v\n", pakJson)
    if err != 0 {
        printf("Error reading pak.json: %s\n", strerror(err))
        return
    }
    printf("%s\n", pakJson)
    printf("%s\n", str([]char(pakJson)))
        
    err = tar.extract("extracted")
    if err != 0 {