Exemplo n.º 1
0
void nodes::set_nodes(int part_)
{
	if (part_ == 0)
	{
		//type 0 = nodo rojo, 1 nodo azul, 2 estacion espacial
		Node* n1 = add_new_node(2,184,311);
		Node* n2 = add_new_node(0,167,157,n1);
		Node* n3 = add_new_node(0,348,53,n2);
		Node* n4 = add_new_node(0,348,259,n2);
		Node* n5 = add_new_node(0,450,157,n4);
		link_nodes(n5,n3);
		Node* n6 = add_new_node(0,638,107,n5);
		//Node* n7 = add_new_node(0,885,58,n6);
		//Node* n8 = add_new_node(0,845,345,n6);
		//Node* n9 = add_new_node(0,597,542,n8);
		//Node* n10 = add_new_node(0,807,536,n6);
		//Node* n11 = add_new_node(0,475,280,n9);
		Node* n12 = add_new_node(0,426,590,n6);
		Node* n13 = add_new_node(0,278,439,n12);
		Node* n14 = add_new_node(0,76,568,n13);

		//std::vector<CL_Vec2f> res = aStarNodeVersion(n5,n1);

		spaceships_.push_back(spaceship());
		main_station_node_ = n1;
		spaceships_[0].set_current_pos(main_station_node_);
		//spaceships_[0].create_path(n9);
	}
}
Exemplo n.º 2
0
static int parse_LSA(char *LSAs) {
	FILE *fLSA;
	char buf[BUFSIZE];
	MList* tmp;
	node_t* tmp_node;

	char name[NAME_LENGTH];
	int seq_num;
	char neighbors[BUFSIZE];
	char* neighbor;


	fLSA = fopen(LSAs, "r");
	if (fLSA == NULL) return -1;

	while (fgets(buf, BUFSIZE, fLSA)) {
		sscanf(buf, "%s %d %s", name, &seq_num, neighbors);

		tmp = add_new_node(name);
		tmp_node = (node_t*)tmp->data;

		/* make client list if necessary */
		if (!mlist_find(servs, (void*)tmp)) {
			// this is not a server
			if (!startsWith(name, "router")) {
				// this is not a router either, then this is a client for sure
				if (!mlist_find(clits, (void*)tmp)) {
					// do I add this already?
					clits = mlist_append(clits, (void*)tmp);
				}
			}
		}

		if (tmp_node->seq_num >= seq_num) continue; // skip if the seq is not more recent
		tmp_node->seq_num = seq_num;

		mlist_free(tmp_node->neighbors);
		tmp_node->neighbors = NULL;  // free all and re-add all neighbors

		neighbor = strtok(neighbors, ",");
		while (neighbor) {
			tmp = add_new_node(neighbor); // tmp would be a MList* of node_t*
			add_new_neighbor(tmp_node, tmp);
			neighbor = strtok(NULL, ",");
		}
	}
	fclose(fLSA);
	return 0;
}
Exemplo n.º 3
0
/**
 * add_nodes
 *
 * @param op
 * @param n
 */
static void
add_nodes(unsigned int *op, unsigned int n)
{
    unsigned int i, pphandle, drcindex;

    for (i = 0; i < n; i++) {
        pphandle = *op++;
        drcindex = *op++;
        say(DEBUG, "Add node with parent phandle %8.8x and drc index "
            "%8.8x\n", pphandle, drcindex);
        add_new_node(pphandle, drcindex);
    }
}
Exemplo n.º 4
0
struct Node *parse_files (int argc, char **argv)
{
	/* parses all the files in the list passed as argc, argv returning
       a pointer to the resulting node list
     */

	FILE
		*f;

	struct Node
		*node_tree = NULL,
                *current_node = NULL;
//		*t;

	error_count = 0;

