xrxs/realm.c

93 lines
No EOL
1.8 KiB
C

#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 (f != nil) {
if (fgets(buf, 256, f)) {
self = malloc(sizeof(Realm));
sscanf(buf, "%hu %llu", &(self->max), &(self->password));
fclose(f);
} else {
return nil;
}
} 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++;
}
return nil;
}
void save_realm(Realm* self) {
FILE* f;
char path[64];
char file[64];
scat(path, "realms/");
scat(path, self->name);
scpy(path, file, 64);
scat(file, "/realm");
f = fopen(file, "w");
if (f != nil) {
fprintf(f, "%hu %llu", self->max, self->password);
fclose(f);
save_universe(self->universe, self->name);
}
}
void destroy_realm(Realm* self) {
if (self != nil) {
if (self->universe != nil) {
destroy_universe(self->universe);
}
free(self);
self = nil;
}
}