added timeToStr and cmd_users
[NeonServV5.git] / tools.c
diff --git a/tools.c b/tools.c
index 7911e5497eebd773aa63036c31aa2e65fc810254..cb00985435eb4142e9b130aa908e024e93fd14d5 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 */
 /*
@@ -77,25 +95,28 @@ struct Table *table_init(int width, int length, int flags) {
     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->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->c_entry = 0;
+    table->entrys = 0;
     table->maxwidth = calloc(length, sizeof(int));
-    table->table = NULL;
+    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->c_entry][col] = ((table->flags & TABLE_FLAG_USE_POINTER) ? entry[col] : strdup(entry[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->c_entry++;
+    table->entrys++;
+    return 1;
 }
 
 int table_set_bold(struct Table *table, int collum, int bold) {
@@ -103,44 +124,45 @@ int table_set_bold(struct Table *table, int collum, int 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, pos,i;
-    if(table->table) return table->table;
-    table->table = malloc(table->length * sizeof(*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;
     }
-    for(row = 0; row < table->row; row++) {
-        table->table[row] = malloc(tablewidth * sizeof(*table->table[row]))
+    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[row][pos++] = '\002';
+                table->table_lines[row][pos++] = '\002';
             for(i = 0; i < strlen(table->contents[row][col]); i++) {
-                table->table[row][pos++] = table->contents[row][col][i];
+                table->table_lines[row][pos++] = table->contents[row][col][i];
             }
             for(;i < table->maxwidth[col]; i++) {
-                table->table[row][pos++] = ' ';
+                table->table_lines[row][pos++] = ' ';
             }
             if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
-                table->table[row][pos++] = '\002';
+                table->table_lines[row][pos++] = '\002';
             if(col < table->width-1)
-                table->table[row][pos++] = ' ';
+                table->table_lines[row][pos++] = ' ';
             else
-                table->table[row][pos++] = '\0';
+                table->table_lines[row][pos++] = '\0';
         }
     }
-    return table->table;
+    return table->table_lines;
 }
 
 void table_free(struct Table *table) {
     int row, col;
-    for(row = 0; row < length; row++) {
-        if(!(table->flags & TABLE_FLAG_USE_POINTER)) {
+    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]);
             }
@@ -150,12 +172,62 @@ void table_free(struct Table *table) {
     free(table->contents);
     free(table->col_flags);
     free(table->maxwidth);
-    if(table->table) {
-        for(row = 0; row < length; row++) {
-            free(table->table[row]);
+    if(table->table_lines) {
+        for(row = 0; row < table->entrys; row++) {
+            free(table->table_lines[row]);
         }
-        free(table->table);
+        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 = 86400
+        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);
+}