r20427 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r20426‎ | r20427 | r20428 >
Date:01:35, 14 March 2007
Author:river
Status:old
Tags:
Comment:
configurable prompt format
remove some debug output
Modified paths:
  • /trunk/skirmish/linereader.cc (modified) (history)
  • /trunk/skirmish/linereader.h (modified) (history)
  • /trunk/skirmish/maxdb.cc (modified) (history)
  • /trunk/skirmish/skirmish.cc (modified) (history)
  • /trunk/skirmish/sqlite.cc (modified) (history)

Diff [purge]

Index: trunk/skirmish/maxdb.cc
@@ -92,7 +92,6 @@
9393 , conn(conn)
9494 , sql(q)
9595 {
96 -std::cout << "q = ["<<q<<"]\n";
9796 if ((stmt = conn->conn->createPreparedStatement()) == NULL)
9897 throw db::error(conn->error(conn->conn->error()));
9998 if (stmt->prepare(q.c_str(), q.size(), SQLDBC_StringEncodingUTF8) != SQLDBC_OK)
@@ -109,13 +108,11 @@
110109 throw db::error("could not retrieve parameter length");
111110 #endif
112111 std::vector<char> paramname(namelen + 1);
113 -std::cout << "namelen = " << namelen << '\n';
114112 if (pmd->getParameterName(i, &paramname[0], SQLDBC_StringEncodingUTF8, paramname.size() + 1, &namelen) != SQLDBC_OK)
115113 throw db::error("could not retrieve parameter name");
116114 param p;
117115 p.name.assign(&paramname[0], &paramname[0] + namelen);
118116 p.pos = i;
119 -std::cout << "i = " << i << " nparams = " << nparams << " name = [" << p.name << "] len = " << namelen << "\n";
120117 params.push_back(p);
121118 }
122119 }
Index: trunk/skirmish/sqlite.cc
@@ -164,7 +164,6 @@
165165 std::vector<db::table> ret;
166166 result::iterator it = r->begin(), end = r->end();
167167 for (; it != end; ++it) {
168 -std::cout << "got row\n";
169168 names.push_back(it->string_value(0));
170169 }
171170
Index: trunk/skirmish/linereader.cc
@@ -3,6 +3,7 @@
44 #include <readline/history.h>
55
66 #include <cstdlib>
 7+#include <iostream>
78
89 #include "linereader.h"
910
@@ -17,7 +18,7 @@
1819 bool
1920 linereader::readline(std::string &r, std::string const &prompt)
2021 {
21 - char *s = ::readline(prompt.c_str());
 22+ char *s = ::readline(form_prompt(prompt).c_str());
2223 if (s == NULL)
2324 return false;
2425 r = s;
@@ -26,3 +27,43 @@
2728 std::free(s);
2829 return true;
2930 }
 31+
 32+std::string
 33+linereader::form_prompt(std::string const &src)
 34+{
 35+ std::string::size_type i, last = 0;
 36+ std::string pr;
 37+
 38+ while ((i = src.find('$', last)) != std::string::npos) {
 39+ pr.append(src.substr(last, i - last));
 40+ if (i == src.size()-1)
 41+ return pr + "$";
 42+ switch (src[i + 1]) {
 43+ case '$':
 44+ pr += '$';
 45+ last = i + 2;
 46+ continue;
 47+ case '(':
 48+ break;
 49+ default:
 50+ return src;
 51+ }
 52+ if ((last = src.find(')', i)) == std::string::npos)
 53+ return src;
 54+ std::string key = src.substr(i + 2, last - i - 2);
 55+ std::string value;
 56+ std::map<std::string, std::string>::iterator it;
 57+ if ((it = promptvars.find(key)) != promptvars.end())
 58+ value = it->second;
 59+ pr += value;
 60+ last++;
 61+ }
 62+ pr += src.substr(last);
 63+ return pr;
 64+}
 65+
 66+void
 67+linereader::set_prompt_variable(std::string const &var, std::string const &key)
 68+{
 69+ promptvars[var] = key;
 70+}
Index: trunk/skirmish/linereader.h
@@ -2,12 +2,19 @@
33 #define LINEREADER_H
44
55 #include <string>
 6+#include <map>
67
78 struct linereader {
89 linereader();
910 ~linereader();
1011
1112 bool readline(std::string &, std::string const &);
 13+ void set_prompt_variable(std::string const &var, std::string const &value);
 14+
 15+private:
 16+ std::string form_prompt(std::string const &);
 17+
 18+ std::map<std::string, std::string> promptvars;
1219 };
1320
1421 #endif
Index: trunk/skirmish/skirmish.cc
@@ -18,11 +18,14 @@
1919 static void switch_connection(std::string const &);
2020 static void close_connection(std::string const &);
2121 static void list_tables(std::string const &);
 22+static void c_prompt(std::string const &);
2223 static void describe_table(std::string const &);
2324
2425 typedef std::vector<std::vector<std::string> > tabulated_t;
2526 static void show_tabulated(tabulated_t const &);
2627
 28+static std::string prompt = "skirmish [$(cnr)]>";
 29+
2730 struct conndesc {
2831 db::connectionptr conn;
2932 std::string desc;
@@ -39,6 +42,8 @@
4043 ("\\close", close_connection)
4144 ("\\lt", list_tables)
4245 ("\\dt", describe_table)
 46+ ("\\pr", c_prompt)
 47+ ("\\prompt", c_prompt)
4348 ;
4449
4550 static db::connectionptr
@@ -74,12 +79,16 @@
7580 add_connection(argv[a]);
7681
7782 db::connectionptr conn;
78 - std::string input, prompt;
 83+ std::string input;
7984 for (;;) {
80 - if (cnr == -1)
81 - prompt = "skirmish (none)>";
82 - else
83 - prompt = str(boost::format("skirmish [%d]>") % cnr);
 85+ std::string cnrs;
 86+ if (cnr == -1) {
 87+ rl.set_prompt_variable("desc", "not connected");
 88+ rl.set_prompt_variable("cnr", "none");
 89+ } else {
 90+ rl.set_prompt_variable("cnr", boost::lexical_cast<std::string>(cnr));
 91+ rl.set_prompt_variable("desc", conns[cnr]->desc);
 92+ }
8493
8594 if (!rl.readline(input, prompt))
8695 break;
@@ -381,3 +390,12 @@
382391 }
383392 }
384393 }
 394+
 395+static void
 396+c_prompt(std::string const &arg)
 397+{
 398+ if (arg.empty())
 399+ std::cout << prompt << '\n';
 400+ else
 401+ prompt = arg;
 402+}