I am using a TIVA TM4C to get messages about balance and expiry on a SIM card in a SIM900 cell phone modem.
Below is the code I am using to parse the string. I am concerned because the code relies on spaces being in specific locations. It seems like it would be more reliable if I could parse only for numbers, and based on order it would be simple to determine which was dollars, cents, month, etc.
Is there a better method I could use, to parse just for numbers? The currency isn't as essential.
Here is the code:
char *message = NULL; // Balance and expiry message
char expiry[] = "00/00/00"; // Expiry date
char balance[] = "999.99"; // Remaining balance
char currency[] = "USD"; // Balance currency
char *ptr; // Pointer for parsing balance message
int ctr=0; // For stepping through tokens
// Get the balance/expiry message
message = getMessage();
// The message will always look like this:
// +CUSD: 0,"Your account balance is 49.10 USD and will expire on 04/03/16.",64
// Parse
ptr = strtok (message," ");
while (ptr != NULL){
ptr = strtok (NULL," ");
if (ctr == 4) { strcpy(balance,ptr); }
else if (ctr == 5) { strcpy(currency,ptr); }
else if (ctr == 10) { strncpy(expiry,ptr,8); }
ctr++;
}