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);
+}
+
#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