Пример #1
0
Файл: main.c Проект: wuwx/simba
static int test_set(struct harness_t *harness_p)
{
    struct configfile_t configfile;
    char buf[68];

    memset(buf, '\0', sizeof(buf));
    BTASSERT(configfile_init(&configfile, buf, sizeof(buf)) == 0);

#if 0

    /* Set the value of property 'milk' in section 'shopping list'. */
    BTASSERT(configfile_set(&configfile, "shopping list", "milk", "2") == 0);

    /* Set the value of property 'cheese' in section 'shopping
       list'. */
    BTASSERT(configfile_set(&configfile, "shopping list", "cheese", "brie") == 0);

    /* Set the value of property 'skirt' in section 'clothes'. */
    BTASSERT(configfile_set(&configfile, "clothes", "skirt", "1") == 0);

    /* No room left in the buffer for another property. */
    BTASSERT(configfile_set(&configfile, "clothes", "pants", "2") == -1);

    BTASSERT(memcmp(buf,
                    "[shopping list]\r\n"
                    "milk: 2\r\n"
                    "cheese: brie\r\n"
                    "[clothes]\r\n"
                    "skirt: 1\r\n",
                    66) == 0);

#endif

    return (0);
}
Пример #2
0
g_error set_param_str(const char *section, const char *key,
		      const char *value) {
  return configfile_set(configfile_makesection(section),key,value);
}
Пример #3
0
g_error sub_configfile_parse(const char *filename, struct cfg_section **section) {
  FILE *f;
  char line[LINESIZE];
  char *p,*q;

  f = fopen(filename,"r");
  if (!f)
    return mkerror(PG_ERRT_IO,37);      /* Can't open config file */

  while (fgets(line,LINESIZE,f)) {
    p = line;

    /* Chomp up all the leading whitespace */
    p = strip_head(p);
    
    /* Skip blank lines and comments */
    if ((!*p) || *p=='#')
      continue;

    /* Process section lines */
    if (*p=='[') {
      /* Skip the open bracket */
      p++;
      /* Stick a null in place of the close bracket */
      q = strchr(p,']');
      if (!q)
	return mkerror(PG_ERRT_BADPARAM,38); /* Missing ']' */
      *q = 0;
      *section = configfile_makesection(p);
      continue;
    }

    if (!*section)
      return mkerror(PG_ERRT_BADPARAM,39);   /* Undefined section */

    /* Want to source another file ? */
    if (p[0]=='.' && (p[1]==' '||p[1]=='\t')) {
      g_error err;
      q = p+2;
      q = strip_head(q);
      strip_tail(q);
      err = sub_configfile_parse(q, section);
      if (iserror(err))
	return err;
      else continue;
    }

    /* If we got this far, it's a key/value line to a defined section */

    /* Get a pointer to the key in p and a pointer to the value in q */
    q = strchr(p,'=');
    if (!q) {
      /* Missing "=", assume the value is boolean and it's being defined as 1 */
      q = "1";
    }
    else {
      *q = 0;
      q++;
    }

    /* Cut leading whitespace from q */
    q = strip_head(q);

    /* Chop off trailing whitespace from p and q */
    strip_tail(p);
    strip_tail(q);

    /* Set the parameter! */
    configfile_set(*section,p,q);
  }

  fclose(f);
  return success;
}