June 23, 2024, 01:11:29 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Some Tips & Tricks

Started by Ficko, April 28, 2007, 07:01:30 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Ficko

April 28, 2007, 07:01:30 AM Last Edit: April 29, 2007, 03:13:34 AM by Ficko
I have some Tips & Tricks to share may somebody find it useful. :)

1.)
'This program demonstrates how you can load a file as one chunk into the memory.
'You need a <NULL> "0" terminated binary file "TEST.txt" to run this demo!
'Like "HELLO WORLD!"+0x00.
'You can easily generate one with a HEX editor.
'Otherwise PRINT #<STRING>MyP can freeze!

DEF MyP AS Pointer
'Alternative way:
'DEF MyM AS Memory
DEF MyF AS BFile

OPENCONSOLE

IF OPENFILE(MyF,GETSTARTPATH+"TEST.txt", "R") = 0
   MyP = NEW(Char,LEN(MyF))
   'Alternative way:
   'MyM = MyP
   'READ MyF,MyM
   READ MyF,##<Memory>MyP
    CLOSEFILE MyF
   PRINT #<String>MyP
   DELETE MyP
ELSE
   PRINT "Can't open file!"
ENDIF
PRINT "PRESS <ANY> KEY TO END!"
DO:UNTIL INKEY$<>""
END

2.)
How you can screw up the memory.
-I did it lately  ;) may this save you some debuging!-

'If you modify the Pointer declared by "NEW" statement the "DELETE" function will not work properly!
'You have to restore the original value (address) before issuing the "DELETE" statement!
'You run this program without taking out the "REM"s will eat up all of your memory.

DEF MyP AS Pointer
'DEF MyP_T AS Pointer
DEF A AS Int64
OPENCONSOLE
DO
   PRINT A
   MyP = NEW(Char,0xFFFF)
   'MyP_T = MyP
   MyP += 0x0FFF
   'MyP = MyP_T
   DELETE MyP
   A++
UNTIL INKEY$<>""
END

3.)
'This sample shows how you can use "WAIT" to Refresh() or Update() a dialog.

CONST BUTTON_START = 1
CONST BUTTON_ABORT = BUTTON_START+1
CONST EDIT = BUTTON_ABORT+1

DEF RUN AS Uint
DEF Dialog01 AS Dialog

CREATEDIALOG Dialog01,0,0,300,100,0x80C80080,0,"Refresh Demo",&Dialog01_handler
CONTROL Dialog01,@BUTTON,"Start",35,53,70,20,0x50000000,BUTTON_START
CONTROL Dialog01,@BUTTON,"Abort",176,54,70,20,0x50000000,BUTTON_ABORT
CONTROL Dialog01,@EDIT,"00000000",109,17,100,20,@CTEDITCENTER|@CTEDITRO,EDIT

SHOWDIALOG Dialog01
WAITUNTIL RUN = 1
END

SUB Dialog01_handler
   SELECT @MESSAGE
      CASE @IDINITDIALOG
         CENTERWINDOW Dialog01
      CASE @IDCLOSEWINDOW
         RUN = 1
      CASE @IDCONTROL
         SELECT @CONTROLID
            CASE BUTTON_START
               IF @NOTIFYCODE = 0
                  SETCONTROLTEXT Dialog01,EDIT,"Nothing happens!"
                  WAIT 0   
                  Counter()
                  SETCONTROLTEXT Dialog01,EDIT,"Finished"
               ENDIF
            CASE BUTTON_ABORT
               IF @NOTIFYCODE = 0
                  RUN = 1
               ENDIF
         ENDSELECT
   ENDSELECT
RETURN
ENDSUB

SUB Counter
   FOR A = 0 TO 1000
      'You have to take care of "CLOSEDIALOG" here.
      'Due to you are still in the message loop.
      'If don't you can't close the dialog by "SHOWDIALOG" or by "DOMODAL" the
      '"SUB Counter" will still run in the background despite the dialog being shot down!
      IF RUN = 1 THEN
         CLOSEDIALOG Dialog01
         RETURN
      ENDIF
      SETCONTROLTEXT Dialog01,EDIT,USING("0########",A)
      'Inserting a "WAIT" it's acting like a "refresh" for the dialog.
      'Try to take it out to see the difference!
      WAIT 1
      FOR B = 0 TO 0xFFFFFF
      NEXT B
   NEXT A
