fixed comment copy fail :D
[NeonServV5.git] / tools.c
diff --git a/tools.c b/tools.c
index 32d4ab3f26f83d4e30ed5d402125e132ef2b155d..3bc3374139748594d1a62848c34d0a4d5dfb9bb8 100644 (file)
--- a/tools.c
+++ b/tools.c
@@ -1,4 +1,22 @@
 #include "tools.h"
+#include "UserNode.h"
+#include "lang.h"
+
+static const struct default_language_entry msgtab[] = {
+    {"TIME_MASK_2_ITEMS", "%s and %s"},
+    {"TIME_MASK_3_ITEMS", "%s, %s and %s"},
+    {"TIME_YEAR", "%d year"},
+    {"TIME_YEARS", "%d years"},
+    {"TIME_DAY", "%d day"},
+    {"TIME_DAYS", "%d days"},
+    {"TIME_HOUR", "%d hour"},
+    {"TIME_HOURS", "%d hours"},
+    {"TIME_MINUTE", "%d minute"},
+    {"TIME_MINUTES", "%d minutes"},
+    {"TIME_SECOND", "%d second"},
+    {"TIME_SECONDS", "%d seconds"},
+    {NULL, NULL}
+};
 
 /* copied from IRCU 2.10.12 match.c */
 /*
@@ -68,4 +86,148 @@ 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[row]));
+    }
+    table->length = length;
+    table->width = width;
+    table->flags = flags;
+    table->col_flags = calloc(length, sizeof(int));
+    table->entrys = 0;
+    table->maxwidth = calloc(length, sizeof(int));
+    table->table_lines = NULL;
+    return table;
+}
+
+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->maxwidth[col] = strlen(entry[col]);
+    }
+    table->entrys++;
+    return 1;
+}
+
+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;
+    return 1;
+}
+
+char **table_end(struct Table *table) {
+    int row, col, tablewidth = 0, pos,i;
+    if(!table->entrys) return NULL;
+    for(col = 0; col < table->width; col++) {
+        tablewidth += table->maxwidth[col]+1;
+        if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
+            tablewidth += 2;
+    }
+    table->table_lines = malloc(table->entrys * sizeof(table->table_lines));
+    for(row = 0; row < table->entrys; row++) {
+        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_BOLD)
+                table->table_lines[row][pos++] = '\002';
+            for(i = 0; i < strlen(table->contents[row][col]); i++) {
+                table->table_lines[row][pos++] = table->contents[row][col][i];
+            }
+            for(;i < table->maxwidth[col]; i++) {
+                table->table_lines[row][pos++] = ' ';
+            }
+            if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
+                table->table_lines[row][pos++] = '\002';
+            if(col < table->width-1)
+                table->table_lines[row][pos++] = ' ';
+            else
+                table->table_lines[row][pos++] = '\0';
+        }
+    }
+    return table->table_lines;
+}
+
+void table_free(struct Table *table) {
+    int row, col;
+    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]);
+            }
+        }
+        free(table->contents[row]);
+    }
+    free(table->contents);
+    free(table->col_flags);
+    free(table->maxwidth);
+    if(table->table_lines) {
+        for(row = 0; row < table->entrys; row++) {
+            free(table->table_lines[row]);
+        }
+        free(table->table_lines);
+    }
+    free(table);
+}
+
+int timeToStr(struct UserNode *user, int seconds, int items, char *buf) {
+    char *item[items];
+    int tmp, citem = 0;
+    if(citem != items && seconds >= 31536000) { //60*60*24*365 = 31536000
+        
+        tmp = seconds / 31536000;
+        item[citem++] = build_language_string(user, NULL, (tmp == 1 ? "TIME_YEAR" : "TIME_YEARS"), tmp);
+        seconds -= tmp * 31536000;
+    }
+    if(citem != items && seconds >= 86400) { //60*60*24 = 86400
+        tmp = seconds / 86400;
+        item[citem++] = build_language_string(user, NULL, (tmp == 1 ? "TIME_DAY" : "TIME_DAYS"), tmp);
+        seconds -= tmp * 86400;
+    }
+    if(citem != items && seconds >= 3600) { //60*60 = 3600
+        tmp = seconds / 3600;
+        item[citem++] = build_language_string(user, NULL, (tmp == 1 ? "TIME_HOUR" : "TIME_HOURS"), tmp);
+        seconds -= tmp * 3600;
+    }
+    if(citem != items && seconds >= 60) {
+        tmp = seconds / 60;
+        item[citem++] = build_language_string(user, NULL, (tmp == 1 ? "TIME_MINUTE" : "TIME_MINUTES"), tmp);
+        seconds -= tmp * 60;
+    }
+    if(citem != items && seconds >= 1) {
+        item[citem++] = build_language_string(user, NULL, (seconds == 1 ? "TIME_SECOND" : "TIME_SECONDS"), seconds);
+    }
+    if(items == 2) {
+        build_language_string(user, buf, "TIME_MASK_2_ITEMS", item[0], item[1]);
+    } else if(items == 3) {
+        build_language_string(user, buf, "TIME_MASK_3_ITEMS", item[0], item[1], item[2]);
+    } else {
+        int i, ii, p = 0;
+        for(i = 0; i < items; i++) {
+            for(ii = 0; ii < strlen(item[i]); ii++) {
+                buf[p++] = item[i][ii];
+            }
+            buf[p++] = ' ';
+        }
+        buf[p-1] = '\0';
+    }
+    return 1;
+}
+
+
+
+
+void init_tools() {
+    register_default_language_table(msgtab);
+}