From 0e98821277deeb7609486b0d4c70724281838663 Mon Sep 17 00:00:00 2001 From: visor Date: Thu, 15 Apr 2010 00:10:39 +0900 Subject: [PATCH] list-files function. fix entity reference bug of wiki. --- lib/util_string.cc | 19 ++++++++++++ lib/util_string.h | 1 + modules/ml-store.cc | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++- modules/ml-store.h | 1 + wiki/wikimotor.cc | 8 +++++ wiki/wikimotor.h | 1 + 6 files changed, 113 insertions(+), 1 deletion(-) diff --git a/lib/util_string.cc b/lib/util_string.cc index 744d019..8c73d20 100644 --- a/lib/util_string.cc +++ b/lib/util_string.cc @@ -593,6 +593,25 @@ ustring filenameEncode (const ustring& text) { return ans; } +ustring filenameDecode (const ustring& text) { + static uregex re (":([0-9a-fA-F][0-9a-fA-F])"); + Splitter sp (text, re); + ustring ans; + int c; + + ans.reserve (text.length ()); + while (sp.next ()) { + if (sp.begin () < sp.end ()) + ans.append (sp.begin (), sp.end ()); + if (sp.match (1)) { + c = hex (*(sp.matchBegin (1))) * 16 + hex (*(sp.matchBegin (1) + 1)); + if (32 <= c && c < 256) + ans.append (1, c); + } + } + return ans; +} + bool matchSkip (uiterator& b, uiterator e, const char* t, size_t s) { if (e - b >= s && memcmp (t, &b[0], s) == 0) { b += s; diff --git a/lib/util_string.h b/lib/util_string.h index 52a5a2c..d864b78 100644 --- a/lib/util_string.h +++ b/lib/util_string.h @@ -201,6 +201,7 @@ void split (uiterator b, uiterator e, uregex& re, std::vector& ans); bool splitChar (uiterator b, uiterator e, uiterator::value_type ch, uiterator& m1); ustring base64Encode (uiterator b, uiterator e); ustring filenameEncode (const ustring& text); +ustring filenameDecode (const ustring& text); ustring escape_re (const ustring& text); ustring slashEncode (const ustring& text); ustring slashDecode (const ustring& text); diff --git a/modules/ml-store.cc b/modules/ml-store.cc index 4fd5031..361b4ee 100644 --- a/modules/ml-store.cc +++ b/modules/ml-store.cc @@ -21,10 +21,39 @@ #include #include #include +#include #include #define StoreFileNameMax 48 +static MNode* listFiles (const ustring& dirpath) { + MNodeList ans; + DIR* dir; + struct dirent de; + struct dirent* dep; + ustring name; + ustring t; + + dir = opendir (dirpath.c_str ()); + if (dir) { + while (readdir_r (dir, &de, &dep) == 0 && dep != NULL) { +#ifndef NOT_HAVE_D_NAMLEN + name.assign (dep->d_name, dep->d_namlen); +#else + name.assign (dep->d_name); +#endif + if (name.length () > 0 && name[0] != '.') { + ans.append (newMNode_str (new ustring (filenameDecode (name)))); + } + } + closedir (dir); + dir = NULL; + } + return ans.release (); +} + +//============================================================ + /*DOC: ==data store module== @@ -929,7 +958,6 @@ MNode* ml_response_file (MNode* cell, MlEnv* mlenv) { MNode* ml_filesize (MNode* cell, MlEnv* mlenv) { MNode* arg = cell->cdr (); ustring filename; - ustring type; enum { F_NONE, F_SERIAL, @@ -977,3 +1005,57 @@ MNode* ml_filesize (MNode* cell, MlEnv* mlenv) { return NULL; } } + +/*DOC: +===list-files=== + (list-files [#serial | #named | :serial BOOL | :named BOOL]) -> LIST_of_FILENAMES + +*/ +//#AFUNC list-files ml_list_files +MNode* ml_list_files (MNode* cell, MlEnv* mlenv) { + MNode* arg = cell->cdr (); + enum { + F_NONE, + F_SERIAL, + F_NAMED, + } storetype; + ustring src; + std::vector keywords; + static paramList kwlist[] = { + {CharConst ("serial"), true}, + {CharConst ("named"), true}, + {NULL, 0, 0} + }; + + if (mlenv->env->storagedir.length () > 0) { + storetype = F_NAMED; + } else if (mlenv->env->storedir.length () > 0) { + storetype = F_SERIAL; + } else { + storetype = F_NONE; + } + + setParams (arg, 0, NULL, kwlist, &keywords, NULL); + if (eval_bool (keywords[0], mlenv)) // serial + storetype = F_SERIAL; + if (eval_bool (keywords[1], mlenv)) // named + storetype = F_NAMED; + + switch (storetype) { + case F_SERIAL: + if (mlenv->env->storedir.empty ()) + throw (uErrorNoStore); + src = mlenv->env->storedir; + break; + case F_NAMED: + if (mlenv->env->storagedir.empty ()) + throw (uErrorNoStorage); + src = mlenv->env->storagedir; + break; + default: + throw (uErrorNoStore); + } + + return listFiles (src); +} + diff --git a/modules/ml-store.h b/modules/ml-store.h index f9dc13c..23ae3df 100644 --- a/modules/ml-store.h +++ b/modules/ml-store.h @@ -21,5 +21,6 @@ MNode* ml_delete_file (MNode* cell, MlEnv* mlenv); MNode* ml_response_motor (MNode* cell, MlEnv* mlenv); MNode* ml_response_file (MNode* cell, MlEnv* mlenv); MNode* ml_filesize (MNode* cell, MlEnv* mlenv); +MNode* ml_list_files (MNode* cell, MlEnv* mlenv); #endif /* ML_STORE_H */ diff --git a/wiki/wikimotor.cc b/wiki/wikimotor.cc index bb9a4d5..72c4c66 100644 --- a/wiki/wikimotor.cc +++ b/wiki/wikimotor.cc @@ -569,6 +569,14 @@ ustring WikiMotorObjChar::textOut (WikiFormat* wiki) { return text; } +ustring WikiMotorObjChar::htmlOut (WikiFormat* wiki) { + if (text.length () > 1) { + return text; + } else { + return WikiMotorObj::htmlOut (wiki); + } +} + ustring WikiMotorObjChar::dump () { return text; } diff --git a/wiki/wikimotor.h b/wiki/wikimotor.h index 899dfe3..67dab12 100644 --- a/wiki/wikimotor.h +++ b/wiki/wikimotor.h @@ -130,6 +130,7 @@ class WikiMotorObjChar: public WikiMotorObj { }; virtual ~WikiMotorObjChar () {}; virtual ustring textOut (WikiFormat* wiki); + virtual ustring htmlOut (WikiFormat* wiki); virtual ustring dump (); }; -- 2.11.0