CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

String Management

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

String Management
PostPosted: Thu Jun 04, 2020 4:07 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 04, 2020 5:50 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 05, 2020 12:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Aug 08, 2020 12:26 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Aug 08, 2020 1:02 pm     Reply with quote

#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

View user's profile Send private message

PostPosted: Sat Aug 08, 2020 2:29 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Aug 08, 2020 4:15 pm     Reply with quote

Only IF you have #case ahead of it......
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sun Aug 09, 2020 6:43 pm     Reply with quote

... 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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group