String Sort problem

General FreeBASIC programming questions.
Dinosaur
Posts: 1343
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

String Sort problem

Postby Dinosaur » Oct 03, 2019 23:52

Hi All

Linux, FB 1.06

The typical String:

Code: Select all

{"system":{"get_sysinfo":{"sw_ver":"1.5.5 Build 181225 Rel.102720","hw_ver":"2.0","type":"IOT.SMARTPLUGSWITCH","model":"HS110(AU)","mac":"B0:BE:76:C1:B8:3C","dev_name":"Smart Wi-Fi Plug With Energy Monitoring","alias":" Device1","relay_state":1,"on_time":6949,"active_mode":"none","feature":"TIM:ENE","updating":0,"icon_hash":"","rssi":-25,"led_off":0,"longitude_i":0,"latitude_i":0,"hwId":"A28C8BB92AFCB6CAFB83A8C00145F7E2","fwId":"00000000000000000000000000000000","deviceId":"80061922201888E1FBE72378A55A2C581ADF3EAA","oemId":"6480C2101948463DC65D7009CAECDECC","next_action":{"type":-1},"err_code":0}}}

The objective:
After each : (colon) there is a string that I am interested in.
For example: "hw_ver":"2.0" , I am interested in 2.0

How I went about it.
1. Replaced each comma with a LF, and then stripped all of the undesirable char's. ie: " { }
which results in this.

Code: Select all

system:get_sysinfo:sw_ver:1.5.5 Build 181225 Rel.102720
hw_ver:2.0
type:IOT.SMARTPLUGSWITCH
model:HS110(AU)
mac:B0:BE:76:C1:B8:3C
dev_name:Smart Wi-Fi Plug With Energy Monitoring
alias: Device1
relay_state:1
on_time:6949
active_mode:none
feature:TIM:ENE
updating:0
icon_hash:
rssi:-25
led_off:0
longitude_i:0
latitude_i:0
hwId:A28C8BB92AFCB6CAFB83A8C00145F7E2
fwId:00000000000000000000000000000000
deviceId:80061922201888E1FBE72378A55A2C581ADF3EAA
oemId:6480C2101948463DC65D7009CAECDECC
next_action:type:-1
err_code:0


So far I can do all this without an error.
Next I want to capture all the char's after the colon : but before the next LF.
That's where I get into trouble, the (Segmentation fault (core dumped)) kind of trouble.
Each time I replace a Comma, I remembered the position with LFCnt.LF(X):

Code: Select all

            With LFCnt
                .LF(1) = 1
                Dim X as Integer
                For X = 1 to 23
                    Control.Cnt = Instr(Temp,",")
                    If Control.Cnt > 0 Then
                            Mid(Temp,Control.Cnt) = Chr(&H0A)
                            .LF(X) = Control.Cnt
                            .dString(X) = Mid(Temp,.LF(X-1)+1,(.LF(X)-.LF(X-1))-1)
                        Else
                            Exit For
                    EndIf
                Next
                .dString(X) = Right(Temp,10)    'Last Char of Temp
            End With
At the same time I am capturing the target string dString(X) by using the current LF position minus the previous LF position.
That all seems to work (which the printing shows) but for the Segmentation error that can happen anywhere.

I have modified the code for days:
Using String * n instead of just String
Using zString * n
Expanding the dString(X) = statement into 3 or 4 steps.

All to no avail.

I am sure there are String guru's here, and would love to hear what I am doing wrong , OR what other way to do it.

Regards
dafhi
Posts: 1343
Joined: Jun 04, 2005 9:51

Re: String Sort problem

Postby dafhi » Oct 04, 2019 0:48

Could treat all LF colon and comma as the same char?

If not, well ima go for a walk anyway
srvaldez
Posts: 2429
Joined: Sep 25, 2005 21:54

Re: String Sort problem

Postby srvaldez » Oct 04, 2019 1:47

Hi Dinosaur
perhaps this will give you an idea on how to tackle the problem

Code: Select all

