parsing strings... and more x_lib stuff... x_parse() x_val()

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

parsing strings... and more x_lib stuff... x_parse() x_val()

Post by blueznl »

Code updated For 5.20+

some small stuff for chopping up command line parameters etc. nothing special for the experts but it might save you some typing

Code: Select all

Procedure x_parsenext()
  Global x_parseflags, x_parsed.s, x_parse.s, x_eol, x_retval, x_parsesearch.s
  ;
  ; *** get next section, see x_parse() and x_parsestring()
  ;
  If x_parseflags & %010 = 0                                  ; trim
    x_parse = Trim(x_parse)
  EndIf
  ;
  If Len(x_parse) = 0                                           ; empty string
    x_retval = #False
    x_eol = #True
    x_parsed = ""
    x_parse = ""
  Else
    ;   
    If parseflags & %001 <> 0                                 ; case doesn't matter
      p = FindString(UCase(x_parse.s),UCase(x_parsesearch.s),1)
    Else                                                        ; case matters
      p = FindString(x_parse.s,x_parsesearch.s,1)
    EndIf
    ;
    If p = 0                                                    ; didn't find the search string
      x_parsed = x_parse
      x_parse = ""
      x_eol = #True
      If x_parseflags & %100 = 0                                ; eol is considered a match
        x_retval = #True
      Else
        x_retval = #False
      EndIf
    Else                                                        ; x_retval
      x_parsed = Left(x_parse,p-1)
      x_parse = Mid(x_parse,p+Len(x_parsesearch),Len(x_parse))
      x_retval = #True
      If Len(x_parse) = 0                                       ; the match just came before the eol
        x_eol = #True
      Else
        x_eol = #False
      EndIf
    EndIf
    ;
    If x_parseflags & %010 = 0
      x_parse = Trim(x_parse)
      x_parsed = Trim(x_parsed)
    EndIf
  EndIf
  ;
  ProcedureReturn x_retval
EndProcedure

Procedure x_parsestring(search.s)
  Global x_parsesearch.s
  ;
  x_parsesearch.s = search
  ProcedureReturn x_parsenext()
EndProcedure

