console.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <cstdio>
14 #include <string>
16 #include "../../include/ecl/devices/console.hpp"
17 
18 /*****************************************************************************
19 ** Namespaces
20 *****************************************************************************/
21 
22 namespace ecl {
23 
24 /*****************************************************************************
25 ** Implementation [OConsole]
26 *****************************************************************************/
27 
28 long OConsole::write(const char &c)
29 {
30  long n = buffer.append(c);
31  if ( buffer.full() ) {
32  flush();
33  }
34  return n;
35 }
36 
37 long OConsole::write(const char* s, unsigned long n)
38 {
39  unsigned int no_written = 0;
40  while ( no_written < n ) {
41  no_written += buffer.append(s+no_written,n-no_written);
42  if ( buffer.full() ) {
43  flush();
44  }
45  }
46  return n;
47 }
48 
49 void OConsole::flush() {
50  fputs(buffer.c_str(),stdout);
51  buffer.clear();
52  int result = fflush(stdout);
53  ecl_assert_throw ( result == 0, StandardException(LOC, WriteError, std::string("Could not flush to the standard output device.")));
54 }
55 
56 /*****************************************************************************
57 ** Implementation [EConsole]
58 *****************************************************************************/
59 
60 long EConsole::write(const char &c)
61 {
62  long n = buffer.append(c);
63  if ( buffer.full() ) {
64  flush();
65  }
66  return n;
67 }
68 
69 long EConsole::write(const char* s, unsigned long n)
70 {
71  unsigned int no_written = 0;
72  while ( no_written < n ) {
73  no_written += buffer.append(s+no_written,n-no_written);
74  if ( buffer.full() ) {
75  flush();
76  }
77  }
78  return n;
79 }
80 
81 void EConsole::flush() {
82  fputs(buffer.c_str(),stderr);
83  buffer.clear();
84  int result = fflush(stderr);
85  ecl_assert_throw ( result == 0, StandardException(LOC, WriteError, std::string("Could not flush to the standard output device.")));
86 }
87 
88 /*****************************************************************************
89 ** Implementation [IConsole]
90 *****************************************************************************/
91 
92 long IConsole::read(char &c)
93 {
94  // fgets is a problem, it doesn't read the newline with the character,
95  // which means the next read from stdin will catch that newline first.
96  c = static_cast<char>(fgetc(stdin));
97  if ( c == EOF ) {
98  ecl_assert_throw( c != EOF, StandardException(LOC, ReadError, "Failed to read from standard input."));
99  return 0; // fallover if not in debug mode.
100  } else {
101 // while ( fgetc(stdin) != '\n' ) {}
102  return 1;
103  }
104 }
105 
106 long IConsole::read(char* s, const unsigned long &n)
107 {
108  char *result = fgets(s,n,stdin);
109  if ( result == NULL ) {
110  ecl_debug_throw( StandardException(LOC, ReadError, "Failed to read from standard input."));
111  return 0; // Fallover if not in debug mode.
112  } else {
113  // fgets always terminates with a null character, even if it manages
114  // to read n-1 chars. We also need to make sure we drop the newline
115  // character that was returned to trigger the input.
116  size_t length = strlen(s); // This doesn't count the \0 character.
117 // *(s+length-1) = '\0'; // drop the newline
118 // return length-1;
119  return length;
120  }
121 }
122 
123 } // namespace ecl
ecl::WriteError
WriteError
ecl::OConsole::buffer
devices::CharStringBuffer buffer
Definition: console.hpp:119
ecl::IConsole::read
long read(char &c)
Read a character from standard input.
Definition: console.cpp:96
ecl::EConsole::flush
void flush()
Flush the internal buffer.
Definition: console.cpp:85
ecl::devices::CharStringBuffer::full
bool full() const
Definition: character_buffer.cpp:76
ecl::OConsole::write
long write(const char &c)
Write a character to the buffer.
Definition: console.cpp:32
LOC
#define LOC
ecl::OConsole::flush
void flush()
Flush the internal buffer.
Definition: console.cpp:53
ecl::StandardException
ecl::devices::CharStringBuffer::c_str
const char * c_str()
Definition: character_buffer.cpp:113
ecl::devices::CharStringBuffer::clear
void clear()
Definition: character_buffer.cpp:109
ecl::EConsole::buffer
devices::CharStringBuffer buffer
Definition: console.hpp:191
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
standard_exception.hpp
ecl::EConsole::write
long write(const char &c)
Write a character to the buffer.
Definition: console.cpp:64
ecl::devices::CharStringBuffer::append
long append(const char &c)
Definition: character_buffer.cpp:84
ecl::ReadError
ReadError
ecl
Embedded control libraries.
ecl_debug_throw
#define ecl_debug_throw(exception)


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:45