Ejemplo n.º 1
0
 struct node *putloop(struct node 
void testcases()
{
	struct node *res1,*ptr1,*ptr2,*res;
	char *ip1,*op;
	int i,check;
	for(i=0;i<2;i++)
	{
		res1=NULL;
		ip1=NULL;
		ip1=NULL;
		ip1=malloc_str(testDB[i].input1);
		ip1=valid_str(ip1);
		if(ip1!=NULL)
		{
			ip1=rem_space(ip1);
			res1=create_list(ip1);
			res1=putloop(res1);
			res1=removeloop(res1);

			
		}
		
		
		
		display(res);
		 
		free(op);
		free(ip1);
		free(res);
		free(output);

	}

}
Ejemplo n.º 2
0
void testcases()
{
	struct node *res,*output;
	char *ip,*op;
	int i,check;
	for(i=0;i<9;i++)
	{
		res=output=NULL;
		ip=op=NULL;
		ip=malloc_str(testDB[i].input);
		
		ip=valid_str(ip);
		//if(ip==NULL)
			//printf("hai\n");
		if(ip!=NULL)
		{
			ip=rem_space(ip);
			res=create_list(ip);
			//display(res);
			res=swap_adj_nodes(res);
		}
		else
		{
			res=NULL;
		}
		op=malloc_str(testDB[i].output);
		output=create_list(op);
		//if(res==NULL)
			
		check=list_cmp(res,output);
		display(res);
		display(output);
		if(check==0)
			printf("passed\n");
		else 
			printf("failed\n");
		free(op);
		free(ip);
		free(res);
		free(output);

	}

}
Ejemplo n.º 3
0
void testcases()
{
	struct node *res;
	char *ip;
	int i,check;
	float res1;
	for(i=0;i<10;i++)
	{
		res=NULL;
		ip=NULL;
		ip=malloc_str(testDB[i].input);
		
		ip=valid_str(ip);
		if(ip!=NULL)
		{
			ip=rem_space(ip);
			res=create_list(ip);
			res=mergesort(res);
			res1=get_median(res);
		}
		else
		{
			res1=0;
		}
			
		check=val_cmp(res1,testDB[i].value);
		//printf("%f\t%f\n",res1,testDB[i].value);
		
		if(check==0)
			printf("passed\n");
		else 
			printf("failed\n");
		
		free(ip);
		free(res);
		

	}

}
Ejemplo n.º 4
0
int init(char *filename)
{
	FILE *fp;
	
	int read, length;
	char *line;
	char *temp;
	char *word1, *word2;
	char *att, *att_value;
	int count1 = 0;
	int count2 = 0;
	int len;
	int count;
	line = NULL;
	len = 0;
	fp = fopen(filename, "r");
	if(!fp) {
	  print_error_and_exit("File not exist.");
	}

	count = 0;
	while ((read = readline(fp, &line, &len)) != -1) {
		length = strlen(line);
		if(length == 0)
			continue;
		//line[length - 1] = '\0';
		//printf("%s\n", line);
		if(line[0] == ';')
			continue;
		word1 = line;
		temp = line;
		word2 = NULL;
		while(*temp != '\0') {
			if(*temp == '=') {
				*temp = '\0';
				word2 = temp + 1;
				break;
			}
			temp++;
		}
					
		if((strcmp(word1, "[init]")) == 0) {
			count1++;
			if(count2 == 1)
				count2++;
			continue;
		}
		
		if((strcmp(word1, "[beacons]")) == 0 ) {
			count2++;
			if(count1 == 1)
				count1++;
			continue;
		}
	
		att = rem_space(word1);
		att_value = rem_space(word2);
		
		///printf("%sTTT%s\n", att, att_value);
		if(strlen(att) == 0)
			continue;

		if(count1 == 1) {
		
			if(( strcmp(att, "port")) == 0) {
				ini.port = atol(att_value);
				count++;
	
			} else if(( strcmp(att, "location")) == 0) {
				ini.location = atoll(att_value);
				count++;
			
			} else if(( strcmp(att, "homedir")) == 0) {
				ini.homedir = (char *)calloc(strlen(att_value), sizeof(char));
				strcpy(ini.homedir, att_value);
				count++;
	
			} else if(( strcmp(att, "logfilename")) == 0) {
				strcpy(ini.logfile, att_value);
					
			} else if(( strcmp(att, "autoshutdown")) == 0) {
				ini.autoshutdown = atoi(att_value);
	
			} else if(( strcmp(att, "ttl")) == 0) {
				ini.ttl = atoi(att_value);
	
			} else if(( strcmp(att, "msglifetime")) == 0) {
				ini.msglifetime = atoi(att_value);
			
			} else if(( strcmp(att, "getmsglifetime")) == 0) {
				ini.getmsglifetime = atoi(att_value);
	
			} else if(( strcmp(att, "initneighbors")) == 0) {
				ini.initneighbor = atoi(att_value);
			
			} else if(( strcmp(att, "jointimeout")) == 0) {
				ini.jointo = atoi(att_value);
	
			} else if(( strcmp(att, "keepalivetimeout")) == 0) {
				ini.keepaliveto = atoi(att_value);
			
			} else if(( strcmp(att, "minneighbors")) == 0) {
				ini.minneighbor = atoi(att_value);
			
			} else if(( strcmp(att, "nocheck")) == 0) {
				ini.nocheck = atoi(att_value);
	
			} else if(( strcmp(att, "cacheprob")) == 0) {
				ini.cacheprob = strtod(att_value, NULL);
			
			} else if(( strcmp(att, "storeprob")) == 0) {
				ini.storeprob = strtod(att_value, NULL);
			
			} else if(( strcmp(att, "neighborstoreprob")) == 0) {
				ini.nstoreprob = strtod(att_value, NULL);
			
			} else if(( strcmp(att, "cachesize")) == 0) {
				ini.cachesize = atol(att_value);
	
			} else if(( strcmp(att, "permsize")) == 0) {
				ini.permsize = atoi(att_value);
	
			} else {
				//printf("bad attribute in the ini file  =%s... ignoring it\n", att);
			}
		}
		if(count2 == 1) {
			if(( strcmp(att,"retry")) == 0) {
				ini.retry = atoi(att_value);
				continue;
			}
			
			temp = att;

			while(*temp != '\0') {
				if(*temp == ':') {
					*temp = '\0';
					att_value = temp + 1;
				}
				temp++;
			}
			
			string str = att;
			ini.beacon_name.push_back(str);	
			ini.beacon_port.push_back(atoi(att_value));
		
		}
		free(line);
	//	free(att);
	}
	unsigned int i;
	char *ip, *myip, hostname[NAME_LEN];
	
	int len_hostname = strlen(ini.homedir);
	if(ini.homedir[len_hostname - 1] != '/') {
		char *new_home_dir;
		new_home_dir = (char *)calloc(len_hostname + 2, sizeof(char));
		fflush(stdout);
		if(new_home_dir == NULL) {
			printf("Memory allocation Failed !!!");
			exit(1);
		}
		strcpy(new_home_dir, ini.homedir);
		if(DEBUG_RC1) printf("old: %s\n", new_home_dir);
		new_home_dir[len_hostname] = '/';
		new_home_dir[len_hostname + 1] = '\0';
		if(DEBUG_RC1) printf("new: %s\n", new_home_dir);
		free(ini.hostname);
		ini.homedir = new_home_dir;
	}
	const char *buffer;
	struct hostent *h;
	gethostname(hostname, NAME_LEN);
	//cout<<hostname;
	ini.hostname = (char *)calloc(sizeof(hostname) + 1, sizeof(char));
	ini.file_path = ini.homedir;
	ini.file_path +="files/";
	strcpy(ini.hostname, hostname);

	ini.node_id = (char *)calloc(sizeof(hostname) + 6, sizeof(char));
	snprintf(ini.node_id, sizeof(hostname) + 6, "%s_%d", ini.hostname, ini.port);
	ini.node_inst_id = (char *)calloc(sizeof(hostname) + 26, sizeof(char));
	snprintf(ini.node_inst_id, sizeof(hostname) + 26, "%s_%ld", ini.node_id, time(NULL));
	
	if((h = gethostbyname(hostname)) == NULL) {
		//perror("Address Request in init()");
		return 1;
	}
	myip = inet_ntoa(*((struct in_addr *)h->h_addr));
	//printf("%s\n", myip);	
	for(i=0; i< ini.beacon_name.size(); i++) {
		buffer = ini.beacon_name[i].c_str();
		if((h = gethostbyname(buffer)) == NULL) {
			//perror("Address Request in init()");
			return 1;
		}
		ip = inet_ntoa(*((struct in_addr *)h->h_addr));
		//printf("%s\n", ip);
		if((strcmp(myip, ip) == 0) && (ini.port == ini.beacon_port[i]))
				return 1; //True for a beacon node
	}


	return 0;
}