refined some types, implemented byte/char reading from files, and implemented some cartridge functions

This commit is contained in:
Iris Lightshard 2021-07-07 23:37:18 -06:00
parent 2d4f1f5ec4
commit cd612551fd
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
9 changed files with 183 additions and 19 deletions

76
cart.c
View file

@ -0,0 +1,76 @@
#include <u.h>
#include <libc.h>
#include "util.h"
#include "user.h"
#include "cart.h"
Cart* create_cart(char* name) {
char path[64] = {0};
char file[64] = {0};
Cart* cart;
char* cart_data;
scat(path, "carts/");
scat(path, name);
scpy(path, file, 64);
ccat(file, '/');
scat(file, name);
scat(file, ".rom");
cart_data = read_bytes(file);
if (cart_data != nil) {
cart = (Cart*)malloc(sizeof(Cart));
scpy(name, cart->name, 32);
cart->rom = cart_data;
scpy(path, file, 64);
scat(file, "/data/sprites");
cart->sprite_data = read_bytes(file);
scpy(path, file, 64);
scat(file, "/data/text");
cart->txt_data = read_bytes(file);
scpy(path, file, 64);
scat(file, "/data/audio");
cart->audio_data = read_bytes(file);
return cart;
}
return nil;
}
Cart* find_cart(UserInfo* table, char* name) {
UserInfo* u = table;
int i = 0;
for (i = 0; i < 64; i++) {
if (u->name != 0 && u->cart != nil && scmp(u->cart->name, name))
return u->cart;
u++;
}
return nil;
}
uint count_carts(UserInfo* table, char* name) {
UserInfo* u = table;
uint i, j;
j = 0;
for (i = 0; i < 64; i++) {
if (u->name != 0 && u->cart != nil && scmp(u->cart->name, name))
j++;
}
return j;
}
void destroy_cart(Cart* self) {
if (self != nil) {
free(self->rom);
if (self->sprite_data != nil)
free(self->sprite_data);
if (self->audio_data != nil)
free(self->audio_data);
if (self->txt_data != nil)
free(self->txt_data);
free(self);
self = nil;
}
}

18
cart.h
View file

@ -1,7 +1,15 @@
typedef struct UserInfo UserInfo;
typedef unsigned int uint;
typedef struct Cart {
char name[32];
char* cart_rom;
char* cart_txt_data;
char* cart_sprite_data;
char* cart_audio_data;
} Cart;
char* rom;
char* txt_data;
char* sprite_data;
char* audio_data;
} Cart;
Cart* create_cart(char* name);
Cart* find_cart(UserInfo* table, char* name);
uint count_carts(UserInfo* table, char* name);
void destroy_cart(Cart* self);

6
mkfile
View file

@ -5,6 +5,12 @@ SHORTLIB=9p
TARG=xrxs
OFILES=\
util.$O\
aux.$O\
cart.$O\
universe.$O\
realm.$O\
user.$O\
xrxs.$O\
<$PLAN9/src/mkone

View file

