summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/l_global.cc
diff options
context:
space:
mode:
authorRobert Vollmert <rvollmert@gmx.net>2009-10-20 22:05:19 +0200
committerRobert Vollmert <rvollmert@gmx.net>2009-10-20 22:27:33 +0200
commitc6df756b86ae4e28bc342dffa6ab41a7c865fa9a (patch)
treebf186cca4cd530cc72229f1998626187e0665afd /crawl-ref/source/l_global.cc
parent52818f7c83ffb5143f798412ee8d4800fc9de7b7 (diff)
downloadcrawl-ref-c6df756b86ae4e28bc342dffa6ab41a7c865fa9a.tar.gz
crawl-ref-c6df756b86ae4e28bc342dffa6ab41a7c865fa9a.zip
Move remaining libraries from clua.cc.
That's l_file.cc, l_food.cc, l_global.cc.
Diffstat (limited to 'crawl-ref/source/l_global.cc')
-rw-r--r--crawl-ref/source/l_global.cc50
1 files changed, 50 insertions, 0 deletions
diff --git a/crawl-ref/source/l_global.cc b/crawl-ref/source/l_global.cc
new file mode 100644
index 0000000000..5aa50f7df3
--- /dev/null
+++ b/crawl-ref/source/l_global.cc
@@ -0,0 +1,50 @@
+#include "AppHdr.h"
+
+#include "clua.h"
+#include "l_libs.h"
+
+//////////////////////////////////////////////////////////////////////
+// Miscellaneous globals
+
+#define PATTERN_FLUSH_CEILING 100
+
+typedef std::map<std::string, text_pattern> pattern_map;
+static pattern_map pattern_cache;
+
+static text_pattern &get_text_pattern(const std::string &s, bool checkcase)
+{
+ pattern_map::iterator i = pattern_cache.find(s);
+ if (i != pattern_cache.end())
+ return i->second;
+
+ if (pattern_cache.size() > PATTERN_FLUSH_CEILING)
+ pattern_cache.clear();
+
+ pattern_cache[s] = text_pattern(s, !checkcase);
+ return (pattern_cache[s]);
+}
+
+static int lua_pmatch(lua_State *ls)
+{
+ const char *pattern = luaL_checkstring(ls, 1);
+ if (!pattern)
+ return (0);
+
+ const char *text = luaL_checkstring(ls, 2);
+ if (!text)
+ return (0);
+
+ bool checkcase = true;
+ if (lua_isboolean(ls, 3))
+ checkcase = lua_toboolean(ls, 3);
+
+ text_pattern &tp = get_text_pattern(pattern, checkcase);
+ lua_pushboolean( ls, tp.matches(text) );
+ return (1);
+}
+
+void cluaopen_globals(lua_State *ls)
+{
+ lua_pushcfunction(ls, lua_pmatch);
+ lua_setglobal(ls, "pmatch");
+}