コード例 #1
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
HDF_ATTR* hdf_get_attr (HDF *hdf, const char *name)
{
  HDF *obj;
  _walk_hdf(hdf, name, &obj);
  if (obj != NULL) return obj->attr;
  return NULL;
}
コード例 #2
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
HDF* hdf_get_obj (HDF *hdf, const char *name)
{
  HDF *obj;

  _walk_hdf(hdf, name, &obj);
  return obj;
}
コード例 #3
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
HDF* hdf_get_child (HDF *hdf, const char *name)
{
  HDF *obj;
  _walk_hdf(hdf, name, &obj);
  if (obj != NULL) return obj->child;
  return obj;
}
コード例 #4
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
NEOERR* hdf_get_copy (HDF *hdf, const char *name, char **value,
                      const char *defval)
{
  HDF *node;

  if ((_walk_hdf(hdf, name, &node) == 0) && (node->value != NULL))
  {
    *value = strdup(node->value);
    if (*value == NULL)
    {
      return nerr_raise (NERR_NOMEM, "Unable to allocate copy of %s", name);
    }
  }
  else
  {
    if (defval == NULL)
      *value = NULL;
    else
    {
      *value = strdup(defval);
      if (*value == NULL)
      {
	return nerr_raise (NERR_NOMEM, "Unable to allocate copy of %s", name);
      }
    }
  }
  return STATUS_OK;
}
コード例 #5
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
NEOERR* hdf_set_attr (HDF *hdf, const char *name, const char *key,
                      const char *value)
{
  HDF *obj;
  HDF_ATTR *attr, *last;

  _walk_hdf(hdf, name, &obj);
  if (obj == NULL)
    return nerr_raise(NERR_ASSERT, "Unable to set attribute on none existant node");

  if (obj->attr != NULL)
  {
    attr = obj->attr;
    last = attr;
    while (attr != NULL)
    {
      if (!strcmp(attr->key, key))
      {
	if (attr->value) free(attr->value);
	/* a set of NULL deletes the attr */
	if (value == NULL)
	{
	  if (attr == obj->attr)
	    obj->attr = attr->next;
	  else
	    last->next = attr->next;
	  free(attr->key);
	  free(attr);
	  return STATUS_OK;
	}
	attr->value = strdup(value);
	if (attr->value == NULL)
	  return nerr_raise(NERR_NOMEM, "Unable to set attr %s to %s", key, value);
	return STATUS_OK;
      }
      last = attr;
      attr = attr->next;
    }
    last->next = (HDF_ATTR *) calloc(1, sizeof(HDF_ATTR));
    if (last->next == NULL)
      return nerr_raise(NERR_NOMEM, "Unable to set attr %s to %s", key, value);
    attr = last->next;
  }
  else
  {
    if (value == NULL) return STATUS_OK;
    obj->attr = (HDF_ATTR *) calloc(1, sizeof(HDF_ATTR));
    if (obj->attr == NULL)
      return nerr_raise(NERR_NOMEM, "Unable to set attr %s to %s", key, value);
    attr = obj->attr;
  }
  attr->key = strdup(key);
  attr->value = strdup(value);
  if (attr->key == NULL || attr->value == NULL)
    return nerr_raise(NERR_NOMEM, "Unable to set attr %s to %s", key, value);

  return STATUS_OK;
}
コード例 #6
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
NEOERR* hdf_get_node (HDF *hdf, const char *name, HDF **ret)
{
  _walk_hdf(hdf, name, ret);
  if (*ret == NULL)
  {
    return nerr_pass(_set_value (hdf, name, NULL, 0, 1, 0, NULL, ret));
  }
  return STATUS_OK;
}
コード例 #7
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
NEOERR* hdf_set_copy (HDF *hdf, const char *dest, const char *src)
{
  HDF *node;
  if ((_walk_hdf(hdf, src, &node) == 0) && (node->value != NULL))
  {
    return nerr_pass(_set_value (hdf, dest, node->value, 0, 0, 0, NULL, NULL));
  }
  return nerr_raise (NERR_NOT_FOUND, "Unable to find %s", src);
}
コード例 #8
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
/* This should return a const char *, but changing this would have big
 * repurcussions for any C code using this function, so no change for now */
