[Gambas-user] Questions about source code

Benoît Minisini gambas at ...1...
Tue Jul 16 23:23:32 CEST 2002


Le Mardi 16 Juillet 2002 18:30, Michael Stibane a écrit :
> Hi list!

Hi you

>
> I tried to comment the console example. I'm no programmer ... better a
> preNewbie, please help me out with the questions and false
> interpretations between the lines of code for my understanding.

It's a good idea. I'm just going to try to explain you a few things
about how to comment code.

>
> If I see this I could imagine to have a database component for mysql -
> simply accessing it via commandline like the bash in this case.

Yes. Command line tools is the power of Unix !! But don't worry, a
database component for Gambas will come one day ;-)

>
> STIBS
>
> -------------------------------
> ' Gambas class file
>
> PRIVATE §hProcess AS Process
> ' defines the process identificator variable format

First, I suggest putting comments before the code they explain. I think it is 
the tradition.

This comment is not necessary. Why ? Because the code is self-descriptive.
Such a code must NEVER be commented, because you are creating duplicates.
Each time you modify the code, you must modify the comment. Comment ARE
part of the program, like lines of code. One of the ten commandments of
the programmer must be "do not create duplicates. Never"

But, and I can hear your question, why is this line of code self-descriptive ?
It is because it is following coding rules in the name of the variable :

- "h" tells you that the variable will receive an object handle.
- "Process" tells you that it will be a process.
- And the type of the variable confirms that it will be an object of Process 
class.

But, and you are going to say that I don't know what I want, you can continue
to put comment like that, because it reassures the newbies.

I just wanted to display my science ;-)

>
> STATIC PUBLIC SUB Main()
> ' Main routine declaration

Self-descriptive !

>   DIM hForm AS Form
>
>   hForm = NEW FConsole
>   hForm.Show
> ' shows the form

Self-descriptive !

> END
>
>
> PUBLIC SUB _new()
> ' on opening the form happens the following

The _new method is not executed when you OPEN the form (by calling Show), but
when you create the Form (by calling NEW)

>   EXEC "bash --noediting -i" FOR READ WRITE AS §hProcess
> ' executes the bash for rw as our process
>   txtCommand.SetFocus
> ' sets the focus to the text input line
> END
>
>
> PUBLIC SUB Form_Close()
> ' on hitting the close button x
>   §hProcess.Kill
> ' the process will be killed

Self-descriptive !

> END
>
>
>
> PUBLIC SUB Form_Resize()
> ' Some screen geometry declarations when the window gets resized
>   txtCommand.Move(0, ME.ClientH - txtCommand.H, ME.ClientW, txtCommand.H)
> ' the ME.X inherits from the forms geometry

What do you want to say ?

>   txtConsole.Move(0, 0, ME.ClientW, txtCommand.Y)
>
> END
>
>
> PUBLIC SUB Process_Write(sStr AS String)
> ' getting the string sent to the process also in the TextArea displayed
>   txtConsole.Pos = txtConsole.Length
> ' positions the cursor at the end of the TextArea.Text
>   txtConsole.Insert(Normalize(sStr))
> ' Inserting the normalized command into the TextArea Output
> END
>
>
> PUBLIC SUB Process_Error(sStr AS String)
> ' getting the error message  from process displayed
>   txtConsole.Pos = txtConsole.Length
> ' positions the cursor at the end of the TextArea.Text
>   txtConsole.Insert(Normalize(sStr))
> ' Inserting the normalized Error String into the TextArea Output
> END
>
>
> PUBLIC SUB Process_Kill()
> ' No clue if this is needed because it's Form_Close() is already definded
>   '§hProcess = NULL
>   TRY ME.Close
> ' this is maybe additional when the process dies that the form also dies

Yes. The process can die while the form is open, but it can die between the 
form close and the form deallocation !

> END
>
>
>
> PUBLIC SUB txtCommand_Activate()
> ' So Activate is when Enter is hit? :o)

Yes !

>   DIM sLig AS String
> ' defining variable type
>   sLig = txtCommand.Text & gb.NewLine
> ' The Text written in the textfield plus a new line is submitted to sLig
>   txtConsole.Insert(sLig)
> ' Hmmm? did I understand Process_Write in the wrong way?

No. It is because bash does not echoing the command.

>   txtCommand.Clear
> ' cleaning
>   §hProcess.Send(sLig)
> ' Sending the input to the process
> END
>
>
> STATIC PRIVATE FUNCTION Normalize(sStr AS String) AS String
> ' that's the next point I don't understand fully
> ' Does the process just understand ASCII numbers?

This function is just a quick hack for removing all the non-ASCII characters 
printed by bash (there are terminal command). Making a real terminal emulator 
will be far more complex than such a quick example.

>
>   DIM sNorm AS String
>   ' normalized String
>   DIM iInd AS Integer
>   ' index # of the characters position in the string
>   DIM iCar AS Integer
>   ' needed for the logic
>   DIM bEsc AS Boolean
>   ' needed for the logic
>   FOR iInd = 1 TO Len(sStr)
> ' for all character in the strings (input or output)
>     iCar = Asc(sStr, iInd)
> ' get the ASCII numbers
>     IF iCar = 27 THEN
>       bEsc = TRUE
>       CONTINUE
>     ENDIF
>

"bEsc" is a just a quick hack to remove terminal commands : these terminal 
commands begin with a ESC character (code 27) and seem to ending with a 
non-ascii characters. Every character between must be discarded !

It is REALLY the perfect example of what nobody should program : a quick and
dirty function that does certainly not work in every case :-(

>     IF bEsc THEN
>       IF iCar < 32 THEN bEsc = FALSE
>       CONTINUE
>     ENDIF
> ' Some logic about the character format
> ' Where can I read about this?
> ' Somewhere has to be an ASCII table with numbers where I also find keys
> like <- or ->
> ' help me out please!
>     IF iCar < 32 AND iCar <> 10 THEN iCar = 32

Replace all non-ascii characters by spaces (ASCII code 32)

> ' additional logic
>     sNorm = sNorm & Chr$(iCar)
> ' the normalized text
>   NEXT
>
>   RETURN sNorm
> ' returns the normalized string to the program ... but where is it used?
> ' I don't see sNorm somewhere else ... am I blind?

Maybe ;-) It is a local variable of the function that contains the normalized 
string.

> END
>
> Thanx alot!

-- 
Benoît Minisini
mailto:gambas at ...1...




More information about the User mailing list