]> git.deb.at Git - pkg/abook.git/blobdiff - edit.c
Fixed dcgettext() definition (not used anywhere, but still...)
[pkg/abook.git] / edit.c
diff --git a/edit.c b/edit.c
index a08fdc9599cee2fb87aefb22ff439684a20f488c..940a57db0b05739be47997ce61d00c756652158b 100644 (file)
--- a/edit.c
+++ b/edit.c
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
+#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
+#       include <locale.h>
+#endif 
+
+
+static void locale_date(char *str, size_t str_len, int year, int month, int day);
 
 /*
  * some extern variables
@@ -33,8 +39,6 @@ extern int views_count;
 
 WINDOW *editw;
 
-static int parse_date_string(char *s, int *day, int *month, int *year);
-
 
 static void
 editor_tab(const int tab)
@@ -244,19 +248,18 @@ editor_print_data(int tab, int item)
                        abook_list_free(&emails);
                } else if(cur->field->type == FIELD_DATE) {
                        int day, month, year;
-                       char buf[12];
+                       char buf[64];
 
+                       /* put raw representation of date in buf */
                        find_field_number(cur->field->key, &nb);
                        if((str = db_fget_byid(item, nb)) != NULL)
                                strncpy(buf, str, sizeof(buf));
 
                        if(str && parse_date_string(buf, &day, &month, &year)) {
-                               str = strdup_printf(year ? " %04d-%02d-%02d" :
-                                       "%c%02d-%02d", year ? year : ' ',
-                                       month, day);
-                               mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
-                                       bytes2width(str, FIELD_MAX_WIDTH));
-                               free(str);
+                               /* put locale representation of date in buf */
+                               locale_date(buf, sizeof(buf), year, month, day);
+                               mvwaddnstr(editw, y, TAB_COLON_POS + 2, buf,
+                                       bytes2width(buf, FIELD_MAX_WIDTH));
                        }
                } else {
                        find_field_number(cur->field->key, &nb);
@@ -286,7 +289,7 @@ editor_print_data(int tab, int item)
  *  valid string
  */
 static int
-change_field(char *msg, char **field, int max_len)
+change_field(char *msg, char **field, size_t max_len)
 {
        char *old;
        int ret = 0;
@@ -311,7 +314,7 @@ change_field(char *msg, char **field, int max_len)
 }
 
 static int
-change_name_field(char *msg, char **field, int max_len)
+change_name_field(char *msg, char **field, size_t max_len)
 {
        char *tmp;
        int ret;
@@ -396,6 +399,59 @@ edit_list(int item, int nb, int isemail)
        abook_list_free(&list);
 }
 
+/*
+ * str is a buffer of max length str_len, which, after calling, will
+ * contain a representation of the given [y, m, d] date using the
+ * current locale (as defined by LC_TIME).
+ *
+ * Default is an ISO 8601 representation.
+ *
+ * %-sequences available to translators: %y, %Y, %m, %M, %d, %D represent
+ * year, month, and day (the uppercase version telling to fill with leading
+ * zeros if necessary)
+ */
+static void
+locale_date(char *str, size_t str_len, int year, int month, int day)
+{
+       char *s = str, *fmt;
+       size_t len;
+
+#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
+       fmt = year ?    dcgettext(PACKAGE, "%Y-%M-%D", LC_TIME) :
+                       dcgettext(PACKAGE, "--%M-%D", LC_TIME);
+#else
+       if(year)
+               snprintf(str, str_len, "%04d-%02d-%02d", year, month, day);
+       else
+               snprintf(str, str_len, "--%02d-%02d", month, day);
+       return;
+#endif
+
+       while(*fmt && (s - str + 1 < str_len)) {
+               if(*fmt != '%') {
+                       *s++ = *fmt++;
+                       continue;
+               }
+
+               len = str_len - (str - s);
+               switch(*++fmt) {
+                       case 'y': s += snprintf(s, len, "%d", year); break;
+                       case 'Y': s += snprintf(s, len, "%04d", year); break;
+                       case 'm': s += snprintf(s, len, "%d", month); break;
+                       case 'M': s += snprintf(s, len, "%02d", month); break;
+                       case 'd': s += snprintf(s, len, "%d", day); break;
+                       case 'D': s += snprintf(s, len, "%02d", day); break;
+                       case '%': /* fall through */
+                       default:
+                               *s++ = '%';
+                               *s++ = *fmt;
+                               break;
+               }
+               fmt++;
+       }
+       *++s = 0;
+}
+
 static int is_valid_date(const int day, const int month, const int year)
 {
        int valid = 1;
@@ -418,7 +474,7 @@ static int is_valid_date(const int day, const int month, const int year)
        return valid;
 }
 
-static int
+int
 parse_date_string(char *s, int *day, int *month, int *year)
 {
        int i = 0;
@@ -440,8 +496,8 @@ parse_date_string(char *s, int *day, int *month, int *year)
                                return FALSE;
                        *s++ = '\0';
                        switch(i) {
-                               case 1: *year = atoi(p); break;
-                               case 2: *month = atoi(p); break;
+                               case 1: *year = safe_atoi(p); break;
+                               case 2: *month = safe_atoi(p); break;
                        }
                        p = s;
                } else
@@ -456,17 +512,6 @@ parse_date_string(char *s, int *day, int *month, int *year)
        return is_valid_date(*day, *month, *year);
 }
 
-static int
-is_number(char *s)
-{
-       char *p;
-
-       for(p = s; *p; p++)
-               if(!isdigit(*p))
-                       return FALSE;
-       return TRUE;
-}
-
 static void
 edit_date(int item, int nb)
 {
@@ -497,8 +542,12 @@ edit_date(int item, int nb)
 
        /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
        if(is_valid_date(date[0], date[1], date[2])) {
-               s = strdup_printf(date[2] ? "%04d-%02d-%02d" : "%c-%02d-%02d",
-                       date[2] ? date[2] : '-', date[1], date[0]);
+               if(date[2])
+                       s = strdup_printf("%04d-%02d-%02d",
+                               date[2], date[1], date[0]);
+               else
+                       s = strdup_printf("--%02d-%02d", date[1], date[0]);
+
                db_fput_byid(item, nb, xstrdup(s));
        } else
                statusline_msg(_("Invalid date"));