예제 #1
0
static int parse_advert_packet (const char *buf, char **port, char **fs2protocol, char **id)
{
    char *fs2protocol_field, *port_or_auto_field, *id_field;


    fs2protocol_field = get_next_field(&buf);
    if (!fs2protocol_field) goto err_fs2proto;
    *fs2protocol = fs2protocol_field;

    /* autoindexnode packets are the clients saying they /can/ become indexnodes
     * if needed. There is also a weight field - an indication of how powerful
     * the machine is - that is used to vote on the client that becomes
     * indexnode. All irrelevant to us. */
    port_or_auto_field = get_next_field(&buf);
    if (!port_or_auto_field) goto err_port_auto;
    if (!strcmp(port_or_auto_field, "autoindexnode")) goto err_id;
    *port = port_or_auto_field;

    id_field = get_next_field(&buf);
    if (!id_field) goto err_id;
    *id = id_field;


    return 0;

err_id:
    free(port_or_auto_field);
err_port_auto:
    free(fs2protocol_field);
err_fs2proto:
    return 1;
}
예제 #2
0
//---------------------------------------------------------------------------
bool read_training_data_from_proben1_format(const char *filename, char list_separator, int num_classes, double **&training_data, double *&target, int &num_training_data, int &num_variables, char* error_message)
{
	
	FILE* f = fopen(filename, "r");
	if (!f) {
		strcpy(error_message, "Cannot find file! Please specify the full path!");
		return false;
	}
	char *buf = new char[10000];
	char * start_buf = buf;
	// count the number of training data and the number of variables
	num_training_data = 0;
	while (fgets(buf, 10000, f)) {
		if (strlen(buf) > 1)
			num_training_data++;
		if (num_training_data == 1) {
			num_variables = 0;

			char tmp_str[10000];
			int size;
			int skip_size;
			bool result = get_next_field(buf, list_separator, tmp_str, size, skip_size);
			while (result) {
				buf = buf + size + 1 + skip_size;
				result = get_next_field(buf, list_separator, tmp_str, size, skip_size);
				num_variables++;
			}
		}
		buf = start_buf;
	}
	delete[] start_buf;
	num_variables--;
	rewind(f);

	num_variables -= num_classes - 1;

	allocate_training_data(training_data, target, num_training_data, num_variables);

	int value;
	for (int i = 0; i < num_training_data; i++) {
		for (int j = 0; j < num_variables; j++)
			fscanf(f, "%lf", &training_data[i][j]);
		for (int j = 0; j < num_classes; j++) {
			int num_read = fscanf(f, "%d", &value);
			if (num_read != 1) {
				strcpy(error_message, "Incorrect format!");
				return false;
			}
			if (value == 1)
			  target[i] = j;
		}
	}
	fclose(f);
	return true;
}
예제 #3
0
파일: pft.c 프로젝트: budlover/441-dhttp
/*
    Add a new obj and the node id to the prefix tree.
    Repeate obj and nid association will not be added twice
 */
