implemented functions for Universe and some for Realm; fixed find_cart()

This commit is contained in:
Iris Lightshard 2021-07-10 00:46:04 -06:00
parent cd612551fd
commit 92bbf397ac
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
6 changed files with 174 additions and 7 deletions

4
cart.c
View file

@ -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 @@ uint count_carts(UserInfo* table, char* name) {
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;

63
realm.c
View file

@ -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) {}

View file

@ -10,6 +10,6 @@ typedef struct Realm {
Realm* create_realm(char* name);
Realm* parse_realm(char* name);
Realm* find_realm(UserInfo** table, char* name);
Realm* find_realm(UserInfo* table, char* name);
void save_realm(Realm* self);
void destroy_realm(Realm* self);

View file

@ -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;
}
}

View file

@ -9,14 +9,13 @@ struct Atom {
};
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);

4
util.c
View file

@ -108,7 +108,7 @@ char* read_bytes(char* path) {
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 @@ char* read_chars(char* path) {
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;