Jump to content

Search the Community

Showing results for tags 'Parsing'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • News
    • Announcements
    • Suggestions
    • New users say Hi!
  • Spotlight!
    • Sponsor Spotlight
    • Sponsor Giveaways
  • Energia
    • Energia - MSP
    • Energia - TivaC/CC3XXX
    • Energia - C2000
    • Energia Libraries
  • MSP Technical Forums
    • General
    • Compilers and IDEs
    • Development Kits
    • Programmers and Debuggers
    • Code vault
    • Projects
    • Booster Packs
    • Energia
  • Tiva-C, Hercules, CCXXXX ARM Technical Forums
    • General
    • SensorTag
    • Tiva-C, Hercules, CC3XXX Launchpad Booster Packs
    • Code Vault
    • Projects
    • Compilers and IDEs
    • Development Kits and Custom Boards
  • Beagle ARM Cortex A8 Technical Forums
    • General
    • Code Snippets and Scripts
    • Cases, Capes and Plugin Boards
    • Projects
  • General Electronics Forum
    • General Electronics
    • Other Microcontrollers
  • Connect
    • Embedded Systems/Test Equipment Deals
    • Buy, Trade and Sell
    • The 43oh Store
    • Community Projects
    • Fireside Chat
  • C2000 Technical Forums
    • General
    • Development Kits
    • Code Vault
    • Projects
    • BoosterPacks

Calendars

There are no results to display.


Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Website URL


Location


Interests


Sparkfun


Github

Found 1 result

  1. I'm attempting to parse a byte/char array that contains the following data cmd=led&color=red&state=on This data is made available to me in a callback function whose signature looks like this. void callback(char* topic, byte* payload, unsigned int length) { The payload parameter is the one that holds the data I'm interested in. I've tried various ways to parse this string and I've been unsuccessful (I mean besides shear brute force and tons of memory allocations). I eventually resorted with coding a test harness using Visual Studio and C/C++ and I get it where it works but then when I move it to Energia/Code Composer Studio it doesn't work. Also, I'm not sure if what I have is the right/best way to do this. Here is my C/C++ code that works in Visual Studio. Known issues 1. payload variable is declared as a char* while the actual payload parameter is a byte* so I'm not sure how to get past that. I'm guess I can cast the byte* to a char*? 2. I know the strtok function modifies the original. So I need to use something else. Not sure what #include "stdafx.h" #include <string.h> #include <stdio.h> enum Command { Led, Motor, Servo, Ligth }; enum LedColor { red, green, blue }; enum LedState { on, off }; typedef struct RemoteCommand { Command command; LedColor ledColor; LedState ledState; }; char* getValuePart(char* str); bool startsWith(const char *pre, const char *str); int main() { const char parameterDelimiter[2] = "&"; char payload[] = "cmd=led&color=red&state=off"; RemoteCommand remoteCommand; char *token = strtok(payload, parameterDelimiter); if (startsWith(token, "cmd")) { char* separator = strchr(token, '='); if (separator != NULL) { ++separator; } // Parse the LED Command if (strstr(token, "led") != NULL) { remoteCommand.command = Command::Led; // Parse the Color attribute token = strtok(NULL, parameterDelimiter); if (startsWith(token, "color")) { if (strstr(token, "red") != NULL) remoteCommand.ledColor = LedColor::red; else if (strstr(token, "green") != NULL) remoteCommand.ledColor = LedColor::green; else if (strstr(token, "blue") != NULL) remoteCommand.ledColor = LedColor::blue; } // Parse the State attribute token = strtok(NULL, parameterDelimiter); if (startsWith(token, "state")) { if (strstr(token, "on") != NULL) remoteCommand.ledState = LedState::on; else if (strstr(token, "off") != NULL) remoteCommand.ledState = LedState::off; } } } return(0); } char* getValuePart(char* str) { char* color = strchr(str, '='); if (color != NULL) { ++color; } return color; } bool startsWith(const char *str, const char *pre) { return strncmp(pre, str, strlen(pre)) == 0; } Any suggestions to improve this code (less memory allocation, more efficient etc.) are most welcome.
×
×
  • Create New...