From: pk910 Date: Sun, 21 Aug 2011 02:42:51 +0000 (+0200) Subject: added table system X-Git-Tag: v5.3~507 X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=commitdiff_plain;h=743aa67e614a5c6afa741bcb999fe35957c7237c added table system --- diff --git a/bot_NeonServ.c b/bot_NeonServ.c index 2641447..feb1616 100644 --- a/bot_NeonServ.c +++ b/bot_NeonServ.c @@ -12,6 +12,7 @@ #include "HandleInfoHandler.h" #include "WHOHandler.h" #include "DBHelper.h" +#include "tools.h" #define BOTID 1 diff --git a/tools.c b/tools.c index 32d4ab3..7911e54 100644 --- a/tools.c +++ b/tools.c @@ -68,4 +68,94 @@ int match(const char *mask, const char *name) n++; break; } -} \ No newline at end of file +} + + +//TABLES +struct Table *table_init(int width, int length, int flags) { + int row; + struct Table *table = malloc(sizeof(*table)); + table->contents = malloc(length * sizeof(*table->contents)); + for(row = 0; row < length; row++) { + table->contents[row] = calloc(width, sizeof(*table->contents)); + } + table->length = length; + table->width = width; + table->flags = flags; + table->col_flags = calloc(length, sizeof(int)); + table->c_entry = 0; + table->maxwidth = calloc(length, sizeof(int)); + table->table = NULL; +} + +int table_add(struct Table *table, char **entry) { + int col; + for(col = 0; col < table->width; col++) { + table->contents[table->c_entry][col] = ((table->flags & TABLE_FLAG_USE_POINTER) ? entry[col] : strdup(entry[col])); + if(strlen(entry[col]) > table->maxwidth[col]) + table->maxwidth[col] = strlen(entry[col]); + } + table->c_entry++; +} + +int table_set_bold(struct Table *table, int collum, int bold) { + if(bold) + table->col_flags[collum] |= TABLE_FLAG_COL_BOLD; + else + table->col_flags[collum] &= ~TABLE_FLAG_COL_BOLD; +} + +char **table_end(struct Table *table) { + int row, col, tablewidth, pos,i; + if(table->table) return table->table; + table->table = malloc(table->length * sizeof(*table->table)); + for(col = 0; col < table->width; col++) { + tablewidth += table->maxwidth[col]+1; + if(table->col_flags[col] & TABLE_FLAG_COL_BOLD) + tablewidth += 2; + } + for(row = 0; row < table->row; row++) { + table->table[row] = malloc(tablewidth * sizeof(*table->table[row])) + pos = 0; + for(col = 0; col < table->width; col++) { + if(table->col_flags[col] & TABLE_FLAG_COL_BOLD) + table->table[row][pos++] = '\002'; + for(i = 0; i < strlen(table->contents[row][col]); i++) { + table->table[row][pos++] = table->contents[row][col][i]; + } + for(;i < table->maxwidth[col]; i++) { + table->table[row][pos++] = ' '; + } + if(table->col_flags[col] & TABLE_FLAG_COL_BOLD) + table->table[row][pos++] = '\002'; + if(col < table->width-1) + table->table[row][pos++] = ' '; + else + table->table[row][pos++] = '\0'; + } + } + return table->table; +} + +void table_free(struct Table *table) { + int row, col; + for(row = 0; row < length; row++) { + if(!(table->flags & TABLE_FLAG_USE_POINTER)) { + for(col = 0; col < table->width; col++) { + free(table->contents[row][col]); + } + } + free(table->contents[row]); + } + free(table->contents); + free(table->col_flags); + free(table->maxwidth); + if(table->table) { + for(row = 0; row < length; row++) { + free(table->table[row]); + } + free(table->table); + } + free(table); +} + diff --git a/tools.h b/tools.h index 089a760..22c1301 100644 --- a/tools.h +++ b/tools.h @@ -3,6 +3,28 @@ #include "main.h" +#define TABLE_FLAG_USE_POINTER 0x01 +#define TABLE_FLAG_COL_BOLD 0x02 + +struct Table { + char ***contents; + int length; + int width; + int flags; + int *col_flags; + + int c_entry; + int *maxwidth; + + char **table; //we just store this to free it in table_free +}; + int match(const char *mask, const char *name); +struct Table *table_init(int width, int length, int flags); +int table_add(struct Table *table, char **entry); +int table_set_bold(struct Table *table, int collum, int bold); +char **table_end(struct Table *table); +void table_free(struct Table *table); + #endif \ No newline at end of file