当前位置: 首页>>代码示例>>C++>>正文


C++ bsearch函数代码示例

本文整理汇总了C++中bsearch函数的典型用法代码示例。如果您正苦于以下问题:C++ bsearch函数的具体用法?C++ bsearch怎么用?C++ bsearch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了bsearch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Tdi1GetDbi

int Tdi1GetDbi(int opcode, int narg, struct descriptor *list[],
               struct descriptor_xd *out_ptr)
{
    int status = 1;
    struct descriptor_d string = { 0, DTYPE_T, CLASS_D, 0 };
    struct descriptor_xd tmp = EMPTY_XD;
    struct item *key_ptr = 0;
    int index;
    DBI_ITM lst[] = { {sizeof(index), DbiINDEX, 0, 0}
    , EOL, EOL };
    lst[0].pointer = (unsigned char *)&index;
        /**********************
        String of item to find.
        **********************/
    status = TdiData(list[0], &tmp MDS_END_ARG);
    if (status & 1)
        status = TdiUpcase(&tmp, &string MDS_END_ARG);
    if (status & 1) {
        key_ptr =
            (struct item *)bsearch(&string, table, numtab, siztab,
                                   (int (*)(const void *, const void *))
                                   compare);
        if (key_ptr == 0)
            status = TdiBAD_INDEX;
    }
    StrFree1Dx(&string);
    MdsFree1Dx(&tmp, NULL);
        /**********************************
        Somebody might want others in pool.
        **********************************/
    if (status & 1 && narg > 1)
        status = TdiGetLong(list[1], &index);
    else
        index = 0;
        /***********************
        Get the item asked for.
        Fixed length or varying.
        ***********************/
    if (status & 1) {
        lst[1].code = key_ptr->item_code;
        if ((lst[1].buffer_length = key_ptr->item_length) != 0) {
            status =
                MdsGet1DxS((unsigned short *)&lst[1].buffer_length,
                           &key_ptr->item_dtype, out_ptr);
            if (status & 1) {
                lst[1].pointer = (unsigned char *)out_ptr->pointer->pointer;
                status = TreeGetDbi(lst);
            }
        } else {
            lst[1].buffer_length = 0;
            lst[1].pointer = NULL;
            status = TreeGetDbi(lst);
            if (status & 1) {
                struct descriptor ans = { 0, DTYPE_T, CLASS_S, 0 };
                if (lst[1].pointer) {
                    ans.length = strlen((char *)lst[1].pointer);
                    ans.pointer = (char *)lst[1].pointer;
                }
                status = MdsCopyDxXd(&ans, out_ptr);
                if (ans.pointer)
                    TreeFree(ans.pointer);
            }
        }
    }
    return status;
}
开发者ID:LucyScott,项目名称:mdsplus,代码行数:66,代码来源:TdiGetDbi.c

示例2: cmp_procname

	const char *name;
	void	(*proc)();
};

static const struct pspgl_proc proctable[] = {
#include "pspgl_proctable.h"
};
#define NPROC	(sizeof(proctable)/sizeof(proctable[0]))

static int cmp_procname(const void *a, const void *b)
{
	const char *name = a;
	const struct pspgl_proc *entry = b;

	return strcmp(name, entry->name);
}

GLAPI void (* APIENTRY eglGetProcAddress (const char *procname))()
{
	struct pspgl_proc *entry;

	entry = bsearch(procname, proctable, NPROC, sizeof(struct pspgl_proc),
			cmp_procname);

	if (entry != NULL)
		return entry->proc;

	return NULL;
}

开发者ID:Bracket-,项目名称:psp-ports,代码行数:29,代码来源:eglGetProcAddress.c

示例3: pax_options


//.........这里部分代码省略.........
			break;
		case 't':
			/*
			 * preserve access time on file system nodes we read
			 */
			tflag = 1;
			flg |= TF;
			break;
		case 'u':
			/*
			 * ignore those older files
			 */
			uflag = 1;
			flg |= UF;
			break;
		case 'v':
			/*
			 * verbose operation mode
			 */
			vflag = 1;
			flg |= VF;
			break;
		case 'w':
			/*
			 * write an archive
			 */
			flg |= WF;
			break;
		case 'x':
			/*
			 * specify an archive format on write
			 */
			tmp.name = optarg;
			if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
				flg |= XF;
				break;
			}
			paxwarn(1, "Unknown -x format: %s", optarg);
			(void)fputs("pax: Known -x formats are:", stderr);
			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
				(void)fprintf(stderr, " %s", fsub[i].name);
			(void)fputs("\n\n", stderr);
			pax_usage();
			break;
		case 'z':
			/*
			 * use gzip.  Non standard option.
			 */
			gzip_program = GZIP_CMD;
			break;
		case 'B':
			/*
			 * non-standard option on number of bytes written on a
			 * single archive volume.
			 */
			if ((wrlimit = str_offt(optarg)) <= 0) {
				paxwarn(1, "Invalid write limit %s", optarg);
				pax_usage();
			}
			if (wrlimit % BLKMULT) {
				paxwarn(1, "Write limit is not a %d byte multiple",
				    BLKMULT);
				pax_usage();
			}
			flg |= CBF;