int pft_add_obj(char *obj, uint32_t nid)
{
    assert(strlen(obj) <= OBJ_NAME_LEN);
    char buff[OBJ_NAME_LEN + 1];
    int start_idx = 0;

    pf_node_t **curr_lv_rt = &g_obj_pft; // current level root prefix
    pf_node_t *curr_node = *curr_lv_rt;
    pf_node_t *target = NULL;

    while(0 == get_next_field(obj, start_idx, '/', buff, &start_idx))
    {
        // search the current level prefix
        while(curr_node)
        {
            if(!strcmp(buff, curr_node->prefix))
                break;

            curr_node = curr_node->next_peer;
        }
        
        if(!curr_node) // not found
        {
            curr_node = new_node();
            if(!curr_node)
                return -1;

            strcpy(curr_node->prefix, buff);
            curr_node->next_peer = *curr_lv_rt;
            *curr_lv_rt = curr_node;
        }
      
        target = curr_node; 
        curr_lv_rt = &curr_node->next_child;
        curr_node = *curr_lv_rt;
    }

    assert(target != NULL);
   
    // store the nid to the current prefix level node
    if(target->darr == NULL)
    {
        target->darr = new_darr();
    
        if(target->darr == NULL)
            return -1;
    }

    // check repeating before add
    if(-1 == get_elem_idx(target->darr, nid))
    {
        if(-1 == add_elem(target->darr, nid))
            return -1;
    }

    return 0;
}
예제 #4
0
파일: pft.c 프로젝트: budlover/441-dhttp
/*
    Search the prefix tree for an obj, return the array of nodes that contains
    the object, if there is.
    The all_pre param will always be set with a not-NULL value. This array may
    contain all all nodes that claimed an aggregate prefix.
    The last_pre param may be set to point to a array containing nodes with
    the longest aggragate prefix match.

    For example, for a obj '/a/b/obj', if node 1, 2, 3 all claim to have the
    obj and node 1 also claim to have /a/, node 2 claims to have /a/b/.
    Then the darr return will indicate node 1, 2, 3,
    The last_pre will indicate node 2
    All pre will indicate node 1 and 2.
 */
darr_t *pft_get_obj_list(const char *obj, darr_t **last_pre, darr_t **all_pre)
{
    char buff[OBJ_NAME_LEN + 1];
    int start_idx = 0;

    pf_node_t *curr_node = g_obj_pft; 
    pf_node_t *target = NULL;

    static darr_t *al_pre = NULL;
    if(al_pre == NULL)
    {
        al_pre = new_darr(); 
        if(!al_pre)
            return NULL;
    }
    *all_pre = NULL;
    *last_pre = NULL;

    al_pre->curr_elem = 0;

    while(0 == get_next_field(obj, start_idx, '/', buff, &start_idx))
    {
        while(curr_node)
        {
            if(!strcmp(buff, curr_node->prefix))
                break;

            curr_node = curr_node->next_peer;
        }
        
        if(!curr_node) // not found
        {
            break;  // longest prefix match up to now
        }
      
        target = curr_node; 
        curr_node = curr_node->next_child;

        if(target->darr && target->darr->curr_elem != 0)
        {
            merge_darr(al_pre, target->darr);
            *last_pre  = target->darr;
        }
    }

    if(!target)
        return NULL;

    if(al_pre->curr_elem > 0) //remove the match node's last appned id
    {
        al_pre->curr_elem -= target->darr->curr_elem;
    }
    *all_pre = al_pre;

    return target->darr;
}
예제 #5
0
파일: downboy2.c 프로젝트: anl/IT
void read_stat(char *proc_id,override_type *entry,int k,int uid)
{
   char *stat_filename=make_name(proc_id,1);
   int done=0;

   char *field;
   FILE *fp;
   int n; /* current field number */

   char *proc_name;
   unsigned utime;
   int priority;
   int nice;

   if ((fp=fopen(stat_filename,"r"))==NULL)
      fprintf(stderr,"Error: failed to open '%s'!\n",stat_filename);
   else
      {
      for (n=0; !done && (field=get_next_field(fp)); n++)
         switch (n)
            {
            case 1: /* proc name */
               proc_name=alloca(strlen(field)+1);
               strcpy(proc_name,strip_parens(field));
               break;

            case 13: /* utime */
               utime=atol(field)/(float)USER_HZ;
               break;

            case 17: /* priority */
               priority=atoi(field);
               break;

            case 18: /* nice */
               nice=atoi(field);
               done=1; /* end iteration after last interesting field */
               break;
            }

      fclose(fp);
      }

   if (done) /* reached last field OK */
      {
      /* if exceeded defined kill threshold and not an exception... */
      if (k>0 && (!entry || entry->elapsed!=-1) && utime>k)
         kill_proc(atoi(proc_id),uid,k);
      else
         adjust_sched(atoi(proc_id),proc_name,utime,priority,nice,entry);
      }
}
예제 #6
0
파일: notice.c 프로젝트: andersk/zephyr
string get_field(char *data,
		 int length,
		 int num)
{
    /*
     * While num>1 and there are fields left, skip a field & decrement num:
     */
    while (length && num>1) {
	if (!*data)
	  num--;
	length--;
	data++;
    }

    /*
     * If any more fields left, the first field is the one we want.
     * Otherwise, there is no such field as num -- return "".
     */
    if (length)
      return(get_next_field(&data, &length));
    else
      return(string_Copy(""));
}
예제 #7
0
파일: pft.c 프로젝트: budlover/441-dhttp
/* 
   Remove an object associated with a node
 */
