'===========================================================================
' Subject: WINDOWS CGI EXAMPLE                Date: 06-29-98 (16:58)       
'  Author: Dave Navarro, Jr.                  Code: PBCC                   
'  Origin: dave@powerbasic.com              Packet: PBCC.ABC
'===========================================================================
'==============================================================================
'
'  WinCGI Example for PowerBASIC Console Compiler
'  Copyright (c) 1998 by PowerBASIC, Inc.
'
'  Uses the Windows API to retrieve the current local date and time and
'  display them using the preferred formats specified by the user and
'  country settings.
'
'  Prints an HTTP header for HTML so that it can be used on a web server such
'  as Personal Web Server in Win95/98 and Peer Web Services or IIS in WinNT.
'
'==============================================================================

$DIM ALL

$INCLUDE "WINCGI.INC"

'==============================================================================

%NULL                  = 0
%LOCALE_SLONGDATE      = &H20        '  long date format string
%LOCALE_STIMEFORMAT    = &H1003      '  time format string
%LOCALE_USER_DEFAULT   = &H0000
%DATE_SHORTDATE        = &H1         '  use short date picture
%DATE_LONGDATE         = &H2         '  use long date picture
%TIME_NOSECONDS        = &H2&        '  do not use seconds

TYPE SYSTEMTIME
  wYear AS INTEGER
  wMonth AS INTEGER
  wDayOfWeek AS INTEGER
  wDay AS INTEGER
  wHour AS INTEGER
  wMinute AS INTEGER
  wSecond AS INTEGER
  wMilliseconds AS INTEGER
END TYPE

DECLARE SUB GetLocalTime LIB "KERNEL32.DLL" ALIAS "GetLocalTime" (lpSystemTime AS SYSTEMTIME)
DECLARE FUNCTION GetDateFormat LIB "KERNEL32.DLL" ALIAS "GetDateFormatA" (BYVAL Locale AS LONG, BYVAL dwFlags AS LONG, lpDate AS SYSTEMTIME, lpFormat AS ASCIIZ, lpDateStr AS ASCIIZ, BYVAL cchDate AS LONG) AS LONG
DECLARE FUNCTION GetTimeFormat LIB "KERNEL32.DLL" ALIAS "GetTimeFormatA" (BYVAL Locale AS LONG, BYVAL dwFlags AS LONG, lpTime AS SYSTEMTIME, lpFormat AS ASCIIZ, lpTimeStr AS ASCIIZ, BYVAL cchTime AS LONG) AS LONG

'==============================================================================

FUNCTION PbMain ()

  LOCAL st AS SYSTEMTIME
  LOCAL d  AS ASCIIZ * 64
  LOCAL t  AS ASCIIZ * 64
  LOCAL s  AS ASCIIZ * 64
  LOCAL x  AS STRING
  LOCAL p  AS LONG
  LOCAL i  AS LONG

  DIM Param(1) AS STRING

  ' Get date and time
  GetLocalTime st

  GetDateFormat %LOCALE_USER_DEFAULT, %DATE_LONGDATE, st, BYVAL %NULL, d, 64
  GetDateFormat %LOCALE_USER_DEFAULT, %DATE_SHORTDATE, st, BYVAL %NULL, s, 64
  GetTimeFormat %LOCALE_USER_DEFAULT, %TIME_NOSECONDS, st, BYVAL %NULL, t, 64

  ' Read from STDIN
  x = ReadCGI

  ' Count and partse the parameters into an array
  p = ParseParams(x, Param())

  ' Create a web page
  WriteCGI "<HTML>"

  WriteCGI "<P>At the tone the time will be <B>" & t & ", " & d & " (" & s & ")</B>"

  WriteCGI "<P>QUERY_METHOD = " + Query_Method
  WriteCGI "<P>REQUEST_METHOD = " + Request_Method
  WriteCGI "<P>CONTENT_LENGTH = " + FORMAT$(Content_Length)
  WriteCGI "<P>Form Data = " + CHR$(34) + x + CHR$(34)

  IF p THEN
    FOR i = 1 TO p
      WriteCGI "<LI>" + DecodeCGI( Param(i) )
    NEXT i
  END IF

  WriteCGI "</HTML>"

END FUNCTION
