示例#1
0
文件: io.c 项目: seeslab/rgraph
/**
@brief Free the memory allocated to binary tree.
*/
void
free_node_tree(void *root)
{
    struct node_t *focal = (struct node_t*) root;
    if (focal != NULL) {
        struct node *leaf = (struct node*) focal->key;
        free_node_tree(focal->left);
        free_node_tree(focal->right);
        free(leaf);
        free(focal);
    }
}
示例#2
0
/* free just the current node and children*/
void free_node(node *n) {
	free(n->str);
	n->str = NULL;

	free_link_data(n->link_data);
	n->link_data = NULL;
	
	if (n->children != NULL) {
		free_node_tree(n->children);
		n->children = NULL;
	}
	free(n);
}
示例#3
0
/* free just the current node and children*/
void free_node(node *n) {
	if (n == NULL)
		return;
	
	if (n->str != NULL)
		free(n->str);
	n->str = NULL;

	free_link_data(n->link_data);
	n->link_data = NULL;
	
	if (n->children != NULL) {
		free_node_tree(n->children);
		n->children = NULL;
	}
	n->next = NULL;
	free(n);
}
示例#4
0
/* print_html_endnotes */
void print_html_endnotes(GString *out, scratch_pad *scratch) {
	int counter = 0;
	int random;
	node *reversed = copy_node_tree(scratch->used_notes);

	reversed = reverse_list(reversed);

	scratch->printing_notes = 1;
	
	node *note = reversed;
#ifdef DEBUG_ON
	fprintf(stderr, "start endnotes\n");
#endif
	
	if ((note == NULL) || ((note->key == KEY_COUNTER) && (note->next == NULL))) {
		free_node_tree(reversed);
		return;
	}

#ifdef DEBUG_ON
	fprintf(stderr, "there are endnotes to print\n");
#endif

	pad(out,2, scratch);
	g_string_append_printf(out, "<div class=\"footnotes\">\n<hr />\n<ol>");
	while ( note != NULL) {
		if (note->key == KEY_COUNTER) {
			note = note->next;
			continue;
		}
		
		counter++;
		pad(out, 1, scratch);
		
		if (scratch->extensions & EXT_RANDOM_FOOT) {
			srand(scratch->random_seed_base + counter);
			random = rand() % 99999 + 1;
		} else {
			random = counter;
		}
		
		if (note->key == CITATIONSOURCE) {
			g_string_append_printf(out, "<li id=\"fn:%d\" class=\"citation\"><span class=\"citekey\" style=\"display:none\">%s</span>", 
				random, note->str);
		} else {
			g_string_append_printf(out, "<li id=\"fn:%d\">\n", random);
		}
		
		
		scratch->padded = 2;
		if ((note->key == NOTESOURCE) || (note->key == GLOSSARYSOURCE))
			scratch->footnote_to_print = counter;
		scratch->footnote_para_counter = tree_contains_key_count(note->children,PARA);
		print_html_node(out, note, scratch);
		pad(out, 1, scratch);
		g_string_append_printf(out, "</li>");
		
		note = note->next;
	}
	pad(out,1, scratch);
	g_string_append_printf(out, "</ol>\n</div>\n");
	scratch->padded = 0;

	free_node_tree(reversed);
#ifdef DEBUG_ON
	fprintf(stderr, "finish endnotes\n");
#endif
}
示例#5
0
文件: io.c 项目: seeslab/rgraph
/**
Read an edge list from a file and convert it to arrays.
@param nodes_in_p,nodes_out_p,weights_p Edge list, nodes id and weight of each edge.
@param labels_p Labels of nodes whose index are in nodes_in/out
@param E_p N_p Number of edges, number of nodes.
@param weighted If True
@param bipartite 0,1,2. If 1 (resp 2), treat the conlumns independently and store only the first (resp 2nd) column labels.
**/
int
EdgeListFileInput(FILE *inFile, int weighted, int bipartite, unsigned int **nodes_in_p,
                  unsigned int **nodes_out_p,double **weights_p,
                  char ***labels_p, unsigned int *E_p, unsigned int *N_p) {

    unsigned int *nodes_in=NULL, *nodes_out=NULL;
    double *weights=NULL;
    char **labels=NULL;

    if (bipartite > 2) return 2;

    int i = 0;

    int noReadItems;
    char *line = NULL;
    size_t bufsiz = 0;
    ssize_t nbytes;

    unsigned int Nmax = 10;
    unsigned int Emax = Nmax*Nmax;

    labels = calloc(Nmax+1,sizeof(char*));
    weights = malloc((Emax+1)*sizeof(double));
    nodes_in = malloc((Emax+1)*sizeof(unsigned int));
    nodes_out = malloc((Emax+1)*sizeof(unsigned int));

    unsigned int N = 0;
    unsigned int N_other_side = 0;
    unsigned int E = 0;

    struct node *nodes_root = NULL;
    struct node *nodes_root_bipartite = NULL;

    struct node *nodeptr = NULL;

    unsigned int noReadItemsExpected = weighted ? 3:2;

    // Go through the input file
    while ((nbytes = getline(&line, &bufsiz, inFile)) != -1) {
        //printf("N=%d/%d, E=%d/%d\n",N,Nmax,E,Emax);

        struct node *node1 = malloc(sizeof(struct node));
        struct node *node2 = malloc(sizeof(struct node));
        struct node *temp = NULL;


        /* Read the labels (and edge weight, if necessary) */
        if (weighted == 0) {
            noReadItems = sscanf(line, "%s %s", node1->label, node2->label);
            weights[E] = 1;
        } else {
            noReadItems = sscanf(line,"%s %s %lf", node1->label, node2->label, &weights[E]);
        }

        if (bipartite>1) {
            temp = node1;
            node1 = node2;
            node2 = temp;
        }

        /* Check input sanity */
        if(noReadItems != noReadItemsExpected) {
            printf ("Failed to read input: not enough fields in line %s (%d!=%d). \n",
                    line, noReadItems,noReadItemsExpected);
            return 1;
        }

        // Check if the first label was encountered already.
        nodeptr = (struct node*) tsearch((void*) node1, (void **)&nodes_root, node_compare);
        struct node *answer = NULL;
        answer = *(struct node **)nodeptr;

        // If the node was not present in the tree, save a new label,
        // Otherwise free the read node to avoid memory leak.
        if (answer==node1) {
            answer->id = N;
            labels[N] = malloc(sizeof(answer->label));
            strcpy(labels[N],answer->label);
            N++;
        } else {
            free(node1);
        }
        nodes_in[E] = answer->id;

        // Likewise for the second label
        struct node *answer2 = NULL;
        if(!bipartite) {
            nodeptr = (struct node*) tsearch((void*) node2, (void **)&nodes_root, node_compare);
            answer2 = *(struct node **)nodeptr;
            if (answer2==node2) {
                answer2->id = N;
                labels[N] = malloc(sizeof(answer2->label));
                strcpy(labels[N],answer2->label);
                N++;
            } else {
                free(node2);
            }
        }
        // For bipartite networks,
        // - Each colums is a different namespace.
        // - the node label of the second column are not saved.
        else {
            nodeptr = (struct node*) tsearch((void*) node2, (void **)&nodes_root_bipartite, node_compare);
            answer2 = *(struct node **)nodeptr;
            if (answer2==node2) {
                answer2->id = N_other_side;
                N_other_side++;
            } else {
                free(node2);
            }
        }
        nodes_out[E] = answer2->id;
        E++;
        // Increase the allocated memory if needed.
        if(N+2>=Nmax) {
            printf("%d > %d, doubling memory for nodes\n",N,Nmax );
            Nmax *= 2;
            labels = realloc(labels, (Nmax+1)*sizeof(char*));
        }
        if(E+1>=Emax) {
            printf("%d > %d, doubling memory for edges",E,Emax );
            Emax *= 2;
            nodes_in = realloc(nodes_in, (Emax+1)*sizeof(unsigned int));
            nodes_out = realloc(nodes_out, (Emax+1)*sizeof(unsigned int));
            weights = realloc(weights, (Emax+1)*sizeof(double));
        }
    }

    // Free unused memory
    nodes_in = realloc(nodes_in, (E+1)*sizeof(unsigned int));
    nodes_out = realloc(nodes_out, (E+1)*sizeof(unsigned int));
    weights = realloc(weights, (E+1)*sizeof(double));
    labels = realloc(labels, (N+1)*sizeof(char*));
    free_node_tree(nodes_root);
    free_node_tree(nodes_root_bipartite);

    // Assign
    *nodes_in_p = nodes_in;
    *nodes_out_p = nodes_out;
    *labels_p = labels;
    *weights_p = weights;
    *E_p = E;
    *N_p = N;
    free(line);

    if (!bipartite)
        printf("Read %d nodes and %d edges \n ", *N_p,*E_p);
    else
        printf("Read %d/%d nodes and %d edges \n ", *N_p,N_other_side,*E_p);
    //
    // for (i=0;i<*E_p;i++){
    //   printf("%d\t [%d] %s \t [%d] %s \t %f\n",i, nodes_in[i],labels[nodes_in[i]],nodes_out[i],labels[nodes_out[i]],weights[i]);
    // }
    // for (i=0;i<*N_p+1;i++){
    //   printf("%d %s\n",i, labels[i]);
    // }
    return 0;
}