Created
October 18, 2012 21:47
-
-
Save mgdm/3914954 to your computer and use it in GitHub Desktop.
Address parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define LCD_ALIAS_REAL "DS18B20.28b8c81d300e5.Temp,DS18B20.2816401d3005f.Temp,DS18B20.28ac871d300e4.Temp" | |
#define LCD_ALIAS_ALIAS "Front_air_intake,Bottom_hose_temp,Top_hose_temp" | |
int main() { | |
char **devices; | |
char *lcd_device_string = strdup(LCD_ALIAS_REAL); | |
char *lcd_alias_string = strdup(LCD_ALIAS_ALIAS); | |
char *deviceToken, *aliasToken, *current, *currentDevice, *currentAlias; | |
int deviceIndex = 0, deviceCount = 1; | |
current = LCD_ALIAS_REAL; | |
while (*(current++) != '\0') { | |
if (*current == ',') deviceCount++; | |
} | |
// Allocate an array of pointers to strings, twice the number of devices | |
// to account for aliases too | |
devices = (char **) calloc(sizeof(char *), 2 * deviceCount); | |
deviceToken = strtok_r(lcd_device_string, ",", ¤tDevice); | |
aliasToken = strtok_r(lcd_alias_string, ",", ¤tAlias); | |
while (deviceToken != NULL) { | |
// Print the current token | |
printf("%s -> %s\n", deviceToken, aliasToken); | |
// Put the device name into the array | |
devices[deviceIndex] = strdup(deviceToken); | |
devices[deviceIndex + 1] = strdup(aliasToken); | |
// Move to the next token, after the , | |
deviceToken = strtok_r(NULL,",", ¤tDevice); | |
aliasToken = strtok_r(NULL, ",", ¤tAlias); | |
// Increment the index - FIXME this should happen after the second list is parsed | |
deviceIndex += 2; | |
} | |
free(lcd_device_string); | |
free(lcd_alias_string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment