OSDN Git Service

change date function in wiki.
authorvisor <visor@users.sourceforge.jp>
Mon, 23 Nov 2009 14:12:00 +0000 (23:12 +0900)
committervisor <visor@users.sourceforge.jp>
Mon, 23 Nov 2009 14:12:00 +0000 (23:12 +0900)
lib/motoroutput.cc
lib/motoroutput.h
wiki/wikiline.cc

index b9ae3f4..e1b02f0 100644 (file)
@@ -192,6 +192,30 @@ static void  format_int0 (ustring& ans, MNode* a, std::vector<ustring>& par) {
     format_int_sub (ans, a, par, true);
 }
 
+static void  format_int (ustring& ans, MNode* a, int c, bool pad0 = false) {
+    uint32_t  v = 0;
+    char  buf[32];
+    size_t  s;
+
+    if (a)
+       v = to_int (a);
+
+    if (c > 0) {
+       if (c > 20)
+           c = 20;
+       if (pad0)
+           s = snprintf (buf, 32, "%.*ld", c, v);
+       else
+           s = snprintf (buf, 32, "%*ld", c, v);
+       if (s > c)
+           ans.append (buf + s - c, c);
+       else
+           ans.append (buf, s);
+    } else {
+       ans.append (boost::lexical_cast<ustring> (v));
+    }
+}
+
 static void  format_float (ustring& ans, MNode* a, std::vector<ustring>& par) {
     int  p1 = 0;
     int  p2 = 0;
@@ -343,4 +367,75 @@ ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par)
     return ans;
 }
 
+/*
+ ${Y:4}, ${Y:2}
+ ${M:2}, ${M}
+ ${D:2}, ${D}
+ ${h:2}, ${h}
+ ${m:2}, ${m}
+ ${s:2}, ${s}
+ ${W}, ${w}
+*/
+ustring  formatDateString (const ustring& format, boost::ptr_vector<MNodePtr>& par) {
+    ustring  ans;
+    uiterator  b, e;
+    umatch  m;
+    u_int  i;
+    MNode*  a;
+    static uregex  re ("\\$\\{([YMDhmsWw])(:([0-9]))?\\}");
+
+    b = format.begin ();
+    e = format.end ();
+    while (usearch (b, e, m, re)) {
+       std::vector<ustring>  fpar;
+       ans.append (b, m[0].first);
+       b = m[0].second;
+       switch (*m[1].first) {
+       case 'Y':
+           a = par[0] ();
+           break;
+       case 'M':
+           a = par[1] ();
+           break;
+       case 'D':
+           a = par[2] ();
+           break;
+       case 'h':
+           a = par[3] ();
+           break;
+       case 'm':
+           a = par[4] ();
+           break;
+       case 's':
+           a = par[5] ();
+           break;
+       case 'W':
+       case 'w':
+           a = par[6] ();
+           break;
+       default:
+           a = NULL;
+       }
+
+       if (! m[2].matched) {
+           switch (*m[1].first) {
+           case 'W':
+               format_Week (ans, a, fpar);
+               break;
+           case 'w':
+               format_week (ans, a, fpar);
+               break;
+           default:
+               if (a)
+                   ans.append (to_string (a));
+           }
+       } else {
+           format_int (ans, a, strtol (ustring (m[3].first, m[3].second)), true);
+       }
+    }
+    ans.append (b, e);
+
+    return ans;
+}
+
 /* ============================================================ */
index d1b3c74..b889744 100644 (file)
@@ -69,5 +69,6 @@ class  MotorOutputString: public MotorOutput {
 
 class  MNodePtr;
 ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par);
+ustring  formatDateString (const ustring& format, boost::ptr_vector<MNodePtr>& par);
 
 #endif /* MOTOROUTPUT_H */
index acfecdb..dc1c52e 100644 (file)
@@ -588,7 +588,8 @@ bool  wl_date (std::vector<ustring>& args, const ustring& arg2, MotorOutput* out
        format = uTimeFormat;
 
     datetime_list (par, tm);
-    out->out_HTML (formatString (format, par));
+//    out->out_HTML (formatString (format, par));
+    out->out_HTML (formatDateString (format, par));
     return true;
 }