void pft_rm_obj(char *obj, uint32_t nid)
{
    char buff[OBJ_NAME_LEN + 1];
    int start_idx = 0;
    size_t idx;

    pf_node_t *curr_node = g_obj_pft; 
    pf_node_t *target = NULL;

    while(0 == get_next_field(obj, start_idx, '/', buff, &start_idx))
    {
        while(curr_node)
        {
            if(!strcmp(buff, curr_node->prefix))
                break;

            curr_node = curr_node->next_peer;
        }
        
        if(!curr_node) // not found
        {
            return ;
        }
      
        target = curr_node; 
        curr_node = curr_node->next_child;
    }

    if(!target)
        return ;

    // check existance before remove
    if(-1 != (idx = get_elem_idx(target->darr, nid)))
    {
        rm_elem_by_idx(target->darr, idx);
    }
}
예제 #8
0
파일: mep_data.cpp 프로젝트: Tsingke/libmep
//-----------------------------------------------------------------
bool t_mep_data::from_csv_double(const char *filename) 
{
	FILE *f = NULL;
#ifdef WIN32
	int count_chars = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
	wchar_t *w_filename = new wchar_t[count_chars];
	MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, count_chars);

	f = _wfopen(w_filename, L"r");

	delete[] w_filename;
#else
	f = fopen(filename, "r");
#endif

	if (!f)
		return false;

	delete_data();

	char *buf = new char[MAX_ROW_CHARS];
	char * start_buf = buf;

	num_data = 0;
	data_type = MEP_DATA_DOUBLE;

	while (my_fgets(buf, MAX_ROW_CHARS, f)) {
		long len = strlen(buf);
		if (len > 1)
			num_data++;
		if (num_data == 1) {
			num_cols = 0;

			char tmp_str[1000];
			int size;
			int skipped;
			bool result = get_next_field(buf, list_separator, tmp_str, size, skipped);
			while (result) {
				num_cols++;
				if (!buf[size])
					break;
				buf = buf + size + skipped;
				result = get_next_field(buf, list_separator, tmp_str, size, skipped);

			}
			buf = start_buf;
		}
	}
	//	num_cols--;
	rewind(f);

	_data_double = new double*[num_data];
	int count_mep_data = 0;

	while (my_fgets(buf, MAX_ROW_CHARS, f)) {
		long len = strlen(buf);
		if (len > 1) {
			int col = 0;
			char tmp_str[MAX_ROW_CHARS];
			int size;
			int skipped;
			_data_double[count_mep_data] = new double[num_cols];
			bool result = get_next_field(buf, list_separator, tmp_str, size, skipped);
			while (result) {
				if (col < num_cols)
					_data_double[count_mep_data][col] = atof(tmp_str);
				else {
					break;
				}
				buf = buf + size + skipped;
				result = get_next_field(buf, list_separator, tmp_str, size, skipped);

				col++;
			}
			count_mep_data++;
		}
		buf = start_buf;
	}
	fclose(f);
	delete[] buf;
	return true;
}
예제 #9
0
파일: mep_data.cpp 프로젝트: Tsingke/libmep
//-----------------------------------------------------------------
bool t_mep_data::from_csv_string(const char *filename) 
{
	FILE *f = NULL;
#ifdef WIN32
	int count_chars = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
	wchar_t *w_filename = new wchar_t[count_chars];
	MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, count_chars);

	f = _wfopen(w_filename, L"r");

	delete[] w_filename;
