All pastes #594656 Raw Edit

Something

public text v1 · immutable
#594656 ·published 2007-06-29 01:11 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

parse_config(const char *filename)
{
  char buffer[4096];
  char * line = NULL;

  FILE * file = fopen(filename, "r");
  if (file == NULL) {
    perror(filename);
    exit(1);
  }

  for (line = fgets(buffer, 4096, file); line != NULL; line = fgets(buffer, 4096, file)) {
    // skip spaces and read the key
    line += strspn(line, " \t");
    char * key = strsep(&line, " \t=");
    if (line == NULL) continue;

    // skip spaces and read the value
    line += strspn(line, " \t=");
    char * value = strsep(&line, " \t\n");
    if (line == NULL) continue;

    printf("k = %s and v = %s\n", key, value);
  }   
}

void usage(const char * cmd)
{
  printf("Usage: %s <config file>\n", cmd);
  exit(1);
}

int main(int argc, char **argv)
{
  if (argc != 2)
    usage(argv[0]);

  parse_config(argv[1]);

  return 0;
}