maxmods / bah.mod

A collection of BlitzMax modules by Brucey
25 stars 17 forks source link

serial.mod requires bah.regex #63

Open GWRon opened 5 years ago

GWRon commented 5 years ago

serial.mod/serial.bmx utilizes bah.regex (a 10MB source+docs package) for a function which is not used or "private" (_getIds()).

Is this by intention?

woollybah commented 5 years ago

It is used by the Win32 glue to extract some stuff from HIDs, as part of TSerialPortInfo.

GWRon commented 5 years ago

I know.. but it is not used in all cases ... But adds a "heavy" dependency. Maybe it is useful to replace it with some simple code portion.

woollybah commented 5 years ago

Feel free to submit a PR with code that replicates the functionality :-)

GWRon commented 5 years ago

This should work - at least in my tests. Code could of course get shorter or more performant but it is nothing you call a thousand times per cycle.


rem
'example
local vId:int = 0
local pId:int = 0
_getIds("something\USB&VID_0403&PID_6010", varptr vId, varptr pId)
print "vId="+vId+" (=1027?)  pId="+pId +" (=24592?)"

vId = 0
pId = 0
_getIds("something\COMPORT&VID_0403&PID_6010", varptr vId, varptr pId)
print "vId="+vId+" (=1027?)  pId="+pId +" (=24592?)"

vId = 0
pId = 0
_getIds("SomethingInvalid&VID_&PID_6010", varptr vId, varptr pId)
print "vId="+vId+" (=0?)  pId="+pId +" (=0?)"

vId = 0
pId = 0
_getIds("SomethingInvalid&VID_0403", varptr vId, varptr pId)
print "vId="+vId+" (=0?)  pId="+pId +" (=0?)"
endrem

Function IsWordCharacter:Int(char:Int)
    Return (char >= Asc("A") And char <= Asc("Z")) Or (char >= Asc("a") And char <= Asc("z")) Or (char >= Asc("0") And char <= Asc("9")) Or char = Asc("_")
End Function

Function _getIds(hids:String, vendorId:Int Ptr, productId:Int Ptr) { nomangle }
    local vidPos:int = hids.Find("VID_")
    local pidPos:int = hids.Find("&PID_")
    'not found or no vendorId contained
    if vidPos < 0 or pidPos < 0 or vidPos+4 = pidPos then return

    local vidString:string
    for local i:int = (vidPos+4) until pidPos
        'invalid code
        if not IsWordCharacter(hids[i]) then return

        vidString :+ chr(hids[i])
    next

    local pidString:string
    for local i:int = (pidPos+5) until hids.length
        'something else is starting
        if not IsWordCharacter(hids[i]) then exit

        pidString :+ chr(hids[i])
    next

    if vidString and pidString
        'print "vidString: ~q" + vidString +"~q"
        'print "pidString: ~q" + pidString +"~q"
        vendorId[0] = hexToInt(vidString)
        productId[0] = hexToInt(pidString)
    endif
End Function
GWRon commented 5 years ago

I used above's code when checking the serial.mod-update on a windows7 computer and at least it did there what it should. So no 100% guarantee - will leave it up to you to either merge it in by hand. You might also just drop a short reply here to make me sending in a Pull Request ;-)