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); }
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; }
/* Recursively build a nodelist from a tree of GhtNodes */ GhtErr ght_node_to_nodelist(const GhtNode *node, GhtNodeList *nodelist, GhtAttribute *attr, GhtHash *hash) { static int hash_array_len = GHT_MAX_HASH_LENGTH+1; GhtHash h[hash_array_len]; GhtAttribute *a; /* Add our part of the hash to the incoming part */ memset(h, 0, hash_array_len); strncpy(h, hash, GHT_MAX_HASH_LENGTH); if ( node->hash ) { /* TODO: limit write to GHT_MAX_HASH_LENGTH - strlen(node->hash) */ strcat(h, node->hash); } /* Make a copy of all the incoming attributes */ GHT_TRY(ght_attribute_union(node->attributes, attr, &a)); /* Recurse down to leaf nodes, copying attributes and passing them down */ if ( node->children && node->children->num_nodes > 0 ) { int i; for ( i = 0; i < node->children->num_nodes; i++ ) { GHT_TRY(ght_node_to_nodelist(node->children->nodes[i], nodelist, a, h)); } ght_attribute_free(a); } /* This is a leaf node, create a new node and add to list */ else { GhtNode *n; GHT_TRY(ght_node_new_from_hash(h, &n)); GHT_TRY(ght_node_add_attribute(n, a)); GHT_TRY(ght_nodelist_add_node(nodelist, n)); } return GHT_OK; }