clii/src/ctx.c
reemo 1b775f59cc i finally get to go home
not touching a computer for a while weow
2023-12-28 18:08:23 +00:00

180 lines
3.2 KiB
C

#include "ctx.h"
#define DEF_CTX_LENS 8
struct {
int self;
user_t* users;
channel_t* channels;
int users_length,
channels_length;
} _ctx;
static void _users_init(user_t* users, int n) {
for(int i = 0; i < n; ++i)
users[i].id = -1;
}
static void _channels_init(channel_t* channels, int n) {
for(int i = 0; i < n; ++i)
channels[i].name[0] = '\0';
}
void ctx_init() {
_ctx.self = -1;
_ctx.users =
malloc(sizeof(user_t) * DEF_CTX_LENS);
_ctx.channels =
malloc(sizeof(channel_t) * DEF_CTX_LENS);
_ctx.users_length
= _ctx.channels_length
= DEF_CTX_LENS;
_users_init(_ctx.users, _ctx.users_length);
_channels_init(_ctx.channels, _ctx.channels_length);
}
void ctx_free() {
free(_ctx.users);
free(_ctx.channels);
}
int parse_color(const char* color) {
return 0;
}
uint64_t parse_flags(const char* flags) {
int length = strlen(flags);
uint64_t out = 0;
for(int i = 0; i < length; --i)
if(flags[length - i - 1] == '1')
out |= 1ull << i;
return out;
}
void parse_perms
(const char* perms, int n, int* out)
{
const char* ptr = perms;
for(int i = 0; i < n; ++i)
out[i] = (int)strtol(ptr, (char**)&ptr, 10);
}
/** USER FUNCTIONS **/
/**********************/
static user_t* _get_user(int id) {
for(int i = 0; i < _ctx.users_length; ++i)
if(_ctx.users[i].id == id)
return _ctx.users + i;
return NULL;
}
static inline user_t* _get_empty_user() {
return _get_user(-1);
}
void add_user(const user_t* user) {
if(user->id == -1)
return;
user_t* to = _get_user(user->id);
if(to == NULL)
to = _get_empty_user();
if(to == NULL) {
_ctx.users = realloc(_ctx.users,
sizeof(user_t) * _ctx.users_length * 2);
_users_init(
_ctx.users + _ctx.users_length,
_ctx.users_length
);
to = _ctx.users + _ctx.users_length;
_ctx.users_length *= 2;
}
memcpy(to, user, sizeof(user_t));
}
const user_t* get_user(int id) {
if(id == -1)
return NULL;
return _get_user(id);
}
void rm_user(int id) {
if(id == -1)
return;
user_t* user = _get_user(id);
if(user != NULL)
user->id = -1;
}
void set_self(int id) {
_ctx.self = id;
}
const user_t* get_self() {
return get_user(_ctx.self);
}
int get_self_id() {
return _ctx.self;
}
/** CHANNEL FUNCTIONS **/
/*************************/
static channel_t* _get_channel(const char* name) {
for(int i = 0; i < _ctx.channels_length; ++i)
if(strcmp(_ctx.channels[i].name, name) == 0)
return _ctx.channels + i;
return NULL;
}
static channel_t* _get_empty_channel() {
return _get_channel("");
}
void add_channel(const channel_t* channel) {
if(channel->name[0] == '\0')
return;
channel_t* to = _get_channel(channel->name);
if(to == NULL)
to = _get_empty_channel();
if(to == NULL) {
_ctx.channels = realloc(_ctx.channels,
sizeof(channel_t) * _ctx.channels_length * 2);
_channels_init(
_ctx.channels + _ctx.channels_length,
_ctx.channels_length
);
to = _ctx.channels + _ctx.channels_length;
_ctx.channels_length *= 2;
}
memcpy(to, channel, sizeof(channel_t));
}
const channel_t* get_channel(const char* name) {
if(name[0] == '\0')
return NULL;
return _get_channel(name);
}
void rm_channel(const char* name) {
if(name[0] == '\0')
return;
channel_t* channel = _get_channel(name);
if(channel != NULL)
channel->name[0] = '\0';
}