added possibilities to change table content after adding it & added cmd_bans
[NeonServV5.git] / tools.c
diff --git a/tools.c b/tools.c
index a6b6f531f88e8c7368773fa49ae0086258d47e63..2f8427f6efc70b08eb47cff2dcebd41ed2bb30b8 100644 (file)
--- a/tools.c
+++ b/tools.c
@@ -117,14 +117,44 @@ int table_add(struct Table *table, char **entry) {
     int col;
     if(table->entrys == table->length) return 0;
     for(col = 0; col < table->width; col++) {
-        table->contents[table->entrys][col] = ((table->flags & TABLE_FLAG_USE_POINTER) ? entry[col] : strdup(entry[col]));
-        if(strlen(entry[col]) > table->maxwidth[col])
+        table->contents[table->entrys][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col]));
+        if(table->contents[table->entrys][col])
+            table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
+        if(entry[col] && strlen(entry[col]) > table->maxwidth[col])
             table->maxwidth[col] = strlen(entry[col]);
     }
     table->entrys++;
     return 1;
 }
 
+int table_change(struct Table *table, int row, char **entry) {
+    int col;
+    if(row >= table->length) return 0;
+    for(col = 0; col < table->width; col++) {
+        if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER))
+            free(table->contents[row][col]);
+        table->contents[row][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col]));
+        if(table->contents[row][col])
+            table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
+        if(entry[col] && strlen(entry[col]) > table->maxwidth[col])
+            table->maxwidth[col] = strlen(entry[col]);
+    }
+    return 1;
+}
+
+int table_change_field(struct Table *table, int row, int col, char *entry) {
+    if(row >= table->length) return 0;
+    if(col >= table->width) return 0;
+    if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER))
+        free(table->contents[row][col]);
+    table->contents[row][col] = (((table->flags & TABLE_FLAG_USE_POINTER) || !entry) ? entry : strdup(entry));
+    if(table->contents[row][col])
+        table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
+    if(entry && strlen(entry) > table->maxwidth[col])
+        table->maxwidth[col] = strlen(entry);
+    return 1;
+}
+
 int table_set_bold(struct Table *table, int collum, int bold) {
     if(bold)
         table->col_flags[collum] |= TABLE_FLAG_COL_BOLD;
@@ -146,6 +176,7 @@ char **table_end(struct Table *table) {
         table->table_lines[row] = malloc(tablewidth * sizeof(*table->table_lines[row]));
         pos = 0;
         for(col = 0; col < table->width; col++) {
+            if(!(table->col_flags[col] & TABLE_FLAG_COL_CONTENTS)) continue;
             if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
                 table->table_lines[row][pos++] = '\002';
             for(i = 0; i < strlen(table->contents[row][col]); i++) {
@@ -171,7 +202,8 @@ void table_free(struct Table *table) {
     for(row = 0; row < table->length; row++) {
         if(!(table->flags & TABLE_FLAG_USE_POINTER) && table->entrys > row) {
             for(col = 0; col < table->width; col++) {
-                free(table->contents[row][col]);
+                if(table->contents[row][col])
+                    free(table->contents[row][col]);
             }
         }
         free(table->contents[row]);