Procedure x_parse(string.s,search.s,flags)
  Global x_parseflags, x_parsed.s, x_parse.s, x_parsesearch.s
  ;
  ; *** parse a string (retrieve parameters etc.)
  ;
  ; in:     string.s                 - string to parse
  ;         search.s                 - string or seperator to find
  ;         flags = 0              - trim and slice
  ;         flags = %001 (1)       - no case check
  ;         flags = %010 (2)       - don't trim
  ;         flags = %100 (3)       - when NOT set eol is considered a matching search
  ; retval: 0 (#False)               - not found
  ;         1 (#True)                - found
  ; out:    x_parsed.s                - part of string BEFORE the x_retval searchstring
  ;         x_parse.s                 - part of string AFTER the x_retval searchstring (can be reparsed)
  ;         x_eol = 1 (#True)      - flag to indicate end of line was reached
  ;         x_retval = 1 (#true)   - as retval
  ;
  x_parse = string
  x_parseflags = flags
  x_parsesearch = search
  x_parsed = ""
  ;
  ProcedureReturn x_parsenext()
  ;
EndProcedure
Last edited by blueznl on Sun Jan 25, 2004 10:47 pm, edited 3 times in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Please add a testing section to your prog; not only the function.
Users want just to copy, paste and see................
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

good remark, i'll do so... rewrote the procedures a little though they now rely on some minimal stuff from x_lib... lemme' think...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

as psycho requested...

all procs in the x_lib.pb are simply purebasic (with some borrowed asm :-))

here's some samples how to use them, you'll need to download the x_lib.pb file to test them

latest addition: x_parse() and x_val()

Code: Select all

IncludeFile "x_lib.pb"

Debug x_parsefield("option1 option2"," ",#x_trim|#x_eol|#x_anycase)
Debug x_parse_field(1)
Debug x_parse_field(2)

Debug x_parse("dit is een test van deze functie"," ",0)
Debug x_parsed+" <> "+x_parse
Debug x_parsenext()
Debug x_parsed+" <> "+x_parse
Debug x_parsestring("van")
Debug x_parsed+" <> "+x_parse

Code: Select all

; purebasic survival guide
; x_lib_samples.pb - 25.01.2004 ejn (blueznl)
; http://www.xs4all.nl/~bluez/datatalk/pure1.htm
;
; - the x_lib.pb file is a sort of library IN PUREBASIC SOURCECODE, not a dll or regular pb library
; - you're free to use it in whole or part, credits would be nice but not necessary :-)
;
; - go through all functions in the x_lib, they're fairly well documented
; - not all functions are sampled here, check the x_lib file itself!
; - not all functions might be useful / make sense
; - some functions have been / will be replaced by pb statements
;
IncludeFile "x_lib.pb"
;
Debug "*** initialize"
;
; you always need to init before using any functions
; most declarations of constants, lists, etc. take place inside x_init, split per group
; to move a procedure to your own programm, cut / paste the procedure(s) and associated parts of x_init
; don't forget to use x_end() as well
;
x_init()
Debug "ok"
;
;
Debug "*** reserve and free unique numbers for opening files etc. x_nr() x_freenr()"
;
nr1.l = x_nr()                           ; get it, so you can follow up with openfile(nr1,name.s) etc.
nr2.l = x_nr()
nr3.l = x_nr()
Debug Str(nr1)+" "+Str(nr2)+" "+Str(nr3)
;
x_freenr(nr1)                            ; release it so it can be resused again
x_freenr(nr2)                            ; release it so it can be resused again
nr4.l = x_nr()                           ; get a new one, one of the old freed ones will be re-used
Debug Str(nr4)
;
;
Debug "*** the missing not statement x_not()"
;
bool.l = #False                          ; defined in x_init()
Debug bool
If x_not(bool)
  Debug x_not(bool)
EndIf
;
; 
Debug "*** some math functions missing in pb (3.81) x_min() x_max() x_limit() x_abs() x_sgn()"
;
Debug x_min(10,1)                 ; smallest of both, here it returns 1
Debug x_max(10,1)                 ; biggest of both, here it returns 10
Debug x_limit(x,1,10)             ; limits a var to a certain range, here if x<1 then x=1, if x>10 then x=10
Debug x_abs(-1)                   ; the regular abs() function is a float function
Debug x_sgn(-1)                   ; an sgn() function
;
; x_lib contains additional functions for floats
;
;
Debug "*** string functions x_countchar() x_countstring() x_chop() x_rfindstring()"
;
Debug x_countchar("dit is een test","e")           ; count nr. or 'e' charachters in the given string, x_countstring() is in asm and faster!
Debug x_countstring("dit is een test","dit")       ; count occurances of one string in another string, asm
Debug x_chop("dit is een test",1,2)                ; chop 1 char left, 2 chars right
Debug FindString("12 13","1",1)                    ; the normal findstring, find from the left side
Debug x_rfindstring("12 13","1")                   ; find first occurance of '1' from the right side
;
;
Debug "*** string function: parse() parsenext() parsestring()"
;
; usefull for command line parsing, chopping strings to pieces, or process strings piece by piece
;
x_parse("dit is een test van deze functie"," ",#x_eol|#x_trim)      ; see the comments in the source code for all parameters
Debug x_parsed+" <> "+x_parse
x_parsenext()                                                       ; do it again with same parameters
Debug x_parsed+" <> "+x_parse
x_parsestring("van")                                                ; do it again for a different search string
Debug x_parsed+" <> "+x_parse
Debug x_parsefield("dit is een test van deze functie"," ",#x_eol|#x_trim)
Debug x_parse_field(3)
;
;
Debug "*** see x_lib for x_waitkey() and x_waitkeywindow()"
;
; k.s = x_waitkey()
; k.s = x_waitkeywindow()
;
;
Debug "*** x_val()"
;
Debug Val("$10")
Debug x_val("$10")
Debug x_val("\20")
Debug x_val("0x10")
Debug x_val("%10000")
;
;
Debug "*** registry, see x_lib for details on functions x_regget(), x_regset() etc."
;
Debug x_regget(#HKEY_CURRENT_USER,"Control Panel\Desktop","TileWallPaper")    ; is the wallpaper tiled? (yes = 1)
;
;
Debug "*** filename / file system functions"
;
Debug x_exist("x_lib_samples.pb")                         ; only interesting to do something like if x_exist()
Debug FileSize("x_lib_samples.pb")                        ; filesize<0 means it doesn't exist
;
; match a filename to a certain pattern (similar but not identical to dos / windows behavior)
;
Debug x_matchpattern("apekool.txt","ape*.*|*.txt",0)      ; multiple patterns and wildcards allowed
Debug x_matchpattern("apekool.txt","???kool*.?x*",0)      ; see source x_lib for details on ? and *?
;
; retrieve all contents of a (sub)dir, recursive, matching a pattern
;
Debug x_dir("c:\","*.bat",2,500,2)   ; find all *.bat files, list them including path, max. 500 files, max. 2 levels deep
Debug x_dir_list(1)                  ; just display the first one found
;
;
Debug "*** multi monitor detection"
;
Debug x_monitor_detect()
;
;
Debug "*** clean up, if any libraries are openened or memory is reserved they are properly freed and closed"
;
; not entirely necessary under pb, i know...
;
x_end()
Debug "ok"
;
;
Debug "*** close debug windows (uncomment next line)"
;
; x_hidedebugger()
;

hey, psychopanta, better this way? :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply