String Sort problem

General FreeBASIC programming questions.
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: String Sort problem

Postby MrSwiss » Oct 05, 2019 0:25

Just remembered: try Open Cons instead of Open Pipe
I've never used Open Pipe, but I did use Open Cons successfully, on WIN.
Dinosaur
Posts: 1302
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 05, 2019 1:05

Hi All

I have replaced and tested by running the script in the terminal and directing the output into a file.
Then

Code: Select all

    Dim as Long pf = Freefile
    Dim s as String * 18
    Open "max.txt" for Input as #pf
        Get #pf,, s
    Close #pf
    Print s
Same fault in the same place, after it comes back from reading the file.
I converted every Short, Integer to Long.
Removed all With End With Statements.(wasn't confident on that.
Reduced memory usage by only having 2 copies of udt instead of the desired 20.

Running out of options.
It seems (Just like at the start of this thread) the error happens in a place in the code, regardless of the code.????
Sounds stupid , I know.

Next step is stepping through with Gede debugger.

Regards
grindstone
Posts: 656
Joined: May 05, 2015 5:35
Location: Germany

Re: String Sort problem

Postby grindstone » Oct 05, 2019 15:11

Dinosaur wrote:Each time I replace a Comma, I remembered the position with LFCnt.LF(X):

Could it be that you DIMmed an array inside your UDT and try to access it beyond its upper bound?
Dinosaur
Posts: 1302
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 05, 2019 15:46

Hi All

Thanks for the wakeup call grindstone.
No, I don't think so, BUT do we have the same restrictions within a Select Case ?

But I will now check for it.

Regards
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: String Sort problem

Postby MrSwiss » Oct 05, 2019 16:39

There might be another reason for the "Core Dump".

Have you checked, that you don't have a inadvertently induced "Division by Null"?

Dinosaur wrote:BUT do we have the same restrictions within a Select Case ?
Not that I know of.
There are some, if you're using: Select Case As Const ...
Dinosaur
Posts: 1302
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 05, 2019 17:22

Hi All

My wife tells me that I have the patience of Jobe. (except with printers)

Finally found the reason.
In Command 14

Code: Select all

        Case 14 'Get IP from MAC
            Dim as Long PF = FreeFile
            Dim as String s , IPAddr = "", Chrs
            input "MAC Address in Lower Case :"; s
            Open pipe "./macscan.sh " + s For Input As #PF
            Do Until Eof(PF)
                Line Input #PF, Chrs
                IPAddr += Chrs
            Loop
            Print IPAddr, Len(IPAddr)
Mistake 1: I did not actually create a command to send to the device at the end of this step 14.
After this returns from getting the IP by communicating with the device, the fault happens when the following code times/bombs out.

Code: Select all

            Case 3  ''Now see if we can Get a reply, if so convert .Buffer to .RxData
                If Control.PrintFlag > 0 Then Print "Step-3"
                If pConnection(Control.Device)->CanGet() = 1 Then
                    dim as Integer nBytes = pConnection(Control.Device)->GetData(.pBuffer, 1024, 1)
                    .RxData = mid(*cast(zstring ptr, .pBuffer + 4), 1, nBytes - 4)
                    .StepNbr += 1
                EndIf
Mistake 2: I did not write a Time out for this step. Then for some reason this then creates the (Segmentation fault (core dumped) fault.
THAT, I don't understand. If it could not get data from the connection, one would think it would sit there all day.

Either way, I now know the source of the fault and can correct it.
Will use the script in the Terminal to get the IP, and then type it in.

Many thanks for the suggestions and help in solving this.

Regards
D.J.Peters
Posts: 7838
Joined: May 28, 2005 3:28

Re: String Sort problem

Postby D.J.Peters » Oct 05, 2019 18:22

Why not using the FreeBASIC JSON library ?

viewtopic.php?f=8&t=24206

Joshy
Dinosaur
Posts: 1302
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 05, 2019 18:31

Hi All

My partner in crime (badidea) has been looking into that.

Regards
badidea
Posts: 1582
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: String Sort problem

Postby badidea » Oct 05, 2019 21:42

Yes, but I do not understand how to use the library. Posted a question there, just now.
badidea
Posts: 1582
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: String Sort problem

Postby badidea » Oct 06, 2019 23:02

Although the sorting out string question was already solved, I did give string parsing a try today.
Data keys & values are not stored in a array. Just valueStr = getJsonValue(jsonStr, keyStr).
Not so compact code:

Code: Select all

' Get value from a JSON string for a certain key

' "key" : "value string"
' "key" : number
' "key" : [ array ]
' "key" : { object }

' 1. look for "key"
' 2. skip ' ' (spaces)
' 3. check if char is ':' else fail
' 4. skip ' ' (spaces)
' 5. check if char:
'   a. '"' --> scan for next '"'
'   b. '0'...'9', '+', '-' --> scan until not 0-9,+,-
'   c. '{' --> fail
'   d. '[' --> fail

' Known Bugs/issues/omissions:
' * fails on nested object
' * fails on array
' * No propper return value
' * No floating point number support (0.0 or 1E+1)
' * No escaped characters
' * No UTF-8 support
' * Everything is returned as string

const DQ = chr(34) 'double quote
const SQ = "'" 'single quote

function getJsonValue(jsonStr as string, key_ as string) as string
   dim as string key = !"\"" + key_ + !"\""
   dim as integer keyPos = instr(jsonStr, key)
   if keyPos = 0 then return "FAIL_NO_KEY" 'key not found
   dim as integer jsonStrLen = len(jsonStr)
   dim as integer cursor = keyPos + len(key) - 1
   dim as integer startPos, endPos '0 is first char
   'skip spaces
   while 1
      if cursor >= jsonStrLen then return "FAIL_END_SPC1"
      if jsonStr[cursor] <> asc(" ")  then exit while
      cursor += 1
   wend
   'check for colon
   if jsonStr[cursor] = asc(":") then
      cursor += 1
   else
      return "FAIL_NO_COLON"
   end if
   'skip spaces again
   while 1
      if cursor >= jsonStrLen then return "FAIL_END_SPC2"
      if jsonStr[cursor] <> asc(" ")  then exit while
      cursor += 1
   wend
   'check if char: [, {, ", number
   select case jsonStr[cursor]
      case asc("[")
         return "FAIL_IS_ARRAY"
      case asc("{")
         return "FAIL_IS_OBJECT"
      case asc(DQ)
         cursor += 1 'skip '"'
         startPos = cursor
         while 1
            if cursor >= jsonStrLen then return "FAIL_END_DQ"
            if jsonStr[cursor] = asc(DQ) then exit while
            cursor += 1
         wend
         endPos = cursor
      case asc("0") to asc("9"), asc("+"), asc("-")
         startPos = cursor 'include this last one in return value
         cursor += 1
         while 1
            if cursor >= jsonStrLen then return "FAIL_END_NUM"
            if jsonStr[cursor] < asc("0") or jsonStr[cursor] > asc("9") then exit while
            cursor += 1
         wend
         endPos = cursor
      case else
         return "FAIL_NO_SELECT"
   end select
   return mid(jsonStr, startPos + 1, endPos - startPos)
end function

Test1:
dim as string jsonStr1 = !"{\"time\":{\"get_time\":{\"year\":2000,\"month\":1,\"mday\":2,\"hour\":2,\"min\":53,\"sec\":13,\"err_code\":0}}}"
print jsonStr1
dim as string keyStr(...) = {"time", "get_time", "year", "month", "mday", "hour", "min", "sec", "err_code"}
'get value for key in list
for i as integer = 0 to ubound(keyStr)
   print SQ + keyStr(i) + SQ, SQ + getJsonValue(jsonStr1, keyStr(i)) + SQ
next

Test2:
dim as string jsonStr2 = "{"+DQ+"system"+DQ+":{"+DQ+"get_sysinfo"+DQ+":{"+DQ+"sw_ver"+DQ+":"+DQ+"1.5.5 Build 181225 Rel.102720"+DQ+","+DQ+"hw_ver"+DQ+":""2.0"+DQ+",""type"+DQ+":"+DQ+"IOT.SMARTPLUGSWITCH"+DQ+","+DQ+"model"+DQ+":"+DQ+"HS110(AU)"+DQ+","+DQ+"mac"+DQ+":"+DQ+"B0:BE:76:C1:B8:3C"+DQ+","+DQ+"dev_name"+DQ+":"+DQ+"Smart Wi-Fi Plug With Energy Monitoring"+DQ+","+DQ+"alias"+DQ+":"+DQ+" Device1"+DQ+","+DQ+"relay_state"+DQ+":1,"+DQ+"on_time"+DQ+":6949,"+DQ+"active_mode"+DQ+":"+DQ+"none"+DQ+","+DQ+"feature"+DQ+":"+DQ+"TIM:ENE"+DQ+","+DQ+"updating"+DQ+":0,"+DQ+"icon_hash"+DQ+":"+DQ+""+DQ+","+DQ+"rssi"+DQ+":-25,"+DQ+"led_off"+DQ+":0,"+DQ+"longitude_i"+DQ+":0,"+DQ+"latitude_i"+DQ+":0,"+DQ+"hwId"+DQ+":"+DQ+"A28C8BB92AFCB6CAFB83A8C00145F7E2"+DQ+","+DQ+"fwId"+DQ+":"+DQ+"00000000000000000000000000000000"+DQ+","+DQ+"deviceId"+DQ+":"+DQ+"80061922201888E1FBE72378A55A2C581ADF3EAA"+DQ+","+DQ+"oemId"+DQ+":"+DQ+"6480C2101948463DC65D7009CAECDECC"+DQ+","+DQ+"next_action"+DQ+":{"+DQ+"type"+DQ+":-1},"+DQ+"err_code"+DQ+":0}}}"
print jsonStr2
dim as string keyStr2(...) = {"system", "get_sysinfo", "sw_ver", "hw_ver", "type", "model", "mac", "dev_name", "alias", "relay_state", "on_time", "active_mode", "feature", "updating", "icon_hash", "rssi", "led_off", "longitude_i", "latitude_i", "hwId", "fwId", "deviceId", "oemId", "next_action", "err_code"}
'get value for key in list
for i as integer = 0 to ubound(keyStr2)
   print SQ + keyStr2(i) + SQ, SQ + getJsonValue(jsonStr2, keyStr2(i)) + SQ
next

Return to “General”

Who is online

Users browsing this forum: MSN [Bot] and 1 guest