refined some types, implemented byte/char reading from files, and implemented some cartridge functions
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmDmjw4ACgkQO3+8IhRO Y5gcag/8DUnpEV2AhOqZVDJvUaVPqne7MRLTsc7ExIUOPGl09wrGmLLI5EdHv6k1 nZh8w3zIw2/gzsSRrbmOZrRSd/0hO+53kqtVDbQ4fKcUEsxjAIDOujbS93uy+iOI UbeKJC02Y0zNhVXKFYPN5QOT+uKR47K9261ZC7AaFwDRW0HeMDVP3BHk7O3/bFha kDl6/eoqwq86OtGRW42M5UG2/d7N8WSRbu4T7cDUS2458l2aZzmECzonRPZt2mgj byt+zyZsItSuXKp7I9JpU8v/CKgFntq97TjNJHbTV/SGTgEXzArm1YpNvVK98NQb PH4qChbseOVQUPG0KKlKYNTSGHpvyxNHIl4VeUaCPyz2N+9kvHGz4akDiPFarhfG VH4G/oYMX5+vgEfATmj/vVrV/7G1YpmFZOmV24e7JEvl2wxkMys2BDZ+eKO5UJ3D 5e9HVxn9kdvSN2VQ5sGJwsYI7mNeizPL084ZNigrQg6SO0Ba1y+T1vyNvN2MplIG uKEsNjczVBjAt7K7KvWyPIStmVxNHl+iKnVGQ+3hSpD8BBJyBGSIwaofkaaRpCUR uwja5Q0NKzjpBw3c+yOW8Ir9bbr62BNoD5PJx9PtkU3Kq+o7KMQ5eNSlKN+eEC46 RYO2WR4Ka6GnTQ+LlZSK6xVpPGox1IDhWq/iL8B7nCGL0LXdcrs= =Ap/E -----END PGP SIGNATURE-----
M
cart.c
→
cart.c
@@ -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; + } +}
M
cart.h
→
cart.h
@@ -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);
M
realm.h
→
realm.h
@@ -1,4 +1,5 @@
typedef struct Universe Universe; +typedef struct UserInfo UserInfo; typedef struct Realm { char name[32];@@ -6,3 +7,9 @@ ushort max;
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);
M
universe.h
→
universe.h
@@ -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);
M
util.c
→
util.c
@@ -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 @@ dst[len] = 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; + } +}
M
util.h
→
util.h
@@ -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);
M
xrxs.c
→
xrxs.c
@@ -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 @@ }
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 @@ */
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 @@ for (i = 0; i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) {
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 @@ case PROTECT:
// protect(c); break; case ENTER: - // enter(c); + // enter(r->fid->uid, c); break; case LEAVE: // leave(r->fid->uid);@@ -318,7 +318,7 @@ String** cart;
/* 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; }