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 support@ccsinfo.com

Strings: strcpy and constant strings

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



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

Strings: strcpy and constant strings
PostPosted: Fri Jan 19, 2007 6:23 am     Reply with quote

Hi,

I have

Code:
#define BRAILLE_COMMANDO "BRAI"
#define E3_SUSPEND "SUSP"
#define BATTERIJ_COMMANDO "BATT"
...


Code:
   strcpy (command,BRAILLE_COMMANDO);        // Commando gaan vergelijken met de geprogrammeerde commando's
   if ( !strncmp (temp_array,command,4) )
      return SCHRIJF_NAAR_LEESREGEL;               // Schrijf Braille

   strcpy (command,E3_SUSPEND);
   if ( strncmp (temp_array,command,4) == 0 )
      return E3_IN_SUSPEND;                        // Na 30s idlen gaat de E3 in suspend

   strcpy (command,BATTERIJ_COMMANDO);
   if ( strncmp (temp_array,command,4) == 0 )
      return VBAT_METEN;
...


Everytime I have to do the same function. I want this:

Copy_string_to_ram ( command, BRAILLE_COMMANDO )
{
strcpy (command, ?);
}

Is that possbile?
Are their other ways to make this code smaller?
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Fri Jan 19, 2007 7:49 am     Reply with quote

The shortest but least managable code would be to make a jump tree based on single characters.

Do you have to check for syntax errors ? Or you can assume all commandos are from the valid set ? (i.e. is it human input or machine to machine communication ?)
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Fri Jan 19, 2007 8:10 am     Reply with quote

machine to machine communication. All commandos should be OK.
ferrumvir



Joined: 01 Feb 2006
Posts: 64
Location: England

View user's profile Send private message Visit poster's website

PostPosted: Fri Jan 19, 2007 8:12 am     Reply with quote

I use code like this to initialise some global variables, this uses up RAM instead of ROM but would speed the execution of the section you mentioned. This routine is called once, at start up. You can then lose all the copy ROM to RAM (strcpy) functions from your section, by using the global STR variables. Use this if you want to increase speed and are not limited on RAM.

The jump tree mentioned by libor above will be even quicker, but much harder to develop, debug and change.

Code:

//==========================================================================
//==========================================================================
// STRING CONSTANTS
//==========================================================================
//==========================================================================
char STRstart[6];
char STRstop[5];
char STRshelp[6];
char STRset_[5];
char STRpwm_[5];
void str_init()
{
  strcpy(STRstart,"start");
  strcpy(STRstop,"stop");
  strcpy(STRshelp,"shelp");
  strcpy(STRset_,"set ");
  strcpy(STRpwm_,"pwm ");
}
//==========================================================================
//==========================================================================
// END - STRING CONSTANTS
//==========================================================================
//==========================================================================
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Fri Jan 19, 2007 8:33 am     Reply with quote

You can store all your commandos also in ROM:

const char commandos[] = {"BRAISUSPBATT"}

or for more readibility: though you lose the 4-letter word phenomena (no pun intended :-)
const char commandos[] = {"BRAI SUSP BATT"}


...and write an #asm routine* (or a C routine with read_program_memory() instruction) to find your string in the program memory-located table returning the position index. Which index (assuming all your commandos are 4 letter words) you could rotate right to get the commandos sequential address in the table. This index could serve in a switch() case jump table.

Be sure not to make a default: case, this will help the compiler to construct a much smaller jump table from the switch() inctruction (rather use one single index beyond the table as the 'everything else' case.

* you will have to #locate your ROM table in a fixed memory postion to find it. or use the label_address()
BTW. How many different commandos do you have ? It is not worth the hassle if you have only these three.
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