View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
String Management |
Posted: Thu Jun 04, 2020 4:07 pm |
|
|
Hi All,
I have situation where my program has a lot of:
Code: | if(STRSTR(Message,"Some_Text")!=NULL) Do_Something(); |
I'm wondering if there is a better way, i have over 30 and growing IF's of this kind.
I'm looking for something like a miracle Switch Statement that could process strings if that ever could exist.
Most of these just set a few flags or variables, maybe print a confirmation to terminal. a switch would be ideal.
I've thought of mapping each string to a value and then switching on that but that looks like a maintenance nightmare.
Any advice?
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Thu Jun 04, 2020 5:50 pm |
|
|
Maybe something like a hashmap that uses char * as the key and function pointer as the value? There's obviously some overhead for the function pointers, but you could avoid heap at least by using pointers to preallocated strings. Still might have some maintenance fun, but maybe less than trying to map values to strings directly. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Jun 05, 2020 12:25 am |
|
|
Why not just use an array based search:
Code: |
char look_for[4][12] = {"Some Text", \
"More text", "Another", "Yet more" };
int ctr;
for (ctr=0;ctr<4;ctr++)
if (strstr(Message,&look_for[ctr][0]) !=NULL)
break;
//You get here with ctr=0 to 4, according to if any string matched.
//4==no match
switch(ctr)
{
case 0:
//Here we found 'Some Text'
break;
case 1:
//Here 'More text'
break;
case 2:
//Here 'Another'
break;
case 3:
//Here 'Yet more'
break;
case 4:
//Here no match
break;
}
|
That is the standard 'C' way of doing this. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Aug 08, 2020 12:26 pm |
|
|
Hi Ttelmah,
I'm trying/using your example above.
I'm need to basically do this with strings stored in ROM, since the strings are "Commands" that won't and should never change.
I can't get it to work, i know it's related to the way memory is accessed.
but i don't know how to do it.
I'm using this same concept for other strings that are variable, and its working fine.
I have the strings in ram for now, but i would feel a lot safer with everything fixed in rom.
Thanks. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Aug 08, 2020 1:02 pm |
|
|
#device PASS_STRINGS=IN_RAM
You need this or pointers can't be constructed to const strings. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Aug 08, 2020 2:29 pm |
|
|
Strange, i have it defined!
I have it as #DEVICE instead Of #device... maybe its case sensitive? _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Aug 08, 2020 4:15 pm |
|
|
Only IF you have #case ahead of it...... |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sun Aug 09, 2020 6:43 pm |
|
|
... any other suggestions?
I have the #device directive as discussed above but still have the same issue.
using 5.093. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|