FIFO comes from ye olde English song 'Fee FIFO Fumb, I smell the blood of an English Mon'
Arrays ay? and their crazy 'out of bounds' way of crashing the entire system, a programmers bane since days of yore.
There is probably a really neat way of putting this into an object and calling it something like, oh I don't know, FIFO maybe?
Then you could create a new instance of the FIFO object, give it a name and reference its elephants.. i mean elements.
However just for playing (and to cover up the fact that I don't know how to do the above obectifyin') here's how to do it prehistorically.
Just for testing I created a new project, went into the designer and put three buttons on and called em'
fill
print
fifo
then I added the click members, which you can just do by hand in your Main code thusly:
Sub fill_Click
End Sub
Sub print_Click
End Sub
Sub fifo_Click
End Sub
In my Sub Process_Globals I have
Dim smsFifo as List
then in my Sub Activity_Create(FirstTime as Boolean) I have
If FirstTime Then
smsFifo.Initialize
End If
And don't forget your Activity.LoadLayout("Test") 'I called mine Test ....
I put the smsFifo.Initialize in that If clause because it would seem that Activity_Create is not always called only once and if I
don't then my List might get randomly initilized and thusly cleared.
The reason I want a FIFO (First In First Out) list is because I'm writing a routine that will capture an incoming SMS text message where the first word in the message should be a password, if it matches then that message is nabbed and prevented from going further on to the messaging app and the rest of the message is split up into 'commands' where the split delimiter is the semi-colon so for testing and to represent the incoming SMS text here is a string of stringyness:
Dim s As String = " Bti say Hello how are you?; well here we go; this is another string; sfx2"
so here 'Bti' is the first word in the text and if this matches the 'password' then the text will be nabbed etc. etc. etc. then the rest of the message will be split up into 'commands', put into a FIFO list then dealt with by another routine (activated by a timer) as and when it is convenient. A sort of computerized bureaucracy where the FIFO list can be added to at any time.
So here's the code. All the testing is done by writing to the log so have the log screen cleared and in view.
I'm sure it can be done more elegantly than this but I'm still a relative newbie to B4A and its all good learning.
Let me know what you think and feel free to chop and change it about etc.
Arrays ay? and their crazy 'out of bounds' way of crashing the entire system, a programmers bane since days of yore.
There is probably a really neat way of putting this into an object and calling it something like, oh I don't know, FIFO maybe?
Then you could create a new instance of the FIFO object, give it a name and reference its elephants.. i mean elements.
However just for playing (and to cover up the fact that I don't know how to do the above obectifyin') here's how to do it prehistorically.
Just for testing I created a new project, went into the designer and put three buttons on and called em'
fill
fifo
then I added the click members, which you can just do by hand in your Main code thusly:
Sub fill_Click
End Sub
Sub print_Click
End Sub
Sub fifo_Click
End Sub
In my Sub Process_Globals I have
Dim smsFifo as List
then in my Sub Activity_Create(FirstTime as Boolean) I have
If FirstTime Then
smsFifo.Initialize
End If
And don't forget your Activity.LoadLayout("Test") 'I called mine Test ....
I put the smsFifo.Initialize in that If clause because it would seem that Activity_Create is not always called only once and if I
don't then my List might get randomly initilized and thusly cleared.
The reason I want a FIFO (First In First Out) list is because I'm writing a routine that will capture an incoming SMS text message where the first word in the message should be a password, if it matches then that message is nabbed and prevented from going further on to the messaging app and the rest of the message is split up into 'commands' where the split delimiter is the semi-colon so for testing and to represent the incoming SMS text here is a string of stringyness:
Dim s As String = " Bti say Hello how are you?; well here we go; this is another string; sfx2"
so here 'Bti' is the first word in the text and if this matches the 'password' then the text will be nabbed etc. etc. etc. then the rest of the message will be split up into 'commands', put into a FIFO list then dealt with by another routine (activated by a timer) as and when it is convenient. A sort of computerized bureaucracy where the FIFO list can be added to at any time.
So here's the code. All the testing is done by writing to the log so have the log screen cleared and in view.
I'm sure it can be done more elegantly than this but I'm still a relative newbie to B4A and its all good learning.
Let me know what you think and feel free to chop and change it about etc.
B4X:
Sub FIFO As String
Dim s As String
Try
s = smsFifo.Get(0)
smsFifo.RemoveAt(0)
Return s
Catch
Log("Kabooom!")
End Try
Return "" 'don't need an ELSE IF because if it Returns s then it doesn't get this far.
'in fact don't need a Return "" either because it either returns something or it crashes :)
End Sub
Sub fill_Click
Dim theMagicWord As String = "bTi"
Dim s As String = " Bti say Hello how are you?; well here we go; this is another string; sfx2"
s = s.Trim
If s.ToLowerCase.Trim.StartsWith(theMagicWord.ToLowerCase.Trim) Then ' its one for us otherwise let it go.
Dim parts() As String 'Will also clear it right?
parts = Regex.Split(";", s)
parts(0) = parts(0).SubString(theMagicWord.Length)
For Each j As String In parts
Log(j.Trim)
smsFifo.Add(j) 'add it to the list baby! but don't clear it, add more if needed.
Next
Log("smsFifo Size = " & smsFifo.Size)
Else
Log("Nope, not one for us")
End If
End Sub
Sub print_Click
logspace 'print a few blank lines in the log to make it a little easier to read.
Log("List Print")
For Each s As String In smsFifo
Log(smsFifo.IndexOf(s) & " " & s) 'so I want to print out the list element number and then whatever is in that list element.
Next
End Sub
Sub fifo_Click
Dim s As String = FIFO
If s <> "" Then
Log("FIFO Returned: " & s)
Else
Log("nah, FIFO returned an empty string")
End If
End Sub
Sub logspace 'just to print a few blank lines in the log for easy reading
For a = 1 To 4
Log(" ")
Next
End Sub
Last edited: