1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
|
// This code was generated by CLI, a command line interface
// compiler for C++.
//
// Begin prologue.
//
//
// End prologue.
#include "options.hxx"
#include <map>
#include <set>
#include <string>
#include <vector>
#include <ostream>
#include <sstream>
#include <cstring>
#include <fstream>
namespace odb
{
namespace sqlite
{
namespace details
{
namespace cli
{
// unknown_option
//
unknown_option::
~unknown_option () throw ()
{
}
void unknown_option::
print (::std::ostream& os) const
{
os << "unknown option '" << option ().c_str () << "'";
}
const char* unknown_option::
what () const throw ()
{
return "unknown option";
}
// unknown_argument
//
unknown_argument::
~unknown_argument () throw ()
{
}
void unknown_argument::
print (::std::ostream& os) const
{
os << "unknown argument '" << argument ().c_str () << "'";
}
const char* unknown_argument::
what () const throw ()
{
return "unknown argument";
}
// missing_value
//
missing_value::
~missing_value () throw ()
{
}
void missing_value::
print (::std::ostream& os) const
{
os << "missing value for option '" << option ().c_str () << "'";
}
const char* missing_value::
what () const throw ()
{
return "missing option value";
}
// invalid_value
//
invalid_value::
~invalid_value () throw ()
{
}
void invalid_value::
print (::std::ostream& os) const
{
os << "invalid value '" << value ().c_str () << "' for option '"
<< option ().c_str () << "'";
}
const char* invalid_value::
what () const throw ()
{
return "invalid option value";
}
// eos_reached
//
void eos_reached::
print (::std::ostream& os) const
{
os << what ();
}
const char* eos_reached::
what () const throw ()
{
return "end of argument stream reached";
}
// file_io_failure
//
file_io_failure::
~file_io_failure () throw ()
{
}
void file_io_failure::
print (::std::ostream& os) const
{
os << "unable to open file '" << file ().c_str () << "' or read failure";
}
const char* file_io_failure::
what () const throw ()
{
return "unable to open file or read failure";
}
// unmatched_quote
//
unmatched_quote::
~unmatched_quote () throw ()
{
}
void unmatched_quote::
print (::std::ostream& os) const
{
os << "unmatched quote in argument '" << argument ().c_str () << "'";
}
const char* unmatched_quote::
what () const throw ()
{
return "unmatched quote";
}
// scanner
//
scanner::
~scanner ()
{
}
// argv_scanner
//
bool argv_scanner::
more ()
{
return i_ < argc_;
}
const char* argv_scanner::
peek ()
{
if (i_ < argc_)
return argv_[i_];
else
throw eos_reached ();
}
const char* argv_scanner::
next ()
{
if (i_ < argc_)
{
const char* r (argv_[i_]);
if (erase_)
{
for (int i (i_ + 1); i < argc_; ++i)
argv_[i - 1] = argv_[i];
--argc_;
argv_[argc_] = 0;
}
else
++i_;
return r;
}
else
throw eos_reached ();
}
void argv_scanner::
skip ()
{
if (i_ < argc_)
++i_;
else
throw eos_reached ();
}
// argv_file_scanner
//
bool argv_file_scanner::
more ()
{
if (!args_.empty ())
return true;
while (base::more ())
{
// See if the next argument is the file option.
//
const char* a (base::peek ());
const option_info* oi;
if (!skip_ && (oi = find (a)))
{
base::next ();
if (!base::more ())
throw missing_value (oi->option);
if (oi->search_func != 0)
{
std::string f (oi->search_func (base::next (), oi->arg));
if (!f.empty ())
load (f);
}
else
load (base::next ());
if (!args_.empty ())
return true;
}
else
{
if (!skip_)
skip_ = (std::strcmp (a, "--") == 0);
return true;
}
}
return false;
}
const char* argv_file_scanner::
peek ()
{
if (!more ())
throw eos_reached ();
return args_.empty () ? base::peek () : args_.front ().c_str ();
}
const char* argv_file_scanner::
next ()
{
if (!more ())
throw eos_reached ();
if (args_.empty ())
return base::next ();
else
{
hold_.swap (args_.front ());
args_.pop_front ();
return hold_.c_str ();
}
}
void argv_file_scanner::
skip ()
{
if (!more ())
throw eos_reached ();
if (args_.empty ())
return base::skip ();
else
args_.pop_front ();
}
const argv_file_scanner::option_info* argv_file_scanner::
find (const char* a) const
{
for (std::size_t i (0); i < options_count_; ++i)
if (std::strcmp (a, options_[i].option) == 0)
return &options_[i];
return 0;
}
void argv_file_scanner::
load (const std::string& file)
{
using namespace std;
ifstream is (file.c_str ());
if (!is.is_open ())
throw file_io_failure (file);
while (!is.eof ())
{
string line;
getline (is, line);
if (is.fail () && !is.eof ())
throw file_io_failure (file);
string::size_type n (line.size ());
// Trim the line from leading and trailing whitespaces.
//
if (n != 0)
{
const char* f (line.c_str ());
const char* l (f + n);
const char* of (f);
while (f < l && (*f == ' ' || *f == '\t' || *f == '\r'))
++f;
--l;
const char* ol (l);
while (l > f && (*l == ' ' || *l == '\t' || *l == '\r'))
--l;
if (f != of || l != ol)
line = f <= l ? string (f, l - f + 1) : string ();
}
// Ignore empty lines, those that start with #.
//
if (line.empty () || line[0] == '#')
continue;
string::size_type p (line.find (' '));
if (p == string::npos)
{
if (!skip_)
skip_ = (line == "--");
args_.push_back (line);
}
else
{
string s1 (line, 0, p);
// Skip leading whitespaces in the argument.
//
n = line.size ();
for (++p; p < n; ++p)
{
char c (line[p]);
if (c != ' ' && c != '\t' && c != '\r')
break;
}
string s2 (line, p);
// If the string is wrapped in quotes, remove them.
//
n = s2.size ();
char cf (s2[0]), cl (s2[n - 1]);
if (cf == '"' || cf == '\'' || cl == '"' || cl == '\'')
{
if (n == 1 || cf != cl)
throw unmatched_quote (s2);
s2 = string (s2, 1, n - 2);
}
const option_info* oi;
if (!skip_ && (oi = find (s1.c_str ())))
{
if (s2.empty ())
throw missing_value (oi->option);
if (oi->search_func != 0)
{
std::string f (oi->search_func (s2.c_str (), oi->arg));
if (!f.empty ())
load (f);
}
else
load (s2);
}
else
{
args_.push_back (s1);
args_.push_back (s2);
}
}
}
}
template <typename X>
struct parser
{
static void
parse (X& x, scanner& s)
{
std::string o (s.next ());
if (s.more ())
{
std::string v (s.next ());
std::istringstream is (v);
if (!(is >> x && is.eof ()))
throw invalid_value (o, v);
}
else
throw missing_value (o);
}
};
template <>
struct parser<bool>
{
static void
parse (bool& x, scanner& s)
{
s.next ();
x = true;
}
};
template <>
struct parser<std::string>
{
static void
parse (std::string& x, scanner& s)
{
const char* o (s.next ());
if (s.more ())
x = s.next ();
else
throw missing_value (o);
}
};
template <typename X>
struct parser<std::vector<X> >
{
static void
parse (std::vector<X>& c, scanner& s)
{
X x;
parser<X>::parse (x, s);
c.push_back (x);
}
};
template <typename X>
struct parser<std::set<X> >
{
static void
parse (std::set<X>& c, scanner& s)
{
X x;
parser<X>::parse (x, s);
c.insert (x);
}
};
template <typename K, typename V>
struct parser<std::map<K, V> >
{
static void
parse (std::map<K, V>& m, scanner& s)
{
std::string o (s.next ());
if (s.more ())
{
std::string ov (s.next ());
std::string::size_type p = ov.find ('=');
if (p == std::string::npos)
{
K k = K ();
if (!ov.empty ())
{
std::istringstream ks (ov);
if (!(ks >> k && ks.eof ()))
throw invalid_value (o, ov);
}
m[k] = V ();
}
else
{
K k = K ();
V v = V ();
std::string kstr (ov, 0, p);
std::string vstr (ov, p + 1);
if (!kstr.empty ())
{
std::istringstream ks (kstr);
if (!(ks >> k && ks.eof ()))
throw invalid_value (o, ov);
}
if (!vstr.empty ())
{
std::istringstream vs (vstr);
if (!(vs >> v && vs.eof ()))
throw invalid_value (o, ov);
}
m[k] = v;
}
}
else
throw missing_value (o);
}
};
template <typename X, typename T, T X::*M>
void
thunk (X& x, scanner& s)
{
parser<T>::parse (x.*M, s);
}
}
}
}
}
#include <map>
#include <cstring>
namespace odb
{
namespace sqlite
{
namespace details
{
// options
//
options::
options ()
: database_ (),
create_ (),
read_only_ (),
options_file_ ()
{
}
options::
options (int& argc,
char** argv,
bool erase,
::odb::sqlite::details::cli::unknown_mode opt,
::odb::sqlite::details::cli::unknown_mode arg)
: database_ (),
create_ (),
read_only_ (),
options_file_ ()
{
::odb::sqlite::details::cli::argv_scanner s (argc, argv, erase);
_parse (s, opt, arg);
}
options::
options (int start,
int& argc,
char** argv,
bool erase,
::odb::sqlite::details::cli::unknown_mode opt,
::odb::sqlite::details::cli::unknown_mode arg)
: database_ (),
create_ (),
read_only_ (),
options_file_ ()
{
::odb::sqlite::details::cli::argv_scanner s (start, argc, argv, erase);
_parse (s, opt, arg);
}
options::
options (int& argc,
char** argv,
int& end,
bool erase,
::odb::sqlite::details::cli::unknown_mode opt,
::odb::sqlite::details::cli::unknown_mode arg)
: database_ (),
create_ (),
read_only_ (),
options_file_ ()
{
::odb::sqlite::details::cli::argv_scanner s (argc, argv, erase);
_parse (s, opt, arg);
end = s.end ();
}
options::
options (int start,
int& argc,
char** argv,
int& end,
bool erase,
::odb::sqlite::details::cli::unknown_mode opt,
::odb::sqlite::details::cli::unknown_mode arg)
: database_ (),
create_ (),
read_only_ (),
options_file_ ()
{
::odb::sqlite::details::cli::argv_scanner s (start, argc, argv, erase);
_parse (s, opt, arg);
end = s.end ();
}
options::
options (::odb::sqlite::details::cli::scanner& s,
::odb::sqlite::details::cli::unknown_mode opt,
::odb::sqlite::details::cli::unknown_mode arg)
: database_ (),
create_ (),
read_only_ (),
options_file_ ()
{
_parse (s, opt, arg);
}
void options::
print_usage (::std::ostream& os)
{
os << "--database <filename> SQLite database file name. If the database file is not" << ::std::endl
<< " specified then a private, temporary on-disk database will" << ::std::endl
<< " be created. Use the ':memory:' special name to create a" << ::std::endl
<< " private, temporary in-memory database." << ::std::endl;
os << "--create Create the SQLite database if it does not already exist." << ::std::endl
<< " By default opening the database fails if it does not" << ::std::endl
<< " already exist." << ::std::endl;
os << "--read-only Open the SQLite database in read-only mode. By default" << ::std::endl
<< " the database is opened for reading and writing if" << ::std::endl
<< " possible, or reading only if the file is write-protected" << ::std::endl
<< " by the operating system." << ::std::endl;
os << "--options-file <file> Read additional options from <file>. Each option" << ::std::endl
<< " appearing on a separate line optionally followed by space" << ::std::endl
<< " and an option value. Empty lines and lines starting with" << ::std::endl
<< " '#' are ignored." << ::std::endl;
}
typedef
std::map<std::string, void (*) (options&, ::odb::sqlite::details::cli::scanner&)>
_cli_options_map;
static _cli_options_map _cli_options_map_;
struct _cli_options_map_init
{
_cli_options_map_init ()
{
_cli_options_map_["--database"] =
&::odb::sqlite::details::cli::thunk< options, std::string, &options::database_ >;
_cli_options_map_["--create"] =
&::odb::sqlite::details::cli::thunk< options, bool, &options::create_ >;
_cli_options_map_["--read-only"] =
&::odb::sqlite::details::cli::thunk< options, bool, &options::read_only_ >;
_cli_options_map_["--options-file"] =
&::odb::sqlite::details::cli::thunk< options, std::string, &options::options_file_ >;
}
};
static _cli_options_map_init _cli_options_map_init_;
bool options::
_parse (const char* o, ::odb::sqlite::details::cli::scanner& s)
{
_cli_options_map::const_iterator i (_cli_options_map_.find (o));
if (i != _cli_options_map_.end ())
{
(*(i->second)) (*this, s);
return true;
}
return false;
}
void options::
_parse (::odb::sqlite::details::cli::scanner& s,
::odb::sqlite::details::cli::unknown_mode opt_mode,
::odb::sqlite::details::cli::unknown_mode arg_mode)
{
bool opt = true;
while (s.more ())
{
const char* o = s.peek ();
if (std::strcmp (o, "--") == 0)
{
s.skip ();
opt = false;
continue;
}
if (opt && _parse (o, s));
else if (opt && std::strncmp (o, "-", 1) == 0 && o[1] != '\0')
{
switch (opt_mode)
{
case ::odb::sqlite::details::cli::unknown_mode::skip:
{
s.skip ();
continue;
}
case ::odb::sqlite::details::cli::unknown_mode::stop:
{
break;
}
case ::odb::sqlite::details::cli::unknown_mode::fail:
{
throw ::odb::sqlite::details::cli::unknown_option (o);
}
}
break;
}
else
{
switch (arg_mode)
{
case ::odb::sqlite::details::cli::unknown_mode::skip:
{
s.skip ();
continue;
}
case ::odb::sqlite::details::cli::unknown_mode::stop:
{
break;
}
case ::odb::sqlite::details::cli::unknown_mode::fail:
{
throw ::odb::sqlite::details::cli::unknown_argument (o);
}
}
break;
}
}
}
}
}
}
// Begin epilogue.
//
//
// End epilogue.
|