summaryrefslogtreecommitdiffstats
path: root/trunk/source/libutil.h
diff options
context:
space:
mode:
authordshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2006-08-02 12:54:15 +0000
committerdshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2006-08-02 12:54:15 +0000
commitd5e5340c3926d1cf97f6cba151ffaecb20bfb35f (patch)
treed1faf7d5b27df8f3c523a8dd33357804118e62b1 /trunk/source/libutil.h
parent7b2204d69f21d7075e4666ee032d7a129081bc4b (diff)
downloadcrawl-ref-d5e5340c3926d1cf97f6cba151ffaecb20bfb35f.tar.gz
crawl-ref-d5e5340c3926d1cf97f6cba151ffaecb20bfb35f.zip
Integrated travel patch as of 20060727
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@7 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'trunk/source/libutil.h')
-rw-r--r--trunk/source/libutil.h193
1 files changed, 193 insertions, 0 deletions
diff --git a/trunk/source/libutil.h b/trunk/source/libutil.h
index 73f8a849cf..bc05362604 100644
--- a/trunk/source/libutil.h
+++ b/trunk/source/libutil.h
@@ -11,8 +11,30 @@
#ifndef LIBUTIL_H
#define LIBUTIL_H
+#include <string>
+#include <vector>
+
+// getch() that returns a consistent set of values for all platforms.
+int c_getch();
+
+void play_sound(const char *file);
+
+// Pattern matching
+void *compile_pattern(const char *pattern, bool ignore_case = false);
+void free_compiled_pattern(void *cp);
+bool pattern_match(void *compiled_pattern, const char *text, int length);
+
void get_input_line( char *const buff, int len );
+class input_history;
+
+// Returns true if user pressed Enter, false if user hit Escape.
+bool cancelable_get_line( char *buf, int len, int wrapcol = 80,
+ input_history *mh = NULL );
+
+std::string & trim_string( std::string &str );
+std::vector<std::string> split_string(const char *sep, std::string s);
+
#ifdef NEED_USLEEP
void usleep( unsigned long time );
#endif
@@ -21,4 +43,175 @@ void usleep( unsigned long time );
int snprintf( char *str, size_t size, const char *format, ... );
#endif
+// Keys that getch() must return for keys Crawl is interested in.
+enum KEYS
+{
+ CK_ENTER = '\r',
+ CK_BKSP = 8,
+ CK_ESCAPE = '\x1b',
+
+ // 128 is off-limits because it's the code that's used when running
+ CK_DELETE = 129,
+
+ // This sequence of enums should not be rearranged.
+ CK_UP,
+ CK_DOWN,
+ CK_LEFT,
+ CK_RIGHT,
+
+ CK_INSERT,
+
+ CK_HOME,
+ CK_END,
+ CK_CLEAR,
+
+ CK_PGUP,
+ CK_PGDN,
+
+ CK_SHIFT_UP,
+ CK_SHIFT_DOWN,
+ CK_SHIFT_LEFT,
+ CK_SHIFT_RIGHT,
+
+ CK_SHIFT_INSERT,
+
+ CK_SHIFT_HOME,
+ CK_SHIFT_END,
+ CK_SHIFT_CLEAR,
+
+ CK_SHIFT_PGUP,
+ CK_SHIFT_PGDN,
+
+ CK_CTRL_UP,
+ CK_CTRL_DOWN,
+ CK_CTRL_LEFT,
+ CK_CTRL_RIGHT,
+
+ CK_CTRL_INSERT,
+
+ CK_CTRL_HOME,
+ CK_CTRL_END,
+ CK_CTRL_CLEAR,
+
+ CK_CTRL_PGUP,
+ CK_CTRL_PGDN,
+};
+
+class base_pattern
+{
+public:
+ virtual ~base_pattern() { };
+
+ virtual bool valid() const = 0;
+ virtual bool matches(const std::string &s) const = 0;
+};
+
+class text_pattern : public base_pattern
+{
+public:
+ text_pattern(const std::string &s, bool icase = false)
+ : pattern(s), compiled_pattern(NULL),
+ isvalid(true), ignore_case(icase)
+ {
+ }
+
+ text_pattern()
+ : pattern(), compiled_pattern(NULL),
+ isvalid(false), ignore_case(false)
+ {
+ }
+
+ text_pattern(const text_pattern &tp)
+ : pattern(tp.pattern),
+ compiled_pattern(NULL),
+ isvalid(tp.isvalid),
+ ignore_case(tp.ignore_case)
+ {
+ }
+
+ ~text_pattern()
+ {
+ if (compiled_pattern)
+ free_compiled_pattern(compiled_pattern);
+ }
+
+ const text_pattern &operator= (const text_pattern &tp)
+ {
+ if (this == &tp)
+ return tp;
+
+ if (compiled_pattern)
+ free_compiled_pattern(compiled_pattern);
+ pattern = tp.pattern;
+ compiled_pattern = NULL;
+ isvalid = tp.isvalid;
+ ignore_case = tp.ignore_case;
+ return *this;
+ }
+
+ const text_pattern &operator= (const std::string &spattern)
+ {
+ if (pattern == spattern)
+ return *this;
+
+ if (compiled_pattern)
+ free_compiled_pattern(compiled_pattern);
+ pattern = spattern;
+ compiled_pattern = NULL;
+ isvalid = true;
+ // We don't change ignore_case
+ return *this;
+ }
+
+ bool compile() const
+ {
+ // This function is const because compiled_pattern is not really part of
+ // the state of the object.
+
+ void *&cp = const_cast<text_pattern*>( this )->compiled_pattern;
+ return !empty()?
+ !!(cp = compile_pattern(pattern.c_str(), ignore_case))
+ : false;
+ }
+
+ bool empty() const
+ {
+ return !pattern.length();
+ }
+
+ bool valid() const
+ {
+ return isvalid &&
+ (compiled_pattern ||
+ (const_cast<text_pattern*>( this )->isvalid = compile()));
+ }
+
+ bool matches(const char *s, int length) const
+ {
+ return valid() && pattern_match(compiled_pattern, s, length);
+ }
+
+ bool matches(const char *s) const
+ {
+ return matches(std::string(s));
+ }
+
+ bool matches(const std::string &s) const
+ {
+ return matches(s.c_str(), s.length());
+ }
+
+ const std::string &tostring() const
+ {
+ return pattern;
+ }
+
+private:
+ std::string pattern;
+ void *compiled_pattern;
+ bool isvalid;
+ bool ignore_case;
+};
+
+
#endif