示例#1
0
int read_data_1d_cols(FILE *fp, int ncols, int *cols, int *nlines, double **data){
	struct line_reader lr;
	size_t len = 0;
	char *line = NULL;
	int j;
	
	int nlinefields = 64;
	double *linefields = (double*)malloc(sizeof(double) * nlinefields);
	
	*nlines = 0;
	lr_init(&lr, fp);
	int nlines_alloc = 0;
	while(NULL != (line = next_line(&lr, &len))){
		if(line[0] == '#'){ continue; }
		line[len] = '\0';
		
		if(*nlines >= nlines_alloc){
			nlines_alloc *= 2;
			if(0 == nlines_alloc){ nlines_alloc = 64; }
			*data = (double*)realloc(*data, sizeof(double) * (ncols) * nlines_alloc);
		}
		
		int nf = 0;
		char *saveptr = NULL;
		char *field;
		field = STRTOK(line, " \t", &saveptr);
		while(NULL != field){
			if(nf >= nlinefields){
				nlinefields *= 2;
				linefields = (double*)realloc(linefields, sizeof(double) * nlinefields);
			}
			linefields[nf] = atof(field);
			++nf;
			field = STRTOK(NULL, " \t", &saveptr);
		}
		
		for(j = 0; j < ncols; ++j){
			if(cols[j] < nf){
				(*data)[j+(*nlines)*ncols] = linefields[cols[j]];
			}else{
				(*data)[j+(*nlines)*ncols] = 0;
			}
		}
		
		(*nlines)++;
	}
	if(!feof(fp)){
		return -1;
	}
	lr_free(&lr);
	free(linefields);
	return 0;
}
示例#2
0
int read_data_1d(FILE *fp, int *ncols, int *nlines, double **data){
	struct line_reader lr;
	size_t len;
	char *line;
	int got_first_line = 0;
	*ncols = 0;
	*nlines = 0;
	lr_init(&lr, fp);
	int nlines_alloc = 0;
	while(NULL != (line = next_line(&lr, &len))){
		if(line[0] == '#'){ continue; }
		line[len] = '\0';
		if(!got_first_line){
			char *saveptr;
			char *field;
			field = STRTOK(line, " \t", &saveptr);
			while(NULL != field){
				(*ncols)++;
				field = STRTOK(NULL, " \t", &saveptr);
			}
			
			nlines_alloc = 256;
			*data = (double*)realloc(*data, sizeof(double) * (*ncols) * nlines_alloc);
			
			got_first_line = 1;
		}
		
		if(*nlines >= nlines_alloc){
			nlines_alloc *= 2;
			*data = (double*)realloc(*data, sizeof(double) * (*ncols) * nlines_alloc);
		}
		
		int nf = 0;
		char *saveptr;
		char *field;
		field = STRTOK(line, " \t", &saveptr);
		while(NULL != field && nf < *ncols){
			(*data)[nf+(*nlines)*(*ncols)] = atof(field);
			++nf;
			field = STRTOK(NULL, " \t", &saveptr);
		}
		(*nlines)++;
	}
	if(!feof(fp)){
		return -1;
	}
	lr_free(&lr);
	return 0;
}
示例#3
0
//PRINT_TYPE LOG_DEBUG =debug_log;
void  split(const char * data,std::vector<std::string>& items,const std::string& sep)
{
	int size = strlen(data)+1;
	char *local_data = (char*)malloc(size);
	memcpy(local_data,data,size-1);
	*(local_data+size-1) = '\0';

	char * save_ptr = NULL;
	char * str      = NULL;
	char * token    = NULL;
	int i = 0;
	for(str=local_data; ;++i,str=NULL){
		token = STRTOK(str,sep.c_str(),&save_ptr);
		if(token == NULL)
			break;
		
		int len = strlen(token);
		char * ptr_token = token;
		int  not_blank_pos = 0;
		SKIP_HEAD_BLANK(ptr_token,not_blank_pos);
		if(len == not_blank_pos){
			//空行
			continue;
		}

		std::string value ;
		value.assign(token);	
		items.push_back(value);		
	}
	
	free(local_data);
	local_data = NULL;
}
示例#4
0
char VerifyIPAddress(char* src, u8 * ip)
{
	int i;
	int tnum;
	char tsrc[50];
	char* tok = tsrc;
	
	strcpy(tsrc,src);
	
	for(i = 0; i < 4; i++)
	{
		tok = STRTOK(tok,".");
		if ( !tok ) return 0;
		if(tok[0] == '0' && tok[1] == 'x')
		{
			if(!ValidATOI(tok+2,0x10,&tnum)) return 0;
		}
		else if(!ValidATOI(tok,10,&tnum)) return 0;

		ip[i] = tnum;
		
		if(tnum < 0 || tnum > 255) return 0;
		tok = NULL;
	}
	return 1;	
}
示例#5
0
LDAP_CALL
ldap_explode_dns( const char *dn )
{
	int	ncomps, maxcomps;
	char	*s, *cpydn;
	char	**rdns;
#ifdef HAVE_STRTOK_R	/* defined in portable.h */
	char	*lasts;
#endif

	if ( dn == NULL ) {
		dn = "";
	}

	if ( (rdns = (char **)NSLDAPI_MALLOC( 8 * sizeof(char *) )) == NULL ) {
		return( NULL );
	}

	maxcomps = 8;
	ncomps = 0;
	cpydn = nsldapi_strdup( (char *)dn );
	for ( s = STRTOK( cpydn, "@.", &lasts ); s != NULL;
	    s = STRTOK( NULL, "@.", &lasts ) ) {
		if ( ncomps == maxcomps ) {
			maxcomps *= 2;
			if ( (rdns = (char **)NSLDAPI_REALLOC( rdns, maxcomps *
			    sizeof(char *) )) == NULL ) {
				NSLDAPI_FREE( cpydn );
				return( NULL );
			}
		}
		rdns[ncomps++] = nsldapi_strdup( s );
	}
	rdns[ncomps] = NULL;
	NSLDAPI_FREE( cpydn );

	return( rdns );
}
示例#6
0
bool CXml::FindElemEx(const char *pszElemPath)
{
	const char* seps = "/"; // 字符串分隔符
	bool returnFlag = true;

	// 检查传入的路径是否为空
	CHECK_POINTER(pszElemPath,false);
    unsigned int iNewStrLen = strlen(pszElemPath) + 1;

	// 创建临时字符串
    char* pszPath = NULL;
    SAFE_NEW_A(pszPath, char, iNewStrLen);
    CHECK_POINTER(pszPath,false);
    memset(pszPath, 0, iNewStrLen);
    char* pszTmpPath = pszPath;

    // 复制字符串
	strncpy(pszTmpPath, pszElemPath, iNewStrLen);
    //strcpy(pszTmpPath,pszElemPath);
    
    if(*pszTmpPath == '/')
    {
        ++pszTmpPath;   //lint !e810
        this->GetRootPos();
        (void)this->IntoElem();
    }
    else
    {
        // 将xml的指针指向根节点
        (void)this->GetBasePos();
    }
    char *saveptr = NULL;
    char* token = STRTOK( pszTmpPath, seps, &saveptr); 
	while( token != NULL )
    {//lint !e525
	   const char* tmpStr = strstr(token,"[");
	   if(NULL != tmpStr)
	   {   
		    // 处理多个同名节点的情况
            int index = atoi(tmpStr + 1);
			std::string  strTemp(token,static_cast<unsigned int>(tmpStr - token));
			if(!this->FindElem(strTemp.c_str()))
			{
				returnFlag = false;
				break;
			}

			if(!this->NextElemEx(index))
			{
				returnFlag = false;
				break;
			}
	   }
	   else
	   {
		   // 处理单节点的情况
		   if(!this->FindElem(token))
		   {
			   returnFlag = false;
			   break;
		   }
	   }

	   // 进入该节点
       (void)this->IntoElem();
	  // Get next token: 
	  token = STRTOK( NULL, seps, &saveptr); 
    }
    SAFE_DELETE_A(pszPath);//lint !e774 !e1775
    return returnFlag;//lint !e438
}
示例#7
0
bool CXml::MkAndFindElemEx(const char *pszElemPath)
{
	// 创建临时变量
	
	char* token;
	const char* seps = "/"; // 字符串分隔符
	bool returnFlag = true;

	// 检查传入的路径是否为空
	CHECK_POINTER(pszElemPath,true);

	// 将xml的指针指向根节点
	(void)this->GetBasePos();

	// 创建临时字符串
    unsigned int iNewStrLen = strlen(pszElemPath) + 1;
    char* pszPath = NULL;
    SAFE_NEW_A(pszPath, char, iNewStrLen);
    CHECK_POINTER(pszPath, false);
    memset(pszPath, 0, iNewStrLen);
    char* pszTmpPath = pszPath;    

	// 复制字符串
	strncpy(pszTmpPath, pszElemPath, iNewStrLen);
	//strcpy(pszTmpPath,pszElemPath);
    char *saveptr = NULL;
	token = STRTOK( pszTmpPath, seps, &saveptr);
	std::string pszParentNode = "";
	while( token != NULL )
	{
		// 当前指针指向的节点名称
		const char* curNodeName = this->GetElem();
		char* pszTmpNode = strstr(token,"[");
		if(NULL != pszTmpNode)
		{   
			// 处理多个同名节点的情况
			int index = atoi(pszTmpNode + 1);
			std::string  strTemp(token,static_cast<unsigned int>(pszTmpNode - token));
            if(NULL == curNodeName)
            {
                 (void)this->AddElem(strTemp.c_str());
            }
			if(!this->FindElem(strTemp.c_str()))
			{
				returnFlag = false;
				break;

				//// 如果发现没有此节点则创建该节点
    //            if(0 != strcmp(curNodeName,pszParentNode.c_str()))//lint !e527
				//{
    //                 (void)this->AddElem(strTemp.c_str());
				//}else
				//{
    //                 (void)this->AddChildElem(strTemp.c_str());
				//}
    //            (void)this->FindElem(strTemp.c_str());
			}

			if(!this->MkAndNextElemEx(index))
			{
				returnFlag = false;
				break;
			}
			// 记录当前节点为上一节点的父节点
		    pszParentNode = strTemp;
		}
		else
		{
            if(NULL == curNodeName)
            {
                (void)this->AddElem(token);
            }
			// 处理单节点的情况
			if(!this->FindElem(token))
			{
                if (NULL != curNodeName)
                {
				    if(0 != strcmp(curNodeName,pszParentNode.c_str()))
				    {
                         (void)this->AddElem(token);
				    }else
				    {
                         (void)this->AddChildElem(token);
				    }
                }
                (void)this->FindElem(token);
			}
			// 记录当前节点为上一节点的父节点
		    pszParentNode = token;
		}

		// 进入该节点
		(void)this->IntoElem();

		// Get next token: 
		token = STRTOK( NULL, seps, &saveptr ); 
	}
    SAFE_DELETE_A(pszPath);//lint !e424 !e774 !e1775
    return returnFlag;//lint !e438
  //return false;
}