开发者ID:jpostel,项目名称:FreeBSD-mirror,代码行数:67,代码来源:options.c

示例4: populateVars

static void populateVars(Context ctx,const char *key,void *value,int type,int elems){
    int ret,ncgid,cnt;
    DimRef dr;
    double *pdDepth=NULL;
    char *pstrSource=NULL;
    size_t dep,*basei=NULL,*endi=NULL;
    size_t i=0;
    if(ctx.ncid<=0)return;
    if(type==LUA_TTABLE){//any other type at this level will be disgarded
	if(NC_NOERR!=(ret=nc_def_grp(ctx.ncid,key,&ncgid)))
	    nc_error(ret,__LINE__);
	ctx.ncid=ncgid;
	ctx.pfunCallback=populateAttrs;
	loopTable(&ctx,NULL,0);
	if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_FEATURE_TYPE_INSTANCE,1,&(dr.dim1[0]))))
	    nc_error(ret,__LINE__);
	if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_TIME,NC_UNLIMITED,&(dr.dim2[1]))))
	    nc_error(ret,__LINE__);
	pdDepth=(double*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_Z,DEPTH_LEVEL);
	if(pdDepth){
	    dep=(size_t)*pdDepth;
            if(NC_NOERR!=(ret=nc_def_dim(ctx.ncid,VAR_Z,dep,&(dr.dim3[2]))))
	        nc_error(ret,__LINE__);
	    free(pdDepth);
	}
	dr.dim2[0]=dr.dim3[0]=dr.dim1[0];
	dr.dim1t[0]=dr.dim3[1]=dr.dim2[1];
	dr.dim1z[0]=dr.dim3[2];
	ctx.pfunCallback=defineVars;
	ctx.param=(void*)&dr;
	loopTable(&ctx,LUA_VARS_ROOT,0);
	pstrSource=(char*)queryTableValue(ctx.L,LUA_VARS_ROOT,NULL,DATA_SOURCE);
	if(pstrSource&&dep>0){
	    pdDepth=(double*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,OBS_COUNT);
	    if(dep>1){
		basei=(size_t*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,TS_MIN);
		endi=(size_t*)queryTableValue(ctx.L,LUA_VARS_ROOT,VAR_META,TS_MAX);
	    }
	    if(pdDepth){//dpDepth represents tscount here
		cnt=(int)(*pdDepth)*dep;
		free(pdDepth);
		ctx.obs=(double*)malloc(sizeof(double)*cnt);
		ctx.time=(double*)malloc(sizeof(double)*cnt);
		FILE *f=fopen(pstrSource,"r");
    		if(f){
		    double f1,f2,f3;
		    int ret=2;
		    if(1==dep){
		        while(!feof(f)&&ret==2&&cnt--){
			    ret=fscanf(f,"%lf,%lf",&f1,&f2);
	    		    ctx.time[i]=f1;
			    ctx.obs[i++]=f2;
			}
		    }else{
			if(basei&&endi&&*endi>=*basei){
			    double *pdKey=(double*)malloc(sizeof(double)*(*pdDepth));
			    ctx.height=(double*)malloc(sizeof(double)*cnt);
			    if(populateArray(ctx.L,VAR_Z,DEPTHS,LUA_TNUMBER,ctx.height,cnt)){
				qsort(ctx.height,dep,sizeof(double),compareDoubles);
				for(;i<cnt;++i)
				    *(ctx.obs+i)=ctx.missingval;
			        while(!feof(f)&&ret==2&&cnt--){
			            ret=fscanf(f,"%lf,%lf,%lf",&f1,&f2,&f3);
				    bsearch(f3,ctx.height,dep,sizeof(double),compareDoubles);
	    		            ctx.time[i]=f1;
			            ctx.obs[i++]=f2;
			        }
			    }
			    else{
				free(ctx.height,dep,sizeof(double),);
			  	ctx.height=NULL;
			    }
			    if(pdKey)free(pdKey);
			}
			else{
			    free(ctx.obs);
			    free(ctx.time);
			    ctx.obs=ctx.time=NULL;
			}
			
		    }
		    fclose(f);
		    ctx.pfunCallback=putVars;
		    ctx.len=i;
	            loopTable(&ctx,LUA_VARS_ROOT,0);
    		}
		if(basei)free(basei);
		if(endi)free(endi);
		if(ctx.height)free(ctx.height);
		if(ctx.time)free(ctx.time);
		if(ctx.obs)free(ctx.obs);
		free(pstrSource);
	    }
开发者ID:glos,项目名称:glos_server_script,代码行数:93,代码来源:test.c

示例5: grit_xp_gbfs


//.........这里部分代码省略.........
	// --- create header and finish entries ---
	// Create basic header
	GBFS_FILE gbhdr;
	memcpy(gbhdr.magic, GBFS_magic, sizeof(GBFS_magic));
	gbhdr.dir_off= GBFL_SIZE;

	char name[MAXPATHLEN];
	strcpy(name, gr->dstPath);
	path_fix_sep(name);

	lprintf(LOG_STATUS, "Export to GBFS: %s into %s .\n", 
		gr->symName, name);


	FILE *fout= fopen("gbfs.tmp", "wb");
	if(fout == NULL)
		return false;

	u32 fpos= GBFL_SIZE + gr_count*GBEN_SIZE;
	fseek(fout, fpos, SEEK_SET);	// skip directory

	// Insert mode
	// Replace old with same name; insert new
	if(gr->bAppend)
	{
		FILE *fin= fopen(name, "rb");
		do
		{
			if(fin == NULL)
				break;

			GBFS_FILE oldhdr;
			// read and check file's header
			if(fread(&oldhdr, GBFL_SIZE, 1, fin) < 1)
			{	break;	}
			if(memcmp(oldhdr.magic, GBFS_magic, 16))
				break;

			// if we're here, we have a valid GBFS

			int old_count= oldhdr.dir_nmemb;
			gbenD= (GBFS_ENTRY*)malloc(old_count*GBEN_SIZE);
			if(gbenD==NULL)
				break;

			// Read directory
			fread(gbenD, GBEN_SIZE, old_count, fin);
			// check for obsoletes
			for(ii=0; ii<gr_count; ii++)
			{
				gbenL= (GBFS_ENTRY*)bsearch(&gr_gben[ii], gbenD, 
					old_count, GBEN_SIZE, gbfs_namecmp);
				if(gbenL == NULL)
					continue;
				// Obsolete found; remove from list;
				jj= (gbenL-gbenD);
				old_count--;
				memmove(gbenL, gbenL+1, (old_count-jj)*GBEN_SIZE);
			}
			// NOTE: old_count may have been modified
			gb_count= gr_count+old_count;

			// obsoletes are removed; write the rest to fout
			BYTE buf[1024];
			fpos= GBFL_SIZE + gb_count*GBEN_SIZE;
			fseek(fout, fpos, SEEK_SET);

			for(ii=0; ii<old_count; ii++)
			{
				fseek(fin, gbenD[ii].data_offset, SEEK_SET);
				gbenD[ii].data_offset= fpos;
				
				jj= gbenD[ii].len >> 10;
				while(jj--)
				{
					fread(buf, 1024, 1, fin);
					fwrite(buf, 1024, 1, fout);
				}
				if((jj= gbenD[ii].len & 1023) != 0)
				{
					fread(buf, jj, 1, fin);
					fwrite(buf, jj, 1, fout);
				}

				// pad to 16byte boundary
				for(fpos=ftell(fout); fpos & 0x0F; fpos++)
					fputc(0, fout);
			}

			// Combine lists
			gbenL= (GBFS_ENTRY*)malloc(gb_count*GBEN_SIZE);
			memcpy(gbenL, gr_gben, gr_count*GBEN_SIZE);
			memcpy(&gbenL[gr_count], gbenD, old_count*GBEN_SIZE);
			free(gbenD);
			gbenD= gbenL;
		} while(0);

		if(fin)
			fclose(fin);
	}
开发者ID:alvasnaedis,项目名称:grit,代码行数:101,代码来源:grit_xp.cpp

示例6: parray_bsearch

void *
parray_bsearch(parray *array, const void *key, int(*compare)(const void *, const void *))
{
    return bsearch(&key, array->data, array->used, sizeof(void *), compare);
}
开发者ID:stalkerg,项目名称:pg_arman,代码行数:5,代码来源:parray.c

示例7: NativeEnumerated_encode_uper

asn_enc_rval_t
NativeEnumerated_encode_uper(asn_TYPE_descriptor_t *td,
	asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
	asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
	asn_enc_rval_t er;
	long native, value;
	asn_per_constraint_t *ct;
	int inext = 0;
	asn_INTEGER_enum_map_t key;
	asn_INTEGER_enum_map_t *kf;

	if(!sptr) _ASN_ENCODE_FAILED;
	if(!specs) _ASN_ENCODE_FAILED;

	if(constraints) ct = &constraints->value;
	else if(td->per_constraints) ct = &td->per_constraints->value;
	else _ASN_ENCODE_FAILED;	/* Mandatory! */

	ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);

	er.encoded = 0;

	native = *(long *)sptr;
	if(native < 0) _ASN_ENCODE_FAILED;

	key.nat_value = native;
	kf = bsearch(&key, specs->value2enum, specs->map_count,
		sizeof(key), NativeEnumerated__compar_value2enum);
	if(!kf) {
		ASN_DEBUG("No element corresponds to %ld", native);
		_ASN_ENCODE_FAILED;
	}
	value = kf - specs->value2enum;

	if(ct->range_bits >= 0) {
		int cmpWith = specs->extension
				? specs->extension - 1 : specs->map_count;
		if(value >= cmpWith)
			inext = 1;
	}
	if(ct->flags & APC_EXTENSIBLE) {
		if(per_put_few_bits(po, inext, 0))
			_ASN_ENCODE_FAILED;
		ct = 0;
	} else if(inext) {
		_ASN_ENCODE_FAILED;
	}

	if(ct && ct->range_bits >= 0) {
		if(per_put_few_bits(po, value, ct->range_bits))
			_ASN_ENCODE_FAILED;
		_ASN_ENCODED_OK(er);
	}

	if(!specs->extension)
		_ASN_ENCODE_FAILED;

	/*
	 * X.691, #10.6: normally small non-negative whole number;
	 */
	if(uper_put_nsnnwn(po, value - (specs->extension - 1)))
		_ASN_ENCODE_FAILED;

	_ASN_ENCODED_OK(er);
}
开发者ID:AvidehST,项目名称:freeipa,代码行数:65,代码来源:NativeEnumerated.c

