diff options
-rw-r--r-- | include/hashcomp.h | 13 | ||||
-rw-r--r-- | src/hashcomp.cpp | 44 | ||||
-rw-r--r-- | src/inspircd.cpp | 60 |
3 files changed, 117 insertions, 0 deletions
diff --git a/include/hashcomp.h b/include/hashcomp.h index 36e5dd0c3..ad6a2133e 100644 --- a/include/hashcomp.h +++ b/include/hashcomp.h @@ -79,6 +79,19 @@ namespace irc bool operator()(const in_addr &s1, const in_addr &s2) const; }; + class tokenstream + { + private: + std::string tokenbuffer; + std::vector<std::string> tokens; + public: + tokenstream(std::string &source); + ~tokenstream(); + + unsigned int GetNumTokens(); + const std::string& GetToken(unsigned int index); + }; + /** The irc_char_traits class is used for RFC-style comparison of strings. * This class is used to implement irc::string, a case-insensitive, RFC- diff --git a/src/hashcomp.cpp b/src/hashcomp.cpp index 3271e599c..08fbc8e2f 100644 --- a/src/hashcomp.cpp +++ b/src/hashcomp.cpp @@ -177,3 +177,47 @@ std::istream& operator>>(std::istream &is, irc::string &str) str = tmp.c_str(); return is; } + +irc::tokenstream::tokenstream(std::string &source) +{ + std::string::iterator last_starting_position = source.begin(); + bool last_pushed = false; + + for (std::string::iterator n = source.begin(); n != source.end(); n++) + { + if ((last_pushed) && (*n == ':')) + { + /* If we find a token thats not the first and starts with :, + * this is the last token on the line + */ + tokens.push_back(std::string(n+1, source.end())); + break; + } + + last_pushed = false; + + if ((*n == ' ') || (n+1 == source.end())) + { + /* If we find a space, or end of string, this is the end of a token. + */ + tokens.push_back(std::string(last_starting_position, n+1 == source.end() ? n+1 : n)); + last_starting_position = n+1; + last_pushed = true; + } + } +} + +irc::tokenstream::~tokenstream() +{ +} + +unsigned int irc::tokenstream::GetNumTokens() +{ + return tokens.size(); +} + +const std::string& irc::tokenstream::GetToken(unsigned int index) +{ + return tokens[index]; +} + diff --git a/src/inspircd.cpp b/src/inspircd.cpp index 23a232c46..9635f707a 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -944,6 +944,66 @@ int InspIRCd::Run() int main(int argc, char** argv) { + /* TEST SUITE FOR TOKENSTREAM + * + * Expected output: + * + * String: 'PRIVMSG #test FOO BAR' + * NumItems = 4 + * Token 0 = 'PRIVMSG' + * Token 1 = '#test' + * Token 2 = 'FOO' + * Token 3 = 'BAR' + * String: 'PRIVMSG #test :FOO BAR BAZ' + * NumItems = 3 + * Token 0 = 'PRIVMSG' + * Token 1 = '#test' + * Token 2 = 'FOO BAR BAZ' + * String: ':PRIVMSG #test :FOO BAR BAZ' + * NumItems = 3 + * Token 0 = ':PRIVMSG' + * String: 'AAAAAAA' + * NumItems = 1 + * Token 0 = 'AAAAAAA' + * String: '' + * NumItems = 0 + * + std::string a = "PRIVMSG #test FOO BAR"; + printf("String: '%s'\n",a.c_str()); + irc::tokenstream test(a); + printf("NumItems = %d\n",test.GetNumTokens()); + printf("Token 0 = '%s'\n",test.GetToken(0).c_str()); + printf("Token 1 = '%s'\n",test.GetToken(1).c_str()); + printf("Token 2 = '%s'\n",test.GetToken(2).c_str()); + printf("Token 3 = '%s'\n",test.GetToken(3).c_str()); + + std::string b = "PRIVMSG #test :FOO BAR BAZ"; + printf("String: '%s'\n",b.c_str()); + irc::tokenstream test2(b); + printf("NumItems = %d\n",test2.GetNumTokens()); + printf("Token 0 = '%s'\n",test2.GetToken(0).c_str()); + printf("Token 1 = '%s'\n",test2.GetToken(1).c_str()); + printf("Token 2 = '%s'\n",test2.GetToken(2).c_str()); + + std::string c = ":PRIVMSG #test :FOO BAR BAZ"; + printf("String: '%s'\n",c.c_str()); + irc::tokenstream test3(c); + printf("NumItems = %d\n",test3.GetNumTokens()); + printf("Token 0 = '%s'\n",test3.GetToken(0).c_str()); + + c = "AAAAAAA"; + printf("String: '%s'\n",c.c_str()); + irc::tokenstream test4(c); + printf("NumItems = %d\n",test4.GetNumTokens()); + printf("Token 0 = '%s'\n",test4.GetToken(0).c_str()); + + c = ""; + printf("String: '%s'\n",c.c_str()); + irc::tokenstream test5(c); + printf("NumItems = %d\n",test5.GetNumTokens()); + + exit(0); */ + try { ServerInstance = new InspIRCd(argc, argv); |