main.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * main.cpp for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #include <iostream>
  7. #include <locale.h>
  8. #include "streamConsumer.hh"
  9. #include "curseOutput.hh"
  10. #include "params.hh"
  11. #include "jsonException.hh"
  12. void displayException(const Params *params, const std::string &type, const JsonException &e)
  13. {
  14. std::string buffer = e.getHistory();
  15. std::cerr << params->getProgName() << ": [" << type << "] at line " << e.currentLine() << ", " << e.currentCol() << " (" << e.what() << ") while reading" << std::endl;
  16. std::cerr << buffer << std::endl << std::string(buffer.size() -1, '~') << '^' << std::endl;
  17. }
  18. void run(Params *params)
  19. {
  20. StreamConsumer stream(StreamConsumer(params->getInput()));
  21. stream.withConfig(params);
  22. CurseOutput *out;
  23. JSonElement *root;
  24. if (!params->isIgnoringUnicode())
  25. setlocale(LC_ALL, "");
  26. try
  27. {
  28. root = stream.read()->getRoot();
  29. if (!root)
  30. throw EofException();
  31. }
  32. catch (EofException &e)
  33. {
  34. std::cerr << params->getProgName() << ": " << Warning::getType(e) << " (" << e.what() << ") error while reading" << std::endl;
  35. return;
  36. }
  37. catch (JsonException &e)
  38. {
  39. std::cerr << "Error: ";
  40. displayException(params, Warning::getType(e), e);
  41. return;
  42. }
  43. for (Warning w : stream.getMessages())
  44. {
  45. std::cerr << "Warning: ";
  46. displayException(params, w.getType(), w());
  47. }
  48. out = new CurseOutput(root, *params);
  49. out->run();
  50. delete out;
  51. }
  52. int main(int ac, char **av)
  53. {
  54. (void) ac;
  55. Params *params = new Params(av);
  56. bool _run = false;
  57. try {
  58. _run = params->read();
  59. }
  60. catch (std::runtime_error &e)
  61. {
  62. std::cerr << *av << ": " << e.what() << std::endl;
  63. params->usage();
  64. delete params;
  65. exit (EXIT_FAILURE);
  66. }
  67. if (_run)
  68. run(params);
  69. delete params;
  70. return 0;
  71. }