console.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <cstdio>
00014 #include <string>
00015 #include <ecl/exceptions/standard_exception.hpp>
00016 #include "../../include/ecl/devices/console.hpp"
00017 
00018 /*****************************************************************************
00019 ** Namespaces
00020 *****************************************************************************/
00021 
00022 namespace ecl {
00023 
00024 /*****************************************************************************
00025 ** Implementation [OConsole]
00026 *****************************************************************************/
00027 
00028 long OConsole::write(const char &c) ecl_assert_throw_decl(StandardException)
00029 {
00030         long n = buffer.append(c);
00031         if ( buffer.full() ) {
00032                 flush();
00033         }
00034         return n;
00035 }
00036 
00037 long OConsole::write(const char* s, unsigned long n) ecl_assert_throw_decl(StandardException)
00038 {
00039         unsigned int no_written = 0;
00040         while ( no_written < n ) {
00041                 no_written += buffer.append(s+no_written,n-no_written);
00042                 if ( buffer.full() ) {
00043                         flush();
00044                 }
00045         }
00046         return n;
00047 }
00048 
00049 void OConsole::flush() ecl_assert_throw_decl(StandardException) {
00050     fputs(buffer.c_str(),stdout);
00051     buffer.clear();
00052         int result = fflush(stdout);
00053         ecl_assert_throw ( result == 0, StandardException(LOC, WriteError, std::string("Could not flush to the standard output device.")));
00054 }
00055 
00056 /*****************************************************************************
00057 ** Implementation [EConsole]
00058 *****************************************************************************/
00059 
00060 long EConsole::write(const char &c) ecl_assert_throw_decl(StandardException)
00061 {
00062         long n = buffer.append(c);
00063         if ( buffer.full() ) {
00064                 flush();
00065         }
00066         return n;
00067 }
00068 
00069 long EConsole::write(const char* s, unsigned long n) ecl_assert_throw_decl(StandardException)
00070 {
00071         unsigned int no_written = 0;
00072         while ( no_written < n ) {
00073                 no_written += buffer.append(s+no_written,n-no_written);
00074                 if ( buffer.full() ) {
00075                         flush();
00076                 }
00077         }
00078         return n;
00079 }
00080 
00081 void EConsole::flush() ecl_assert_throw_decl(StandardException) {
00082     fputs(buffer.c_str(),stderr);
00083     buffer.clear();
00084         int result = fflush(stderr);
00085         ecl_assert_throw ( result == 0, StandardException(LOC, WriteError, std::string("Could not flush to the standard output device.")));
00086 }
00087 
00088 /*****************************************************************************
00089 ** Implementation [IConsole]
00090 *****************************************************************************/
00091 
00092 long IConsole::read(char &c) ecl_assert_throw_decl(StandardException)
00093 {
00094         // fgets is a problem, it doesn't read the newline with the character,
00095         // which means the next read from stdin will catch that newline first.
00096     c = static_cast<char>(fgetc(stdin));
00097     if ( c == EOF ) {
00098         ecl_assert_throw( c != EOF, StandardException(LOC, ReadError, "Failed to read from standard input."));
00099         return 0; // fallover if not in debug mode.
00100     } else {
00101 //      while ( fgetc(stdin) != '\n' ) {}
00102         return 1;
00103     }
00104 }
00105 
00106 long IConsole::read(char* s, const unsigned long &n) ecl_assert_throw_decl(StandardException)
00107 {
00108     char *result = fgets(s,n,stdin);
00109     if ( result == NULL ) {
00110         ecl_debug_throw( StandardException(LOC, ReadError, "Failed to read from standard input."));
00111         return 0; // Fallover if not in debug mode.
00112     } else {
00113         // fgets always terminates with a null character, even if it manages
00114         // to read n-1 chars. We also need to make sure we drop the newline
00115         // character that was returned to trigger the input.
00116         size_t length = strlen(s); // This doesn't count the \0 character.
00117 //      *(s+length-1) = '\0'; // drop the newline
00118 //      return length-1;
00119         return length;
00120     }
00121 }
00122 
00123 } // namespace ecl


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:37