added table system
[NeonServV5.git] / tools.c
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);
+}
+