#else
	f = fopen(filename, "r");
#endif

	if (!f)
		return false;

	delete_data();

	char *buf = new char[MAX_ROW_CHARS];
	char * start_buf = buf;

	data_type = MEP_DATA_STRING;
	num_data = 0;

	while (my_fgets(buf, MAX_ROW_CHARS, f)) {
		long len = strlen(buf);
		if (len > 1)
			num_data++;
		if (num_data == 1) {
			num_cols = 0;
			int skipped;

			char tmp_str[1000];
			int size;
			bool result = get_next_field(buf, list_separator, tmp_str, size, skipped);
			while (result) {
				num_cols++;
				if (!buf[size + skipped])
					break;
				buf = buf + size + skipped;
				result = get_next_field(buf, list_separator, tmp_str, size, skipped);
			}
			buf = start_buf;
		}
	}
	//	num_cols--;
	rewind(f);

	_data_string = new char**[num_data];
	int count_mep_data = 0;
	//has_missing_values = 0;

	while (my_fgets(buf, MAX_ROW_CHARS, f)) {
		long len = strlen(buf);
		if (len > 1) {
			int col = 0;
			char tmp_str[1000];
			int size;
			_data_string[count_mep_data] = new char*[num_cols];
			for (int c = 0; c < num_cols; c++)
				_data_string[count_mep_data][col] = NULL;

			int skipped = 0;
			bool result = get_next_field(buf, list_separator, tmp_str, size, skipped);
			while (result) {
				if (col < num_cols) {
					_data_string[count_mep_data][col] = new char[strlen(tmp_str) + 1];
					strcpy(_data_string[count_mep_data][col], tmp_str);
				}
				else {
					break;
				}
				buf = buf + size + skipped;
				//if (buf - start_buf >= len)
				//break;
				result = get_next_field(buf, list_separator, tmp_str, size, skipped);

				col++;
			}
			count_mep_data++;
		}
		buf = start_buf;
	}
	fclose(f);
	//delete[] start_buf;
	delete[] buf;
	return true;
}
예제 #10
0
파일: mep_data.cpp 프로젝트: Tsingke/libmep
//-----------------------------------------------------------------
bool t_mep_data::from_PROBEN1_format(const char *filename, int num_classes)
{
	FILE *f = NULL;
#ifdef WIN32
	int count_chars = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
	wchar_t *w_filename = new wchar_t[count_chars];
	MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, count_chars);

	f = _wfopen(w_filename, L"r");

	delete[] w_filename;
#else
	f = fopen(filename, "r");
