shell.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file shell.cpp
  3. /// \brief The "shell" is the model's interface to the world
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date: 2013-10-10 10:20:33 +0200 (Thu, 10 Oct 2013) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #include "config.h"
  10. #include "shell.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <memory>
  14. namespace {
  15. /// The global Shell object
  16. std::auto_ptr<Shell> current_shell;
  17. }
  18. void dprintf(xtring format,...) {
  19. // printf-style function accessible throughout the model code.
  20. // Sends text to currently chosen Shell object, which in turn
  21. // sends it to the screen and/or log file.
  22. va_list v;
  23. va_start(v,format);
  24. xtring output;
  25. formatf(output,format,v);
  26. current_shell->log_message(output);
  27. }
  28. void fail(xtring format,...) {
  29. // printf-style function accessible throughout the model code.
  30. // Sends text to currently chosen Shell, which prints it and
  31. // terminates the program.
  32. va_list v;
  33. va_start(v,format);
  34. xtring output;
  35. formatf(output,format,v);
  36. current_shell->fail(output);
  37. }
  38. void plot(xtring window_name,xtring series_name,double x,double y) {
  39. current_shell->plot(window_name, series_name, x, y);
  40. }
  41. void resetwindow(xtring window_name) {
  42. current_shell->resetwindow(window_name);
  43. }
  44. void clear_all_graphs() {
  45. current_shell->clear_all_graphs();
  46. }
  47. void open3d() {
  48. current_shell->open3d();
  49. }
  50. void plot3d(const char* filename) {
  51. current_shell->plot3d(filename);
  52. }
  53. bool abort_request_received() {
  54. return current_shell->abort_request_received();
  55. }
  56. void set_shell(Shell* s) {
  57. current_shell = std::auto_ptr<Shell>(s);
  58. }
  59. ////////////////////////////////////////////////////////////////////////////////
  60. //// Implementation of CommandLineShell
  61. ////////////////////////////////////////////////////////////////////////////////
  62. CommandLineShell::CommandLineShell(const char* logfile_path) {
  63. logfile = fopen(logfile_path, "wt");
  64. if (!logfile) {
  65. printf("Could not open log file %s for output\n", logfile_path);
  66. exit(99);
  67. }
  68. }
  69. CommandLineShell::~CommandLineShell() {
  70. if (logfile) {
  71. fclose(logfile);
  72. }
  73. }
  74. void CommandLineShell::fail(const char* message) {
  75. fprintf(stderr, "%s\n", message); // ecev3
  76. log_message(xtring(message)+"\n");
  77. exit(99);
  78. }
  79. void CommandLineShell::log_message(const char* message) {
  80. // fprintf(stdout,"%s", message); // ecev3
  81. fprintf(logfile,"%s", message);
  82. fflush(logfile);
  83. }
  84. void CommandLineShell::plot(const char* window_name,
  85. const char* series_name,
  86. double x,
  87. double y) {
  88. // Can't do anything here
  89. }
  90. void CommandLineShell::open3d() {
  91. // Can't do anything here
  92. }
  93. void CommandLineShell::plot3d(const char* filename) {
  94. // Can't do anything here
  95. }
  96. void CommandLineShell::resetwindow(const char* window_name) {
  97. // Can't do anything here
  98. }
  99. void CommandLineShell::clear_all_graphs() {
  100. // Can't do anything here
  101. }
  102. bool CommandLineShell::abort_request_received() {
  103. // Can't do anything here
  104. return false;
  105. }