ENDSUB

4.)
'This Program demonstrates the embadement of any binary file in the source code
'The TEST.txt in this demo have to be a <NULL> "0" terminated file!
'Otherwise PRINT #<STRING>MyP can freeze!
DECLARE MyBinToInc()
DEF MyP AS POINTER
MYP = &MyBinToInc

OPENCONSOLE
PRINT #<STRING>MyP
PRINT "PRESS <ANY> KEY TO END!"
DO:UNTIL INKEY$<>""
CLOSECONSOLE
END
MyBinToInc:
_asm
incbin "TEST.txt"
_endasm

All in a ZIP:

GJ

Thanks Ficko, great idea !!

Hope some more will post their tips and tricks, very usable !!
Here's my debug routine when dont want the build-in debugger, maybe there are folks who are willing to improve or make suggestions to it .

Thanks in advance,


Gertjan (GJ)




AUTODEFINE "OFF"

CONST EDIT_1 = 1
CONST RICHEDIT_2 = 2
CONST BUTTON_3 = 3
CONST STATIC_4 = 4
INT Infocus,Lengte,bcl

DIALOG d1

'----------------------------- needed for debug sub-routine -------------------------
WINDOW win
DIALOG GJBOX
INT GJBOXTELLER,GJBOXVlag
STRING Test$,Dummy$,Dummy1$,Dummy2$

Const LVM_FIRST = 0x1000
Const LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54)
Const LVS_EX_ONECLICKACTIVATE = 0x40
Const LVS_EX_FULLROWSELECT = 0x020
Const LVS_EX_CHECKBOXES = 0x4 
Const LVS_EX_GRIDLINES = 0x1
Const LVS_EX_HEADERDRAGDROP = 0x10
Const LVM_GETITEMSTATE = (LVM_FIRST + 44)
Const LVM_SETBKIMAGE = (LVM_FIRST + 68)
Const LVIS_STATEIMAGEMASK = 0xF000
Const LVM_SETBKCOLOR = (LVM_FIRST + 1)
Const LVIF_STATE = 0x8
Const LVM_SETITEMSTATE = (LVM_FIRST+43)
Const LVM_SETTEXTBKCOLOR = (LVM_FIRST + 38)
Const LVBKIF_SOURCE_URL = 0x2
Const LVBKIF_STYLE_NORMAL = 0x0
Const LVBKIF_STYLE_TILE = 0x10
Const CLR_NONE = 0xFFFFFFFF

CREATEDIALOG GJBOX,0,0,300,202,0x80CB0080|@SIZE,win,"Variables/strings debugger",&GJBOXhandler
CONTROL GJBOX,@LISTVIEW,"",14,15,272,172,0x5081031D,1

'-------------------------------- end of debugger sub-routine ---------------------------------



CREATEDIALOG d1,0,0,300,202,0x80C80080,0,"Edit/Richedit",&d1_handler
CONTROL d1,@EDIT,"",                13,13,70,20,0x50800000,EDIT_1
CONTROL d1,@RICHEDIT,"",           141,15,70,60,0x50A00844,RICHEDIT_2
CONTROL d1,@BUTTON,"Button1",      230,28,70,20,0x40080001,BUTTON_3
CONTROL d1,@STATIC,"Type some text and press ENTER",13,37,77,44,0x50000101,STATIC_4
''CONTROL d1,@RICHEDIT,"",6,6,775,370,0x50B00844,RICHEDIT_2




DOMODAL d1

END


'--------------------------------------------------

SUB d1_handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
       
/* Initialize any controls here */
CASE @IDCLOSEWINDOW
CLOSEDIALOG d1,@IDOK
CASE @IDCONTROL
SELECT @CONTROLID
CASE EDIT_1
                 Infocus=1