dim as string s = "{"+chr(34)+"system"+chr(34)+":{"+chr(34)+"get_sysinfo"+chr(34)+":{"+chr(34)+"sw_ver"+chr(34)+":"+chr(34)+"1.5.5 Build 181225 Rel.102720"+chr(34)+","+chr(34)+"hw_ver"+chr(34)+":""2.0"+chr(34)+",""type"+chr(34)+":"+chr(34)+"IOT.SMARTPLUGSWITCH"+chr(34)+","+chr(34)+"model"+chr(34)+":"+chr(34)+"HS110(AU)"+chr(34)+","+chr(34)+"mac"+chr(34)+":"+chr(34)+"B0:BE:76:C1:B8:3C"+chr(34)+","+chr(34)+"dev_name"+chr(34)+":"+chr(34)+"Smart Wi-Fi Plug With Energy Monitoring"+chr(34)+","+chr(34)+"alias"+chr(34)+":"+chr(34)+" Device1"+chr(34)+","+chr(34)+"relay_state"+chr(34)+":1,"+chr(34)+"on_time"+chr(34)+":6949,"+chr(34)+"active_mode"+chr(34)+":"+chr(34)+"none"+chr(34)+","+chr(34)+"feature"+chr(34)+":"+chr(34)+"TIM:ENE"+chr(34)+","+chr(34)+"updating"+chr(34)+":0,"+chr(34)+"icon_hash"+chr(34)+":"+chr(34)+""+chr(34)+","+chr(34)+"rssi"+chr(34)+":-25,"+chr(34)+"led_off"+chr(34)+":0,"+chr(34)+"longitude_i"+chr(34)+":0,"+chr(34)+"latitude_i"+chr(34)+":0,"+chr(34)+"hwId"+chr(34)+":"+chr(34)+"A28C8BB92AFCB6CAFB83A8C00145F7E2"+chr(34)+","+chr(34)+"fwId"+chr(34)+":"+chr(34)+"00000000000000000000000000000000"+chr(34)+","+chr(34)+"deviceId"+chr(34)+":"+chr(34)+"80061922201888E1FBE72378A55A2C581ADF3EAA"+chr(34)+","+chr(34)+"oemId"+chr(34)+":"+chr(34)+"6480C2101948463DC65D7009CAECDECC"+chr(34)+","+chr(34)+"next_action"+chr(34)+":{"+chr(34)+"type"+chr(34)+":-1},"+chr(34)+"err_code"+chr(34)+":0}}}"

dim as integer i=0, j, c, l=len(s)
while i<l
   c=s[i]=58
   if c then
      j=i+1
      while s[j]<>58 andalso s[j]<>44 andalso j<l
         Print chr(s[j]);
         j+=1
      wend
      Print
      i=j-1
   end if
   i+=1
wend
sleep
srvaldez
Posts: 2429
Joined: Sep 25, 2005 21:54

Re: String Sort problem

Postby srvaldez » Oct 04, 2019 2:26

or

Code: Select all

