params.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * params.cpp for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #include <fstream>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <curses.h>
  10. #include <unistd.h>
  11. #include "params.hh"
  12. #include "config.h"
  13. Params::Params(char **av) :progName(*av), strict(true)
  14. {
  15. av++;
  16. while (*av)
  17. {
  18. params.push_back(*av);
  19. av++;
  20. }
  21. }
  22. bool Params::read()
  23. {
  24. bool written = false;
  25. std::stringstream *input = nullptr;
  26. ignoreUnicode = false;
  27. colorMode = true;
  28. for (std::list<std::string>::const_iterator i = params.cbegin(); i != params.cend(); i++)
  29. {
  30. std::string tmp(*i);
  31. if (!input)
  32. {
  33. if (tmp == "-f")
  34. {
  35. i++;
  36. if (i == params.cend())
  37. throw std::runtime_error("Invalid use of -f without argument");
  38. tmp = *i;
  39. std::ifstream *in = new std::ifstream(tmp);
  40. if (!in->is_open())
  41. {
  42. delete in;
  43. throw std::runtime_error("Cannot open " +tmp +" for reading");
  44. }
  45. this->input = in;
  46. }
  47. else if (tmp == "--")
  48. {
  49. input = new std::stringstream();
  50. }
  51. else if (tmp == "-W")
  52. strict = false;
  53. else if (tmp == "--ascii")
  54. ignoreUnicode = true;
  55. else if (tmp == "--color")
  56. colorMode = true;
  57. else if (tmp == "--help" || tmp == "-h")
  58. {
  59. usage();
  60. return false;
  61. }
  62. else if (tmp == "--version" || tmp == "-v")
  63. {
  64. version();
  65. return false;
  66. }
  67. else if (tmp.find("--color=") == 0)
  68. {
  69. std::string mode = tmp.substr(8);
  70. if (mode == "always")
  71. colorMode = true;
  72. else if (mode == "never")
  73. colorMode = false;
  74. else
  75. throw std::runtime_error("Invalid option for --color: " +mode);
  76. }
  77. else
  78. throw std::runtime_error("Invalid argument: " +tmp);
  79. }
  80. else
  81. {
  82. if (written)
  83. input->write(" ", sizeof(char));
  84. else
  85. written = true;
  86. input->write(tmp.c_str(), sizeof(char) * tmp.size());
  87. }
  88. }
  89. if (!this->input)
  90. this->input = input;
  91. return true;
  92. }
  93. Params::~Params()
  94. {
  95. if (input)
  96. delete input;
  97. }
  98. std::basic_istream<char> &Params::getInput() const
  99. {
  100. if (input != nullptr)
  101. return *input;
  102. return std::cin;
  103. }
  104. void Params::usage() const noexcept
  105. {
  106. std::cout << "Usage: " << progName << " [OPTIONS] [--] INPUT" << std::endl
  107. << "Read json input and print it using ncurse" << std::endl << std::endl
  108. << "if not INPUT nor -f, use standard input" << std::endl << std::endl
  109. << " -f filename\t\tread input from filename instead of stdin" << std::endl
  110. << " -W \t\t\tconsider continuing on non-blocking errors" << std::endl
  111. << " --ascii\t\tignore unicode values" << std::endl
  112. << " --color[=MODE]\tcolorize output, MODE can be never or always (default when ommited)" << std::endl
  113. << " -v, -version\t\tdisplay version information" << std::endl
  114. << " -h, --helph\t\tshow this message and exit" << std::endl << std::endl
  115. << "Examples:" << std::endl
  116. << STROLL_PROGNAME << " -f f.json\tOutput f.json's content" << std::endl << std::endl
  117. << "Report bugs to <isundill@gmail.com>" << std::endl;
  118. }
  119. void Params::version() const noexcept
  120. {
  121. std::cout << STROLL_PROGNAME << " (jsonstroller suite) " << VERSION << " generated on " << VERSIONDATE << std::endl << std::endl
  122. << "Copyright (C) 2016 Free Software Foundation, Inc." << std::endl
  123. << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>." << std::endl
  124. << "This is free software: you are free to change and redistribute it." << std::endl
  125. << "There is NO WARRANTY, to the extent permitted by law." << std::endl << std::endl
  126. << "Written by isundil <isundill@gmail.com>." << std::endl;
  127. }
  128. bool Params::isStrict() const
  129. { return strict; }
  130. bool Params::colorEnabled() const
  131. { return colorMode; }
  132. bool Params::isIgnoringUnicode() const
  133. { return ignoreUnicode; }
  134. const std::string &Params::getProgName() const
  135. { return progName; }