]> saetta.ns0.it Git - reptool/libreptool/commitdiff
Enabled formatting for @Date and @Time special values.
authorAndrea Zagli <azagli@libero.it>
Mon, 6 Sep 2010 16:05:23 +0000 (18:05 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 6 Sep 2010 16:05:23 +0000 (18:05 +0200)
src/lexycal.fl
src/lexycal.yy.c
src/lexycal.yy.h
src/rptreport.c

index 0ef117dc37e751bb4aebf2505c5a506ba8fafac8..beaec2a80d2c0f3074519521b5c0842f518f0290 100644 (file)
@@ -37,7 +37,9 @@ DIGIT [0-9]
 "@Page" |
 "@Pages" |
 "@Date" |
-"@Time"        {
+"@Date{"[^}]*"}" |
+"@Time" |
+"@Time{"[^}]*"}"       {
                /*printf ("A special value: %s\n", yytext);*/
                yylval = g_strdup (yytext);
                return SPECIAL;
index b61fc35b7e973669e0df1eda476798b54fdfeaf5..376f157ea33874ab1f46a76c1cf8ff78818c92db 100644 (file)
@@ -367,8 +367,8 @@ static void yy_fatal_error (yyconst char msg[]  );
        *yy_cp = '\0'; \
        (yy_c_buf_p) = yy_cp;
 
-#define YY_NUM_RULES 12
-#define YY_END_OF_BUFFER 13
+#define YY_NUM_RULES 14
+#define YY_END_OF_BUFFER 15
 /* This struct is not used in this scanner,
    but its presence is necessary. */
 struct yy_trans_info
@@ -376,12 +376,13 @@ struct yy_trans_info
        flex_int32_t yy_verify;
        flex_int32_t yy_nxt;
        };
-static yyconst flex_int16_t yy_accept[37] =
+static yyconst flex_int16_t yy_accept[43] =
     {   0,
-        0,    0,   13,   11,   11,   11,    9,    1,   11,   11,
-       11,    0,    3,    2,    1,    0,    0,    0,    0,    0,
-        0,    0,    2,    0,    0,    0,   10,    4,    0,    0,
-        0,    7,    5,    8,    6,    0
+        0,    0,   15,   13,   13,   13,   11,    1,   13,   13,
+       13,    0,    3,    2,    1,    0,    0,    0,    0,    0,
+        0,    0,    2,    0,    0,    0,   12,    4,    0,    0,
+        0,    7,    5,    9,    0,    6,    0,    0,    8,    0,
+       10,    0
     } ;
 
 static yyconst flex_int32_t yy_ec[256] =
@@ -399,7 +400,7 @@ static yyconst flex_int32_t yy_ec[256] =
 
        23,   15,   24,   15,   25,   15,   15,   15,   26,   15,
        15,   15,   15,   15,   27,   28,   15,   15,   15,   15,
-       15,   15,    1,    1,    1,    1,    1,    1,    1,    1,
+       15,   15,   29,    1,   30,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
@@ -416,57 +417,61 @@ static yyconst flex_int32_t yy_ec[256] =
         1,    1,    1,    1,    1
     } ;
 
-static yyconst flex_int32_t yy_meta[29] =
+static yyconst flex_int32_t yy_meta[31] =
     {   0,
         1,    2,    3,    1,    1,    3,    1,    1,    1,    1,
         1,    1,    3,    1,    3,    3,    3,    3,    1,    1,
-        3,    3,    3,    3,    3,    3,    3,    3
+        3,    3,    3,    3,    3,    3,    3,    3,    1,    1
     } ;
 
-static yyconst flex_int16_t yy_base[40] =
+static yyconst flex_int16_t yy_base[48] =
     {   0,
-        0,    0,   65,   66,   66,   60,   66,   18,   16,   32,
-        0,   59,   66,   49,   26,   39,   38,   34,   37,   51,
-       38,   37,   43,   27,   30,   27,   66,   32,   28,   26,
-       19,   66,    9,   66,   66,   66,   44,   27,   47
+        0,    0,   79,   80,   80,   74,   80,   20,   18,   34,
+        0,   73,   80,   63,   28,   53,   52,   48,   39,   65,
+       40,   51,   57,   41,   44,   41,   80,   46,   42,   41,
+       40,   33,   34,   31,   29,   80,   21,   14,   80,    8,
+       80,   80,   46,   29,   49,   52,   55
     } ;
 
