Commits

Anonymous committed a5d804c

Borland Console I/O replacement (non-standard)

Comments (0)

Files changed (9)

+This stuff supports text mode color functions etc. for whatever platform,
+it is implemented differently on every OS but the hi-level functions work
+the same. */
+#ifndef INC_DLCONIO_H
+#define INC_DLCONIO_H
+/*-------------------------------------------------------------------------*/
+
+/*
+  This header is to support the Borland console mode graphics-type functions,
+  things which change color and that sort of thing.  It uses their names and
+  passes it on to libraries built for a particular operating system.
+*/
+
+#if defined(__WIN32__) && defined(__UNIX__)
+# error ("dlconio: Cannot define both __WIN32__ and __UNIX__")
+#endif
+
+#if defined(__WIN32__) /* Is the program being built for Micro$oft Win32? */
+
+# include "w32cio.h" /* Win32 console functions (hidden under Borland compatible layer) */
+
+# define PutBlock W32CIO_PutBlock
+# define DrawLine W32CIO_DrawLine
+# define DrawRectangle W32CIO_DrawRectangle
+# define GetChar W32CIO_GetChar
+# define PutChar W32CIO_PutChar
+# define putch W32CIO_putch
+# define cputs W32CIO_cputs
+# define wherex W32CIO_wherex
+# define wherey W32CIO_wherey
+# define gotoxy W32CIO_gotoxy
+# define settextcolor W32CIO_settextcolor
+# define gettextcolor W32CIO_gettextcolor
+# define gettext W32CIO_gettext
+# define puttext W32CIO_puttext
+# define getmaxx() W32CIO_getmax((const unsigned char)0U)
+# define getmaxy() W32CIO_getmax((const unsigned char)~0U)
+
+#elif defined(__UNIX__)
+
+# error ("dlconio: No UNIX->curses support, contact Daybo Logic.")
+
+#endif /*__WIN32__*/
+
+/*-------------------------------------------------------------------------*/
+#endif /*!INC_DLCONIO_H*/
+project : w:\shared\dlconio\dlconio.dll .SYMBOLIC
+
+!include w:\shared\dlconio\dlconio.mk1
+!define BLANK ""
+w:\shared\dlconio\w32cio.obj : w:\shared\dlconio\w32cio.c .AUTODEPEND
+ @w:
+ cd w:\shared\dlconio
+ *wcc386 w32cio.c -i=C:\watcom\h;C:\watcom\h\nt;w:\shared;w:\shared\stds -w4&
+ -e25 -zq -od -d2 -bd -5r -bt=nt -mf
+
+w:\shared\dlconio\dlconio.dll : w:\shared\dlconio\w32cio.obj .AUTODEPEND
+ @w:
+ cd w:\shared\dlconio
+ @%write dlconio.lk1 FIL w32cio.obj
+ @%append dlconio.lk1 
+!ifneq BLANK ""
+ *wlib -q -n -b dlconio.imp 
+ @%append dlconio.lk1 LIBR dlconio.imp
+!endif
+!ifneq BLANK ""
+ @%append dlconio.lk1 op resource=
+!endif
+ *wlink name dlconio d all op inc SYS nt_dll op m op maxe=25 op q op symf @d&
+lconio.lk1
+ wlib -n -b dlconio.lib +dlconio.dll
+
+40
+targetIdent
+0
+MProject
+1
+MComponent
+0
+2
+WString
+4
+NDLL
+3
+WString
+5
+n_2dn
+1
+0
+1
+4
+MCommand
+0
+5
+MCommand
+0
+6
+MItem
+11
+dlconio.dll
+7
+WString
+4
+NDLL
+8
+WVList
+0
+9
+WVList
+0
+-1
+1
+1
+0
+10
+WPickList
+2
+11
+MItem
+3
+*.c
+12
+WString
+4
+COBJ
+13
+WVList
+1
+14
+MVState
+15
+WString
+3
+WCC
+16
+WString
+25
+n????Include directories:
+1
+17
+WString
+53
+$(%watcom)\h;$(%watcom)\h\nt;w:\shared;w:\shared\stds
+0
+18
+WVList
+0
+-1
+1
+1
+0
+19
+MItem
+8
+w32cio.c
+20
+WString
+4
+COBJ
+21
+WVList
+0
+22
+WVList
+0
+11
+1
+1
+0
+40
+projectIdent
+0
+VpeMain
+1
+WRect
+0
+0
+7680
+9200
+2
+MProject
+3
+MCommand
+0
+4
+MCommand
+0
+1
+5
+WFileName
+11
+dlconio.tgt
+6
+WVList
+1
+7
+VComponent
+8
+WRect
+0
+0
+5700
+4280
+0
+0
+9
+WFileName
+11
+dlconio.tgt
+0
+1
+7
+/*
+  This was created at the Isle of Wight college in 1998 to help port programs
+  that were using Borland color functions and such to Micro$oft's compiler.
+  It was in the days before the Daybo Logic standards.  So I've added the
+  dependancy to the header - Overlord David Duncan Ross Palmer 9th Feb 2000
+
+  Copyright 1998-2003 Daybo Logic.
+*/
+
+#include <windows.h>
+#include <conio.h> /* If not present, need more emulation code */
+#include <stdio.h>
+#include "dlstdinc.h" /* <--- Added, see note at top */
+#include "coordins.h"
+#ifdef HDRSTOP
+# pragma hdrstop
+#endif /*HDRSTOP*/
+
+#include "w32cio.h" /* Win32 specific header */
+/*-------------------------------------------------------------------------*/
+void W32CIO_PutBlock(COORD_XY* Coord, int Color)
+{
+  /* Save old cursor position */
+  int OldX = W32CIO_wherex();
+  int OldY = W32CIO_wherey();
+  /* Save old text color */
+  int OldColor = W32CIO_gettextcolor();
+
+  W32CIO_gotoxy(Coord->X,Coord->Y); /* Goto particualar block position ready to draw */
+
+  W32CIO_settextcolor(Color); /* Set correct color */
+  printf("\xDB"); /* Print a block */
+
+  W32CIO_gotoxy(OldX,OldY); /* Restore original cursor position */
+  W32CIO_settextcolor(OldColor); /* Restore original text color */
+}
+/*-------------------------------------------------------------------------*/
+void W32CIO_DrawLine(COORD_XY* Start, int Length, BOOL vert, int Color)
+{
+  int i; /* Looping var */
+
+  for (i=0; i<Length; i++) /* All of units of line */
+  {
+    COORD_XY NextCoord = *Start; /* Coordinate at location */
+    if (vert) /* Drawing vertical lines? */
+    {
+      NextCoord.Y += (SHORT)i; /* Goto current end of line */
+    }
+    else /* Horizontal? */
+    {
+      NextCoord.X += (SHORT)i; /* Goto current end of line */
+    }
+    W32CIO_PutBlock(&NextCoord, Color); /* Put block at draw loc */
+  }
+  return; /* End of routine */
+}
+/*-------------------------------------------------------------------------*/
+void W32CIO_DrawRectangle(COORD_XY* _StartCoord, COORD_XY* _EndCoord, int Color)
+{
+  /* Declarations */
+  COORD StartBottomLine;
+  COORD StartRightLine;
+  COORD StartCoord, EndCoord;
+  COORD_XY _startBottomLine, _startRightLine;
+
+  StartCoord.X = (SHORT)_StartCoord->X;
+  StartCoord.Y = (SHORT)_StartCoord->Y;
+  EndCoord.X = (SHORT)_EndCoord->X;
+  EndCoord.Y = (SHORT)_EndCoord->Y;
+
+  /* Some calculations we must do */
+  StartBottomLine.X = (SHORT)StartCoord.X;
+  StartBottomLine.Y = (SHORT)(EndCoord.Y - StartCoord.Y);
+  StartRightLine.X = (SHORT)(EndCoord.X - StartCoord.X);
+  StartRightLine.Y = (SHORT)StartCoord.Y;
+
+  _startBottomLine.X = (SHORT)StartBottomLine.X;
+  _startBottomLine.Y = (SHORT)StartBottomLine.Y;
+  _startRightLine.X = (SHORT)StartRightLine.X;
+  _startRightLine.Y = (SHORT)StartRightLine.Y;
+
+  /* Draw the lines */
+  W32CIO_DrawLine(_StartCoord, EndCoord.Y+1, TRUE, Color); /* Left line */
+  W32CIO_DrawLine(_StartCoord, EndCoord.X+1, FALSE, Color); /* Top line */
+  W32CIO_DrawLine(&_startBottomLine, EndCoord.X - StartCoord.X, FALSE, Color); /* Bottom line */
+  W32CIO_DrawLine(&_startRightLine, EndCoord.Y - StartCoord.Y, TRUE, Color); /* Right line */
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_wherex()
+{
+  CONSOLE_SCREEN_BUFFER_INFO csbi; /* For reading stuff back from evil Microsoft */
+  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); /* Read console info into above struct */
+  return csbi.dwCursorPosition.X; /* Give requested info to caller */
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_wherey()
+{
+  /* This does the same as the above function but with Y */
+  CONSOLE_SCREEN_BUFFER_INFO csbi;
+  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
+  return csbi.dwCursorPosition.Y;
+}
+/*-------------------------------------------------------------------------*/
+void W32CIO_gotoxy(int x, int y)
+{
+  COORD Coord; /* Holds co-ordinates of new cursor poition */
+  Coord.X = (SHORT)x;
+  Coord.Y = (SHORT)y; /* Set caller's request */
+  /* At this point I'd like to voice my opinion:  Borland did
+  say gotoxy() took ints, in fact screen coords only need shorts
+  that can be signed so Microsoft's function has a better idea here.
+  We have casted them for truncation without warning */
+
+  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Coord); /* Tell Windows of change */
+}
+/*-------------------------------------------------------------------------*/
+void W32CIO_settextcolor(int newcolor)
+{
+  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)newcolor);
+  return;
+  /* We have lost some precision of newcolor to Windows (out bitsize (int) to WORD
+  but it doesn't matter as the color range is small here. */
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_gettextcolor()
+{
+  CONSOLE_SCREEN_BUFFER_INFO csbi; /* For reading stuff back from evil Microsoft */
+  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); /* Read console info into above struct */
+  return (int)csbi.wAttributes; /* Give requested info to caller with increase in precision*/
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_gettext(int left, int top, int right, int bottom, void* destin)
+{
+  /* coord checks */
+  if (left < 1 || top < 1) /* Off Top/left ? */ return 0; /* Fails */
+
+  if (destin) /* Destination pointer to buffer must be passed */
+  {
+    unsigned int posindestin = 0; /* Position in the destination */
+    int VScan; /* Vertical scan iterator */
+    for (VScan=top;VScan<=bottom;VScan++) /* Vertical scan */
+    {
+      int HScan; /* Horizontal scan iterator */
+      for (HScan=left;HScan<=right;HScan++) /* Horizontal scan */
+      {
+        WORD CharAttr; /* Video attribute, send address to function */
+        unsigned char Char; /* Char at coord, send address to function */
+        COORD_XY WantCharCoord;
+            
+        WantCharCoord.X = HScan;
+        WantCharCoord.Y = VScan; /* Make coordinate of current scan position */
+
+        W32CIO_GetChar(&WantCharCoord, &CharAttr, &Char);
+
+        memcpy((unsigned char*)destin + posindestin, &Char, 1); /* Copy charactor to end of queue */
+        posindestin++; /* Increment pointer (NOTE: address of posindestin is always a relative offset from destin) */
+        memcpy((unsigned char*)destin + posindestin, &CharAttr, 1); /* Copy char color byte (video attribute) to end of queue */
+        posindestin++; /* Ready for next loop */
+      } /* horizontal scan loop */
+    } /* vertical scan loop */
+    return 1; /* Success */
+  } /* (destin) */
+  /* (Get here if NULL pointer passed) */
+  return 0; /* Indicates error */
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_puttext(int left, int top, int right, int bottom, void* source)
+{
+  /* coord checks */
+  if (left < 1 || top < 1) /* Off Top/Left ? */ return 0; /* Fails */
+
+  if (source) /* Source pointer to buffer must be passed */
+  {
+    unsigned int posinsource = 0; /* Position in the source buffer */
+    int VScan; /* Vertical scan iterator */
+    for (VScan=top;VScan<=bottom;VScan++) /* Vertical scan */
+    {
+      int HScan; /* Horizontal scan iterator */
+      for (HScan=left;HScan<=right;HScan++) /* Horizontal scan */
+      {
+        COORD_XY NewCharCoord;
+        WORD CharAttr; /* Video attribute, send address to function */
+        unsigned char Char; /* Char at coord, send to function */
+
+        NewCharCoord.X = (SHORT)HScan;
+        NewCharCoord.Y = (SHORT)VScan; /* Make coordinate of current scan position */
+
+        memcpy(&Char, (unsigned char*)source + posinsource, 1); /* Copy character out of buffer into byte on stack */
+        posinsource++; /* Increment pointer (NOTE: address of posinsource is always a relative offset from source) */
+        memcpy(&CharAttr, (unsigned char*)source + posinsource, 1); /* Copy video attribute for current character into WORD on stack */
+        posinsource++; /* Ready for next loop */
+
+        W32CIO_PutChar(&NewCharCoord, CharAttr, Char); /* Put character onto screen */
+      } /* Horizontal scan */
+    } /* Vertical scan */
+    return 1; /* Success */
+  } /* (source) */
+  /* Gets here if (!source) */
+  return 0; /* Done  - (failure) */
+}
+/*-------------------------------------------------------------------------*/
+BOOL W32CIO_GetChar(COORD_XY* Coord, WORD* PVideoAttr, unsigned char* PUChar)
+{
+  unsigned char buff[2]; /* Buffer to recieve chars, two for one NULL at the end */
+  DWORD Dummy; /* Dummy value that function wants a pointer to but in the result we are not interested */
+  BOOL RetVal[2]; /* Return values for Win32 funcs */
+  COORD cvtCoord;
+
+  cvtCoord.X = (SHORT)Coord->X;
+  cvtCoord.Y = (SHORT)Coord->Y;
+
+  RetVal[0] = ReadConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),
+                                         (LPTSTR)buff, 1/*# chars to read*/, cvtCoord, &Dummy);
+
+  /* We asked Windows to get the requested char from the screen */
+  if (PUChar) /* User has provided us with a pointer */
+    memcpy(PUChar, buff, 1); /* Copy byte to caller */
+
+  RetVal[1] = ReadConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
+                                         PVideoAttr, 1/*# chars to read*/, cvtCoord, &Dummy);
+
+  if (!RetVal[0] || !RetVal[1]) /* Either function failed? */
+    return FALSE; /* Report failure */
+  /*( implicit else )*/
+  return TRUE; /* Success */
+}
+/*-------------------------------------------------------------------------*/
+BOOL W32CIO_PutChar(COORD_XY* Coord, WORD VideoAttr, unsigned char UChar)
+{
+  unsigned char buff[2]; /* Buffer to write chars, two for one NULL at the end */
+  DWORD Dummy; /* Dummy value that function wants a pointer to but in the result we are not interested */
+  BOOL RetVal[2]; /* Return values for Win32 functions */
+  COORD cvtCoord;
+
+  cvtCoord.X = (SHORT)Coord->X;
+  cvtCoord.Y = (SHORT)Coord->Y;
+
+  /* Load values for calls to Windows */
+  buff[0] = UChar; /* Load char */ buff[1] = 0x0; /* NULL terminator */
+
+  RetVal[0] = WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),
+                                          (LPCTSTR)buff, 1/*# of chars to write*/, cvtCoord, &Dummy);
+  RetVal[1] = WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
+                                          (CONST WORD*)&VideoAttr, 1/*# attribute bytes to write*/,
+                                          cvtCoord, &Dummy);
+
+  if (!RetVal[0] || !RetVal[1]) /* Either function failed? */
+    return FALSE; /* Report failure */
+  /*( implicit else)*/
+  return TRUE; /* Success */
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_putch(int c)
+{
+  COORD Coord;
+  unsigned char buff[2];
+  DWORD Dummy; /* Dummy parameter for Windows */
+  WORD VidAttr; /* Collecting video attributes */
+  BOOL WINAPI_CallResult[2]; /* Win32 return values */
+
+  Coord.X = (SHORT)W32CIO_wherex();
+  Coord.Y = (SHORT)W32CIO_wherey();
+
+  buff[0] = (unsigned char)c;
+  buff[1] = 0x0; /* Terminator */
+  VidAttr = (WORD)W32CIO_gettextcolor(); /* Get video attribute */
+
+  WINAPI_CallResult[0] = WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),
+                                                     (LPCSTR)buff, 1, Coord, &Dummy);
+  WINAPI_CallResult[1] = WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
+                                                     &VidAttr, 1, Coord, &Dummy);
+
+  Coord.X++; /* Increment cursor postion on line ready for next printout */
+  W32CIO_gotoxy(Coord.X,Coord.Y);/* Make it so */
+
+  /* Behave like putch() */
+  if (WINAPI_CallResult[0] == TRUE && WINAPI_CallResult[1] == TRUE)
+  {
+    return c;
+  }
+  /*(implicit else)*/
+  return EOF;
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_cputs(const char* str)
+{
+  DWORD Dummy; /* An address of this is sent to function but we are not interested in the value that will be written to it */
+  COORD CurPos; /* Holds cursor pos */
+  CurPos.X = (SHORT)W32CIO_wherex();
+  CurPos.Y = (SHORT)W32CIO_wherey(); /* Read cursor position */
+
+  /* Output string */
+  WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),
+                              str, (DWORD)strlen(str), CurPos, &Dummy);
+
+  return 0; /* Dummy return value for Borland compatibility */
+}
+/*-------------------------------------------------------------------------*/
+int W32CIO_getmax(const unsigned char Direction)
+{
+  CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+  if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
+  {
+    SHORT XorY;
+    if (Direction) /* Y wanted? */
+      XorY = csbi.dwSize.Y;
+    else /* X wanted? */
+      XorY = csbi.dwSize.X;
+    return (int)(XorY - 1);
+  }
+
+  return -1;
+}
+/*-------------------------------------------------------------------------*/
+w:\shared\stds\dlstdinc.h(76): Error! E1091: "Must define compiler or add compiler information to standards"
+w:\shared\stds\dlstdinc.h(94): Error! E1141: Expecting end of line but found 'Warning! Processor not defined, use __L_ENDIAN__/__B_ENDIAN__'
+w:\shared\stds\dlstdinc.h(106): Error! E1100: Definition of macro 'MAKEWORD' not identical to previous definition
+w:\shared\stds\dlstdinc.h(108): Error! E1100: Definition of macro 'LOWORD' not identical to previous definition
+w:\shared\stds\dlstdinc.h(109): Error! E1100: Definition of macro 'HIWORD' not identical to previous definition
+w:\shared\stds\dlstdinc.h(111): Error! E1100: Definition of macro 'HIBYTE' not identical to previous definition
+w32cio.h(3): Error! E1055: Unable to open 'beginch.h'
+w32cio.h(29): Error! E1055: Unable to open 'endch.h'
+#ifndef INC_DLCONIO_W32CIO_H
+#define INC_DLCONIO_W32CIO_H
+#include "beginch.h"
+/*-------------------------------------------------------------------------*/
+/* Header for 32-bit Win32 compatibility text-mode simple
+graphics by David Duncan Ross Palmer @ Daybo Logic */
+
+/* Functions form 32Grap.OBJ (32Grap.C) should appear here */
+void W32CIO_PutBlock(COORD_XY* Coord, int Color);
+void W32CIO_DrawLine(COORD_XY* Start, int Length, BOOL vert, int Color);
+void W32CIO_DrawRectangle(COORD_XY* StartCoord, COORD_XY* EndCoord, int Color);
+BOOL W32CIO_GetChar(COORD_XY* Coord, WORD* PVideoAttr, unsigned char* PUChar); /* Gets a character at a coord on the screen and gives them to the caller via the provided pointers, returns FALSE on failure */
+BOOL W32CIO_PutChar(COORD_XY* Coord, WORD VideoAttr, unsigned char UChar); /* Puts a character to a coord on the screen returns FALSE on failure*/
+int W32CIO_putch(int c);
+int W32CIO_cputs(const char* str);
+
+/* Unfortunately EVIL Microsoft does not support the following functions:
+        , so it looks like I'm going
+to have to write them myself with slightly different names! */
+int W32CIO_wherex(void);
+int W32CIO_wherey(void);
+void W32CIO_gotoxy(int x, int y);
+void W32CIO_settextcolor(int newcolor); /* not textcolor, but gettextcolor to allow for settextcolor enhancement. May return zero if color is not supported under Win32 console (because of evil MS) */
+int W32CIO_gettextcolor(void); /* This wasn't in Borland's interpretation, my little helpful enhancement */
+int W32CIO_gettext(int left, int top, int right, int bottom, void* destin); /* Copies text from text mode screen to memory, return 1 on success, 0 on error, if coords out of range it fails, allocate space to store as width*height*2, 2 bytes per char, first byte is char and second is video attribute */
+int W32CIO_puttext(int left, int top, int right, int bottom, void* source); /* Copies text from the buffer pointed to by 'source' previously allocated by puttext() to the screen, if coords are out of range etc it fails and returns 0. 1 indicates success */
+int W32CIO_getmax(const unsigned char Direction); /* Returns max horizontal/vertical position, 0 is X horizontal, nonzero is Y vertical */
+/*-------------------------------------------------------------------------*/
+#include "endch.h"
+#endif /*!INC_DLCONIO_W32CIO_H*/