dim as string s = "{"+chr(34)+"system"+chr(34)+":{"+chr(34)+"get_sysinfo"+chr(34)+":{"+chr(34)+"sw_ver"+chr(34)+":"+chr(34)+"1.5.5 Build 181225 Rel.102720"+chr(34)+","+chr(34)+"hw_ver"+chr(34)+":""2.0"+chr(34)+",""type"+chr(34)+":"+chr(34)+"IOT.SMARTPLUGSWITCH"+chr(34)+","+chr(34)+"model"+chr(34)+":"+chr(34)+"HS110(AU)"+chr(34)+","+chr(34)+"mac"+chr(34)+":"+chr(34)+"B0:BE:76:C1:B8:3C"+chr(34)+","+chr(34)+"dev_name"+chr(34)+":"+chr(34)+"Smart Wi-Fi Plug With Energy Monitoring"+chr(34)+","+chr(34)+"alias"+chr(34)+":"+chr(34)+" Device1"+chr(34)+","+chr(34)+"relay_state"+chr(34)+":1,"+chr(34)+"on_time"+chr(34)+":6949,"+chr(34)+"active_mode"+chr(34)+":"+chr(34)+"none"+chr(34)+","+chr(34)+"feature"+chr(34)+":"+chr(34)+"TIM:ENE"+chr(34)+","+chr(34)+"updating"+chr(34)+":0,"+chr(34)+"icon_hash"+chr(34)+":"+chr(34)+""+chr(34)+","+chr(34)+"rssi"+chr(34)+":-25,"+chr(34)+"led_off"+chr(34)+":0,"+chr(34)+"longitude_i"+chr(34)+":0,"+chr(34)+"latitude_i"+chr(34)+":0,"+chr(34)+"hwId"+chr(34)+":"+chr(34)+"A28C8BB92AFCB6CAFB83A8C00145F7E2"+chr(34)+","+chr(34)+"fwId"+chr(34)+":"+chr(34)+"00000000000000000000000000000000"+chr(34)+","+chr(34)+"deviceId"+chr(34)+":"+chr(34)+"80061922201888E1FBE72378A55A2C581ADF3EAA"+chr(34)+","+chr(34)+"oemId"+chr(34)+":"+chr(34)+"6480C2101948463DC65D7009CAECDECC"+chr(34)+","+chr(34)+"next_action"+chr(34)+":{"+chr(34)+"type"+chr(34)+":-1},"+chr(34)+"err_code"+chr(34)+":0}}}"

dim as integer i=0, j, c, l=len(s), q
while i<l
   c=s[i]=58
   if c then
      j=i+1
      if s[j]=34 then
         q=34
         Print chr(34);
         j+=1
      else
         q=58
      end if
      while s[j]<>q andalso s[j]<>44 andalso j<l
         if s[j]<>123 andalso s[j]<>125 then
            Print chr(s[j]);
         end if
         j+=1
      wend
      if q=34 then Print chr(34);
      Print
      i=j-1
   end if
   i+=1
wend
sleep
Dinosaur
Posts: 1343
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 04, 2019 14:08

Hi All

Many thanks for the responses, but dafhi the walk didn't help.

srvaldez I have implemented your second example and modified it as per below,.

Code: Select all

dim as string d(30)
dim as integer i=0, j, c, l=len(s), q,r
while i<l
   c=s[i]=58   
   if c then
      j=i+1
      if s[j]=34 then
         q=34
         j+=1
      else
         q=58
      end if
      while s[j]<>q andalso s[j]<>44 andalso j<l
         if s[j]<>123 andalso s[j]<>125 then
            If s[j] <> 34 then d(r) += chr(s[j]) 'fill string number r
         end if
         j+=1
      wend
      r+=1  'inc string number
      i=j-1
   end if
   i+=1
wend
For r = 0 to 30
    print d(r)
Next
sleep
I understood the whole concept except for one statement that I have never used before.
Can someone explain this long hand to me.

Code: Select all

   c=s[i]=58   
   if c then


Either way well done, it does the job nicely.

Regards
srvaldez
Posts: 2429
Joined: Sep 25, 2005 21:54

Re: String Sort problem

Postby srvaldez » Oct 04, 2019 14:18

Hi Dinosaur
c=s[i]=58 sets c to true if s[I]=":", in retrospect, I can see that it looks confusing
but I am glad that I was able to help
MrSwiss
Posts: 3513
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: String Sort problem

Postby MrSwiss » Oct 04, 2019 19:59

I think that the thread title is somewhat misleading.

What seems to be the case is (IMO):
    1) clean the string (remove unwanted characters: curly braces and Chr(34)'s)
    2) fix it (the second colon is either a typo or should be a comma)
    3) split it into a string array (see: StrSplit()) using comma, as split-char
Then, you can easily take it from there ...
Dinosaur
Posts: 1343
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 04, 2019 21:46

Hi All

MrSwiss I thought it was that straight forward as well.
In fact I could not find errors in my code and the way I did it.

I kept getting Segmentation fault (core dumped)
In fact I don't recall getting that fault for years.
Now I seem to be getting it every day for some reason that nobody can pick.

Today, AGAIN.
A simple Pipe command to run a script, and capture the char's.

