Go to the documentation of this file.00001
00009
00010
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
00020
00021
00022 namespace ecl {
00023
00024
00025
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
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
00090
00091
00092 long IConsole::read(char &c) ecl_assert_throw_decl(StandardException)
00093 {
00094
00095
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;
00100 } else {
00101
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;
00112 } else {
00113
00114
00115
00116 size_t length = strlen(s);
00117
00118
00119 return length;
00120 }
00121 }
00122
00123 }