-static yyconst flex_int16_t yy_def[40] =
+static yyconst flex_int16_t yy_def[48] =
     {   0,
-       36,    1,   36,   36,   36,   37,   36,   36,   36,   38,
-       39,   37,   36,   36,   36,   36,   36,   36,   36,   36,
-       38,   39,   36,   36,   36,   36,   36,   39,   36,   36,
-       36,   36,   36,   36,   36,    0,   36,   36,   36
+       42,    1,   42,   42,   42,   43,   42,   42,   42,   44,
+       45,   43,   42,   42,   42,   42,   42,   42,   42,   42,
+       44,   45,   42,   42,   42,   42,   42,   45,   42,   42,
+       42,   42,   42,   42,   46,   42,   47,   46,   42,   47,
+       42,    0,   42,   42,   42,   42,   42
     } ;
 
-static yyconst flex_int16_t yy_nxt[95] =
+static yyconst flex_int16_t yy_nxt[111] =
     {   0,
         4,    4,    5,    6,    7,    7,    7,    7,    7,    7,
         4,    7,    8,    9,   10,   10,   10,   10,   11,    4,
-        4,   10,   10,   10,   10,   10,   10,   10,   14,   21,
-       15,   16,   17,   18,   19,   35,   14,   20,   15,   19,
-       19,   34,   20,   20,   12,   12,   12,   22,   33,   22,
-       32,   28,   31,   30,   29,   23,   28,   27,   26,   25,
-       24,   23,   13,   13,   36,    3,   36,   36,   36,   36,
-       36,   36,   36,   36,   36,   36,   36,   36,   36,   36,
-       36,   36,   36,   36,   36,   36,   36,   36,   36,   36,
-       36,   36,   36,   36
-
+        4,   10,   10,   10,   10,   10,   10,   10,    4,    4,
+       14,   21,   15,   16,   17,   18,   19,   41,   14,   20,
+       15,   19,   19,   39,   20,   20,   12,   12,   12,   22,
+       41,   22,   38,   38,   38,   40,   40,   40,   39,   37,
+       36,   35,   34,   33,   32,   28,   31,   30,   29,   23,
+       28,   27,   26,   25,   24,   23,   13,   13,   42,    3,
+       42,   42,   42,   42,   42,   42,   42,   42,   42,   42,
+       42,   42,   42,   42,   42,   42,   42,   42,   42,   42,
+
+       42,   42,   42,   42,   42,   42,   42,   42,   42,   42
     } ;
 
-static yyconst flex_int16_t yy_chk[95] =
+static yyconst flex_int16_t yy_chk[111] =
     {   0,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
-        1,    1,    1,    1,    1,    1,    1,    1,    8,   38,
-        8,    9,    9,    9,   10,   33,   15,   10,   15,   19,
-       21,   31,   19,   21,   37,   37,   37,   39,   30,   39,
-       29,   28,   26,   25,   24,   23,   22,   20,   18,   17,
-       16,   14,   12,    6,    3,   36,   36,   36,   36,   36,
-       36,   36,   36,   36,   36,   36,   36,   36,   36,   36,
-       36,   36,   36,   36,   36,   36,   36,   36,   36,   36,
-       36,   36,   36,   36
-
+        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
+        8,   44,    8,    9,    9,    9,   10,   40,   15,   10,
+       15,   19,   21,   38,   19,   21,   43,   43,   43,   45,
+       37,   45,   46,   46,   46,   47,   47,   47,   35,   34,
+       33,   32,   31,   30,   29,   28,   26,   25,   24,   23,
+       22,   20,   18,   17,   16,   14,   12,    6,    3,   42,
+       42,   42,   42,   42,   42,   42,   42,   42,   42,   42,
+       42,   42,   42,   42,   42,   42,   42,   42,   42,   42,
+
+       42,   42,   42,   42,   42,   42,   42,   42,   42,   42
     } ;
 
 static yy_state_type yy_last_accepting_state;
@@ -490,7 +495,7 @@ char *yytext;
 #include <glib.h>
 
 #include "parser.tab.h"
-#line 494 "lexycal.yy.c"
+#line 499 "lexycal.yy.c"
 
 #define INITIAL 0
 
@@ -680,7 +685,7 @@ YY_DECL
 #line 11 "lexycal.fl"
 
 
-#line 684 "lexycal.yy.c"
+#line 689 "lexycal.yy.c"
 
        if ( !(yy_init) )
                {
@@ -733,13 +738,13 @@ yy_match:
                        while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
                                {
                                yy_current_state = (int) yy_def[yy_current_state];
-                               if ( yy_current_state >= 37 )
+                               if ( yy_current_state >= 43 )
                                        yy_c = yy_meta[(unsigned int) yy_c];
                                }
                        yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
                        ++yy_cp;
                        }
