Author Topic: Console Example  (Read 4627 times)

0 Members and 1 Guest are viewing this topic.

jcfuller

  • Guest
Console Example
« on: October 15, 2010, 10:48:11 AM »
I don't know whose code the original console.inc was but I hacked it up a bit and added a Waitkey function.

James

Console.bi
Code: [Select]
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Console routines
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  library "KERNEL32.DLL"
  Declare Function AllocConsole      Alias "AllocConsole" () As LONG
  Declare Function FreeConsole       Alias "FreeConsole"() As LONG
  Declare Function GetCommandLine    Alias "GetCommandLineA" () As DWORD
  Declare Function GetStdHandle      Alias "GetStdHandle" (Byval handle As DWORD) As DWORD
  Declare Function WriteConsole      Alias "WriteConsoleA" (Byval hConsoleOutput As DWORD, lpBuffer As ASCIIZ, Byval nNumberOfCharsToWrite As LONG, lpNumberOfCharsWritten As LONG, Byval lpReserved As LONG) As LONG
  Declare Function ReadConsole       Alias "ReadConsoleA" (Byval hConsoleInput As DWORD, Byval lpBuffer As DWORD, Byval nNumberOfCharsToRead As LONG, lpNumberOfCharsRead As LONG, pInputControl As ANY) AS LONG
  Declare Function SetConsoleTitle   Alias "SetConsoleTitleA" (lpConsoleTitle As ASCIIZ) As LONG
  Declare Function SetConsoleMode    Alias "SetConsoleMode"(Byval handle As DWORD,Byval dwMode As DWORD)As LONG
  Declare Function GetConsoleMode    Alias "GetConsoleMode"(Byval handle,lpMode As DWORD)
  library ""
'==============================================================================
  %STD_INPUT_HANDLE  = -10
  %STD_OUTPUT_HANDLE = -11
  %STD_ERROR_HANDLE  = -12
'==============================================================================
Function cprint(bufout As string)
      Dim As long ConsOut
      Dim As long buflen,bufrit
      ConsOut = GetStdHandle (%STD_OUTPUT_HANDLE)
    buflen=len bufout
    WriteConsole ConsOut,bufout,buflen,bufrit,0
End Function
'==============================================================================
Function cinput(s As string) As string
      Dim As long ConsIn
      Dim As string bufin
    Dim As long buflen,bufrit     
      bufin=nuls 100     
    cprint s
    ConsIn = GetStdHandle(%STD_INPUT_HANDLE)
    ReadConsole ConsIn,*bufin,100,bufrit,0
    Function=left bufin,bufrit
End Function
'==============================================================================
Sub WaitKey()
    Dim As long ConsIn,blen,mode
    Dim As string b
 
   
    b = nuls 10
    ConsIn = GetStdHandle(%STD_INPUT_HANDLE)
    GetConsoleMode(ConsIn,mode)
    SetConsoleMode(ConsIn,0)
    ReadConsole ConsIn,*b,4,blen,0
    SetConsoleMode(ConsIn,mode)   
End Sub
'==============================================================================
Function commandline() As string
    Dim Byref z As zstring 
    &z=GetCommandLine
    Function=z
End Function
'==============================================================================
Function stripquotes(s As string) As string
    Dim As long a
    a=asc(s,1)
    if a=34 then
      a=instr 2,s,chr(34)
      s=mid s,2,a-2
    end if
    Function=s
End Function
'============================================================================== 


Demo:

Code: [Select]
'#console 
#include "Console.bi"
Function O2Main() As sys 
    Dim i As integer
    Dim As string cr,s

    cr = chr(13)+chr(10)
   
    AllocConsole
    SetConsoleTitle "Test Oxygen Console"
    For i = 1 to 5
        cprint("This is string "+str(i)+ cr)
    Next i
    cprint("press any key" + cr)
    WaitKey

    s = cinput("enter data -> ")
    cprint("Data = " + s + cr)

    cprint("press any key" + cr)
    WaitKey
   
    FreeConsole   
    Function = 1
End Function

O2Main()   



kryton9

  • Guest
Re: Console Example
« Reply #1 on: June 18, 2011, 05:50:29 AM »
I just saw this believe it or not, somehow I missed it. I thought it was worth bumping as having a console to output to is nice. Thanks James.

Charles Pegge

  • Guest
Re: Console Example
« Reply #2 on: June 18, 2011, 09:56:34 AM »
Well spotted Kent.

