Codice:
#include <windows.h>
#include <iostream.h>
class Con_base
{
protected:
static HANDLE hStdout, hStdin;
static DWORD fdwMode, fdwOldMode;
static BOOL firstRun;
static WORD wOldColorAttrs;
DWORD read(void *buf, DWORD size);
BOOL print(const void *buf, const DWORD size);
public:
BOOL GotoXY(int x, int y);
BOOL TextColor(WORD mode);
Con_base();
~Con_base();
};
BOOL Con_base::firstRun = TRUE;
HANDLE Con_base::hStdout = 0;
HANDLE Con_base::hStdin = 0;
DWORD Con_base::fdwMode = 0;
DWORD Con_base::fdwOldMode = 0;
WORD Con_base::wOldColorAttrs = 0;
Con_base::Con_base()
{
if(firstRun)
{
firstRun = FALSE;
hStdin = GetStdHandle(STD_INPUT_HANDLE);
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
if(!GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
{
MessageBox(NULL, "GetConsoleScreenBufferInfo", "Console Error", MB_OK);
return;
}
wOldColorAttrs = csbiInfo.wAttributes;
if(!GetConsoleMode(hStdin, &fdwOldMode))
{
MessageBox(NULL, "GetConsoleMode", "Console Error", MB_OK);
return;
}
fdwMode = fdwOldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
if(!SetConsoleMode(hStdin, fdwMode))
{
MessageBox(NULL, "SetConsoleMode", "Console Error", MB_OK);
return;
}
if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
MessageBox(NULL, "GetStdHandle", "Console Error", MB_OK);
}
}
Con_base::~Con_base()
{
SetConsoleMode(hStdin, fdwOldMode);
SetConsoleTextAttribute(hStdout, wOldColorAttrs);
}
BOOL Con_base::GotoXY(int x, int y)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
if(!GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
return FALSE;
if(x >= csbiInfo.dwSize.X || y >= csbiInfo.dwSize.Y)
return FALSE;
csbiInfo.dwCursorPosition.X = x;
csbiInfo.dwCursorPosition.Y = y;
if(!SetConsoleCursorPosition(hStdout, csbiInfo.dwCursorPosition))
return FALSE;
return TRUE;
}
BOOL Con_base::TextColor(WORD mode)
{
if(!SetConsoleTextAttribute(hStdout, mode))
return FALSE;
return TRUE;
}
BOOL Con_base::print(const void *buf, const DWORD size)
{
DWORD cWritten;
if(!WriteFile(hStdout, buf, size, &cWritten, NULL))
return FALSE;
if(cWritten != size)
return FALSE;
return TRUE;
}
DWORD Con_base::read(void *buf, DWORD size)
{
DWORD cRead;
DWORD pos = 1;
do {
if(!ReadFile(hStdin, ((char *)buf), 1, &cRead, NULL))
return 0;
print(((char *)buf), 1);
} while(isspace(((char *)buf)[0]));
do {
if(!ReadFile(hStdin, ((char *)buf) + pos, 1, &cRead, NULL))
return 0;
if(!isspace(((char *)buf)[pos]))
print(((char *)buf) + pos, 1);
++pos;
} while(!isspace(((char *)buf)[pos-1]) && pos < size);
return pos-1;
}
class ConOut : public Con_base
{
public:
ConOut(){ };
ConOut & operator << (const __int64 w);
ConOut & operator << (const int w);
ConOut & operator << (const char w);
ConOut & operator << (const char *w);
};
ConOut & ConOut::operator << (const __int64 w)
{
char buffer[256];
_i64toa(w, buffer, 10);
print(buffer, strlen(buffer));
return *this;
}
ConOut & ConOut::operator << (const int w)
{
return *this << (__int64)w;
}
ConOut & ConOut::operator << (const char w)
{
print(&w, sizeof(char));
return *this;
}
ConOut & ConOut::operator << (const char *w)
{
print(w, strlen(w));
return *this;
}
#define IN_BUFFER_SIZE 256
class ConIn : public Con_base
{
public:
ConIn(){ };
ConIn & operator >> (__int64 &w);
ConIn & operator >> (int &w);
ConIn & operator >> (char &w);
ConIn & operator >> (char *w);
};
ConIn & ConIn::operator >> (__int64 &w)
{
char buffer[256];
DWORD num = read(buffer, 255);
if(num > 0)
{
buffer[num] = '\0';
w = _atoi64(buffer);
}
return *this;
}
ConIn & ConIn::operator >> (int &w)
{
char buffer[20];
DWORD num = read(buffer, 19);
if(num > 0)
{
buffer[num] = '\0';
w = atoi(buffer);
}
return *this;
}
ConIn & ConIn::operator >> (char &w)
{
read(&w, sizeof(char));
return *this;
}
ConIn & ConIn::operator >> (char *w)
{
char buffer[256];
DWORD num;
if((num = read(buffer, 255)) > 0)
{
buffer[num] = '\0';
strcpy(w, buffer);
}
return *this;
}
void main(void)
{
ConOut out;
ConIn in;
out << 'a' << '<' << 10 ;
out.GotoXY(10, 10);
int a;
in >> a;
out.TextColor(FOREGROUND_BLUE|FOREGROUND_INTENSITY);
out << 'a' << '<' << 10 << ' ' << a;
out.GotoXY(17, 15);
char str[100];
in >> str;
out.TextColor(FOREGROUND_GREEN);
out << "ciao" << "\n\r" << "ciao " << str;
}