@ -1,4 +1,5 @@
typedef struct Universe Universe;
typedef struct UserInfo UserInfo;
typedef struct Realm {
char name[32];
@ -6,3 +7,9 @@ typedef struct Realm {
uvlong password;
Universe* universe;
} Realm;
Realm* create_realm(char* name);
Realm* parse_realm(char* name);
Realm* find_realm(UserInfo** table, char* name);
void save_realm(Realm* self);
void destroy_realm(Realm* self);

View file

@ -1,11 +1,22 @@
typedef unsigned int unit;
typedef struct Atom {
typedef struct Atom Atom;
struct Atom {
char name[16];
char value[64];
} Atom;
Atom* next;
};
typedef struct Universe {
uint count;
Atom** data;
Atom* atoms[256];
} Universe;
Universe* create_universe();
Universe* parse_universe(char* realm_name);
void save_universe(Universe* self, char* realm_name);
void set_atom(Universe* self);
Atom* get_atom(Universe* self, char* name);
void remove_atom(Universe* self, char* name);
void destroy_universe(Universe* self);

5
user.h
View file

@ -6,3 +6,8 @@ typedef struct UserInfo {
Cart* cart;
Realm* realm;
} UserInfo;
int load_cart(char* uname, char* cart_name);
int enter_realm(char* uname, char* realm_name);
int leave_realm(char* uname, Realm* realm);
int unload_cart(char* uname, Cart* cart);

53
util.c
View file

@ -1,14 +1,19 @@
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include "util.h"
uvlong hash(char* str) {
uvlong hash(char* str, int array_sz) {
uvlong h;
uchar* p;
h = 0;
for (p = (unsigned char*)str; *p != '\0'; p++)
h = 37 * h + *p;
return h; // or, h % ARRAY_SIZE;
if (array_sz == 0)
return h;
else
return h % array_sz;
}
char clca(char c) {
@ -90,3 +95,47 @@ char* ccat(char* dst, char c) {
dst[len + 1] = '\0';
return dst;
}
char* read_bytes(char* path) {
FILE* f;
char* buf;
long count;
f = fopen(path, "rb");
if (f == nil)
return nil;
fseek(f, 0, SEEK_END);
count = ftell(f);
rewind(f);
buf = (char*)malloc(count * sizeof(char));
if (!fread(buf, count, 1, f)) {
fclose(f);
return nil;
} else {
fclose(f);
return buf;
}
}
char* read_chars(char* path) {
FILE* f;
char* buf;
long count;
f = fopen(path, "r");
if (f == nil)
return nil;
fseek(f, 0, SEEK_END);
count = ftell(f);
rewind(f);
buf = (char*)malloc(count * sizeof(char));
if (!fread(buf, count, 1, f)) {
fclose(f);
return nil;
} else {
fclose(f);
return buf;
}
}

4
util.h
View file

@ -1,6 +1,6 @@
typedef unsigned long long uvlong;
uvlong hash(char* str);
uvlong hash(char* str, int array_sz);
char clca(char c);
char cuca(char c);
int slen(char* str);
@ -13,3 +13,5 @@ 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);

16
xrxs.c
View file

@ -25,7 +25,7 @@ void xrxs_attach(Req* r) {
/* As it is, once the user detaches, they will stay in the table
* until the server restarts. We have to figure out some way
* to detect a detach to remove them... You can't delay respond()
* or the attach will never complete.
* or the attach will never complete. We have logout() for now...
*/
int i = 0;
int l = 0;
@ -60,7 +60,7 @@ void login(char* uname, char* password) {
int i;
for (i = 0; i < 64; i++) {
if (scmp(uname, users_table[i].name)) {
users_table[i].password = hash(password);
users_table[i].password = hash(password, 0);
}
}
}
@ -80,7 +80,7 @@ int logout(char* uname) {
return 0;
}
int load(char* cart) {
int load(char* uname, char* cart) {
/* 1. get file handle to cartridge file
* 2. make cartridge file available in CART.data for this user
* 3. add cartridge name to user's UserInfo
@ -110,7 +110,7 @@ int protect(char* password) {
return 1;
}
int enter(char* realm) {
int enter(char* uname, char* realm) {
/* 1. get password for realm; if none, skip to 3
* 2. check password for current user against it;
* return 0 if different
@ -167,13 +167,13 @@ void write_ctl(Req* r) {
ccat(cmd, *c++);
}
fprintf(stderr, cmd);
uvlong const cmd_hashv = hash(cmd);
uvlong const cmd_hashv = hash(cmd, 0);
switch (cmd_hashv) {
case LOGIN:
login(r->fid->uid, c);
break;
case LOAD:
// load(c);
// load(r->fid->uid, c);
break;
case CREATE:
// rcreate(c);
@ -182,7 +182,7 @@ void write_ctl(Req* r) {
// protect(c);
break;
case ENTER:
// enter(c);
// enter(r->fid->uid, c);
break;
case LEAVE:
// leave(r->fid->uid);
@ -318,7 +318,7 @@ void threadmain(int argc, char* argv[]) {
/* if -h CMD is given, print the hash value of CMD */
if (argc == 3 && scmp(argv[1], "-h")) {
printf("%llu\n", hash(argv[2]));
printf("%llu\n", hash(argv[2], 0));
return;
}