2009-02-08 04:04:35 +01:00
|
|
|
|
/*
|
|
|
|
|
* This file provides functions for easier accessing of _the_ table
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#include "data.h"
|
|
|
|
|
#include "table.h"
|
|
|
|
|
|
|
|
|
|
/* This is a two-dimensional dynamic array of Container-pointers. I’ve always wanted
|
|
|
|
|
* to be a three-star programmer :) */
|
|
|
|
|
Container ***table = NULL;
|
|
|
|
|
|
|
|
|
|
struct table_dimensions_t table_dims = {0, 0};
|
|
|
|
|
|
2009-02-08 11:25:32 +01:00
|
|
|
|
/*
|
|
|
|
|
* Initialize table
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-02-08 04:04:35 +01:00
|
|
|
|
void init_table() {
|
|
|
|
|
expand_table_cols();
|
|
|
|
|
expand_table_rows();
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-08 11:25:32 +01:00
|
|
|
|
/*
|
|
|
|
|
* Add one row to the table
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-02-08 04:04:35 +01:00
|
|
|
|
void expand_table_rows() {
|
|
|
|
|
int c;
|
|
|
|
|
Container *new;
|
|
|
|
|
|
|
|
|
|
table_dims.y++;
|
|
|
|
|
|
|
|
|
|
for (c = 0; c < table_dims.x; c++) {
|
|
|
|
|
table[c] = realloc(table[c], sizeof(Container*) * table_dims.y);
|
|
|
|
|
new = table[c][table_dims.y-1] = calloc(sizeof(Container), 1);
|
|
|
|
|
CIRCLEQ_INIT(&(new->clients));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-08 11:25:32 +01:00
|
|
|
|
/*
|
|
|
|
|
* Add one column to the table
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-02-08 04:04:35 +01:00
|
|
|
|
void expand_table_cols() {
|
|
|
|
|
int c;
|
|
|
|
|
Container *new;
|
|
|
|
|
|
|
|
|
|
table_dims.x++;
|
|
|
|
|
table = realloc(table, sizeof(Container**) * table_dims.x);
|
|
|
|
|
table[table_dims.x-1] = calloc(sizeof(Container*) * table_dims.y, 1);
|
|
|
|
|
for (c = 0; c < table_dims.y; c++) {
|
|
|
|
|
new = table[table_dims.x-1][c] = calloc(sizeof(Container), 1);
|
|
|
|
|
CIRCLEQ_INIT(&(new->clients));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Performs simple bounds checking for the given column/row
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
bool cell_exists(int col, int row) {
|
|
|
|
|
return (col >= 0 && col < table_dims.x) &&
|
|
|
|
|
(row >= 0 && row < table_dims.y);
|
|
|
|
|
}
|