OSDN Git Service

input-text wiki command.
authorvisor <visor@users.sourceforge.jp>
Sun, 10 Jul 2011 15:32:55 +0000 (00:32 +0900)
committervisor <visor@users.sourceforge.jp>
Sun, 10 Jul 2011 15:32:55 +0000 (00:32 +0900)
modules/ml-formvar.cc
modules/ml-formvar.h
wiki/wikicmd.cc
wiki/wikicmd.h

index 7152c08..6bd5c5b 100644 (file)
 
 */
 
-class  opt_t {
-  public:
-    bool  fvalue;
-    bool  ftextarea;
-    bool  nowhite;
-    size_t  max;
-    ustring  filter;
-    MNodePtr  errFilter;
-    ustring  defaultVal;
-
-    opt_t () {
-       fvalue = false;
-       ftextarea = false;
-       nowhite = false;
-       max = 0;
-    };
-    virtual  ~opt_t () {};
-};
-
-static MNode*  stringNode (const ustring& val) {
+MNode*  FormVarOp::stringNode (const ustring& val) {
     return newMNode_str (new ustring (val));
 }
 
-static MNode*  intNode (const ustring& val) {
+MNode*  FormVarOp::intNode (const ustring& val) {
     long  v = atol (val.c_str ());
     return newMNode_num (v);
 }
 
-static MNode*  intBlankNode (const ustring& val) {
+MNode*  FormVarOp::intBlankNode (const ustring& val) {
     if (val.length () > 0)
        return intNode (val);
     else
        return NULL;
 }
 
-static MNode*  doubleNode (const ustring& val) {
+MNode*  FormVarOp::doubleNode (const ustring& val) {
     double  v = atof (val.c_str ());
     return newMNode_num (v);
 }
 
-static MNode*  doubleBlankNode (const ustring& val) {
+MNode*  FormVarOp::doubleBlankNode (const ustring& val) {
     if (val.length () > 0)
        return doubleNode (val);
     else
        return NULL;
 }
 
-static MNode*  boolNode (const ustring& val) {
+MNode*  FormVarOp::boolNode (const ustring& val) {
     return newMNode_bool (to_bool (val));
 }
 
-static bool opt_filter (const ustring& name, ustring& val, opt_t& opt, MlEnv* mlenv) {
+bool  FormVarOp::optFilter (const ustring& name, ustring& val, MlEnv* mlenv) {
     bool  ans = true;
 
-    if (opt.filter.size () > 0) {
+    if (filter.size () > 0) {
        boost::wsmatch  m;
-       if (wsearch_env (mlenv, val, m, opt.filter)) {
+       if (wsearch_env (mlenv, val, m, filter)) {
            val = wtou (std::wstring (m[0].first, m[0].second));
        } else {
            val.resize (0);
            ans = false;
        }
     }
-    if (opt.nowhite) {
-       if (opt.ftextarea) {
+    if (nowhite) {
+       if (ftextarea) {
            clipNLEnd (val);
        } else {
            clipWhiteEnd (val);
        }
     }
-    if (opt.max > 0) {
-       substring (val, 0, opt.max, true, val);
+    if (max > 0) {
+       substring (val, 0, max, true, val);
     }
-    if (opt.errFilter ()) {
+    if (errFilter ()) {
        MNodePtr  ag;
        MNodePtr  v;
        ag = new MNode;
-       if (ans)
+       if (ans) {
            ag ()->set_car (newMNode_str (new ustring (val)));
-       v = execDefun (mlenv, opt.errFilter (), ag (), uEmpty);
-       if (to_bool (v ())) {
+           v = execDefun (mlenv, errFilter (), ag (), uEmpty);
+           if (to_bool (v ())) {
+               mlenv->env->setErrorVar (name);
+           }
+       } else {
            mlenv->env->setErrorVar (name);
        }
     }
-    if (opt.defaultVal.size () > 0 && val.size () == 0) {
-       val = opt.defaultVal;
+    if (defaultVal.size () > 0 && val.size () == 0) {
+       val = defaultVal;
        ans = true;
     }
     return ans;
 }
 
-static void  formvar_input_readopt (MNode*& arg, MlEnv* mlenv, ustring& name, opt_t& opt) {
+static void  formvar_input_readopt (MNode*& arg, MlEnv* mlenv, ustring& name, FormVarOp& opt) {
     std::vector<MNode*>  params;
     std::vector<MNode*>  keywords;
     static paramList  kwlist[] = {
@@ -198,7 +182,7 @@ static void  formvar_input_readopt (MNode*& arg, MlEnv* mlenv, ustring& name, op
        throw (uErrorVarNameEmpty);
 }
 
-static MNode*  formvar_input_do (MlEnv* mlenv, ustring& name, opt_t& opt, ustring (*prefn)(const ustring&), MNode* (*postfn)(const ustring&)) {
+MNode*  FormVarOp::input (MlEnv* mlenv, ustring& name, ustring (*prefn)(const ustring&), MNode* (FormVarOp::*postfn)(const ustring&)) {
     MNodePtr  h;
     ustring  val;
     ustring  aname;
@@ -206,7 +190,7 @@ static MNode*  formvar_input_do (MlEnv* mlenv, ustring& name, opt_t& opt, ustrin
     if (checkAry (name, aname)) {
        size_t  i, n;
 
-       if (opt.fvalue) {
+       if (fvalue) {
            MNodeList  ans;
            
            n = mlenv->env->form->atSize (aname);
@@ -214,8 +198,8 @@ static MNode*  formvar_input_do (MlEnv* mlenv, ustring& name, opt_t& opt, ustrin
                mlenv->env->form->at (aname, i, val);
                if (prefn)
                    val = (*prefn) (val);
-               if (opt_filter (aname, val, opt, mlenv))
-                   ans.append ((*postfn) (val));
+               if (optFilter (aname, val, mlenv))
+                   ans.append ((this->*postfn) (val));
                else
                    ans.append (NULL);
            }
@@ -226,8 +210,8 @@ static MNode*  formvar_input_do (MlEnv* mlenv, ustring& name, opt_t& opt, ustrin
                mlenv->env->form->at (aname, i, val);
                if (prefn)
                    val = (*prefn) (val);
-               if (opt_filter (aname, val, opt, mlenv))
-                   mlenv->setAry (aname, i + 1, (*postfn) (val));
+               if (optFilter (aname, val, mlenv))
+                   mlenv->setAry (aname, i + 1, (this->*postfn) (val));
                else
                    mlenv->setAry (aname, i + 1, NULL);
            }
@@ -238,15 +222,15 @@ static MNode*  formvar_input_do (MlEnv* mlenv, ustring& name, opt_t& opt, ustrin
        mlenv->env->form->at (name, val);
        if (prefn)
            val = (*prefn) (val);
-       if (opt_filter (name, val, opt, mlenv)) {
-           if (opt.fvalue) {
-               return (*postfn) (val);
+       if (optFilter (name, val, mlenv)) {
+           if (fvalue) {
+               return (this->*postfn) (val);
            } else {
-               mlenv->setVar (name, (*postfn) (val));
+               mlenv->setVar (name, (this->*postfn) (val));
                return NULL;
            }
        } else {
-           if (opt.fvalue) {
+           if (fvalue) {
            } else {
                mlenv->setVar (name, NULL);
            }
@@ -261,10 +245,10 @@ static MNode*  formvar_input_do (MlEnv* mlenv, ustring& name, opt_t& opt, ustrin
 MNode*  ml_formvar_input_text (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
-    return formvar_input_do (mlenv, name, opt, omitCtrl, stringNode);
+    return opt.input (mlenv, name, omitCtrl, &FormVarOp::stringNode);
 }
 
 //#AFUNC       input-textarea  ml_formvar_input_textarea
@@ -272,11 +256,11 @@ MNode*  ml_formvar_input_text (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_formvar_input_textarea (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
     opt.ftextarea = true;
-    return formvar_input_do (mlenv, name, opt, NULL, stringNode);
+    return opt.input (mlenv, name, NULL, &FormVarOp::stringNode);
 }
 
 //#AFUNC       input-int       ml_formvar_input_int
@@ -284,10 +268,10 @@ MNode*  ml_formvar_input_textarea (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_formvar_input_int (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
-    return formvar_input_do (mlenv, name, opt, NULL, intNode);
+    return opt.input (mlenv, name, NULL, &FormVarOp::intNode);
 }
 
 //#AFUNC       input-real      ml_formvar_input_real
@@ -295,10 +279,10 @@ MNode*  ml_formvar_input_int (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_formvar_input_real (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
-    return formvar_input_do (mlenv, name, opt, NULL, doubleNode);
+    return opt.input (mlenv, name, NULL, &FormVarOp::doubleNode);
 }
 
 //#AFUNC       input-int-or-blank      ml_formvar_input_int_blank
@@ -306,10 +290,10 @@ MNode*  ml_formvar_input_real (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_formvar_input_int_blank (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
-    return formvar_input_do (mlenv, name, opt, NULL, intBlankNode);
+    return opt.input (mlenv, name, NULL, &FormVarOp::intBlankNode);
 }
 
 //#AFUNC       input-real-or-blank     ml_formvar_input_real_blank
@@ -317,10 +301,10 @@ MNode*  ml_formvar_input_int_blank (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_formvar_input_real_blank (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
-    return formvar_input_do (mlenv, name, opt, NULL, doubleBlankNode);
+    return opt.input (mlenv, name, NULL, &FormVarOp::doubleBlankNode);
 }
 
 //#AFUNC       input-ascii     ml_formvar_input_ascii
@@ -328,10 +312,10 @@ MNode*  ml_formvar_input_real_blank (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_formvar_input_ascii (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
-    return formvar_input_do (mlenv, name, opt, omitNonAscii, stringNode); // XXX jp
+    return opt.input (mlenv, name, omitNonAscii, &FormVarOp::stringNode); // XXX jp
 }
 
 //#AFUNC       input-bool      ml_formvar_input_bool
@@ -339,10 +323,10 @@ MNode*  ml_formvar_input_ascii (MNode* cell, MlEnv* mlenv) {
 MNode*  ml_formvar_input_bool (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  name;
-    opt_t  opt;
+    FormVarOp  opt;
 
     formvar_input_readopt (arg, mlenv, name, opt);
-    return formvar_input_do (mlenv, name, opt, NULL, boolNode);
+    return opt.input (mlenv, name, NULL, &FormVarOp::boolNode);
 }
 
 /*DOC:
@@ -357,7 +341,7 @@ MNode*  ml_formvar_input_file (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
     ustring  var;
     ustring  fname;
-    opt_t  opt;
+    FormVarOp  opt;
 //    ustring  val;
     ustring  tgt;
     ustring  filename;
index f7aa8a6..1180f7b 100644 (file)
@@ -1,9 +1,41 @@
 #ifndef ML_FORMVAR_H
 #define ML_FORMVAR_H
 
+#include "ml.h"
+#include "ustring.h"
+#include <unistd.h>
+
 class  MNode;
 class  MlEnv;
 
+class  FormVarOp {
+ public:
+    bool  fvalue;
+    bool  ftextarea;
+    bool  nowhite;
+    size_t  max;
+    ustring  filter;
+    MNodePtr  errFilter;
+    ustring  defaultVal;
+       
+    FormVarOp () {
+       fvalue = false;
+       ftextarea = false;
+       nowhite = false;
+       max = 0;
+    };
+    virtual  ~FormVarOp () {};
+
+    virtual MNode*  stringNode (const ustring& val);
+    virtual MNode*  intNode (const ustring& val);
+    virtual MNode*  intBlankNode (const ustring& val);
+    virtual MNode*  doubleNode (const ustring& val);
+    virtual MNode*  doubleBlankNode (const ustring& val);
+    virtual MNode*  boolNode (const ustring& val);
+    virtual bool  optFilter (const ustring& name, ustring& val, MlEnv* mlenv);
+    virtual MNode*  input (MlEnv* mlenv, ustring& name, ustring (*prefn)(const ustring&), MNode* (FormVarOp::*postfn)(const ustring&));
+};
+
 MNode*  ml_formvar_input_text (MNode* cell, MlEnv* mlenv);
 MNode*  ml_formvar_input_textarea (MNode* cell, MlEnv* mlenv);
 MNode*  ml_formvar_input_int (MNode* cell, MlEnv* mlenv);
index c89da51..1e0456a 100644 (file)
@@ -3,6 +3,7 @@
 #include "wikiformat.h"
 #include "wikienv.h"
 #include "wikimotor.h"
+#include "ml-formvar.h"
 #include "ml.h"
 #include "expr.h"
 #include "motorenv.h"
@@ -580,6 +581,102 @@ void  wc_macro (WikiLine* wl, WikiFormat* wiki) {
 }
 
 /* ============================================================ */
+/*DOC:
+===$input-text, $input-textarea, $input-int, $input-real, $input-int-or-blank, $input-real-or-blank, $input-ascii, $input-bool===
+{div:pre
++$input-text:VARIABLE
++$input-textarea:VARIABLE
++$input-int:VARIABLE
++$input-real:VARIABLE
++$input-int-or-blank:VARIABLE
++$input-real-or-blank:VARIABLE
++$input-ascii:VARIABLE
++$input-bool:VARIABLE
+}
+
+*/
+//#WIKICMD     $input-text     wc_input_text
+//#WIKICMD     $input-textarea wc_input_textarea
+//#WIKICMD     $input-int      wc_input_int
+//#WIKICMD     $input-real     wc_input_real
+//#WIKICMD     $input-int-or-blank     wc_input_int_or_blank
+//#WIKICMD     $input-real-or-blank    wc_input_real_or_blank
+//#WIKICMD     $input-ascii    wc_input_ascii
+//#WIKICMD     $input-bool     wc_input_bool
+static void  wc_input_sub (WikiLine* wl, WikiFormat* wiki, ustring (*prefn)(const ustring&), MNode* (FormVarOp::*postfn)(const ustring&)) {
+    WikiMotor  motor (wl->begin, wl->end, wiki);
+    WikiMotorObjVec  objv;
+    WikiMotorObjVecVec  args;
+
+#ifdef DEBUG
+    std::cerr << "(wiki):" << ustring (wl->begin0, wl->end) << "\n";
+#endif /* DEBUG */
+    motor.compile (objv);
+    objv.splitCharA (':', args);
+    ustring  name;
+    int  i;
+    FormVarOp  opt;
+
+    if (args.size () > 0) {
+       name = args[0]->textOut (wiki);
+       for (i = 1; i < args.size (); i ++) {
+           if (args[i]->matchHead (CharConst ("max="))) {
+               ustring  x, y;
+               args[i]->splitChar (wiki, '=', x, y);
+               opt.max = to_uint32 (y);
+           } else if (args[i]->match (CharConst ("nowhite"))) {
+               opt.nowhite = true;
+           } else if (args[i]->match (CharConst ("nw"))) {
+               opt.nowhite = true;
+           } else if (args[i]->matchHead (CharConst ("filter="))) {
+               ustring  x, y;
+               args[i]->splitChar (wiki, '=', x, y);
+               opt.filter = y;
+           } else if (args[i]->matchHead (CharConst ("default="))) {
+               ustring  x, y;
+               args[i]->splitChar (wiki, '=', x, y);
+               opt.defaultVal = y;
+           } else {
+               wiki->errorMsg.append (args[i]->textOut (wiki) + ustring (CharConst (": bad parameter.\n")));
+           }
+       }
+       opt.input (wiki->mlenv, name, prefn, postfn);
+    }
+}
+
+void  wc_input_text (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, omitCtrl, &FormVarOp::stringNode);
+}
+
+void  wc_input_textarea (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, NULL, &FormVarOp::stringNode);
+}
+
+void  wc_input_int (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, NULL, &FormVarOp::intNode);
+}
+
+void  wc_input_real (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, NULL, &FormVarOp::doubleNode);
+}
+
+void  wc_input_int_or_blank (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, NULL, &FormVarOp::intBlankNode);
+}
+
+void  wc_input_real_or_blank (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, NULL, &FormVarOp::doubleBlankNode);
+}
+
+void  wc_input_ascii (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, omitNonAscii, &FormVarOp::stringNode);
+}
+
+void  wc_input_bool (WikiLine* wl, WikiFormat* wiki) {
+    wc_input_sub (wl, wiki, NULL, &FormVarOp::boolNode);
+}
+
+/* ============================================================ */
 static void  call_defun_wikicmd (ustring& name, MNode* wf, WikiMotorObjVec& objv2, WikiLine* wl, WikiFormat* wiki) {
     WikiMotorObjVecVec  argsv;
     MNodePtr  vargs;
index c5c3648..5dd44eb 100644 (file)
@@ -16,6 +16,14 @@ void  wc_insert (WikiLine* wl, WikiFormat* wiki);
 void  wc_data (WikiLine* wl, WikiFormat* wiki);
 void  wc_local (WikiLine* wl, WikiFormat* wiki);
 void  wc_macro (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_text (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_textarea (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_int (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_real (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_int_or_blank (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_real_or_blank (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_ascii (WikiLine* wl, WikiFormat* wiki);
+void  wc_input_bool (WikiLine* wl, WikiFormat* wiki);
 void  wc_call_defun (WikiLine* wl, WikiFormat* wiki);
 void  wikiOutput (const ustring& text, bool fsuper, WikiFormat* wiki);