示例8: input_esc_dispatch

/* Execute escape sequence. */
int
input_esc_dispatch(struct input_ctx *ictx)
{
	struct screen_write_ctx		*sctx = &ictx->ctx;
	struct screen			*s = sctx->s;
	struct input_table_entry	*entry;

	if (ictx->flags & INPUT_DISCARD)
		return (0);
	log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);

	entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
	    sizeof input_esc_table[0], input_table_compare);
	if (entry == NULL) {
		log_debug("%s: unknown '%c'", __func__, ictx->ch);
		return (0);
	}

	switch (entry->type) {
	case INPUT_ESC_RIS:
		input_reset_cell(ictx);
		screen_write_reset(sctx);
		break;
	case INPUT_ESC_IND:
		screen_write_linefeed(sctx, 0);
		break;
	case INPUT_ESC_NEL:
		screen_write_carriagereturn(sctx);
		screen_write_linefeed(sctx, 0);
		break;
	case INPUT_ESC_HTS:
		if (s->cx < screen_size_x(s))
			bit_set(s->tabs, s->cx);
		break;
	case INPUT_ESC_RI:
		screen_write_reverseindex(sctx);
		break;
	case INPUT_ESC_DECKPAM:
		screen_write_mode_set(sctx, MODE_KKEYPAD);
		break;
	case INPUT_ESC_DECKPNM:
		screen_write_mode_clear(sctx, MODE_KKEYPAD);
		break;
	case INPUT_ESC_DECSC:
		memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
		ictx->old_cx = s->cx;
		ictx->old_cy = s->cy;
		break;
	case INPUT_ESC_DECRC:
		memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
		screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
		break;
	case INPUT_ESC_DECALN:
		screen_write_alignmenttest(sctx);
		break;
	case INPUT_ESC_SCSG0_ON:
		ictx->cell.g0set = 1;
		break;
	case INPUT_ESC_SCSG0_OFF:
		ictx->cell.g0set = 0;
		break;
	case INPUT_ESC_SCSG1_ON:
		ictx->cell.g1set = 1;
		break;
	case INPUT_ESC_SCSG1_OFF:
		ictx->cell.g1set = 0;
		break;
	}

	return (0);
}
开发者ID:ChrisSteinbach,项目名称:tmux-sixel,代码行数:72,代码来源:input.c

