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 
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) ecl_assert_throw_decl(StandardException)
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() ecl_assert_throw_decl(StandardException) {
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 
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) ecl_assert_throw_decl(StandardException)
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() ecl_assert_throw_decl(StandardException) {
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 
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) ecl_assert_throw_decl(StandardException)
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
void flush() ecl_assert_throw_decl(StandardException)
Flush the internal buffer.
Definition: console.cpp:49
long write(const char &c) ecl_assert_throw_decl(StandardException)
Write a character to the buffer.
Definition: console.cpp:28
Embedded control libraries.
#define LOC
#define ecl_assert_throw(expression, exception)
WriteError
long read(char &c) ecl_assert_throw_decl(StandardException)
Read a character from standard input.
Definition: console.cpp:92
void flush() ecl_assert_throw_decl(StandardException)
Flush the internal buffer.
Definition: console.cpp:81
#define ecl_assert_throw_decl(exception)
#define ecl_debug_throw(exception)
long write(const char &c) ecl_assert_throw_decl(StandardException)
Write a character to the buffer.
Definition: console.cpp:60
devices::CharStringBuffer buffer
Definition: console.hpp:105


ecl_devices
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:45