	while (argc--)  /* For each requested file */
	{
		if ((f = fopen (*argv, "r")) == NULL)       /* Open the file */
			error (1, NULL, "cannot open file");

		current_file = *argv++;
		line_count = 0;

		get_line (current_line, f);                 /* Get the first line */

		if (*current_pos == ' ')                    /* Should be a nodeid */
			error (1, NULL, "Expected node id");

		do   /* Scan the rest of the file */
			if (*current_pos != ' ')
				current_node = add_new_node (&node_tree);
			else
				parse_line (current_node);
        while (get_line (current_line, f) != -1);

		fclose (f);
	}

	/*	Now go through the list checking that required fields have been
		specified and replacing links which are currently string names
		with the appropriate Node pointer
	 */
        create_all_links(node_tree,node_tree);

	return node_tree;
}
Exemplo n.º 5
0
static int parse_servs(char *servers) {
	FILE *fserv;
	char name[BUFSIZE];
	MList* tmp;
	
	fserv = fopen(servers, "r");
	if (fserv == NULL) return -1;

	while (fgets(name, BUFSIZE, fserv)) {
		//char *this_addr = (char *)malloc(strlen(buf) + 1);
		//strcmp(this_addr, buf);
		name[strlen(name) - 1] = '\0'; //remove ending '\n'
		tmp = add_new_node(name);
		servs = mlist_append(servs, (void*)tmp);
	}
	fclose(fserv);
	return 0;
}
Exemplo n.º 6
0
/** load data from stock file into system struct **/
BOOLEAN load_stock_data(struct ppd_system * system, const char * stock_name)
{
	FILE * stock_file;
	char * token;	
	char * end;	
    char * price_token;	
	char line[MAX_LINE_LEN];
	struct ppd_stock new_stock_item;
	int on_hand = 0;
	
	
	/** open stock file for reading **/
	stock_file = fopen(stock_name, "r");
	
	/** Check if stock file opened correctly **/
	if(stock_file == NULL)
	{
		fprintf(stderr, "Error: stock file failed to open.\n");
		return FALSE;
	}
	
	/** run through stock file line by line and store data in 
	  * ppd_stock struct 
	 **/
	while(fgets(line, MAX_LINE_LEN + EXTRACHARS, stock_file))
	{		
		/** get id from stock file **/
		token = strtok(line, STOCK_DELIM);		
		/** check if id token is valid **/
		if(token == NULL || (strlen(token)) > (IDLEN + EXTRACHARS) || token[FIRSTCHAR] != 'I')
		{
			fprintf(stderr, "Error loading stock file: the id token "
			    "is not valid.\n");
			return FALSE;
		}		
		/** assign id data to new_stock_item struct **/
		strcpy(new_stock_item.id, token);

		
		/** get name from stock file **/
		token = strtok(NULL, STOCK_DELIM);		
		/** check if name is valid **/
		if(token == NULL || (strlen(token)) > (NAMELEN + EXTRACHARS))
		{
			fprintf(stderr, "Error loading stock file: the name token "
			    "is not valid.\n");
			return FALSE;
		}		
		/** assign name data to new_stock_item struct */
		strcpy(new_stock_item.name, token);
		
		
		/** get product description from stock file **/ 
		token = strtok(NULL, STOCK_DELIM);
		/** check if name token is valid **/
		if(token == NULL || (strlen(token)) > (DESCLEN + EXTRACHARS)) 
		{
			fprintf(stderr, "Error loading stock file: the description token "
			    "is not valid.\n");
			return FALSE;
		}
		/** assign description data to new_stock_item struct **/
		strcpy(new_stock_item.desc, token);
		
		
		/** get price token from stock file **/
		price_token = strtok(NULL, STOCK_DELIM);
		/** check if price token exists **/
        if(price_token == NULL)
		{
			fprintf(stderr, "Error loading stock file: the price token "
			    "is not valid.\n");
			return FALSE;
		}		
		
		
		/** get stock on_hand from stock file **/
		token = strtok(NULL, STOCK_DELIM);		
		/** check if on_hand data exists **/
		if(token == NULL)
		{
			fprintf(stderr, "Error loading stock file: the on_hand token "
			    "is not valid.\n");
			return FALSE;
		}
		/** remove \n character from end of string **/
		token[strlen(token)-1] = 0;
		/** convert on_hand data to long **/
		on_hand = strtol(token, &end, 0);		
		/** check if on_hand data is valid **/
		if(*end || on_hand < 0)
		{
			fprintf(stderr, "Error loading stock file: the on_hand token "
			    "is not valid.\n");
			return FALSE;
		}
		/** assign on_hand data to new_stock_item struct **/
		new_stock_item.on_hand = on_hand;		
		
		
		/** check if there is more data in the line, and if there is, 
		  * treat as fatal error 
		 **/
		if((strtok(NULL, STOCK_DELIM) != NULL))
		{
			fprintf(stderr, "Error loading stock file: too many arguments\n");
			return FALSE;
		}
		
		
		/** load the price data into struct at the end to avoid 
		  * interfering with strtok 
		 **/
		switch(load_price_data(&new_stock_item, price_token))
	    {
		    /** return false **/
		    case FALSE:
		    {
				fprintf(stderr, "Error loading stock file: the price token "
		        "is not valid.\n");
		    	return FALSE;
		    }
		    /** continue as normal **/
		    case TRUE:
		    {
		    	;
		    }		
	    }	
		
		/** add new stock item to system item_list **/
		add_new_node(system, new_stock_item);
		
			
	}
	
	fclose(stock_file);
	
	return TRUE;
}
Exemplo n.º 7
0
void process_input(char *input, struct Graph *g){
    int i = 0;
    char name_a[50];
    char name_b[50];
    char type[5];
    sscanf(input, "%s %s %s", type, name_a, name_b);
    if(!strcmp(type,"new")){
        add_new_node(name_a, g);
    }
    else if(!strcmp(type,"link")){
        add_new_link(name_a, name_b, g);
    }
    else if(!strcmp(type,"out")){
        print_outgoing_nodes(name_a, g);
    }
    else if(!strcmp(type,"in")){
        print_incoming_nodes(name_a, g);
    }
    else if(!strcmp(type,"wn")){
        write_data_prompt(name_a, g);
    }
    else if(!strcmp(type,"rn")){
        read_data(name_a, g);
    }
    else if(!strcmp(type, "all")){
        print_graph(g);
    }
    else if(!strcmp(type,"wl")){
        write_link_data_prompt(name_a, name_b, g);
    }
    else if(!strcmp(type,"rl")){
        read_link_data(name_a, name_b, g);
    }
    else if(!strcmp(type, "path")){
        // printf("Hey");
        test_for_path(name_a, name_b, g);
    }
    else if(!strcmp(type, "save")){
        save_to_disk(g, name_a);
    }
    else if(!strcmp(type, "load")){
        read_from_disk(name_a, g);
    }
    else if(!strcmp(type, "getl")){
        get_links(g);
        // all links (node pairs) with X data
    }
    else if(!strcmp(type, "getn")){
        get_nodes(g);
        // all nodes with X data
    }
    else if(!strcmp(type, "add")){
        add_nodes(name_a, name_b, g);
    }
    else if(!strcmp(type, "div")){
        divide_nodes(name_a, name_b, g);
    }
    else if(!strcmp(type, "sub")){
        subtract_nodes(name_a, name_b, g);
    }
    else if(!strcmp(type, "mult")){
        multiply_nodes(name_a, name_b, g);
    }
    else if(!strcmp(type, "cmd")){
        run_command(name_a, g);
    }
    else if(!strcmp(type, "id")){
        long long_val;
        long_val = strtol(name_a, NULL, 10);
        int id = (int) long_val;
        struct Node *node; 
        node = get_node_by_id(id, g);
        if(!strcmp(node->name, "NULL")){
            printf("{ \"error\": \"No node with id %d\" }\n", id);
            free(node->name);
            free(node);
        } else {
            printf("{ \"id: %d, \"name\": \"%s\", \"data\": \"%s\" }\n", id, node->name, node->data);
        }
    }
}