Looks like one of mine originally - thanks for the WaitKey James. I did not know how to do that.

I'll put this into  the inc/ folder :)

Code: Text
  1.  
  2.   '================
  3.   'CONSOLE ROUTINES
  4.   '================
  5.  
  6.  
  7.   'http://msdn.microsoft.com/en-us/library/ms686033(v=vs.85).aspx
  8.  
  9.   'Console Modes:
  10.   '==============
  11.   '
  12.   '001h Enable processed input
  13.   '002h Enable line input
  14.   '004h Enable input echo
  15.   '008h Enable window input
  16.   '040h Enable quick edit mode
  17.   '080h Enable extended flags
  18.   '020h Insert mode
  19.   '
  20.   '
  21.  
  22.   '------------------------
  23.   extern lib "kernel32.dll"
  24.   '========================
  25.   sys AllocConsole()
  26.   sys FreeConsole()
  27.   sys GetCommandLineA()
  28.   sys GetStdHandle(sys handle)
  29.   sys WriteConsoleA(sys hConsoleOutput,zstring*lpBuffer,sys nNumberOfCharsToWrite,
  30.       *lpNumberOfCharsWritten,void*lpReserved)
  31.   sys ReadConsoleA (sys hConsoleInput,zstring*lpBuffer,sys nNumberOfCharsToRead,
  32.       *lpNumberOfCharsRead, void*pInputControl)
  33.   sys SetConsoleTitleA(Asciiz*lpConsoleTitle)
  34.   sys SetConsoleMode(sys handle,dwMode)
  35.   sys GetConsoleMode(sys handle,lpMode)
  36.   end extern
  37.  
  38.  
  39.   '
  40.   def GetCommandLine GetCommandLineA
  41.   def WriteConsole WriteConsoleA
  42.   def ReadConsole ReadConsoleA
  43.   def SetConsoleTitle SetConsoleTitleA
  44.   def WaitKey GetKey
  45.   '
  46.  
  47.   '=======================
  48.   %STD_INPUT_HANDLE  -10
  49.   %STD_OUTPUT_HANDLE -11
  50.   %STD_ERROR_HANDLE  -12
  51.   '=======================
  52.  
  53.  
  54.   '--------------------------------
  55.   Function cprint(bufout As string)
  56.   '================================
  57.   sys ConsOut,buflen,bufrit
  58.   ConsOut = GetStdHandle STD_OUTPUT_HANDLE
  59.   buflen=len bufout
  60.   WriteConsole ConsOut,bufout,buflen,bufrit,byval 0
  61.   End Function
  62.  
  63.  
  64.   '-------------------------------------
  65.   Function cinput(s As string) As string
  66.   '=====================================
  67.   sys ConsIn,buflen,bufrit      
  68.   zstring bufin[100]      
  69.   cprint s
  70.   ConsIn = GetStdHandle STD_INPUT_HANDLE
  71.   ReadConsole ConsIn,bufin,99,bufrit,byval 0
  72.   return left bufin,bufrit
  73.   End Function
  74.  
  75.  
  76.   '-----------------------
  77.   Function GetKey() as sys
  78.   '=======================
  79.   sys ConsIn,blen,mode
  80.   zstring z[4]
  81.   ConsIn = GetStdHandle STD_INPUT_HANDLE
  82.   GetConsoleMode ConsIn,mode
  83.   SetConsoleMode ConsIn,0
  84.   ReadConsole ConsIn,z,4,blen,byval 0
  85.   SetConsoleMode ConsIn,mode
  86.   return ?z
  87.   End Function
  88.  
  89.  
  90.   '-------------------------------
  91.   Function Commandline() As string
  92.   '===============================
  93.   zstring*z  
  94.   @z=GetCommandLine
  95.   return z
  96.   End Function
  97.  
  98.  
  99.   '------------------------------------------
  100.   Function stripquotes(s As string) As string
  101.   '==========================================
  102.   sys a,b
  103.   a=instr s,chr 34
  104.   if a then
  105.     a++
  106.     b=instr a,s,chr 34
  107.     if b then
  108.       return mid s,a,b-a-1
  109.     end if
  110.   end if
  111.   End Function
  112.  
  113.  

Charles

kryton9

  • Guest
Re: Console Example
« Reply #3 on: June 18, 2011, 10:02:20 AM »
Super, it will be nice for all sorts of uses!