default_stream.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2008-2018 Samuel Newbold
  2. #include <stdio.h>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <readline/readline.h>
  7. #include <readline/history.h>
  8. #include <string>
  9. #include <vector>
  10. #include "rwsh_stream.h"
  11. #include "default_stream.h"
  12. Default_istream::Default_istream(int fd_i) : Rwsh_istream(fd_i) {
  13. switch (fd_v) {
  14. case 0: c_style = stdin; break;
  15. default: std::abort();}}
  16. Rwsh_istream* Default_istream::copy_pointer(void) {std::abort();}; // not needed
  17. int Default_istream::fd(void) {return fd_v;}
  18. Rwsh_istream& Default_istream::getline(std::string& dest) {
  19. if (readline_enabled) return readline_getline(dest);
  20. else return read_getline(dest);}
  21. Rwsh_istream& Default_istream::readline_getline(std::string& dest) {
  22. char *line = readline("");
  23. if (!line) {
  24. fail_v = true;
  25. return *this;
  26. }
  27. add_history(line);
  28. dest = std::string(line);
  29. free(line);
  30. return *this;}
  31. std::string Default_istream::str(void) const {return "<&1";}
  32. Default_ostream::Default_ostream(int fd_i) : Rwsh_ostream(fd_i) {
  33. switch (fd_v) {
  34. case 1: cpp_style = &std::cout; c_style = stdout; break;
  35. case 2: cpp_style = &std::cerr; c_style = stderr; break;
  36. default: std::abort();}}
  37. Rwsh_ostream* Default_ostream::copy_pointer(void) {std::abort();}; // not needed
  38. Rwsh_ostream& Default_ostream::operator<<(const std::string& r) {
  39. *cpp_style <<r;
  40. return *this;}
  41. Rwsh_ostream& Default_ostream::operator<<(int r) {
  42. *cpp_style <<r;
  43. return *this;}
  44. Rwsh_ostream& Default_ostream::operator<<(unsigned int r) {
  45. *cpp_style <<r;
  46. return *this;}
  47. Rwsh_ostream& Default_ostream::operator<<(double r) {
  48. *cpp_style <<r;
  49. return *this;}
  50. Rwsh_ostream& Default_ostream::operator<<(struct timeval r) {
  51. *cpp_style <<r.tv_sec <<"."
  52. <<std::setw(6) <<std::setfill('0') <<r.tv_usec;
  53. return *this;}
  54. int Default_ostream::fd(void) {return fd_v;}
  55. void Default_ostream::flush(void) {cpp_style->flush();}
  56. std::string Default_ostream::str(void) const {return ">&0";}