c - Minimizing Memory Consumption on sscanf -


i have time&date string acquired gsm module.

here time&date format:

"yy/mm/dd,hh:mm:ss±zz" 

and example string:

"+cclk: "17/04/07,12:57:43+03"" 

i need parse , convert unix timestamp. have below implementation , works well:

time struct:

struct timestruct{  uint16 year; uint8 month; uint8 day; uint8 hour; uint8 minute; uint8 second; }; 

gsm clock command parser:

void load_clock(void) {  struct timestruct gsmtimestruct;  /* parse year,month,day,hour,minute,second , gmt timezone */ sscanf(gsm_clk_string, "+cclk: \"%hd/%d/%d,%d:%d:%d%c%d", &(gsmtimestruct.year), &(gsmtimestruct.month), &(gsmtimestruct.day), &(gsmtimestruct.hour), &(gsmtimestruct.minute), &(gsmtimestruct.second),&conezone,&timezone);  unixtime = (uint32_t)rtc_datetimetounix(gsmtimestruct);  /* collect gmt difference */ if(conezone == '+')     unixtime = unixtime - timezone*3600; else     unixtime = unixtime + timezone*3600;  } 

that sscanf function consumes 1776 bytes flash memory huge number mcu. unix conversion function consumes 700 bytes bit normal has mathematical calculations in it.

are there bright ideas on parsing string without consuming such big flash memory usage?

note: error checking command string not issue since gsm module it. so, if want change parsing implementation, don't have it. can assume numbers there.

edit: tried below snippet, works well. saved 1.7 kilobytes memory i'm not sure if proper way.

gsmtimestruct.year      = (gsm_clk_string[8]-'0')*10 + (gsm_clk_string[9]-'0'); gsmtimestruct.month     = (gsm_clk_string[11]-'0')*10 + (gsm_clk_string[12]-'0'); gsmtimestruct.day       = (gsm_clk_string[14]-'0')*10 + (gsm_clk_string[15]-'0'); gsmtimestruct.hour      = (gsm_clk_string[17]-'0')*10 + (gsm_clk_string[18]-'0'); gsmtimestruct.minute    = (gsm_clk_string[20]-'0')*10 + (gsm_clk_string[21]-'0'); gsmtimestruct.second    = (gsm_clk_string[23]-'0')*10 + (gsm_clk_string[24]-'0'); timezone                = (gsm_clk_string[26]-'0')*10 + (gsm_clk_string[27]-'0'); conezone                = gsm_clk_string[25]; 

i think you'd need this, move char ptr accordingly. in code example below assume first character in gsm_clk_string "

/*                     "+cclk: "2017/04/07, 12:57:43+03""    */ /*  gsm_clk_string[] : 0123456789012345678902134567890123    */  char *ptr;  ptr = gsm_clk_string; ptr += 9;  gsmtimestruct.year = (uint16) ((*ptr - '0') * 1000); ptr++; gsmtimestruct.year += (uint16) ((*ptr - '0') * 100); ptr++; gsmtimestruct.year += (uint16) ((*ptr - '0') * 10); ptr++; gsmtimestruct.year += (uint16) ((*ptr - '0'));  ptr += 2;  gsmtimestruct.month = (uint8) ((*ptr - '0') * 10); ptr++; gsmtimestruct.month += (uint8) (*ptr - '0');  ptr += 2;  gsmtimestruct.day = (uint8) ((*ptr - '0') * 10); ptr++; gsmtimestruct.day += (uint8) (*ptr - '0');  ptr += 3;  gsmtimestruct.hour = (uint8) ((*ptr - '0') * 10); ptr++; gsmtimestruct.hour += (uint8) (*ptr - '0');  ptr += 2;  gsmtimestruct.minute = (uint8) ((*ptr - '0') * 10); ptr++; gsmtimestruct.minute += (uint8) (*ptr - '0');  ptr += 2;  gsmtimestruct.second = (uint8) ((*ptr - '0') * 10); ptr++; gsmtimestruct.second += (uint8) (*ptr - '0');  ptr++;  if ( *ptr == '+' ) {    ptr++;    timezone = (whatever_type_timezone) ((*ptr - '0') * 10);    ptr++    timezone += (whatever_type_timezone) ((*ptr - '0'));     unixtime = (uint32_t) rtc_datetimetounix( gsmtimestruct ) - ( timezone * 3600 );     } else if ( *ptr == '-' ) {    ptr++;    timezone = (whatever_type_timezone) ((*ptr - '0') * 10);    ptr++    timezone += (whatever_type_timezone) ((*ptr - '0'));     unixtime = (uint32_t) rtc_datetimetounix( gsmtimestruct ) + ( timezone * 3600 ); } else {     /* problem */ } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -