Ejemplo n.º 1
0
/** 
 * Recursive node deserialization
 */
GhtErr 
ght_node_read(GhtReader *reader, GhtNode **node)
{
	int i;
	uint8_t attrcount;
	uint8_t childcount;
	uint8_t ghtFlag = 0; //TODO pour l'instant

	GhtHash *hash = NULL;
	GhtNode *n = NULL;
	GhtAttribute *attr = NULL;

	/* Read the hash string */
	ght_hash_read(reader, &hash);
	if ( hash )
	{
		GHT_TRY(ght_node_new_from_hash(hash, &n));
	}
	else
	{
		GHT_TRY(ght_node_new(&n));
	}

	/* Read the attributes */
	ght_read(reader, &attrcount, 1);
	while ( attrcount )
	{
		GHT_TRY(ght_attribute_read(reader, &attr));
		GHT_TRY(ght_node_add_attribute(n, attr));
		attrcount--;
	}

	/* Read the flagGHT */

	ght_read(reader, &ghtFlag, 1);


	/* Read the children */
	ght_read(reader, &childcount, 1);
	/* Set up an exactly sized node list to hold the children */
	if ( childcount > 0 )
	{
		GHT_TRY(ght_nodelist_new(childcount, &(n->children)));
	}
	for ( i = 0; i < childcount; i++ )
	{
		GhtNode *nc = NULL;
		GHT_TRY(ght_node_read(reader, &nc));
		if ( nc )
		{
			GHT_TRY(ght_node_add_child(n, nc));
		}
	}

	*node = n;
	return GHT_OK;
}
Ejemplo n.º 2
0
static GhtErr
ght_node_add_child(GhtNode *parent, GhtNode *child)
{
	if ( ! parent->children )
	{
		ght_nodelist_new(1, &(parent->children));
	}
	return ght_nodelist_add_node(parent->children, child);
}
Ejemplo n.º 3
0
GhtNodeList *
tsv_file_to_node_list(const char *fname, const GhtSchema *schema)
{
    GhtNodeList *nodelist;
    char *ptr_start, *ptr_end, *tmp;
    char *filestr = file_to_str(fname);
    double dblval[16]; /* Only going to handle files 16 columns wide */
    int field_num = 0;

    if ( ! filestr ) return NULL;

    ght_nodelist_new(16, &nodelist);
    
    ptr_start = ptr_end = filestr;
    
    while( 1 ) 
    {
        if ( *ptr_end == '\t' || *ptr_end == '\n' || *ptr_end == '\0' )
        {
            char ptr_tmp = *ptr_end;
            *ptr_end = '\0';
            dblval[field_num] = atof(ptr_start);
            *ptr_end = ptr_tmp;
            ptr_start = ptr_end;
            if ( *ptr_end == '\n' || ! ptr_end ) 
            {
                int i;
                GhtCoordinate coord;
                GhtNode *node;
                
                if ( schema->num_dims != field_num + 1 )
                    return NULL;
                
                coord.x = dblval[0];
                coord.y = dblval[1];
                
                ght_node_new_from_coordinate(&coord, 16, &node);
                
                for ( i = 2; i < schema->num_dims; i++ )
                {
                    GhtAttribute *a;
                    ght_attribute_new_from_double(schema->dims[i], dblval[i], &a);
                    ght_node_add_attribute(node, a);
                }
                
                ght_nodelist_add_node(nodelist, node);
                field_num = 0;
            }
            else field_num++;
            /* All done! */
            if ( *ptr_end == '\0' ) break;
        }
        ptr_end++;
    }
    return nodelist;
}
Ejemplo n.º 4
0
PCPATCH_UNCOMPRESSED *
pc_patch_uncompressed_from_ght(const PCPATCH_GHT *paght)
{
#ifndef HAVE_LIBGHT
	pcerror("%s: libght support is not enabled", __func__);
	return NULL;
#else
	int i, npoints;
	PCPATCH_UNCOMPRESSED *patch;
	PCPOINT point;
	const PCSCHEMA *schema;
	GhtNodeListPtr nodelist;
	GhtCoordinate coord;
	GhtNodePtr node;
	GhtTreePtr tree;
	GhtAttributePtr attr;

	/* Build a structured tree from the tree serialization */
	if ( ! paght || ! paght->ght ) return NULL;
	tree = ght_tree_from_pc_patch(paght);
	if ( ! tree ) return NULL;

	/* Convert tree to nodelist */
	ght_nodelist_new(paght->npoints, &nodelist);
	ght_tree_to_nodelist(tree, nodelist);

	/* Allocate uncompressed patch */
	ght_nodelist_get_num_nodes(nodelist, &npoints);
	schema = paght->schema;
	patch = pcalloc(sizeof(PCPATCH_UNCOMPRESSED));
	patch->type = PC_NONE;
	patch->readonly = PC_FALSE;
	patch->schema = schema;
	patch->npoints = npoints;
	patch->bounds = paght->bounds;
	patch->stats = pc_stats_clone(paght->stats);
	patch->maxpoints = npoints;
	patch->datasize = schema->size * npoints;
	patch->data = pcalloc(patch->datasize);

	/* Set up utility point */
	point.schema = schema;
	point.readonly = PC_FALSE;
	point.data = patch->data;

	/* Process each point... */
	for ( i = 0; i < npoints; i++ )
	{
		double val;

		/* Read and set X and Y */
		ght_nodelist_get_node(nodelist, i, &node);
		ght_node_get_coordinate(node, &coord);
		pc_point_set_x(&point, coord.x);
		pc_point_set_y(&point, coord.y);

		/* Read and set all the attributes */
		ght_node_get_attributes(node, &attr);
		while ( attr )
		{
			GhtDimensionPtr dim;
			const char *name;
			ght_attribute_get_value(attr, &val);
			ght_attribute_get_dimension(attr, &dim);
			ght_dimension_get_name(dim, &name);
			pc_point_set_double_by_name(&point, name, val);
			ght_attribute_get_next(attr, &attr);
		}
		point.data += schema->size;
	}

	/* Done w/ nodelist and tree */
	ght_nodelist_free_deep(nodelist);
	// ght_tree_free(tree);

	/* Done */
	return patch;
#endif
}