OSDN Git Service

getarray wiki function.
authorvisor <visor@users.sourceforge.jp>
Sat, 20 Oct 2012 05:37:00 +0000 (14:37 +0900)
committervisor <visor@users.sourceforge.jp>
Sat, 20 Oct 2012 05:37:00 +0000 (14:37 +0900)
small fix.

lib/ml.cc
lib/ml.h
lib/util_string.cc
lib/util_string.h
modules/ml-string.cc
wiki/wikiformat.h
wiki/wikiline.cc
wiki/wikiline.h

index 9af4a0d..37cac80 100644 (file)
--- a/lib/ml.cc
+++ b/lib/ml.cc
@@ -73,7 +73,12 @@ double  MNode::to_double () {
     }
 }
 
+bool  to_bool (const ustring& v) {
+    return ! (v.length () == 0 || (v.length () == 1 && v[0] == '0'));
+}
+
 bool  MNode::to_bool () {
+    // 空文字列,文字列の"0",数値の0,nilはfalse
     switch (type) {
     case MNode::MC_NIL:
        return false;
@@ -82,7 +87,7 @@ bool  MNode::to_bool () {
     case MNode::MC_STR:
        return ::to_bool (*str);
     case MNode::MC_SYM:
-       if (sym->length () > 0) {
+       if (sym->length () > 0) { // nilというシンボルはnilではない
            return true;
        } else {
            return false;
@@ -98,56 +103,55 @@ bool  MNode::to_bool () {
     }
 }
 
-ustring  MNode::to_string (bool c, bool listdump) {
+ustring  MNode::cons_to_sexp (bool fcdr) {
+    // fcdr: cdr部の出力
+    ustring  ans;
+
+    assert (type == MC_CONS);
+    if (! fcdr
+       && cons.car && cons.car->type == MC_SYM && cons.car->str->compare ("quote") == 0
+       && cons.cdr && cons.cdr->type == MC_CONS
+       && ::isNil (cons.cdr->cons.cdr)) {
+       ans.append (CharConst ("'"));
+       if (::isNil (cons.cdr->cons.car))
+           ans.append (uNil2);
+       else
+           ans.append (cons.cdr->cons.car->to_sexp ());
+    } else {
+       if (! fcdr)
+           ans.append (CharConst ("("));
+       if (::isNil (cons.car))
+           ans.append (uNil2);
+       else
+           ans.append (cons.car->to_sexp ());
+       if (::isNil (cons.cdr)) {
+           ans.append (CharConst (")"));
+       } else {
+           switch (cons.cdr->type) {
+           case MC_CONS:
+               ans.append (uSPC);
+               ans.append (cons.cdr->cons_to_sexp (true));
+               break;
+           default:
+               ans.append (CharConst (" . "));
+               ans.append (cons.cdr->to_sexp ());
+               ans.append (CharConst (")"));
+           }           
+       }
+    }
+    return ans;
+}
+
+ustring  MNode::to_sexp () {
     switch (type) {
     case MNode::MC_NIL:
-       return uEmpty;
+       return uNil2;
     case MNode::MC_CONS:
-       {
-           ustring  ans;
-           if (! c && cons.car && cons.car->type == MC_SYM && cons.car->str->compare ("quote") == 0 && cons.cdr && cons.cdr->type == MC_CONS) {
-               ans.append (CharConst ("'"));
-               if (! cons.cdr->cons.car)
-                   ans.append (uNil2);
-               else
-                   ans.append (cons.cdr->cons.car->to_string (false, true));
-           } else {
-               if (! c)
-                   ans.append (CharConst ("("));
-               if (cons.car)
-                   ans.append (cons.car->to_string (false, true));
-               else
-                   ans.append (uNil2);
-               if (cons.cdr) {
-                   switch (cons.cdr->type) {
-                   case MC_NIL:
-                       ans.append (CharConst (")"));
-                       break;
-                   case MC_CONS:
-                       ans.append (uSPC);
-                       ans.append (cons.cdr->to_string (true, true));
-                       break;
-                   default:
-                       ans.append (CharConst (" . "));
-                       ans.append (cons.cdr->to_string (true, true));
-                       ans.append (CharConst (")"));
-                   }           
-               } else {
-                   ans.append (CharConst (")"));
-               }
-           }
-           return ans;
-       }
+       return cons_to_sexp ();
     case MNode::MC_STR:
-       if (listdump) {
-           return uQ2 + slashEncode (*str) + uQ2;
-       } else {
-           return ustring (*str);
-//     return *str;
-       }
+       return uQ2 + slashEncode (*str) + uQ2;
     case MNode::MC_SYM:
-       return ustring (*sym);
-//     return *sym;
+       return *sym;
     case MNode::MC_DOUBLE:
        return to_ustring (real);
     default:
@@ -155,113 +159,29 @@ ustring  MNode::to_string (bool c, bool listdump) {
     }
 }
 
-void  MNode::dump (std::ostream& o, bool c) {
+ustring  MNode::to_string () {
     switch (type) {
-    case MC_NIL:
-//     o << uNil;
-       o << uNil2;
-       break;
-    case MC_SYM:
-       o << logText (*sym);
-       break;
-    case MC_STR:
-       o << uQ2 << logText (*str) << uQ2;
-       break;
-    case MC_CONS:
-       if (! c && car () && car ()->type == MC_SYM && car ()->str->compare ("quote") == 0 && cdr () && cdr ()->type == MC_CONS) {
-           o << "'";
-           if (! cdr ()->car ())
-               o << uNil2;
-           else
-               cdr ()->car ()->dump (o);
-       } else {
-           if (! c)
-               o << "(";
-           if (car ())
-               car ()->dump (o);
-           else
-               o << uNil2;
-           if (cdr ()) {
-               switch (cdr ()->type) {
-               case MC_NIL:
-                   o << ")";
-                   break;
-               case MC_CONS:
-                   o << " ";
-                   cdr ()->dump (o, true);
-                   break;
-               default:
-                   o << " . ";
-                   cdr ()->dump (o, true);
-                   o << ")";
-               }
-           } else {
-               o << ")";
-           }
-       }
-       break;
-    case MC_DOUBLE:
-       o << to_ustring (real);
-       break;
+    case MNode::MC_NIL:
+       return uEmpty;
+    case MNode::MC_CONS:
+       return cons_to_sexp ();
+    case MNode::MC_STR:
+       return *str;
+    case MNode::MC_SYM:
+       return *sym;
+    case MNode::MC_DOUBLE:
+       return to_ustring (real);
     default:
        assert (0);
     }
 }
 
+void  MNode::dump (std::ostream& o, bool c) {
+    o << logText (to_sexp ());
+}
+
 ustring  MNode::dump_string (bool c) {
-    switch (type) {
-    case MC_NIL:
-//     return uNil;
-       return uNil2;
-    case MC_CONS:
-       {
-           ustring  ans;
-           if (! c && car () && car ()->type == MC_SYM && car ()->str->compare ("quote") == 0 && cdr () && cdr ()->type == MC_CONS) {
-               ans.append (CharConst ("'"));
-               if (! cdr ()->car ())
-                   ans.append (uNil2);
-               else
-                   ans.append (cdr ()->car ()->dump_string ());
-           } else {
-               if (! c)
-                   ans.append (CharConst ("("));
-               if (car ())
-                   ans.append (car ()->dump_string ());
-               else
-                   ans.append (uNil2);
-               if (cdr ()) {
-                   switch (cdr ()->type) {
-                   case MC_NIL:
-                       ans.append (CharConst (")"));
-                       break;
-                   case MC_CONS:
-                       ans.append (uSPC);
-                       ans.append (cdr ()->dump_string (true));
-                       break;
-                   default:
-                       ans.append (CharConst (" . "));
-                       ans.append (cdr ()->dump_string (true));
-                       ans.append (CharConst (")"));
-                   }
-               } else {
-                   ans.append (CharConst (")"));
-               }
-           }
-           return ans;
-       }
-    case MC_STR:
-       return uQ2 + logText (*str) + uQ2;
-    case MC_SYM:
-       return logText (*sym);
-    case MC_DOUBLE:
-       return to_ustring (real);
-    default:
-       if (type == MC_DELETED)
-           std::cerr << "type:DELETED\n";
-       else
-           std::cerr << "type:" << type << "\n";
-       assert (0);
-    }
+    return logText (to_sexp ());
 }
 
 ustring  MNode::dump_string_short () {
@@ -668,4 +588,3 @@ void  nextNodeNonNil (MNode*& arg) {
     if (! arg)
        throw (uErrorWrongNumber);
 }
-
index aa795c9..152ad41 100644 (file)
--- a/lib/ml.h
+++ b/lib/ml.h
@@ -148,7 +148,9 @@ class  MNode {
        return (int)to_double ();
     };
     bool  to_bool ();
-    ustring  to_string (bool c = false, bool listdump = false);
+    ustring  cons_to_sexp (bool fcdr = false);
+    ustring  to_sexp ();
+    ustring  to_string ();
     void  dump (std::ostream& o, bool c = false);
     ustring  dump_string (bool c = false);
     ustring  dump_string_short ();
@@ -172,6 +174,7 @@ inline int  to_int (MNode* c) {
     return (int)to_double (c);
 }
 
+bool  to_bool (const ustring& v);
 inline bool  to_bool (MNode* c) {
     if (c)
        return c->to_bool ();
@@ -186,6 +189,13 @@ inline ustring  to_string (MNode* c) {
        return uEmpty;
 }
 
+inline ustring  dump_to_sexp (MNode* c) {
+    if (c)
+       return c->to_sexp ();
+    else
+       return uNil2;
+}
+
 inline MNode*  newMNode_sym (ustring* v) {
     MNode*  ans = new MNode;
     ans->set_sym (v);
index 5b41330..a2ed5bf 100644 (file)
@@ -238,60 +238,6 @@ ustring  omitNonAsciiWord (const ustring& str) {
     return omitPattern (str, re);
 }
 
-bool  to_bool (const ustring& v) {
-    if (v.length () == 0 || (v.length () == 1 && v[0] == '0')) {
-       return false;
-    } else {
-       return true;
-    }
-}
-
-#if 0
-static ustring  percentEncode (const ustring& text, uregex& re) {
-    /* $1 -> _
-       $2 -> %HEX
-    */
-    umatch  m;
-    uiterator  b, e;
-    ustring  ans;
-
-    b = text.begin ();
-    e = text.end ();
-    if (b != e && usearch (b, e, m, re)) {
-       if (b != m[0].first) {
-           ans.append (ustring (b, m[0].first));
-       }
-       if (m[1].matched) {
-           ans.append (uUScore);
-       } else if (m[2].matched) {
-           ans.append (percentHex (*m[2].first));
-       } else {
-           assert (0);
-       }
-       b = m[0].second;
-       while (b != e && usearch (b, e, m, re)) {
-           if (b != m[0].first) {
-               ans.append (ustring (b, m[0].first));
-           }
-           if (m[1].matched) {
-               ans.append (uUScore);
-           } else if (m[2].matched) {
-               ans.append (percentHex (*m[2].first));
-           } else {
-               assert (0);
-           }
-           b = m[0].second;
-       }
-       if (b != e) {
-           ans.append (ustring (b, e));
-       }
-       return ans;
-    } else {
-       return text;
-    }
-}
-#endif
-
 static ustring  percentEncode (uiterator b, uiterator e, const uregex& re) {
     // $1 -> _
     // $2 -> %HEX
@@ -316,14 +262,6 @@ static ustring  percentEncode (uiterator b, uiterator e, const uregex& re) {
     return ans;
 }
 
-#if 0
-ustring  urlencode (const ustring& url) {
-    static uregex  re ("(\\x00)|([^a-zA-Z0-9_.,/\x80-\xff-])");
-    
-    return percentEncode (url, re);
-}
-#endif
-
 ustring  percentEncode (uiterator b, uiterator e) {
     static uregex  re ("(\\x00)|([^A-Za-z0-9_.~-])");
 
@@ -422,18 +360,6 @@ ustring  clipColon (const ustring& text) {
     return ans;
 }
 
-#if 0
-ustring  dirPart (char* path) {
-    char*  e = rindex (path, '/');
-
-    if (e && e != path) {
-       return ustring (path, e - path);
-    } else {
-       return uSlash;
-    }
-}
-#endif
-
 ustring  dirPart (const ustring& path) {
     ustring::size_type  s = path.rfind ('/', path.size ());
 
@@ -935,12 +861,6 @@ static ustring::value_type  toLower_ustring_value (ustring::value_type v) {
     }
 }
 
-#if 0
-void  toLower (ustring::iterator* b, ustring::iterator* e) {
-    transform (*b, *e, *b, toLower_ustring_value);
-}
-#endif
-
 ustring  toLower (uiterator b, uiterator e) {
     ustring::iterator  i;
     ustring  ans;
index 7fe8208..1c552c4 100644 (file)
@@ -58,7 +58,6 @@ ustring  omitNul (const ustring& str);
 ustring  omitNL (const ustring& str);
 ustring  omitNonAscii (const ustring& str);
 ustring  omitNonAsciiWord (const ustring& str);
-bool  to_bool (const ustring& v);
 ustring  percentEncode (uiterator b, uiterator e);
 ustring  percentEncode (const ustring& str);
 ustring  percentEncode_path (uiterator b, uiterator e);
@@ -67,7 +66,6 @@ ustring  percentDecode (const ustring& str);
 ustring  cookieencode (const ustring& text);
 ustring  cookiedecode (const ustring& text);
 ustring  clipColon (const ustring& text);
-//ustring  dirPart (char* path);
 ustring  dirPart (const ustring& path);
 ustring  filePart_osSafe (const ustring& path);
 void  split (uiterator b, uiterator e, uregex& re, std::vector<ustring>& ans);
@@ -110,7 +108,6 @@ ustring  toLower (uiterator b, uiterator e);
 
 class  MNodePtr;
 ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par);
-//ustring  formatDateString (const ustring& format, time_t tm);
 ustring  formatDateString (const ustring& format, struct tm& v);
 ustring  toLower (const ustring& str);
 ustring  toUpper (const ustring& str);
index 6ed8741..aaab8a1 100644 (file)
@@ -802,13 +802,7 @@ MNode*  ml_dump_to_sexp (MNode* cell, MlEnv* mlenv) {
        nextNode (arg);
        if (text.length () > 0)
            text.append (CharConst (" "));
-       if (isNil (e ())) {
-           text.append (uNil2);
-       } else if (e ()->isStr ()) {
-           text.append (uQ2).append (slashEncode (*e ()->str)).append (uQ2);
-       } else {
-           text.append (e ()->to_string ());
-       }
+       text.append (dump_to_sexp (e ()));
     }
     return newMNode_str (new ustring (text));
 }
index e9fc331..c131357 100644 (file)
@@ -595,7 +595,7 @@ class  WikiFormat {
        return mlenv->getAry_string (name, i);
     };
     virtual size_t  getArySize (const ustring& name) {
-       return getArySize (name);
+       return mlenv->getArySize (name);
     };
     virtual MNode*  buildArgs (WikiMotorObjVecVec::const_iterator b, WikiMotorObjVecVec::const_iterator e, bool dumpmode = false);
     virtual void  logLispFunctionError (const ustring& msg, const ustring& cmd);
index 0d3009e..eab3d87 100644 (file)
@@ -41,6 +41,7 @@ static void  errorBadParam (WikiMotorObjVec* param, WikiFormat* wiki) {
  [[variable/]]
  [[variable~]]
  [[variable,]]
+// [[@array]]
 
 */
 /*DOC:
@@ -102,6 +103,32 @@ bool  wl_getvar (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wik
     return true;
 }
 
+/*DOC:
+===配列展開===
+ [[getarray:NAME]]
+
+*/
+//#WIKILINE    getarray        wl_getarray
+bool  wl_getarray (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki) {
+    MotorOutputWiki  o (out);
+    ustring  var;
+    ustring  val;
+    size_t  i, n;
+    MNodeList  e;
+
+    if (args->size () == 0 || args->size () > 1)
+       return false;
+    var = (*args)[0]->textOut (wiki);
+    n = wiki->mlenv->getArySize (var);
+    for (i = 1; i <= n; i ++) {
+       e.append (wiki->mlenv->getAry (var, i));
+    }
+    val = fixUTF8 (dump_to_sexp (e ()));
+    o.outamp (val);
+
+    return true;
+}
+
 /* ============================================================ */
 /*DOC:
 ===強調===
index 61dfc01..a7befc7 100644 (file)
@@ -7,6 +7,7 @@ class  WikiMotorObjVec;
 class  WikiMotorObjVecVec;
 
 bool  wl_getvar (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki);
+bool  wl_getarray (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki);
 bool  wl_italic (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki);
 bool  wl_bold (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki);
 bool  wl_bolditalic (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki);