encapsulate file data and length in Blob structure, enable seeking of data files
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmEE3boACgkQO3+8IhRO Y5jIPQ//WkkwZoX4UiaSPlsWRVBVDgKYQKjBpo+34Ny5yOOdgi53UuSzx4cyQAZ+ U2H0uaAX6Hd2Kne0eOK5M8BovXh+3oIz0UYWE64zST9S49LmVQgVvPpLe3Q/lgkg Ope2zvy+B7zu1K1U9R2HjfrMZaRi6I9AJqe1a3+FG/U9myw9GOPA12ImjFhVFfTD JouPm8Fo6zEFq8+D1CPg9ZI3LDtBB6NYbnrQsGLwwSw3p4NUFHc95RAlirXwQtqo H8U1hfANv4B3nPYXyEoKpEolCTZ0V7A0XK/p2v8dzPAk54oPbNaqLMuooy7iaQ6m 68HpSIeds+Zr+/VVfyO2CZMRVauYnMF4lWlzK+knc+o15qquomnsUP6FQ+hN8rhQ qiCGJXv9mncwTfNbEn/y58S7eO1Qs6qsA1AQaH7kOhjcX9QCnOZ7Dw5RrNU6ThmM h8gWOmVroL+M4didovYFogQLjrUqW8LQ/O7jYBYBeia74GEIkXjszwz95EvbJK2V oOc5YAeOHejpcHi9xgw9+78IproaRYtXgSEDXwvKmOeHefeA+ddRlbuExCdG8i3B PZA2K5LHgwdi1D7g8WCWuvCpeRe1YFtOGB1kNPJntflYsz9EEU9sMsasBLNIz+Yx Sh7p2AGeKifQjmjn50voccCGvg7bHhe2MYmrBbT1Q8XzQlChq2A= =k2Cj -----END PGP SIGNATURE-----
M
cart.c
→
cart.c
@@ -100,14 +100,20 @@ }
void destroy_cart(Cart* self) { if (self != nil) { + free(self->rom->data); free(self->rom); - if (self->sprite_data != nil) + if (self->sprite_data != nil) { + free(self->sprite_data->data); free(self->sprite_data); - if (self->audio_data != nil) + } + if (self->audio_data != nil) { + free(self->audio_data->data); free(self->audio_data); - if (self->txt_data != nil) + } + if (self->txt_data != nil) { + free(self->txt_data->data); free(self->txt_data); - + } free(self); self = nil; }
M
cart.h
→
cart.h
@@ -1,12 +1,17 @@
typedef struct UserInfo UserInfo; typedef unsigned int uint; +typedef struct Blob { + char* data; + uvlong length; +} Blob; + typedef struct Cart { char name[32]; - char* rom; - char* txt_data; - char* sprite_data; - char* audio_data; + Blob* rom; + Blob* txt_data; + Blob* sprite_data; + Blob* audio_data; } Cart; Cart* create_cart(char* name);
M
util.c
→
util.c
@@ -1,6 +1,7 @@
#include <u.h> #include <libc.h> #include <stdio.h> +#include "cart.h" #include "util.h" uvlong hash(char* str, int array_sz) {@@ -96,10 +97,11 @@ dst[len + 1] = '\0';
return dst; } -char* read_bytes(char* path) { +Blob* read_bytes(char* path) { FILE* f; char* buf; long count; + Blob* self; f = fopen(path, "rb"); if (f == nil)@@ -114,14 +116,18 @@ fclose(f);
return nil; } else { fclose(f); - return buf; + self = malloc(sizeof(Blob)); + self->data = buf; + self->length = count; + return self; } } -char* read_chars(char* path) { +Blob* read_chars(char* path) { FILE* f; char* buf; long count; + Blob* self; f = fopen(path, "r"); if (f == nil)@@ -136,7 +142,10 @@ fclose(f);
return nil; } else { fclose(f); - return buf; + self = malloc(sizeof(Blob)); + self->data = buf; + self->length = count; + return self; } }
M
util.h
→
util.h
@@ -1,4 +1,5 @@
typedef unsigned long long uvlong; +typedef struct Blob Blob; uvlong hash(char* str, int array_sz); char clca(char c);@@ -13,6 +14,6 @@ char* scsw(char* str, char a, char b);
char* scat(char* dst, const char* src); int ssin(char* str, char* substr); char* ccat(char* dst, char c); -char* read_bytes(char* path); -char* read_chars(char* path); +Blob* read_bytes(char* path); +Blob* read_chars(char* path); void itoa(int, char*, int);
M
xrxs.c
→
xrxs.c
@@ -115,11 +115,9 @@
void write_universe(Req* r) { char key[16] = {0}; char value[64] = {0}; - char* c = r->ifcall.data; char buffer[1024] = {0}; UserInfo* u = find_user(users_table, r->fid->uid); Atom* a; - int i; scpy(r->ifcall.data, buffer, r->ifcall.count); sscanf(buffer, "%15s = %63s", key, value);@@ -276,29 +274,45 @@
if (u->cart == nil) goto end; - readstr(r, u->cart->rom); + readbuf(r, u->cart->rom->data, u->cart->rom->length); end: respond(r, nil); } void read_data(Req* r, FileType t) { UserInfo* u = find_user(users_table, r->fid->uid); + vlong offset = r->ifcall.offset; + long count = r->ifcall.count; if (u->cart == nil) goto end; switch (t) { case SPRITE_DATA: - readstr(r, u->cart->sprite_data); + if ( + u->cart->sprite_data != nil && offset < u->cart->sprite_data->length) { + if (offset + count >= u->cart->sprite_data->length) + count = u->cart->sprite_data->length - offset; + readbuf(r, u->cart->sprite_data->data + offset, count); + } break; case AUDIO_DATA: - readstr(r, u->cart->audio_data); + if (u->cart->audio_data != nil && offset < u->cart->audio_data->length) { + if (offset + count >= u->cart->audio_data->length) + count = u->cart->audio_data->length - offset; + readbuf(r, u->cart->audio_data->data + offset, count); + } break; case TEXT_DATA: - readstr(r, u->cart->txt_data); + if (u->cart->txt_data != nil && offset < u->cart->txt_data->length) { + if (offset + count >= u->cart->txt_data->length) + count = u->cart->txt_data->length - offset; + readbuf(r, u->cart->txt_data->data + offset, count); + } break; } end: + r->ofcall.count = count; respond(r, nil); }@@ -442,6 +456,8 @@ sprintf(buf, "%d\n", rand() % 100);
readstr(r, buf); respond(r, nil); } + +/* Gotta fix this by making it simpler! */ void read_grandom(Req* r) { char buf[8] = {0};