#endif

	if (!f)
		return false;

	delete_data();

	char *buf = new char[MAX_ROW_CHARS];
	char * start_buf = buf;

	num_data = 0;
	data_type = MEP_DATA_DOUBLE;

	while (my_fgets(buf, MAX_ROW_CHARS, f)) {
		long len = strlen(buf);
		if (len > 1)
			num_data++;
		if (num_data == 1) {
			num_cols = 0;

			char tmp_str[1000];
			int size;
			int skipped;
			bool result = get_next_field(buf, ' ', tmp_str, size, skipped);
			while (result) {
				num_cols++;
				if (!buf[size])
					break;
				buf = buf + size + skipped;
				result = get_next_field(buf, ' ', tmp_str, size, skipped);

			}
			buf = start_buf;
		}
	}
	num_cols -= num_classes;
	if (num_classes)
	  num_cols++;

	rewind(f);

	_data_double = new double*[num_data];

	int out_class;
	for (int r = 0; r < num_data; r++) {
		_data_double[r] = new double[num_cols];
		for (int c = 0; c < num_cols - 1; c++)
			fscanf(f, "%lf", &_data_double[r][c]);
		// now scan the outputs
		if (num_classes) // classification problem
		for (int c = 0; c < num_classes; c++) {
			fscanf(f, "%d", &out_class);
			if (out_class == 1)
				_data_double[r][num_cols - 1] = c;
		}
		else// regression problem
			fscanf(f, "%lf", &_data_double[r][num_cols - 1]);
	}

	fclose(f);
	delete[] buf;
	return true;

}
예제 #11
0
파일: mep_data.cpp 프로젝트: Tsingke/libmep
// ---------------------------------------------------------------------------
int t_mep_data::from_xml(pugi::xml_node parent)
{
	clear_data();

	pugi::xml_node node = parent.child("num_data");
	if (node) {
		const char *value_as_cstring = node.child_value();
		num_data = atoi(value_as_cstring);
	}
	else
		num_data = 0;

	node = parent.child("num_variables");
	if (node) {
		const char *value_as_cstring = node.child_value();
		int num_variables = atoi(value_as_cstring);
		if (num_variables)
			num_cols = num_variables + 1;
		else
			num_cols = 0;
	}
	else {
		node = parent.child("num_cols");
		if (node) {
			const char *value_as_cstring = node.child_value();
			num_cols = atoi(value_as_cstring);
		}
		else
			num_cols = 0;
	}

	node = parent.child("num_outputs");
	if (node) {
		const char *value_as_cstring = node.child_value();
		num_outputs = atoi(value_as_cstring);
	}
	else
		num_outputs = 1;

    
    node = parent.child("num_classes");
    if (node) {
        const char *value_as_cstring = node.child_value();
        num_classes = atoi(value_as_cstring);
    }
    else
        num_classes = 0;
    
	node = parent.child("data_type");
	if (node) {
		const char *value_as_cstring = node.child_value();
		data_type = atoi(value_as_cstring);
	}
	else
		data_type = MEP_DATA_DOUBLE;// double by default
	/*
	node = parent.child("has_missing_values");
	if (node) {
	const char *value_as_cstring = node.child_value();
	has_missing_values = atoi(value_as_cstring);
	}
	else
	has_missing_values = 0;// double by default
	*/
	node = parent.child("list_separator");
	if (node) {
		const char *value_as_cstring = node.child_value();
		list_separator = value_as_cstring[0];
	}
	else
		list_separator = ' ';// blank space by default

	if (data_type == MEP_DATA_DOUBLE) {
		if (num_data) {
			_data_double = new double*[num_data];
			for (int r = 0; r < num_data; r++)
				_data_double[r] = new double[num_cols];
		}
	}
	else {
		if (num_data) {
			_data_string = new char**[num_data];
			for (int r = 0; r < num_data; r++) {
				_data_string[r] = new char*[num_cols];
				for (int v = 0; v < num_cols; v++)
					_data_string[r][v] = NULL;
			}
		}
	}

	pugi::xml_node node_data = parent.child("data");
	if (!node_data)
		return true;
	int r = 0;
	if (data_type == MEP_DATA_DOUBLE) {// double
		for (pugi::xml_node row = node_data.child("row"); row; row = row.next_sibling("row"), r++) {
			const char *value_as_cstring = row.child_value();
			int num_jumped_chars = 0;

			for (int c = 0; c < num_cols; c++) {
				sscanf(value_as_cstring + num_jumped_chars, "%lf", &_data_double[r][c]);
				long local_jump = strcspn(value_as_cstring + num_jumped_chars, " ");
				num_jumped_chars += local_jump + 1;
			}
		}
	}
	else {// string
		for (pugi::xml_node row = node_data.child("row"); row; row = row.next_sibling("row"), r++) {
			const char *value_as_cstring = row.child_value();
			char *buf = (char*)value_as_cstring;
			char tmp_str[1000];
			int size;
			int c = 0;
			int skipped;

			bool result = get_next_field(buf, ' ', tmp_str, size, skipped);
			while (result) {
				if (c < num_cols) {
					_data_string[r][c] = new char[strlen(tmp_str) + 1];
					strcpy(_data_string[r][c], tmp_str);
				}
				buf = buf + size + 1 + skipped;
				//if (buf - start_buf >= len)
				//	break;
				result = get_next_field(buf, ' ', tmp_str, size, skipped);

				c++;
			}
		}
	}

	_modified = false;
	return true;
}