added table system
authorpk910 <philipp@zoelle1.de>
Sun, 21 Aug 2011 02:42:51 +0000 (04:42 +0200)
committerpk910 <philipp@zoelle1.de>
Sun, 21 Aug 2011 02:42:51 +0000 (04:42 +0200)
bot_NeonServ.c
tools.c
tools.h

index 26414478aece30bcc688cf3a5186764cca3a2de8..feb16168bd36386698c12d3aa7156055336a136f 100644 (file)
@@ -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 32d4ab3f26f83d4e30ed5d402125e132ef2b155d..7911e5497eebd773aa63036c31aa2e65fc810254 100644 (file)
--- 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 089a760693d69f2e3d341997e2d429b4e7d54452..22c1301ef0f236512ce32f8f3ade670238bdaa20 100644 (file)
--- 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