June 06, 2020, 06:00:41 am

News:

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


A little help anyone?

Started by Andy, April 27, 2020, 04:28:25 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

April 27, 2020, 04:28:25 am Last Edit: April 27, 2020, 04:30:18 am by Andy
I really could do with a little help with this one.

This sub routine breaks up a line into words, it also detect delimiters as I need to know what delimiter it has found.

Code Select
SUB ColourLineZ(INT id,INT LineNoIn,ISTRING LineToWords[255],INT Offset)
    LineLen = LEN(LineToWords)
    WordIn = ""

    SENDMESSAGE(id,EM_SETSEL,1+Offset,LineLen + Offset)
    cf.dwMask = CFM_COLOR
    cf.crTextColor = RGB(0,0,0)
    cf.cbSize = SIZEOF(cf)
    SENDMESSAGE(id,EM_SETCHARFORMAT,SCF_SELECTION,&cf)

    FOR b1 = 1 TO LineLen    '<----------- this

        ThisPos = b1 + Offset
        Letter = LineToWords[b1-1]  '<------ and this
        Del = INSTR(CheckLetter2,Letter)

        SELECT Del

                CASE 0 'Add character to our word
                    'aw = instr(KeyWordLetters,ucase$(Letter)) 'Only alphabet characters a to z plus $ _ and @ symbols
                    WordIn = WordIn + Letter

                CASE 1 'Quotes
                    WordIn = ""
                    StartPos = b1 + (Offset-1)
                    DO
                        b1 ++
                        Letter = LineToWords[b1-1]
                    UNTIL Letter = CHR$(34) OR b1 = LineLen
                    EndPos = b1 + Offset
                    'EndPos = instr(LineToWords,chr$(34),StartPos+1) + Offset

                    SENDMESSAGE(id,EM_SETSEL,StartPos,EndPos)
                    cf.dwMask = CFM_COLOR
                    cf.crTextColor = Purple
                    cf.cbSize = SIZEOF(cf)
                    SENDMESSAGE(id,EM_SETCHARFORMAT,SCF_SELECTION,&cf)

                    IF b1 = LineLen THEN RETURN

                CASE 2 ' comment symbol
                    WordIn = ""
                    StartPos = b1 + (Offset-1)
                    SENDMESSAGE(id,EM_SETSEL,StartPos,LineLen + Offset)
                    cf.dwMask = CFM_COLOR
                    cf.crTextColor = Green
                    cf.cbSize = SIZEOF(cf)
                    SENDMESSAGE(id,EM_SETCHARFORMAT,SCF_SELECTION,&cf)
                    RETURN

              'case 7 'Local subroutine check
                        'if Subs[LineNoIn]  'Local subroutine
                          'StartPos = (ThisPos - len(WordIn))-1
                          'EndPos  = ThisPos-1
                          'SendMessage(id,EM_HIDESELECTION,1,0)
                          'SendMessage(id,EM_SETSEL,StartPos,EndPos)
                          'cf.dwMask = CFM_COLOR
                          'cf.crTextColor = Claret
                          'cf.cbSize = sizeof(cf)
                          'SendMessage(id,EM_SETCHARFORMAT,SCF_SELECTION,&cf)
                          'SendMessage(id,EM_HIDESELECTION,0,0)
                          'breakfor
                        'endif
                    'SendMessage(id,EM_SETSEL,EndPos,EndPos)
                    'goto DoDefault

                CASE 18 'Remove carriage return from last word on line
                    WordIn = MID$(WordIn,1,INSTR(WordIn,CHR$(13))-1)
                    IF LEN(WordIn)> 1
                        WordIn = ChangeWordEditorZ(id,WordIn,(ThisPos - LEN(WordIn))-1,(ThisPos)-1)
                    ENDIF
                    RETURN

                DEFAULT 'Other delimiter - so we have our word
                    LABEL DoDefault
                    IF LEN(WordIn)> 1 'Key words are at least 2 characrers long
                        StartPos = (ThisPos - LEN(WordIn))-1
                        EndPos  = ThisPos-1
                        WordIn = ChangeWordEditorZ(id,WordIn,StartPos,EndPos)
                    ENDIF
                    WordIn = ""

        ENDSELECT
    NEXT b1

RETURN
ENDSUB


Now I know INSTR can have an option start position for a string, so instead of using a FOR / NEXT loop to loop through each character in the string I was wondering how this sub routine could be changed.

I need to know the " character, the "'" character and the "(" ")" characters.

Anyone fancy a little challenge?

Would be appreciated!

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

h3kt0r

Perhaps the xinstr command from the Fletchie's control pak can help here ?

Andy

Thanks Hecktor, will have a look now I finally have a little time after a busy few days.

Thanks again,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

May 01, 2020, 01:06:55 pm #3 Last Edit: May 01, 2020, 02:15:19 pm by fasecero
This can be a starting point. The code breaks up each part of a subroutine:

Code Select
$INCLUDE "windowssdk.inc"

OPENCONSOLE
string indata = "SUB MyFunc(INT value, string text), INT"
string sep = "(),"

PRINT
PRINT indata

string stdata = indata ' indata copy
pointer pdata = &indata ' indata address
pointer token = strtok(indata, sep)

WHILE token
PRINT
PRINT "str: " + RTRIM$(LTRIM$(*<string>token))
PRINT "pos:" + STR$(token - pdata)
IF token - pdata THEN PRINT "sep: " + MID$(stdata, token - pdata, 1) ELSE PRINT "sep: no sep"
token = strtok(NULL, sep)
   ENDWHILE

DO:UNTIL INKEY$ <> ""
CLOSECONSOLE

Andy

Thanks Fasecero,

You are always a very big help!

Will play around with this to see if it helps me.

Thanks again,

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.