Home
CodeBlog
Articles
Downloads
Links
Books
About
|
Websites |
Turbo C console functionsFor those missing the old conio.h from Turbo C, here is an implementation for windows.Also, I would like to complete this code (as you can see there are some functions not implemented) and create a version for linux as well. See also memory to include the bit functions. download source Some help about conio // // Copyright (C) 2008, Thiago Adams (thiago.adams@gmail.com) // Permission to copy, use, modify, sell and distribute this software // is granted provided this copyright notice appears in all copies. // This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // #ifndef __CONSOLE_HEADER__ #define __CONSOLE_HEADER__ namespace console { enum COLORS { BLACK = 0, BLUE = 1, GREEN = 2, CYAN = 3, RED = 4, MAGENTA = 5, BROWN = 6, LIGHTGRAY = 7, DARKGRAY = 8, LIGHTBLUE = 9, LIGHTGREEN = 10, LIGHTCYAN = 11, LIGHTRED = 12, LIGHTMAGENTA = 13, YELLOW = 14, WHITE = 15, BLINK = 128 }; enum CURSORTYPE { _NOCURSOR,// (turns off the cursor) _SOLIDCURSOR,// (solid block cursor) _NORMALCURSOR // (normal underscore cursor) }; struct text_info { unsigned char winleft; /* left window coordinate */ unsigned char wintop; /* top window coordinate */ unsigned char winright; /* right window coordinate */ unsigned char winbottom; /* bottom window coordinate */ unsigned char attribute; /* text attribute */ unsigned char normattr; /* normal attribute */ unsigned char currmode; /* current video mode: BW40, BW80, C40, C80, or C4350 */ unsigned char screenheight; /* text screen's height */ unsigned char screenwidth; /* text screen's width */ unsigned char curx; /* x-coordinate in current window */ unsigned char cury; /* y-coordinate in current window */ }; char *cgets(char *buffer); void clreol(void); void clrscr(); int cputs(const char *str); void delline(void); int getch(void); int getche(void); char *getpass(const char *prompt); int gettext(int left, int top, int right, int bottom, void*destin); void gettextinfo(struct text_info *r); void gotoxy(int x, int y); void highvideo(); void insline(void); int kbhit(void); void lowvideo(); int movetext(int left, int top, int right, int bottom, int destleft, int desttop); void normvideo(); int putch(int c); int puttext(int left, int top, int right, int bottom, const char*source); void _setcursortype(int cur_t); void textattr(int newattr); int textbackground(int newcolor); int textcolor(int newcolor); int ungetch(int ch); int wherex(); int wherey(); //window //TODO } // namespace console #endif //__CONSOLE_HEADER__Windows - Implementation #include "console.h" #include <conio.h> //from windows #include <cassert> #include <windows.h> namespace console { using namespace memory; char *_cgets(char *buffer) { return _cgets(buffer); } int getch(void) { return _getch(); } int getche(void) { return _getche(); } int kbhit(void) { return _kbhit(); } int cputs(const char *str) { return _cputs(str); } void gettextinfo(text_info *r) { if (r == 0) return; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); r->attribute = csbi.wAttributes; r->curx = csbi.dwCursorPosition.X; r->cury = csbi.dwCursorPosition.Y; r->screenwidth = csbi.dwMaximumWindowSize.X; r->screenheight = csbi.dwMaximumWindowSize.X; r->winleft = csbi.srWindow.Left; r->wintop = csbi.srWindow.Top; r->winright = csbi.srWindow.Right; r->winbottom = csbi.srWindow.Bottom; r->normattr = 0; r->currmode = 3; } void _setcursortype(int cur_t) { CONSOLE_CURSOR_INFO ci; switch (cur_t) { case _NOCURSOR:// (turns off the cursor) ci.bVisible = FALSE; ci.dwSize = 0; break; case _SOLIDCURSOR:// (solid block cursor) ci.bVisible = TRUE; ci.dwSize = 100; break; default: case _NORMALCURSOR: // (normal underscore cursor) ci.bVisible = TRUE; ci.dwSize = 50; break; } SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci); } void textattr(int newattr) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), newattr); } int putch(int c) { return _putch(c); } void clreol(void) { //clreol clears all characters from the cursor //position to the end of the line within the //current text window, without moving the //cursor. } char *getpass(const char *prompt) { //reads password // getpass reads a password from the system //console, after prompting with the //null-terminated string prompt and disabling //the echo. //It returns a pointer to a null-terminated //string of up to eight characters (not //counting the null terminator). return 0; } void delline(void) { //tODO inserts blank line } void insline(void) { //tODO inserts blank line } int movetext(int left, int top, int right, int bottom, int destleft, int desttop) { //TODO return 0; } int gettext(int left, int top, int right, int bottom, void*destin) { if (destin == 0) return 0; char * pszText = (char *) destin; int count = 0; for (int k = top; (k <= bottom) && (*pszText); k++) { for (int i = left; (i <= right) && (*pszText); i++) { COORD point = {SHORT(i), SHORT(k) }; TCHAR ch; DWORD c; ReadConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), &ch, 1, point, &c); *pszText = ch; count++; pszText++; } } *pszText = 0; return count; } int puttext(int left, int top, int right, int bottom, const char*source) { if (source == 0) return 0; const char * pszText = (const char *) source; text_info ti; gettextinfo(&ti); int count = 0; for (int k = top; (k <= bottom) && (*pszText); k++) { for (int i = left; (i < right) && (*pszText); i++) { gotoxy(i, k); putch(*pszText); count++; pszText++; } } gotoxy(ti.curx, ti.cury); return count; } int textbackground(int newcolor) { text_info ti; gettextinfo(&ti); unsigned char wColor = ti.attribute; unsigned char old = getbits(wColor, 4, 4); setbits(wColor, 4, 4, newcolor); textattr(wColor); return old; } int textcolor(int newcolor) { text_info ti; gettextinfo(&ti); unsigned char wColor = ti.attribute; int old = getbits(wColor, 0, 4); setbits(wColor, 0, 4, newcolor); textattr(wColor); return old; } void highvideo() { text_info ti; gettextinfo(&ti); setbits(ti.attribute, 3, 1, 1); textattr(ti.attribute); } void lowvideo() { text_info ti; gettextinfo(&ti); setbits(ti.attribute, 3, 1, 0); textattr(ti.attribute); } void normvideo() { text_info ti; gettextinfo(&ti); setbits(ti.attribute, 3, 1, 0); setbits(ti.attribute, 7, 1, 0); textattr(ti.attribute); } int wherex() { text_info ti; gettextinfo(&ti); return ti.curx; } int wherey() { text_info ti; gettextinfo(&ti); return ti.cury; } void gotoxy(int x, int y) { COORD point = {SHORT(x), SHORT(y)}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point); } void clrscr() { COORD coordScreen = {0, 0}; unsigned long cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; unsigned long dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); } int ungetch(int ch) { return _ungetch(ch); } } // namespace consoleUtils void Box(int left, int top, int right, int bottom, int color = console::WHITE, char pattern = ' ') { int i = console::textbackground(color); console::text_info ti; console::gettextinfo(&ti); int count = 0; for (int k = top; (k <= bottom); k++) { for (int i = left; (i < right); i++) { console::gotoxy(i, k); console::putch(pattern); } } console::gotoxy(ti.curx, ti.cury); console::textbackground(i); }
|