char* hdf_get_value (HDF *hdf, const char *name, const char *defval)
{
  HDF *node;

  if ((_walk_hdf(hdf, name, &node) == 0) && (node->value != NULL))
  {
    return node->value;
  }
  return (char *)defval;
}
コード例 #9
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
HDF* hdf_obj_child (HDF *hdf)
{
  HDF *obj;
  if (hdf == NULL) return NULL;
  if (hdf->link)
  {
    if (_walk_hdf(hdf->top, hdf->value, &obj))
      return NULL;
    return obj->child;
  }
  return hdf->child;
}
コード例 #10
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
NEOERR* hdf_copy (HDF *dest, const char *name, HDF *src)
{
  NEOERR *err;
  HDF *node;

  if (_walk_hdf(dest, name, &node) == -1)
  {
    err = _set_value (dest, name, NULL, 0, 0, 0, NULL, &node);
    if (err) return nerr_pass (err);
  }
  return nerr_pass (_copy_nodes (node, src));
}
コード例 #11
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
char* hdf_obj_value (HDF *hdf)
{
  int count = 0;

  if (hdf == NULL) return NULL;
  while (hdf->link && count < 100)
  {
    if (_walk_hdf (hdf->top, hdf->value, &hdf))
      return NULL;
    count++;
  }
  return hdf->value;
}
コード例 #12
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
int hdf_get_int_value (HDF *hdf, const char *name, int defval)
{
  HDF *node;
  int v;
  char *n;

  if ((_walk_hdf(hdf, name, &node) == 0) && (node->value != NULL))
  {
    v = strtol (node->value, &n, 10);
    if (node->value == n) v = defval;
    return v;
  }
  return defval;
}
コード例 #13
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
char* hdf_get_valuevf (HDF *hdf, const char *namefmt, va_list ap)
{
  HDF *node;
  char *name;

  name = vsprintf_alloc(namefmt, ap);
  if (name == NULL) return NULL;
  if ((_walk_hdf(hdf, name, &node) == 0) && (node->value != NULL))
  {
    free(name);
    return node->value;
  }
  free(name);
  return NULL;
}
コード例 #14
0
ファイル: neo_hdf.c プロジェクト: F4m3uS/hiphop-php
NEOERR* hdf_copy (HDF *dest, const char *name, HDF *src)
{
  NEOERR *err;
  HDF *node;
  HDF_ATTR *attr_copy;

  if (_walk_hdf(dest, name, &node) == -1)
  {
    err = _copy_attr(&attr_copy, src->attr);
    if (err) return nerr_pass(err);
    err = _set_value (dest, name, src->value, 1, 1, src->link, attr_copy,
                      &node);
    if (err) {
      _dealloc_hdf_attr(&attr_copy);
      return nerr_pass(err);
    }
  }
  return nerr_pass (_copy_nodes (node, src));
}
コード例 #15
0
ファイル: neo_hdf.c プロジェクト: bigclean/moc
static int _walk_hdf (HDF *hdf, const char *name, HDF **node)
{
  HDF *parent = NULL;
  HDF *hp = hdf;
  HDF hash_key;
  int x = 0;
  const char *s, *n;
  int r;

  *node = NULL;

  if (hdf == NULL) return -1;
  if (name == NULL || name[0] == '\0')
  {
    *node = hdf;
    return 0;
  }

  if (hdf->link)
  {
    r = _walk_hdf (hdf->top, hdf->value, &hp);
    if (r) return r;
    if (hp)
    {
      parent = hp;
      hp = hp->child;
    }
  }
  else
  {
    parent = hdf;
    hp = hdf->child;
  }
  if (hp == NULL)
  {
    return -1;
  }

  n = name;
  s = strchr (n, '.');
  x = (s == NULL) ? strlen(n) : s - n;

  while (1)
  {
    if (parent && parent->hash)
    {
      hash_key.name = (char *)n;
      hash_key.name_len = x;
      hp = ne_hash_lookup(parent->hash, &hash_key);
    }
    else
    {
      while (hp != NULL)
      {
	if (hp->name && (x == hp->name_len) && !strncmp(hp->name, n, x))
	{
	  break;
	}
	else
	{
	  hp = hp->next;
	}
      }
    }
    if (hp == NULL)
    {
      return -1;
    }
    if (s == NULL) break;

    if (hp->link)
    {
      r = _walk_hdf (hp->top, hp->value, &hp);
      if (r) {
	return r;
      }
      parent = hp;
      hp = hp->child;
    }
    else
    {
      parent = hp;
      hp = hp->child;
    }
    n = s + 1;
    s = strchr (n, '.');
    x = (s == NULL) ? strlen(n) : s - n;
  }
  if (hp->link)
  {
    return _walk_hdf (hp->top, hp->value, node);
  }

  *node = hp;
  return 0;
}