示例9: input_csi_dispatch

/* Execute control sequence. */
int
input_csi_dispatch(struct input_ctx *ictx)
{
	struct screen_write_ctx	       *sctx = &ictx->ctx;
	struct screen		       *s = sctx->s;
	struct input_table_entry       *entry;
	int				n, m;
	u_int				cx;

	if (ictx->flags & INPUT_DISCARD)
		return (0);
	if (input_split(ictx) != 0)
		return (0);
	log_debug("%s: '%c' \"%s\" \"%s\"",
	    __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);

	entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
	    sizeof input_csi_table[0], input_table_compare);
	if (entry == NULL) {
		log_debug("%s: unknown '%c'", __func__, ictx->ch);
		return (0);
	}

	switch (entry->type) {
	case INPUT_CSI_CBT:
		/* Find the previous tab point, n times. */
		cx = s->cx;
		if (cx > screen_size_x(s) - 1)
			cx = screen_size_x(s) - 1;
		n = input_get(ictx, 0, 1, 1);
		while (cx > 0 && n-- > 0) {
			do
				cx--;
			while (cx > 0 && !bit_test(s->tabs, cx));
		}
		s->cx = cx;
		break;
	case INPUT_CSI_CUB:
		screen_write_cursorleft(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_CUD:
		screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_CUF:
		screen_write_cursorright(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_CUP:
		n = input_get(ictx, 0, 1, 1);
		m = input_get(ictx, 1, 1, 1);
		screen_write_cursormove(sctx, m - 1, n - 1);
		break;
	case INPUT_CSI_WINOPS:
		input_csi_dispatch_winops(ictx);
		break;
	case INPUT_CSI_CUU:
		screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_CNL:
		screen_write_carriagereturn(sctx);
		screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_CPL:
		screen_write_carriagereturn(sctx);
		screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_DA:
		switch (input_get(ictx, 0, 0, 0)) {
		case 0:
			input_reply(ictx, "\033[?1;2c");
			break;
		default:
			log_debug("%s: unknown '%c'", __func__, ictx->ch);
			break;
		}
		break;
	case INPUT_CSI_DA_TWO:
		switch (input_get(ictx, 0, 0, 0)) {
		case 0:
			input_reply(ictx, "\033[>84;0;0c");
			break;
		default:
			log_debug("%s: unknown '%c'", __func__, ictx->ch);
			break;
		}
		break;
	case INPUT_CSI_ECH:
		screen_write_clearcharacter(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_DCH:
		screen_write_deletecharacter(sctx, input_get(ictx, 0, 1, 1));
		break;
	case INPUT_CSI_DECSTBM:
		n = input_get(ictx, 0, 1, 1);
		m = input_get(ictx, 1, 1, screen_size_y(s));
		screen_write_scrollregion(sctx, n - 1, m - 1);
		break;
	case INPUT_CSI_DL:
		screen_write_deleteline(sctx, input_get(ictx, 0, 1, 1));
		break;
//.........这里部分代码省略.........
开发者ID:ChrisSteinbach,项目名称:tmux-sixel,代码行数:101,代码来源:input.c

示例10: elem_add_element

/**
 * \brief Add new IPFIX element to a proper group
 *
 * Find a group of elements with same Enteprise ID and insert new element inside
 * it. If Enterprise group does not exists, it will create new one.
 * \warning Dupliticity of elements is not check. Groups must be sorted before
 * next usage for searching for elements.
 * \param[in,out] groups Structure with groups of IPFIX elements.
 * \param[in] elem New IPFIX element
 * \return O on success. Otherwise returns non-zero value.
 */
static int elem_add_element(struct elem_groups* groups, ipfix_element_t *elem)
{
	struct elem_en_group *key_ptr, *en_group = NULL;
	struct elem_en_group **aux_ptr;
	
	struct elem_en_group key;
	key.en_id = elem->en;
	key_ptr = &key;
	
	// Find the group with same Enterprise ID
	aux_ptr = bsearch(&key_ptr, groups->groups, groups->elem_used,
		sizeof(struct elem_en_group *), cmp_groups);
	if (aux_ptr) {
		en_group = *aux_ptr;
	}
	
	if (!en_group) {
		// Group not found -> create new one
		if (groups->elem_used == groups->elem_max) {
			// Array is full -> realloc
			struct elem_en_group **new_ptr;
			new_ptr = (struct elem_en_group **) realloc(groups->groups,
				2 * groups->elem_max * sizeof(struct elem_en_group *));
			if (!new_ptr) {
				MSG_ERROR(msg_module, "REALLOC FAILED! (%s:%d)", __FILE__,
					__LINE__);
				return 1;
			}
			
			groups->elem_max *= 2;
			groups->groups = new_ptr;
		}
		
		en_group = (struct elem_en_group *) calloc(1, sizeof(struct elem_en_group));
		if (!en_group) {
			MSG_ERROR(msg_module, "CALLOC FAILED! (%s:%d)", __FILE__, __LINE__);
			return 1;
		}
		
		en_group->en_id = elem->en;
		groups->groups[groups->elem_used++] = en_group;
		
		// We use binary search for searching for groups -> sort
		qsort(groups->groups, groups->elem_used, sizeof(struct elem_en_group *),
			cmp_groups);
	}

	// Add element to the group
	if (en_group->elem_used == en_group->elem_max) {
		// Array is empty or full -> realloc
		size_t new_size = (en_group->elem_max == 0)
			? ELEM_DEF_COUNT : (2 * en_group->elem_max);
		
		ipfix_element_t **new_ptr;
		new_ptr = (ipfix_element_t **) realloc(en_group->elements,
			new_size * sizeof(ipfix_element_t *));
		if (!new_ptr) {
			MSG_ERROR(msg_module, "CALLOC FAILED! (%s:%d)", __FILE__, __LINE__);
			return 1;
		}
		
		en_group->elements = new_ptr;
		en_group->elem_max = new_size;
	}
	
	// Success
	en_group->elements[en_group->elem_used++] = elem;
	return 0;
}
开发者ID:ADTRAN,项目名称:ipfixcol,代码行数:80,代码来源:element.c

示例11: hcstrdup

tuning_db_entry_t *tuning_db_search (hashcat_ctx_t *hashcat_ctx, const char *device_name, const cl_device_type device_type, int attack_mode, const int hash_type)
{
  tuning_db_t *tuning_db = hashcat_ctx->tuning_db;

  static tuning_db_entry_t s;

  // first we need to convert all spaces in the device_name to underscore

  char *device_name_nospace = hcstrdup (device_name);

  int device_name_length = strlen (device_name_nospace);

  int i;

  for (i = 0; i < device_name_length; i++)
  {
    if (device_name_nospace[i] == ' ') device_name_nospace[i] = '_';
  }

  // find out if there's an alias configured

  tuning_db_alias_t a;

  a.device_name = device_name_nospace;

  char *alias_name = NULL;

  for (i = device_name_length; i >= 1; i--)
  {
    device_name_nospace[i] = 0;

    tuning_db_alias_t *alias = bsearch (&a, tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);

    if (alias == NULL) continue;

    alias_name = alias->alias_name;

    break;
  }

  // attack-mode 6 and 7 are attack-mode 1 basically

  if (attack_mode == 6) attack_mode = 1;
  if (attack_mode == 7) attack_mode = 1;

  // bsearch is not ideal but fast enough

  s.device_name = device_name_nospace;
  s.attack_mode = attack_mode;
  s.hash_type   = hash_type;

  tuning_db_entry_t *entry = NULL;

  // this will produce all 2^3 combinations required

  for (i = 0; i < 8; i++)
  {
    s.device_name = (i & 1) ? "*" : device_name_nospace;
    s.attack_mode = (i & 2) ?  -1 : attack_mode;
    s.hash_type   = (i & 4) ?  -1 : hash_type;

    entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);

    if (entry != NULL) break;

    // in non-wildcard mode do some additional checks:

    if ((i & 1) == 0)
    {
      // in case we have an alias-name

      if (alias_name != NULL)
      {
        s.device_name = alias_name;

        entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);

        if (entry != NULL) break;
      }

      // or by device type

      if (device_type & CL_DEVICE_TYPE_CPU)
      {
        s.device_name = "DEVICE_TYPE_CPU";
      }
      else if (device_type & CL_DEVICE_TYPE_GPU)
      {
        s.device_name = "DEVICE_TYPE_GPU";
      }
      else if (device_type & CL_DEVICE_TYPE_ACCELERATOR)
      {
        s.device_name = "DEVICE_TYPE_ACCELERATOR";
      }

      entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);

      if (entry != NULL) break;
    }
  }
//.........这里部分代码省略.........
开发者ID:expressDESERT,项目名称:hashcat,代码行数:101,代码来源:tuningdb.c

示例12: mbox_sync_parse_match_mail

bool mbox_sync_parse_match_mail(struct mbox_mailbox *mbox,
				struct mail_index_view *view, uint32_t seq)
{
        struct mbox_sync_mail_context ctx;
	struct message_header_parser_ctx *hdr_ctx;
	struct message_header_line *hdr;
	struct header_func *func;
	struct mbox_md5_context *mbox_md5_ctx;
	const void *data;
	bool expunged;
	uint32_t uid;
	int ret;

	/* we only wish to be sure that this mail actually is what we expect
	   it to be. If there's X-UID header and it matches our UID, we use it.
	   Otherwise it could mean that the X-UID header is invalid and it's
	   just not yet been rewritten. In that case use MD5 sum, if it
	   exists. */

	mail_index_lookup_uid(view, seq, &uid);
	i_zero(&ctx);
        mbox_md5_ctx = mbox->md5_v.init();

	hdr_ctx = message_parse_header_init(mbox->mbox_stream, NULL, 0);
	while ((ret = message_parse_header_next(hdr_ctx, &hdr)) > 0) {
		if (hdr->eoh)
			break;

		func = bsearch(hdr->name, header_funcs,
			       N_ELEMENTS(header_funcs), sizeof(*header_funcs),
			       mbox_sync_bsearch_header_func_cmp);
		if (func != NULL) {
			if (strcasecmp(hdr->name, "X-UID") == 0) {
				if (hdr->continues) {
					hdr->use_full_value = TRUE;
					continue;
				}
				(void)parse_x_uid(&ctx, hdr);

				if (ctx.mail.uid == uid)
					break;
			}
		} else {
			mbox->md5_v.more(mbox_md5_ctx, hdr);
		}
	}
	i_assert(ret != 0);
	message_parse_header_deinit(&hdr_ctx);

	mbox->md5_v.finish(mbox_md5_ctx, ctx.hdr_md5_sum);

	if (ctx.mail.uid == uid)
		return TRUE;

	/* match by MD5 sum */
	mbox->mbox_save_md5 = TRUE;

	mail_index_lookup_ext(view, seq, mbox->md5hdr_ext_idx,
			      &data, &expunged);
	return data == NULL ? 0 :
		memcmp(data, ctx.hdr_md5_sum, 16) == 0;
}
开发者ID:zatsepin,项目名称:core,代码行数:62,代码来源:mbox-sync-parse.c

示例13: mbox_sync_parse_next_mail

int mbox_sync_parse_next_mail(struct istream *input,
			      struct mbox_sync_mail_context *ctx)
{
	struct mbox_sync_context *sync_ctx = ctx->sync_ctx;
	struct message_header_parser_ctx *hdr_ctx;
	struct message_header_line *hdr;
	struct mbox_sync_header_func *func;
	struct mbox_md5_context *mbox_md5_ctx;
	size_t line_start_pos;
	int i, ret;

	ctx->hdr_offset = ctx->mail.offset;
	ctx->mail.flags = MAIL_RECENT; /* default to having recent flag */

        ctx->header_first_change = (size_t)-1;
	ctx->header_last_change = 0;

	for (i = 0; i < MBOX_HDR_COUNT; i++)
		ctx->hdr_pos[i] = (size_t)-1;

	ctx->content_length = (uoff_t)-1;
	str_truncate(ctx->header, 0);

        mbox_md5_ctx = ctx->sync_ctx->mbox->md5_v.init();

        line_start_pos = 0;
	hdr_ctx = message_parse_header_init(input, NULL, 0);
	while ((ret = message_parse_header_next(hdr_ctx, &hdr)) > 0) {
		if (hdr->eoh) {
			ctx->have_eoh = TRUE;
			break;
		}

		if (!hdr->continued) {
			line_start_pos = str_len(ctx->header);
			str_append(ctx->header, hdr->name);
			str_append_data(ctx->header, hdr->middle, hdr->middle_len);
		}

		func = bsearch(hdr->name, header_funcs,
			       N_ELEMENTS(header_funcs), sizeof(*header_funcs),
			       mbox_sync_bsearch_header_func_cmp);

		if (func != NULL) {
			if (hdr->continues) {
				hdr->use_full_value = TRUE;
				continue;
			}

			if (!func->func(ctx, hdr)) {
				/* this header is broken, remove it */
				ctx->need_rewrite = TRUE;
				str_truncate(ctx->header, line_start_pos);
				if (ctx->header_first_change == (size_t)-1) {
					ctx->header_first_change =
						line_start_pos;
				}
				continue;
			}
			buffer_append(ctx->header, hdr->full_value,
				      hdr->full_value_len);
		} else {
			ctx->sync_ctx->mbox->md5_v.more(mbox_md5_ctx, hdr);
			buffer_append(ctx->header, hdr->value,
				      hdr->value_len);
		}
		if (!hdr->no_newline) {
			if (hdr->crlf_newline)
				str_append_c(ctx->header, '\r');
			str_append_c(ctx->header, '\n');
		}
	}
	i_assert(ret != 0);
	message_parse_header_deinit(&hdr_ctx);

	ctx->sync_ctx->mbox->md5_v.finish(mbox_md5_ctx, ctx->hdr_md5_sum);

	if ((ctx->seq == 1 && !ctx->seen_imapbase) ||
	    (ctx->seq > 1 && sync_ctx->dest_first_mail)) {
		/* missing X-IMAPbase */
		ctx->need_rewrite = TRUE;
		if (sync_ctx->base_uid_validity == 0) {
			/* figure out a new UIDVALIDITY for us. */
			sync_ctx->base_uid_validity =
				sync_ctx->hdr->uid_validity != 0 &&
				!sync_ctx->renumber_uids ?
				sync_ctx->hdr->uid_validity :
				I_MAX((uint32_t)ioloop_time, 1);
		}
	}

	ctx->body_offset = input->v_offset;
	if (input->stream_errno != 0) {
		mbox_sync_set_critical(ctx->sync_ctx, "read(%s) failed: %s",
			i_stream_get_name(input), i_stream_get_error(input));
		return -1;
	}
	return 0;
}
开发者ID:zatsepin,项目名称:core,代码行数:99,代码来源:mbox-sync-parse.c

示例14: pharse_search

gboolean pharse_search(char *s)
{
  return bsearch(&s, phrase, phraseN, sizeof(char *), qcmp_str) != NULL;
}
开发者ID:Tetralet,项目名称:hime,代码行数:4,代码来源:hime-tslearn.c

示例15: cliProcess

void cliProcess(void)
{
    if (!cliMode) {
        cliEnter();
    }

    while (serialTotalBytesWaiting(cliPort)) {
        uint8_t c = serialRead(cliPort);
        if (c == '\t' || c == '?') {
            // do tab completion
            const clicmd_t *cmd, *pstart = NULL, *pend = NULL;
            uint32_t i = bufferIndex;
            for (cmd = cmdTable; cmd < cmdTable + CMD_COUNT; cmd++) {
                if (bufferIndex && (strncasecmp(cliBuffer, cmd->name, bufferIndex) != 0))
                    continue;
                if (!pstart)
                    pstart = cmd;
                pend = cmd;
            }
            if (pstart) {    /* Buffer matches one or more commands */
                for (; ; bufferIndex++) {
                    if (pstart->name[bufferIndex] != pend->name[bufferIndex])
                        break;
                    if (!pstart->name[bufferIndex] && bufferIndex < sizeof(cliBuffer) - 2) {
                        /* Unambiguous -- append a space */
                        cliBuffer[bufferIndex++] = ' ';
                        cliBuffer[bufferIndex] = '\0';
                        break;
                    }
                    cliBuffer[bufferIndex] = pstart->name[bufferIndex];
                }
            }
            if (!bufferIndex || pstart != pend) {
                /* Print list of ambiguous matches */
                cliPrint("\r\033[K");
                for (cmd = pstart; cmd <= pend; cmd++) {
                    cliPrint(cmd->name);
                    cliWrite('\t');
                }
                cliPrompt();
                i = 0;    /* Redraw prompt */
            }
            for (; i < bufferIndex; i++)
                cliWrite(cliBuffer[i]);
        } else if (!bufferIndex && c == 4) {
            cliExit(cliBuffer);
            return;
        } else if (c == 12) {
            // clear screen
            cliPrint("\033[2J\033[1;1H");
            cliPrompt();
        } else if (bufferIndex && (c == '\n' || c == '\r')) {
            // enter pressed
            clicmd_t *cmd = NULL;
            clicmd_t target;
            cliPrint("\r\n");
            cliBuffer[bufferIndex] = 0; // null terminate

            target.name = cliBuffer;
            target.param = NULL;

            cmd = bsearch(&target, cmdTable, CMD_COUNT, sizeof cmdTable[0], cliCompare);
            if (cmd)
                cmd->func(cliBuffer + strlen(cmd->name) + 1);
            else
                cliPrint("Unknown command, try 'help'");

            memset(cliBuffer, 0, sizeof(cliBuffer));
            bufferIndex = 0;

            // 'exit' will reset this flag, so we don't need to print prompt again
            if (!cliMode)
                return;
            cliPrompt();
        } else if (c == 127) {
            // backspace
            if (bufferIndex) {
                cliBuffer[--bufferIndex] = 0;
                cliPrint("\010 \010");
            }
        } else if (bufferIndex < sizeof(cliBuffer) && c >= 32 && c <= 126) {
            if (!bufferIndex && c == 32)
                continue;
            cliBuffer[bufferIndex++] = c;
            cliWrite(c);
        }
    }
}
开发者ID:zatalian,项目名称:cleanflight,代码行数:88,代码来源:serial_cli.c


注:本文中的bsearch函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。