-               while ( yy_base[yy_current_state] != 66 );
+               while ( yy_base[yy_current_state] != 80 );
 
 yy_find_action:
                yy_act = yy_accept[yy_current_state];
@@ -807,44 +812,51 @@ case 6:
 case 7:
 #line 40 "lexycal.fl"
 case 8:
+/* rule 8 can match eol */
+#line 41 "lexycal.fl"
+case 9:
+/* rule 9 can match eol */
+#line 42 "lexycal.fl"
+case 10:
+/* rule 10 can match eol */
 YY_RULE_SETUP
-#line 40 "lexycal.fl"
+#line 42 "lexycal.fl"
 {
                /*printf ("A special value: %s\n", yytext);*/
                yylval = g_strdup (yytext);
                return SPECIAL;
                }
        YY_BREAK
-case 9:
+case 11:
 YY_RULE_SETUP
-#line 46 "lexycal.fl"
+#line 48 "lexycal.fl"
 {
                                        /*printf ("An operator: %s\n", yytext );*/
                                        yylval = NULL;
                                        return (int)yytext[0];
                                        }
        YY_BREAK
-case 10:
+case 12:
 YY_RULE_SETUP
-#line 52 "lexycal.fl"
+#line 54 "lexycal.fl"
 {
                                                                /*printf ("A function: %s\n", yytext);*/
                                                                yylval = g_strdup (yytext);
                                                                return FUNCTION;
                                                                }
        YY_BREAK
-case 11:
-/* rule 11 can match eol */
+case 13:
+/* rule 13 can match eol */
 YY_RULE_SETUP
-#line 58 "lexycal.fl"
+#line 60 "lexycal.fl"
 /* eat up unmatched chars */
        YY_BREAK
-case 12:
+case 14:
 YY_RULE_SETUP
-#line 59 "lexycal.fl"
+#line 61 "lexycal.fl"
 ECHO;
        YY_BREAK
-#line 848 "lexycal.yy.c"
+#line 860 "lexycal.yy.c"
 case YY_STATE_EOF(INITIAL):
        yyterminate();
 
@@ -1136,7 +1148,7 @@ static int yy_get_next_buffer (void)
                while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
                        {
                        yy_current_state = (int) yy_def[yy_current_state];
-                       if ( yy_current_state >= 37 )
+                       if ( yy_current_state >= 43 )
                                yy_c = yy_meta[(unsigned int) yy_c];
                        }
                yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
@@ -1164,11 +1176,11 @@ static int yy_get_next_buffer (void)
        while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
                {
                yy_current_state = (int) yy_def[yy_current_state];
-               if ( yy_current_state >= 37 )
+               if ( yy_current_state >= 43 )
                        yy_c = yy_meta[(unsigned int) yy_c];
                }
        yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
-       yy_is_jam = (yy_current_state == 36);
+       yy_is_jam = (yy_current_state == 42);
 
        return yy_is_jam ? 0 : yy_current_state;
 }
@@ -1842,4 +1854,4 @@ void yyfree (void * ptr )
 
 #define YYTABLES_NAME "yytables"
 
-#line 59 "lexycal.fl"
+#line 61 "lexycal.fl"
index 0cee202907eb43b5060db7e4950d41b26b3e651e..7475d3fb01ed034843a1722074d426e5b6989abd 100644 (file)
@@ -322,7 +322,7 @@ extern int yylex (void);
 #undef YY_DECL
 #endif
 
-#line 59 "lexycal.fl"
+#line 61 "lexycal.fl"
 
 #line 328 "lexycal.yy.h"
 #undef yyIN_HEADER
index f6a480acba657d49822ca6488f87f78e9ae7b419..ef919f0f5ea764b0bed482245134ac1bbe7e9ad9 100644 (file)
@@ -2126,36 +2126,136 @@ gchar
        return ret;
 }
 
