Page 1 of 1
Arrays of strings
Posted: Fri Nov 16, 2018 1:20 am
by TodWulff
I am trying to implement a means to gain some verbosity in my debug code.
I have a integer variable to capture a task state - 0 = not started, 1 = idle, 2 = active, ...
I'd like to do something such as this:
Code: Select all
const task_state_verbose(16) = {
"Not Started",
"Idle",
"Active",
"Paused"
}
...
for i = 0 to _task_count
Print "Task ";i;" TaskState: ","",task_state(i);" - ";task_state_verbose(task_state(i))
next i
Of course I can make use of a select case construct, which I am going to do.
I am just wondering if there is such a thing as constant arrays of strings given ARMBasic has evolved quite a bit over the last 10 years.
Re: Arrays of strings
Posted: Fri Nov 16, 2018 11:34 pm
by olzeke51
I think the closest you can get is a CONST bytearrayname, and use a multiplier on the error status index to get into the array. Kind of like the old-style flat file database. There is wasted space in memory unless you make them all the same size.
'
You might be able to use the PARAMARRAY to hold ADDRESSOF upto 15 messages of CONST strings. and then use your status code as a selector. (theory??) The PARAMARRY does allocate 16 long words(?) [of memory] regardless of how many items you define it with [IIRC].
I have played/used the PRAMARRY as a global. There are several 'posts' in regard to my questions and findings.
'
Hope this isn't a rabbit trail I am leading you on.
Gary z
Re: Arrays of strings
Posted: Sun Nov 18, 2018 2:30 am
by TodWulff
An interesting set of approaches you've posited. ... I am all about being Alice.
Going to have to take a peek at that tonight/tomorrow.
Thank you, kind sir.
-t
Re: Arrays of strings
Posted: Sun Nov 18, 2018 10:03 am
by TodWulff
Ok, this works. I dislike it somewhat, as I think that there are likely better ways to do it.
But, as a proof of concept, it passes muster. Feel free to refactor and optimize.
Code: Select all
defglobals: ' these are the global variables
dim task_state_verbose_ptr (4) as integer
return
sub init() ' init the array of strings - hokey shites here, but it works as a poc
gosub defglobals
dim task_state_verbose1(16) as string
dim task_state_verbose2(16) as string
dim task_state_verbose3(16) as string
dim task_state_verbose4(16) as string
task_state_verbose1 = "Not Started"
task_state_verbose2 = "Idle"
task_state_verbose3 = "Active"
task_state_verbose4 = "Paused"
task_state_verbose_ptr(1) = addressof task_state_verbose1
task_state_verbose_ptr(2) = addressof task_state_verbose2
task_state_verbose_ptr(3) = addressof task_state_verbose3
task_state_verbose_ptr(4) = addressof task_state_verbose4
endsub
function StringViaPointer(pointer as integer) as string
dim strg(255) as string
dim int, idx as integer
strg =""
idx = 0
do
int = *pointer
strg(idx)=(int and $FF)
if strg(idx)=$00 then exit
strg(idx+1)=(int and $FF00) >> 8
if strg(idx+1)=$00 then exit
strg(idx+2)=(int and $FF0000) >> 16
if strg(idx+2)=$00 then exit
strg(idx+3)=(int and $FF000000) >> 24
if strg(idx+3)=$00 then exit
idx += 4
pointer += 4
loop
return strg
endfunction
main:
dim i as integer
init()
for i = 1 to 4
print i,StringViaPointer(task_state_verbose_ptr(i))
next i
end
Here is the result:
Code: Select all
Analyzing S:/_Dev/__My_Code/ARMbasic_Repo/StrArrayTest2.bas
get addresses
stopButton
toggleReset
<= done
bpp.exe -DLPC54102 -I"S:/_Dev/__My_Code/ARMbasic_Repo" -I"C:/Users/TAJS/Documents/_Dev/ARMbasic_libs/CoridiumBASICLibs_v937" "S:/_Dev/__My_Code/ARMbasic_Repo/StrArrayTest2.bas" >C:/Users/TAJS/AppData/Local/Temp/Coridium/StrArrayTest2.bpp
...
Compiling C:/Users/TAJS/AppData/Local/Temp/Coridium/StrArrayTest2.bpp
ARMbasic[9.37m] on the PC Copyright 2017, Coridium Corp.
... ( 0.56K code + 0.03K const)/416K 0.38/80K data programmed
compile done
Programming Flash *+*+
done
Executing...
1 Not Started
2 Idle
3 Active
4 Paused
... Finished in 3 ms