Code: Select all

            Dim as Integer 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)
I add the MAC ie: b0:be:76:c1:b8:3c
The script.

Code: Select all

#!/bin/sh

TMPFILE=`mktemp` || exit 1

sudo nmap -sP 192.168.0.0/24 -oN $TMPFILE 2>/dev/null 1>/dev/null
if [ -z $1 ]; then
  TARGET=MAC
  cat $TMPFILE |head -n -1 |tail -n +2 |paste -d " " - - - |grep -i $TARGET
else
  TARGET=$1
  cat $TMPFILE |head -n -1 |tail -n +2 |paste -d " " - - - |grep -i $TARGET | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]* "
fi

rm $TMPFILE
Replies with:
192.168.0.180 14
Segmentation fault (core dumped)


If you can pick what is wrong with that code "you will be my Hero"

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

Re: String Sort problem

Postby MrSwiss » Oct 04, 2019 22:50

I'm not certain, that the loop is really needed.

Just try below (can't get any worse than it is currently):

Code: Select all

            Dim as Long PF = FreeFile   ' FreeFile returns Long
            Dim as String s, IPAddr = "", Chrs
            input "MAC Address in Lower Case :"; s
            Open pipe "./macscan.sh " + s For Input As #PF
            While Not Eof(PF)
                Line Input #PF, Chrs
                If Len(Chrs) > 0 Then IPAddr += Chrs
            Wend
            Close(PF)
            Print IPAddr, Len(IPAddr)
Shell Scripts on Linux/MAC are not my "everyday job", sorry.

Maybe a test of the script itself on "Terminal", can shed some light, dunno.
Dinosaur
Posts: 1343
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 04, 2019 23:03

Hi all

MrSwiss, tested both changes and , sorry Hero Title still open.
Even taking out the print statement, did nothing to solve the fault.

I am even thinking of taking the very radical step of going back a few versions of FB.
Never before have I had such a spate of these faults.

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

Re: String Sort problem

Postby badidea » Oct 04, 2019 23:05

Dinosaur wrote:Segmentation fault (core dumped)

Did you check with -exx as compile option?
MrSwiss
Posts: 3513
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: String Sort problem

Postby MrSwiss » Oct 04, 2019 23:07

Which version do you currently use?

To be straight out: I suspect, the fault is somewhere else (OS maybe).

Have you recently updated libraries or installed new applications on your PC ?
Dinosaur
Posts: 1343
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 04, 2019 23:39

Hi All

Currently using 1.06 , but just tried 1.05 with no difference.
Have you recently updated libraries or installed new applications on your PC ?

I have been preparing my laptop to hand it over to my successor here in the usa.
But that has mainly involved removing things.

However, I can try using a Timeshift from 2 weeks ago.
Is there any point in locating the "core dump" ?
Probably need a phd to translate it.

Regards
EDIT:Actually I will try different computers.
MrSwiss
Posts: 3513
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: String Sort problem

Postby MrSwiss » Oct 04, 2019 23:44

Dinosaur wrote:Currently using 1.06 , but just tried 1.05 with no difference.
I'd have expected nothig different (both are quite stable, AFAIK).

Dinosaur wrote:I have been preparing my laptop to hand it over to my successor here in the usa.
But that has mainly involved removing things.
You might just have been removing a bit "too much".

Dinosaur wrote:However, I can try using a Timeshift from 2 weeks ago.
Yep, if you can "go back" to before the "great cleaning", might just do the trick.
Dinosaur
Posts: 1343
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Re: String Sort problem

Postby Dinosaur » Oct 05, 2019 0:17

Hi All

Well it is not a Computer problem.
I have tried it on another laptop (complete clean install of every thing)
I have tried it on a BeagleBone Black.

Even when the script is not run (due to permission error) the same fault on return from the shell.
I HATE SHELL Programming, the only reason to do it, because the commands are not available in FB.

Both of these alternate computers have large cgui apps compiled with FB 1.06 running on them
without any errors.

Will perservere until I find the reason.

Regards

Return to “General”

Who is online

Users browsing this forum: jj2007 and 8 guests