Skip to content

Instantly share code, notes, and snippets.

@mgdm
Created October 18, 2012 21:47
Show Gist options
  • Save mgdm/3914954 to your computer and use it in GitHub Desktop.
Save mgdm/3914954 to your computer and use it in GitHub Desktop.
Address parser
#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, ",", &currentDevice);
aliasToken = strtok_r(lcd_alias_string, ",", &currentAlias);
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,",", &currentDevice);
aliasToken = strtok_r(NULL, ",", &currentAlias);
// 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