+/**
+ * rpt_report_replace_str:
+ * @string: the string where make the replace.
+ * @origin: the string to replace.
+ * @replace: the string to insert.
+ *
+ * Returns: a string with replaced string. Must be freed.
+ */
+static gchar
+*rpt_report_str_replace (const gchar *string,
+                         const gchar *origin,
+                         const gchar *replace)
+{
+       gchar *ret;
+       gchar *p;
+
+       p = g_strstr_len (string, -1, origin);
+
+       if (p == NULL)
+               {
+                       return g_strdup (string);
+               }
+
+       ret = g_strndup (string, p - string);
+
+       ret = g_strdup_printf ("%s%s%s", ret, replace, p + strlen (origin));
+
+       return ret;
+}
+
+/**
+ * rpt_report_get_str_from_tm:
+ * @datetime: a tm struct.
+ * @format:
+ *
+ * Returns: a string representation of @datetime based on the format in @format.
+ * It interprets a very little subset of format identifiers from strftime.
+ * %Y: the year with 4 digits.
+ * %m: the month with 2 digits.
+ * %d: the day with 2 digits.
+ * %H: the hours with 2 digits 00-24.
+ * %M: the minutes with 2 digits 00-59.
+ * %S: the seconds with 2 digits 00-59.
+ */
+gchar
+*rpt_report_get_str_from_tm (struct tm *datetime,
+                             const gchar *format)
+{
+       gchar *ret;
+
+       ret = g_strdup ("");
+
+       g_return_val_if_fail (datetime != NULL, ret);
+
+       ret = rpt_report_str_replace (format, "%Y",
+                                     g_strdup_printf ("%04u", datetime->tm_year + 1900));
+       ret = rpt_report_str_replace (ret, "%m",
+                                     g_strdup_printf ("%02u", datetime->tm_mon + 1));
+       ret = rpt_report_str_replace (ret, "%d",
+                                     g_strdup_printf ("%02u", datetime->tm_mday));
+       ret = rpt_report_str_replace (ret, "%H",
+                                     g_strdup_printf ("%02u", datetime->tm_hour));
+       ret = rpt_report_str_replace (ret, "%M",
+                                     g_strdup_printf ("%02u", datetime->tm_min));
+       ret = rpt_report_str_replace (ret, "%S",
+                                     g_strdup_printf ("%02u", datetime->tm_sec));
+
+       return ret;
+}
+
 gchar
 *rpt_report_get_special (RptReport *rpt_report, const gchar *special, gint row)
 {
-       gchar *ret = NULL;
+       gchar *ret;
+       gchar *real_special;
+
+       if (special == NULL) return "";
 
        RptReportPrivate *priv = RPT_REPORT_GET_PRIVATE (rpt_report);
 
-       if (strcmp (special, "@Page") == 0)
+       ret = g_strdup ("");
+       real_special = g_strstrip (g_strdup (special));
+
+       if (strncmp (real_special, "@Page", 5) == 0)
                {
                        ret = g_strdup_printf ("%d", priv->cur_page);
                }
-       else if (strcmp (special, "@Pages") == 0)
+       else if (strncmp (real_special, "@Pages", 6) == 0)
                {
                        ret = g_strdup ("@Pages");
                }
-       else if (strcmp (special, "@Date") == 0)
+       else if (strncmp (real_special, "@Date", 5) == 0)
                {
-                       char date[11] = "\0";
+                       gchar *format;
                        time_t now = time (NULL);
                        struct tm *tm = localtime (&now);
-                       strftime (date, 11, "%F", tm);
-                       ret = g_strdup_printf ("%s", &date);
+
+                       if (strlen (real_special) > 5
+                           && real_special[5] == '{'
+                           && real_special[strlen (real_special) - 1] == '}')
+                               {
+                                       format = g_strndup (real_special + 6, strlen (real_special + 6) - 1);
+                               }
+                       else
+                               {
+                                       /* TODO get from locale */
+                                       format = g_strdup ("%Y-%m-%d");
+                               }
+
+                       ret = g_strdup_printf ("%s", rpt_report_get_str_from_tm (tm, format));
                }
-       else if (strcmp (special, "@Time") == 0)
+       else if (strncmp (real_special, "@Time", 5) == 0)
                {
-                       char date[6] = "";
+                       gchar *format;
                        time_t now = time (NULL);
                        struct tm *tm = localtime (&now);
-                       strftime (date, 6, "%H:%M", tm);
-                       ret = g_strdup_printf ("%s", &date);
+
+                       if (strlen (real_special) > 5
+                           && real_special[5] == '{'
+                           && real_special[strlen (real_special) - 1] == '}')
+                               {
+                                       format = g_strndup (real_special + 6, strlen (real_special + 6) - 1);
+                               }
+                       else
+                               {
+                                       /* TODO get from locale */
+                                       format = g_strdup ("%H:%M:%S");
+                               }
+
+                       ret = g_strdup_printf ("%s", rpt_report_get_str_from_tm (tm, format));
                }
 
        return ret;