/* respond to edit notifications here */
CASE RICHEDIT_2
                 Infocus=2
/* respond to control notifications here */
CASE BUTTON_3
                 IF GETKEYSTATE(13) <> 0 
                  SELECT infocus
                  CASE 1 
                    test$=GETCONTROLTEXT(D1,1)

'---------->>>> print the contents
            DEBUG("test",Test$,"$")
                  printm(d1,GETCONTROLTEXT(d1,1)) : 'print some text
                  SETCONTROLTEXT d1,EDIT_1,""     : 'clears the edit_1 control
                 CASE 2
                 SETFOCUS d1,EDIT_1 
                ENDSELECT 
              ENDIF 

ENDSELECT
ENDSELECT
RETURN
ENDSUB

'--------------------------------------------------

SUB printm(dialoog:dialog,text:string)

select text
case "quit"
case& "exit"
case& "bye"
CLOSEDIALOG dialoog,@IDOK

case "cls"
        messagebox(dialoog,"CLS Command","Not recognized")
endselect

      lengte = CONTROLCMD (dialoog, 2, @RTGETTEXTLENGTH)
      controlcmd dialoog , 2, @RTSETSELECTION, lengte, lengte
      controlcmd dialoog, 2, @RTREPLACESEL, text + CHR$(13)+CHR$(10)
      CONTROLCMD dialoog, 2, @RTSCROLL, 0,1

RETURN
ENDSUB

'------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------


SUB Debug(Dummy$:STRING,Dummy1$:STRING,Dummy2$:STRING)
 
    ' usage DEBUG("name",StringToExamine,"$") etc
     

   IF GJBoxVlag=0
    SHOWDIALOG GJBOX
    CONTROLCMD GJBOX,1,@LVINSERTITEM,GJBOXTeller,"" 
   ENDIF

   CONTROLCMD GJBOX,1,@LVSETTEXT,GJBOXTeller, 0,Dummy$ 

   IF UCASE$(Dummy2$)="CHAR" 
    IF VAL(Dummy1$)>0 AND VAL(Dummy1$)<255
     CONTROLCMD GJBOX,1,@LVSETTEXT,GJBOXTeller, 1,CHR$(VAL(Dummy1$))
    ENDIF
   ENDIF
   
   IF Dummy2$="$"
    CONTROLCMD GJBOX,1,@LVSETTEXT,GJBOXTeller, 1,Dummy1$
   ENDIF

   CONTROLCMD GJBOX,1,@LVINSERTITEM,GJBOXTeller,"" 

RETURN
ENDSUB

'------------------------------the debug subroutine handler-------------------------------------------------
Sub GJBOXHandler

SELECT @MESSAGE
     CASE @IDINITDIALOG
        GJBOXVlag=1:GJBoxTeller=0
        bcl=RGB(255,255,222)
        CONTROLCMD GJBOX,1,@LVINSERTCOLUMN,0,"Name" 
CONTROLCMD GJBOX,1,@LVSETCOLWIDTH,0,150 
CONTROLCMD GJBOX,1,@LVINSERTCOLUMN,1,"Value" 
CONTROLCMD GJBOX,1,@LVSETCOLWIDTH,1,240 
        ShowWindow GJBOX,@SWHide,1
        SendMessage (GJBOX,LVM_SETBKCOLOR,0,bcl,1)       :'Set Background Colour
        SendMessage (GJBOX,LVM_SETTEXTBKCOLOR,0,bcl,1)   :'Set Text Background Colour
        ShowWindow GJBOX,@SWRestore,1
        SENDMESSAGE(GJBOX,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT,1)
CASE @IDCLOSEWINDOW
        GJBoxVlag=0:GJBoxTeller=0
        CONTROLCMD GJBOX,1,@LVDELETEALL
CLOSEDIALOG GJBOX,@IDOK 
     CASE @IDSIZE
        UINT left,top,width,height
        GETCLIENTSIZE GJBOX, left, top, width, height 
        SETSIZE(GJBOX, left+14, top+15, width-27, height-30, 1)
    ENDSELECT

RETURN 
ENDSUB