implemented functions for Universe and some for Realm; fixed find_cart()
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmDpQiwACgkQO3+8IhRO Y5gBvRAAka4Ty3SRSs2tEJl56vuhtcY2Ak1CIxVkEKOFVK7NxNJbXtbKSfHwIKQ7 aZEw5yC0zxF/1xKRKoXxLwLXGUte4Ma21aIJ5WlqAQHXFd409d0E0BHLfTPmK16r pQAy3dPOW+2NahSo+1JHCNG2e+MfsH177AAC/npHWfdHBhgUSgNF3lM8xzMieaTC FpNNsjKrmwyQS9iBDOPqGaYTC+FkldaDBwGhh7T/KemxzV9lUt0mrBVkp8r9gvhi PA9kqfQOzxDyxAJOLInBouX6Ef0AXbyGkocCNJnPRMMGfJgZMfy1tDGqG3BekzsO Tg0apyKqtR2dLuXmW2LMAirwem/koFYAApIvdOkIs8l9bFi4kUSpvrD8BD2McNDs ErZioynhcTNdRAgylp72zKSn51zNvsoydFVZ5RAFPUOSLGyXW4JfURUTWdQ1HicT cUQlIbjtsUEaIukK9M2MKDoPizy+sFlM/Gd9M+Wm5gK18+/gC/vVNR2YZJK0wTny m+z6pxf7vgGTdGuVQxoG4RJrgAPm98Ue/TBiGDsoMpON/xaspLBP1NCSjlWsHzCk nMjsk8jnQee7E4fxBrt9JYEBD19pHQ4qJstxlB2yR4d3n9sDTJD+qBQ4iWHUbQW4 70uKDo6cz2UcNDp4bAjwcoNlmDGUOp50UPulvDhfiWMjjJMy9yI= =LQjr -----END PGP SIGNATURE-----
6 files changed,
174 insertions(+),
7 deletions(-)
M
cart.c
→
cart.c
@@ -42,7 +42,7 @@ 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)) + if (slen(u->name) > 0 && u->cart != nil && scmp(u->cart->name, name)) return u->cart; u++; }@@ -54,7 +54,7 @@ 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)) + if (slen(u->name) > 0 && u->cart != nil && scmp(u->cart->name, name)) j++; } return j;
M
realm.c
→
realm.c
@@ -0,0 +1,63 @@
+#include <u.h> +#include <libc.h> +#include <stdio.h> +#include "util.h" +#include "user.h" +#include "universe.h" +#include "realm.h" + +Realm* create_realm(char* name) { + Realm* self; + char path[64]; + scat(path, "realms/"); + scat(path, name); + if (create(path, OREAD, 0755) < 0) { + return nil; + } else { + self = malloc(sizeof(Realm)); + scpy(name, self->name, 32); + self->max = 4; + self->password = 0; + self->universe = create_universe(); + return self; + } +} + +Realm* parse_realm(char* name) { + Realm* self; + FILE* f; + char path[64]; + char file[64]; + char buf[256]; + scat(path, "realms/"); + scat(path, name); + scpy(path, file, 64); + + scat(file, "/realm"); + f = fopen(file, "r"); + if (fgets(buf, 256, f)) { + self = malloc(sizeof(Realm)); + sscanf(buf, "%hu %llu", &(self->max), &(self->password)); + fclose(f); + } else { + return nil; + } + + scpy(path, file, 64); + scat(file, "/universe"); + self->universe = parse_universe(path); + return self; +} + +Realm* find_realm(UserInfo* table, char* name) { + UserInfo* u = table; + int i; + + for (i = 0; i < 64; i++) { + if (slen(u->name) > 0 && u->realm != nil && scmp(u->realm->name, name)) + return u->realm; + u++; + } +} + +void save_realm(Realm* self) {}
M
universe.c
→
universe.c
@@ -0,0 +1,105 @@
+#include <u.h> +#include <libc.h> +#include <stdio.h> +#include "util.h" +#include "universe.h" + +Universe* create_universe() { + int i; + Universe* self = malloc(sizeof(Universe)); + for (i = 0; i < 256; i++) { + self->atoms[i] = nil; + } + return self; +} + +Universe* parse_universe(char* realm_name) { + char path[64] = {0}; + char buf[256] = {0}; + char name[16] = {0}; + char value[64] = {0}; + FILE* f; + Atom* a; + Universe* self; + + scat(path, "realms/"); + scat(path, realm_name); + scat(path, "/universe"); + + f = fopen(path, "r"); + if (f != nil) { + self = malloc(sizeof(Universe)); + while (fgets(buf, 256, f) != nil) { + sscanf(buf, "%16s = %64s", name, value); + a = malloc(sizeof(Atom)); + scpy(name, a->name, 16); + scpy(value, a->value, 64); + a->next = nil; + set_atom(self, a); + } + fclose(f); + return self; + } + return nil; +} + +void save_universe(Universe* self, char* realm_name) { + char path[64] = {0}; + FILE* f; + Atom* a; + int i; + + scat(path, "realms/"); + scat(path, realm_name); + scat(path, "/universe"); + + f = fopen(path, "w"); + if (f != nil) { + for (i = 0; i < 256; i++) { + a = self->atoms[i]; + while (a != nil) { + fprintf(f, "%s = %s", a->name, a->value); + a = a->next; + } + } + fclose(f); + } +} + +void set_atom(Universe* self, Atom* atom) { + uvlong i = hash(atom->name, 256); + Atom* a = self->atoms[i]; + while (a != nil) { + a = a->next; + } + a = atom; + atom->next = nil; +} + +Atom* get_atom(Universe* self, char* name) { + uvlong i = hash(name, 256); + Atom* a = self->atoms[i]; + while (a != nil && !scmp(a->name, name)) { + a = a->next; + } + return a; +} + +void destroy_universe(Universe* self) { + int i; + Atom* a; + Atom* next; + + if (self != nil) { + for (i = 0; i < 256; i++) { + a = self->atoms[i]; + while (a != nil) { + next = a->next; + free(a); + a = next; + } + } + free(self); + self = nil; + } +}
M
universe.h
→
universe.h
@@ -9,14 +9,13 @@ Atom* next;
}; typedef struct Universe { - uint count; 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); +void set_atom(Universe* self, Atom* atom); Atom* get_atom(Universe* self, char* name); void remove_atom(Universe* self, char* name); void destroy_universe(Universe* self);
M
util.c
→
util.c
@@ -108,7 +108,7 @@ fseek(f, 0, SEEK_END);
count = ftell(f); rewind(f); - buf = (char*)malloc(count * sizeof(char)); + buf = malloc(count * sizeof(char)); if (!fread(buf, count, 1, f)) { fclose(f); return nil;@@ -130,7 +130,7 @@ fseek(f, 0, SEEK_END);
count = ftell(f); rewind(f); - buf = (char*)malloc(count * sizeof(char)); + buf = malloc(count * sizeof(char)); if (!fread(buf, count, 1, f)) { fclose(f); return nil;