示例#1
0
long getNumber (long val) {

	char number[MAX_DIGITS];
	long temp=val,bestValue=-1;
	int count = MAX_DIGITS-1, var=0, len=0,j=0, prevDup=0;

	if (!validateInputLength(val))
		return -1;

	while (temp>0 && count >= 0) {
		number[count--] = temp%10;
		temp  = temp / 10;
	}
	//shifting the elements in number to begin from 0
	for (var = count+1; var < MAX_DIGITS; ++var) {
		number[len]=number[var];
		if(number[len] < 1)
			return -1;
		len++;
	}

	//now we have each digit in number[] and the length in len
	do {
		for (var = 0; var < len; ++var) {
			if ((number[var]+var)%len != (var+1)%len)
				break;
		}
		if (var == len) {
			if ((count=isDuplicates(number, len))) {
				if (bestValue==-1 || prevDup>count) {
					bestValue=findVal(number,len);
					prevDup=count;
				}
				if (!incrementValue(number, &len))
					return bestValue;
			} else //no duplicates and the required number
				return findVal(number,len);
		} else {
			number[var]+=len-(number[var]%len) + 1;
			for (j = var+1; j < len; ++j) {
				number[j] = 1;
			}
			if (number[var] > 9) {
				if (var>0 && number[var-1]<9) {
					number[var]=1;
					number[var-1]++;
				} else if (!incrementDigit(number, &len))	//already reached the greatest number
					return bestValue;
			}
		}
		//printf ("%ld\n",findVal(number,len));
	} while (len <= MAX_DIGITS);
	return bestValue;
}
示例#2
0
文件: csr.c 项目: mattliving/sparse
// Add two CSR matrices together and return a COO matrix
List * addCSR(CSR * A, CSR * B, List * M) {
	M->rows = A->rows;
	M->cols = A->cols;
	int total, i, j, k = 0; 
	for (i = 0; i < M->rows; i++) {
		for (j = 0; j < M->cols; j++) {
				total = findVal(A, i, j) + findVal(B, i, j);
				if (total != 0) { M = add(M, i, j, total); k++; }
		}	
	}
	M->size = k;
	return M;
}
示例#3
0
void processOneGff(char *gffName, FILE *f, char *exonType, struct hash *extraHash,
	struct sanger22extra **pExtraList, boolean isCds)
/* Parse through a GFF and store it in f, hash, and list. */
{
char *line, *name, *group, *fields[9];
struct lineFile *lf = lineFileOpen(gffName, TRUE);
struct sanger22extra *extra;

while (lineFileNext(lf, &line, NULL))
    {
    if (lineToGffFields(lf, line, fields))
	{
	fields[0] = "chr22";
	group = fields[8];
	fields[8] = name = findVal(lf, group, "Sequence");
	if (name == NULL)
	    continue;
	if (endsWith(name, ".mRNA"))
	    chopSuffix(name);
	if (sameString(fields[2], "exon"))
	    {
	    fields[2] = exonType;
	    writeGffLine(f, fields);
	    if ((extra = hashFindVal(extraHash, name)) == NULL)
		{
		extra = sanger22extraNew(name);
		hashAdd(extraHash, name, extra);
		slAddHead(pExtraList, extra);
		}
	    if (isCds)
		extra->cdsType = cloneString(fields[1]);
	    else
		extra->geneType = cloneString(fields[1]);
	    }
	else if (sameString(fields[2], "Sequence"))
	    {
	    if ((extra = hashFindVal(extraHash, name)) == NULL)
		{
		extra = sanger22extraNew(name);
		hashAdd(extraHash, name, extra);
		slAddHead(pExtraList, extra);
		}
	    if (extra->description[0] == 0)
		extra->description = cloneString(findVal(lf, group, "Description"));
	    if (extra->locus[0] == 0)
	        extra->locus = cloneString(findVal(lf, group, "Locus"));
	    }
	}
    }
lineFileClose(&lf);
}
示例#4
0
文件: s57val.c 项目: magwas/renderer
char *stringValue(Val_t val) {
  s57key_t *key = findKey(val.key);
  strcpy(outstr, "");
  switch (val.type) {
    case A:
    case S:
      strcpy(outstr, val.val.a);
      break;
    case E:
      if (findVal(key, val.val.e) == NULL)
        break;
      strcpy(outstr, findEnum(key, val.val.e));
      break;
    case L: {
      strcpy(outstr, "");
      Lst_t *lst = val.val.l;
      while (lst != NULL) {
        strcat(outstr, findEnum(key, lst->val));
        lst = lst->next;
        if (lst != NULL)
          strcat(outstr, ";");
      }
    }
      break;
    case I:
      sprintf(outstr, "%ld", val.val.i);
      break;
    case F:
      sprintf(outstr, "%g", val.val.f);
      break;
  }
  return outstr;
}
示例#5
0
/*
 * Convert rule-value to datum using namefield information
 */
datum *
ruleValueToDatum(__nis_table_mapping_t *t, __nis_rule_value_t *rv, int *statP) {
	__nis_value_t	*val;
	datum		*value;
	char		*str, *cstr, commentSep[3] = {' ', 0, 0};
	char		*myself = "ruleValueToDatum";

	/* No error yet */
	*statP = 0;

	/* Return empty datum if no namefield information available */
	if (t->e == 0) {
		if ((value = am(myself, sizeof (*value))) == 0)
			*statP = MAP_NO_MEMORY;
		return (value);
	}

	val = getMappingFormatArray(t->e->element.match.fmt, rv,
				fa_item, t->e->element.match.numItems,
				t->e->element.match.item);

	if (val && val->val && val->val->value) {
		if ((value = am(myself, sizeof (*value))) == 0) {
			*statP = MAP_NO_MEMORY;
			freeValue(val, 1);
			return (0);
		}

		/* Strip trailing whitespaces */
		cstr = (char *)val->val->value + val->val->length;
		for (; cstr >= (char *)val->val->value &&
			(*cstr == ' ' || *cstr == '\t'); *cstr-- = '\0');

		if (t->commentChar != '\0' &&
		    (str = findVal(N2LCOMMENT, rv, mit_nisplus)) != 0 &&
		    *str != '\0') {
			commentSep[1] = t->commentChar;
			cstr = scat(myself, F, commentSep, str);
			if (cstr) {
				value->dptr = scat(myself, F,
						val->val->value, cstr);
				sfree(cstr);
			}
		} else {
			value->dptr = sdup(myself, T, val->val->value);
		}
		freeValue(val, 1);
		if (value->dptr) {
			value->dsize = strlen(value->dptr);
			return (value);
		} else {
			*statP = MAP_NO_MEMORY;
			sfree(value);
			return (0);
		}
	}

	*statP = MAP_NAMEFIELD_MATCH_ERROR;
	return (0);
}
示例#6
0
double  findValW(char*name)
{
  double val;
  if(findVal(name,&val))
  {  printf(" name %s not found\n",name);
     return 0;
  } else return val;
}
示例#7
0
文件: 440.cpp 项目: mdkhan1/ACM
void main()
{
	int N;
	while(1)
	{
		scanf("%d",&N);
		if(!N) break;

        //printf("%d\n",findVal(N));

		printf("else if(N==%d) printf(\"%d\\n\");\n",N,findVal(N));
	}
}
示例#8
0
void LcsColumnReader::findBounds(
    uint filterPos,
    uint &nLoVal,
    uint &nHiVal,
    TupleDataWithBuffer &readerKeyData)
{
    LcsResidualFilter *filter = filters.filterData[filterPos].get();
    bool getLowerSet = filter->lowerBoundDirective != SEARCH_UNBOUNDED_LOWER;
    bool getLowerBoundStrict =
        filter->lowerBoundDirective != SEARCH_CLOSED_LOWER;
    bool getUpperSet = filter->upperBoundDirective != SEARCH_UNBOUNDED_UPPER;
    bool getUpperBoundStrict =
        filter->upperBoundDirective != SEARCH_CLOSED_UPPER;

    nLoVal =
        getLowerSet
        ? findVal(filterPos, false, getLowerBoundStrict, readerKeyData)
        : 0;
    nHiVal =
        getUpperSet
        ? findVal(filterPos, true, getUpperBoundStrict, readerKeyData)
        : getBatchValCount();
}
示例#9
0
文件: s57val.c 项目: magwas/renderer
char *decodeValue(Attl_t attl, char *atvl) {
  s57key_t *key = findKey(attl);
  if (key == NULL)
    return NULL;
  switch (key->type) {
    case A:
    case S: {
      size_t in = strlen(atvl);
      size_t out = 999;
      memset(outstr,0, out);
      char *to = outstr;
      iconv(conv, &atvl, &in, &to, &out);
      for (int i = 0; outstr[i] != 0; i++) {
        if ((outstr[i] == '<') || (outstr[i] == '>'))
          outstr[i] = '_';
      }
    }
      break;
    case E:
    case L:
      strncpy(instr, atvl, 999);
      strcpy(outstr, "");
      char *item = strtok(instr, ",");
      while (item != NULL) {
        if (strlen(outstr) > 0)
          strcat(outstr, ";");
        char *val = findVal(key, atoi(item));
        if (val == NULL)
          return NULL;
        strcat(outstr, val);
        if (key->type == E)
          break;
        item = strtok(NULL, ",");
      }
      break;
    case I:
      sprintf(outstr, "%d", atoi(atvl));
      break;
    case F:
      sprintf(outstr, "%g", atof(atvl));
      break;
  }
  char *str = outstr;
  while (isspace(*str)) str++;
  if (strlen(str) == 0)
    return NULL;
  return outstr;
}
示例#10
0
int findval_(char*f_name, double*val, int len)
{
  char c_name[20];
  fName2c(f_name,c_name,len);
  return findVal(c_name,val);
}
示例#11
0
double gmuon_(void)
{
/*compute the total g-2 from supersymmetry. Parameters are :
Mmu : muon mass
Mz  : Z-boson's mass
Mw  : W-boson's mass
s2thw : sin^2(thw)
e   : alpha
modM1 & argM1 : modulus and argument of M1		
modM2 & argM2 : modulus and argument of M2
modmu & argmu : modulus and argument of mu		
modAmu & argAmu : modulus and argument of Amu	
*/
/*declaration des variables*/
double thw, cb,cw,sw, ymu, g1,masssneutr, g2,gmuo = 0,beta,Mz,Mw,Mmu,e,tbeta;
matrix massmatrc,massmatrn,massmatrsmu;
matrix Ncomp, Nconj,N,Ucomp,Uconj,U,Vcomp,Vconj,V,X,Xconj,Xcomp;
vecteur massen,massec,massesmu;

double coeff1 ,coeff2;

matrix matrixtemp,matrixtemp2,matrixtemp3,matrixtemp4,matrixtemp5;
my_complex nc1,nc2;

int i,j;
/*Memory Allocation*/

massmatrn=initm(4);
massmatrc=initm(2);
massmatrsmu=initm(2);
Ncomp=initm(4);
Nconj=initm(4);
N=initm(4);

Ucomp=initm(2);
Vcomp=initm(2);
X=initm(2);
U=initm(2);
V=initm(2);
Uconj=initm(2);
Vconj=initm(2);
Xcomp=initm(2);
Xconj=initm(2);
massen=initv(4);
massesmu=initv(2);
massec=initv(2);

matrixtemp=initm(2);
matrixtemp2=initm(2);
matrixtemp3=initm(4);
matrixtemp4=initm(4);
matrixtemp5=initm(4);

/*Get the values for the masses and mixing matrices*/

e=sqrt(4*M_PI*0.00781653);
sw=0.48076;
Mz=91.1876;
Mmu=0.1057;

findVal("tB",&tbeta);


beta = atan(tbeta);
cb = cos(beta);
thw=asin(sw);
cw=cos(thw);
Mw=Mz*cw;
g1 = e/cw;
g2 = e/sw;
ymu = g2 * Mmu / (sqrt(2)*cb*Mw);

findVal("MNE1",&massen.e[0].r);
findVal("MNE2",&massen.e[1].r);
findVal("MNE3",&massen.e[2].r);
findVal("MNE4",&massen.e[3].r);

findVal("MC1",&massec.e[0].r);
findVal("MC2",&massec.e[1].r);

findVal("MSnm",&masssneutr);

findVal("Zn11",&Nconj.e[0][0].r); findVal("Zn12",&Nconj.e[1][0].r); findVal("Zn13",&Nconj.e[2][0].r); findVal("Zn14",&Nconj.e[3][0].r);
findVal("Zn21",&Nconj.e[0][1].r); findVal("Zn22",&Nconj.e[1][1].r); findVal("Zn23",&Nconj.e[2][1].r); findVal("Zn24",&Nconj.e[3][1].r);
findVal("Zn31",&Nconj.e[0][2].r); findVal("Zn32",&Nconj.e[1][2].r); findVal("Zn33",&Nconj.e[2][2].r); findVal("Zn34",&Nconj.e[3][2].r);
findVal("Zn41",&Nconj.e[0][3].r); findVal("Zn42",&Nconj.e[1][3].r); findVal("Zn43",&Nconj.e[2][3].r); findVal("Zn44",&Nconj.e[3][3].r);

findVal("Zu11",&Uconj.e[0][0].r); findVal("Zu12",&Uconj.e[1][0].r);
findVal("Zu21",&Uconj.e[0][1].r); findVal("Zu22",&Uconj.e[1][1].r);

findVal("Zv11",&Vcomp.e[0][0].r); findVal("Zv12",&Vcomp.e[0][1].r);
findVal("Zv21",&Vcomp.e[1][0].r); findVal("Zv22",&Vcomp.e[1][1].r);


{  double MSmuLL,MSmuRR,MSmuLR,Am,mu,MSmuth;

   MSmuLL=findValW("MSmL"); MSmuLL*=MSmuLL;
   MSmuRR=findValW("MSmR"); MSmuRR*=MSmuRR;
   Am=findValW("Am");
   mu=findValW("mu");
   MSmuLR=(Am-mu*tbeta)*Mmu;
 
   massesmu.e[0].r=sqrt((MSmuLL+MSmuRR-sqrt((MSmuLL-MSmuRR)*(MSmuLL-MSmuRR)+4*MSmuLR*MSmuLR))/2);
   massesmu.e[1].r=sqrt((MSmuLL+MSmuRR+sqrt((MSmuLL-MSmuRR)*(MSmuLL-MSmuRR)+4*MSmuLR*MSmuLR))/2);

   MSmuth=atan2(-2*MSmuLR, -MSmuLL+MSmuRR)/2;

   X.e[0][0].r=cos(MSmuth);
   X.e[0][1].r=sin(MSmuth);
   X.e[1][0].r=-X.e[0][1].r;
   X.e[1][1].r=X.e[0][0].r;
}




for(i=0;i<4;i++)
	if (massen.e[i].r<0)
		{for(j=0;j<4;j++)
		     {Nconj.e[j][i]=prod(icomp,Nconj.e[j][i]);
		     }
		 massen.e[i].r=-massen.e[i].r;
		}


for(i=0;i<2;i++)
	{if (massec.e[i].r<0)
		{for(j=0;j<2;j++)
		     { Uconj.e [j][i]=prod(icomp,Uconj.e[j][i]);
		     Vcomp.e [i][j]=prod(icomp,Vcomp.e[i][j]);
		     }
		 massec.e[i].r=-massec.e[i].r;
		}
	if (massesmu.e[i].r<0)
		{for(j=0;j<2;j++)
		     {X.e [i][j]=prod(icomp,X.e[i][j]);
		     
		     }
		massesmu.e[i].r=-massesmu.e[i].r;
		   
		}
	}

N=adj(Nconj);
U=adj(Uconj);

/*Compute the coefficients entering the formula for the neutral  and chargino
contribution to g-2_muon and calculates gmuon*/

/*neutralinos*/

for ( i=0;i<4;i=i+1)
	for ( j=0;j<2;j=j+1)
		{nc1=somme(prodscal(sqrt(2)*g1,prod(N.e[i][0],X.e[j][1])),prodscal(ymu,prod(N.e[i][2],X.e[j][0])));		
		nc2=diff(prodscal(1/sqrt(2),prod(somme(prodscal(g2,N.e[i][1]),prodscal(g1,N.e[i][0])),conjug(X.e[j][0]))),prodscal(ymu,prod(N.e[i][2],conjug(X.e[j][1]))));
		
		
		coeff1 =(somme(prod(nc1,conjug(nc1)),prod(nc2,conjug(nc2)))).r;
		coeff2 = prod(nc1,nc2).r;
			
		if (massesmu.e[j].r<1e-8) 
			{return 0;
			printf("erreur : Mass smuons nul\n");}		
		gmuo=gmuo+calcgmuon (0,Mmu, massen.e[i].r, massesmu.e[j].r,coeff1, coeff2);
	}

/*charginos*/

for (j=0;j<2;j=j+1)
	{
	nc1=prodscal(ymu,U.e[j][1]);
	nc2=prodscal(-g2,conjug(Vcomp.e[j][0]));
	coeff1 =(somme(prod(nc1,conjug(nc1)),prod(nc2,conjug(nc2)))).r;
	coeff2 = prod(nc1,nc2).r;
	if (masssneutr<1e-8) 
		{return 0;
		printf("erreur : Mass sneutrinos nul\n");}
	gmuo=gmuo+calcgmuon (1,Mmu, massec.e[j].r, masssneutr,coeff1, coeff2);	
	}

 return gmuo;

}
示例#12
0
int masslimits_(void)
{
  double tb,c2b,mc1,msne,msnm,msnl,mstau1,mn1,msel,mser,mse1,msmul,msmur,msmu1;
  double mst1,msb1,msul,msur,msdl,msdr;
  int retcode=0;
  int limCharg=0,limSne=0,limSnm=0,limSnl=0,limSeR=0,limSmR=0 ,limSl1=0,limSb1=0,limSt1=0,limSq=0;

  if(findVal("MNE1",&mn1))  return -1; mn1=fabs(mn1);
  if(findVal("MC1",&mc1))   return -1; mc1=fabs(mc1);
  if(findVal("tb",&tb))     return -1; c2b=(1-tb*tb)/(1+tb*tb);
  if(findVal("MSeL",&msel)) return -1; msel=fabs(msel);
  if(findVal("MSeR",&mser)) return -1; mser=fabs(mser);
 /*      mse1=(msel<mser)? msel:mser; */
mse1=mser;
  if(findVal("MSne",&msne)) return -1; msne=fabs(msne);
  if(findVal("MSnm",&msnm)) return -1; msnm=fabs(msnm);
  if(findVal("MSnl",&msnl)) return -1; msnl=fabs(msnl);
  if(findVal("MSmL",&msmul))return -1; msmul=fabs(msmul);
  if(findVal("MSmR",&msmur))return -1; msmur=fabs(msmur);
/*      msmu1=(msmul<msmur)? msmul:msmur;*/
  msmu1=msmur;
  if(findVal("MSl1",&mstau1))return -1;mstau1=fabs(mstau1);
  if(findVal("MSt1",&mst1)) return -1; mst1=fabs(mst1);
  if(findVal("MSb1",&msb1)) return -1; msb1=fabs(msb1);
  if(findVal("MSuL",&msul)) return -1; msul=fabs(msul);
  if(findVal("MSuR",&msur)) return -1; msur=fabs(msur);
  if(findVal("MSdL",&msdl)) return -1; msdl=fabs(msdl);
  if(findVal("MSdR",&msdr)) return -1; msdr=fabs(msdr);



  if(mc1-mn1>60.)
  {if((msne>43. &&msne<55. &&mc1<89.5)   ||
       (msne>55. &&msne<65. &&mc1<80.)   ||
       (msne>65. &&msne<75. &&mc1<74.4)  ||
       (msne>75. &&msne<85. &&mc1<73.1)  ||
       (msne>85. &&msne<95. &&mc1<73.6)  ||
       (msne>95. &&msne<105.&&mc1<75.1)  ||
       (msne>105.&&msne<115.&&mc1<76.9)  ||
       (msne>115.&&msne<125.&&mc1<80.1)  ||
       (msne>125.&&msne<135.&&mc1<83.14) ||
       (msne>135.&&msne<145.&&mc1<85.37) ||
       (msne>145.&&msne<155.&&mc1<88.4)  ||
       (msne>155.&&msne<165.&&mc1<91.04) ||
       (msne>165.&&msne<175.&&mc1<92.4)  ||
       (msne>175.&&msne<185.&&mc1<94.7)  ||
       (msne>185.&&msne<195.&&mc1<95.5)  ||
       (msne>195.&&msne<205.&&mc1<96.4)  ||
       (msne>205.&&msne<275.&&mc1<99.4)  ||
       (msne>275.&&msne<325.&&mc1<100.5) ||
       (msne>325.&&msne<375.&&mc1<101)   ||
       (msne>375.&&msne<425.&&mc1<101.2) ||
       (msne>425.&&mc1<101.5)
      )  limCharg=1;
  }

  if(mc1-mn1<60.0)
   {/*if(msne<45.) return 2;*/
    if((msne>43. &&msne<55. &&mc1<101.)  ||
       (msne>55. &&msne<65. &&mc1<98.)   ||
       (msne>65. &&msne<75. &&mc1<95.)   ||
       (msne>75. &&msne<85. &&mc1<91.)   ||
       (msne>85. &&msne<95. &&mc1<90.)   ||
       (msne>95. &&msne<105.&&mc1<92.)   ||
       (msne>105.&&msne<115.&&mc1<93.)   ||
       (msne>115.&&msne<125.&&mc1<96.)   ||
       (msne>125.&&msne<135.&&mc1<97.)   ||
       (msne>135.&&msne<145.&&mc1<99.)   ||
       (msne>145.&&msne<155.&&mc1<100.)  ||
       (msne>155.&&msne<165.&&mc1<101.)  ||
       (msne>165.&&msne<175.&&mc1<101.)  ||
       (msne>175.&&msne<225.&&mc1<101.)  ||
       (msne>225.&&msne<275.&&mc1<102.)  ||
       (msne>275.&&msne<325.&&mc1<102.5) ||
       (msne>325.&&msne<375.&&mc1<102.9) ||
       (msne>375.&&mc1<103.)
      ) limCharg=1;
  }


  if(msne<43.0)           limSne=1;
  if(msnm<43.0)           limSnm=1;
  if(msnl<43.0)           limSnl=1;
  if(mse1<99.4 &&mn1>40.) limSeR=1;
  if(mse1<100.5&&mn1<40.) limSeR=1;
  if(msmu1<95.0)          limSmR=1;
  if(msul<40.0)           limSq=1;
  if(msur<40.0)           limSq=1;
  if(msdl<40.0)           limSq=1;
  if(msdr<40.0)           limSq=1;
  if(mst1<63.0)           limSt1=1;
  if(msb1<89 && msb1-mn1>8.0)   limSb1=1;



/* These limits are valid only if Mstau-mn1> 3GeV*/

  if((mstau1 < 80.5&& mn1<10.)           ||
     (mstau1 < 82. && mn1>10.&& mn1<20.) ||
     (mstau1 < 84. && mn1>20.&& mn1<30.) ||
     (mstau1 < 85. && mn1>30.&& mn1<40.) ||
     (mstau1 < 87.1&& mn1>40.&& mn1<50.) ||
     (mstau1 < 88. && mn1>50.&& mn1<60.) ||
     (mstau1 < 85. && mn1>60.&& mn1<75.)
    ) limSl1=1;	


  if(limCharg){retcode+=1;   printf("WARNING: Chargino below LEP limit \n");}
  if(limSne)  {retcode+=2;   printf("WARNING: Sneutrino-e_L below LEP limit \n");}
  if(limSnm)  {retcode+=4;   printf("WARNING: Sneutrino-mu_L below LEP limit \n");}
  if(limSnl)  {retcode+=8;   printf("WARNING: Sneutrino-tau_L below LEP limit \n");}
  if(limSeR)  {retcode+=16;  printf("WARNING: Selectron-R below LEP limit \n");} 
  if(limSmR)  {retcode+=32;  printf("WARNING: Smuon-R below LEP limit \n");}
  if(limSl1)  {retcode+=64;  printf("WARNING: Stau1 below LEP limit \n");}
  if(limSt1)  {retcode+=256; printf("WARNING: Stop1 below LEP limit \n");}
  if(limSb1)  {retcode+=512; printf("WARNING: Sbot1 below LEP limit \n");}
  if(limSq)   {retcode+=1024;printf("WARNING: Squarks below LEP limit \n");}



  return retcode;
}
示例#13
0
/*
 * Generate name=values pairs for splitfield names
 *
 * Consider Example:
 *	nisLDAPnameFields club:
 *			("%s %s %s", name, code, members)
 *	nisLDAPsplitField members:
 *			("(%s,%s,%s)", host, user, domain),
 *			("%s", group)
 * On entry,
 * - rv is an array of numVals rule-values each containing
 * name=value pairs for names occuring in nisLDAPsplitField.
 * (i.e host, user, domain, group)
 * - trv contains name=value pairs for names occuring in
 * nisLDAPnameFields. (i.e name, code but not members)
 *
 * For every name in nisLDAPnamefields that is a splitfield,
 * this function applies the data in rv to the corresponding
 * splitfield formats (accessed thru t), to generate a single
 * string value for the corresponding splitfield (members).
 * This new name=value pair is then added to trv.
 * Besides, any uninitialized namefield names are set to empty strings.
 */
suc_code
addSplitFieldValues(__nis_table_mapping_t *t, __nis_rule_value_t *rv,
			__nis_rule_value_t *trv, int numVals, char *domain) {
	__nis_table_mapping_t	*sf;
	__nis_value_t		*val;
	int			i, j, k, nitems, res, statP;
	char			*str, *tempstr;
	char			delim[2] = {0, 0};
	char			*emptystr = "";
	char			*myself = "addSplitFieldValues";

	if (trv == 0)
		return (MAP_INTERNAL_ERROR);

	if (t->e == 0)
		return (SUCCESS);

	nitems = t->e->element.match.numItems;

	/*
	 * Procedure:
	 * - Check each name in nisLDAPnamefield
	 * - if it's a splifield, construct its value and add it to trv
	 * - if not, check if it has a value
	 * - if not, add empty string
	 */
	for (i = 0, sf = 0; i < nitems; i++) {
		if (rv) {
			/*
			 * str will eventually contain the single string
			 * value for the corresponding  splitfield.
			 * No point initializing str if rv == 0 because
			 * splitfield cannot be constructed without rv.
			 * So, only initialized here.
			 */
			str = 0;

			/* Check if it's a splitfield name */
			sf = mappingFromMap(t->e->element.match.item[i].name,
				domain, &statP);

			/*
			 * Return only incase of memory allocation failure.
			 * The other error case (MAP_NO_MAPPING_EXISTS),
			 * indicates that the item name is not a splitfieldname
			 * i.e it's a namefieldname. This case is handled by
			 * the following if (sf == 0)
			 */
			if (statP == MAP_NO_MEMORY)
				return (statP);
		}

		if (sf == 0) {
			/*
			 * Not a splitfield name. Verify if it has a value
			 */
			if (findVal(t->e->element.match.item[i].name,
				trv, mit_nisplus) == 0) {
				/* if not, use empty string */
				res = addCol2RuleValue(vt_string,
					t->e->element.match.item[i].name,
					emptystr, 0, trv);
				if (res == -1) {
					return (MAP_INTERNAL_ERROR);
				}
			}
			/*
			 * If rv == 0 then sf == 0 so we will continue here
			 * i.e. does not matter that str is not yet set up.
			 */
			continue;
		}

		/* Code to construct a single value */

		/* Use the first separator character as the delimiter */
		delim[0] = sf->separatorStr[0];

		for (j = 0; j < numVals; j++) {
			/* sf->numSplits is zero-based */
			for (k = 0; k <= sf->numSplits; k++) {
				val = getMappingFormatArray(
					sf->e[k].element.match.fmt, &rv[j],
					fa_item,
					sf->e[k].element.match.numItems,
					sf->e[k].element.match.item);
				if (val == 0)
					continue;
				if (val->numVals > 0) {
					if (str) {
						tempstr = scat(myself,
							0, str, delim);
						sfree(str);
						if (tempstr)
							str = tempstr;
						else {
							freeValue(val, 1);
							return (MAP_NO_MEMORY);
						}
					}
					tempstr = scat(myself, 0, str,
						val->val->value);
					sfree(str);
					if (tempstr)
						str = tempstr;
					else {
						freeValue(val, 1);
						return (MAP_NO_MEMORY);
					}
				}
				freeValue(val, 1);
			}
		}
		if (str == 0)
			str = emptystr;

		res = addCol2RuleValue(vt_string,
				t->e->element.match.item[i].name,
				str, strlen(str), trv);

		if (str != emptystr)
			sfree(str);

		if (res == -1) {
			return (MAP_INTERNAL_ERROR);
		}
	}

	return (SUCCESS);
}
示例#14
0
/*
 * FUNCTION :	makeNISObject()
 *
 * DESCRIPTION: Sets up a nis Object in the DIT.
 *
 * GIVEN :
 *		Case 1: Both 'domain' and 'dn' are non-NULL
 *			Create nisDomainObject with the given information
 *		Case 2: Only 'domain' is  non-NULL
 *			Obtain the 'dn' from the nisLDAPdomainContext list
 *			Create nisDomainObject with the above information
 *		Case 3: Only 'dn' is  non-NULL
 *			Create an object with the 'dn'
 *			Here we guess the objectclass attribute, based on
 *			oc_lookup table
 *		Case 4: Both 'domain' and 'dn' are NULL
 *			Error
 *
 * RETURNS :	SUCCESS = It worked
 *		FAILURE = There was a problem.
 */
suc_code
makeNISObject(char *domain, char *dn) {
	__nis_rule_value_t	*rv;
	__nis_ldap_search_t	*ls;
	int			i, rc, nr, add_rc;
	char			*val;
	char			*myself = "makeNISObject";

	if (!dn && !domain)
		return (FAILURE);

	/*
	 * If only 'domain' name is provided, then
	 * try to find dn from the nisLDAPdomainContext
	 * list generated by the parser
	 */
	if (!dn) {
		for (i = 0; i < ypDomains.numDomains; i++) {
			if (ypDomains.domainLabels[i] == 0)
				continue;
			if (strcasecmp(domain, ypDomains.domainLabels[i])
								== 0) {
				dn = ypDomains.domains[i];
				break;
			}
		}
		if (!dn)
			return (FAILURE);
	}

	/*
	 * If only 'dn' is given, then it means that the
	 * caller simply wants to a create an entry for
	 * that 'dn'.
	 *
	 * If 'domain' is given, then check if the 'dn'
	 * has already been set up as a nis domain object.
	 * If not, see if we can make it become one.
	 */
	if (domain) {
		/*
		 * Check to see if the nis domain object has
		 * already been set up
		 */
		ls = buildLdapSearch(dn, LDAP_SCOPE_BASE, 0, 0,
			"objectclass=*", 0, 0, 0);
		if (ls == 0) {
			logmsg(MSG_NOTIMECHECK, LOG_ERR,
				"%s: Unable to create ldapSearch "
				"request for dn: %s", myself, dn);
			return (FAILURE);
		}
		nr = -1;
		rv = ldapSearch(ls, &nr, 0, &rc);
		freeLdapSearch(ls);
		if (rc == LDAP_SUCCESS) {
			val = findVal("nisDomain", rv, mit_ldap);
			if (val != NULL) {
				/*
				 * Yes, nis domain object found. Check
				 * to see if the domain names match.
				 * If so, we are done. If not, log
				 * a warning message, and return SUCCESS.
				 */
				if (strcasecmp(val, domain) == 0) {
					freeRuleValue(rv, nr);
					return (SUCCESS);
				} else {
					logmsg(MSG_NOTIMECHECK,
						LOG_WARNING,
						"%s: Entry (dn: %s) already "
						"contains a nis domain name "
						"(%s). The domain name (%s) "
						"is not added.",
						myself, dn, val, domain);
					freeRuleValue(rv, nr);
					return (SUCCESS);
				}
			} else {
				freeRuleValue(rv, nr);
				/*
				 * Entry for the 'dn' exists, but it
				 * is not a nis domain object yet.
				 * Add the nisDoamin attribute and
				 * the nisDomainObject objectclass to
				 * the entry.
				 */
				if ((rv = initRuleValue(1, 0)) == 0)
					return (FAILURE);

				if (addSAttr2RuleValue("nisDomain",
						domain, rv) == -1) {
					freeRuleValue(rv, 1);
					return (FAILURE);
				}
				rc = ldapModify(dn, rv,
					"objectclass=nisDomainObject",
					0);
				freeRuleValue(rv, 1);
				if (rc == LDAP_SUCCESS) {
					logmsg(MSG_NOTIMECHECK,
						LOG_INFO,
						"%s: entry (dn: %s) "
						"modified to be an "
						"nis domain object",
						myself, dn);
					return (SUCCESS);
				} else {
					logmsg(MSG_NOTIMECHECK,
						LOG_ERR,
						"%s: unable to modify "
						"entry (dn: %s) to be "
						"a nis domain object: "
						"ldapModify error %d (%s)",
						myself, dn, rc,
						ldap_err2string(rc));
					return (FAILURE);
				}
			}
		} else { /* search for 'dn' failed */
			freeRuleValue(rv, nr);

			/*
			 * It is OK if no such object, otherwise
			 * log an error.
			 */
			if (rc != LDAP_NO_SUCH_OBJECT) {
				logmsg(MSG_NOTIMECHECK, LOG_ERR,
					"%s: unable to retrieve "
					"entry (dn: %s): "
					"ldapSearch error %d (%s)",
					myself, dn, rc,
					ldap_err2string(rc));
				return (FAILURE);
			}
		}

		/*
		 * If the 'dn' is actually the naming context of
		 * the DIT, we should be able to make it a nis domain
		 * object without worrying about missing parent
		 * entries. If unable to add the entry for the 'dn'
		 * due to missing parent entries, fall through
		 * to create them and then add the nis domain object.
		 */
		if (addNISObject(domain, dn, &add_rc) == SUCCESS)
			return (SUCCESS);
		else if (add_rc != LDAP_NO_SUCH_OBJECT)
			return (FAILURE);
	}

	/* Create parent */
	if (addParent(dn, NULL) == FAILURE)
		return (FAILURE);

	if (addNISObject(domain, dn, NULL) == FAILURE)
		return (FAILURE);

	return (SUCCESS);
}
示例#15
0
/*
 * Maps and writes a single NIS entry to the LDAP DIT
 */
int
singleWriteToDIT(char *map, char *domain, datum *key, datum *value,
						bool_t replace) {
	__nis_table_mapping_t	*t;
	__nis_rule_value_t	*rv, *frv;
	__nis_ldap_search_t	*ls;
	int			statP = SUCCESS, flag;
	int			nv, nr, i, rc, collapse;
	char			*dn = 0, *skey, *svalue, *str;
	char			*myself = "singleWriteToDIT";

	if (!map || !domain || !key || !value) {
		return (MAP_PARAM_ERROR);
	}

	/* Return SUCCESS for empty or whitespace key */
	for (i = 0; i < key->dsize && (key->dptr[i] == 0 ||
		key->dptr[i] == ' ' || key->dptr[i] == '\t'); i++);
	if (i >= key->dsize)
		return (SUCCESS);

	/* Get the mapping information for the map */
	if ((t = mappingFromMap(map, domain, &statP)) == 0) {
		/*
		 * No problem. We don't handle this map and domain. Maybe it's
		 * handled by a service other than NIS.
		 */
		return (statP);
	}

	/* NULL-terminated version of key and value for logging */
	if ((skey = am(myself, key->dsize + 1)) == 0)
		return (MAP_NO_MEMORY);
	(void) memcpy(skey, key->dptr, key->dsize);

	if ((svalue = am(myself, value->dsize + 1)) == 0) {
		sfree(skey);
		return (MAP_NO_MEMORY);
	}
	(void) memcpy(svalue, value->dptr, value->dsize);

	if ((str = getFullMapName(map, domain)) == 0) {
		sfree(skey);
		sfree(svalue);
		return (MAP_NO_MEMORY);
	}

	/* For each alternate mapping */
	for (flag = 0; t != 0; t = t->next) {
		/* Verify objName */
		if (strcmp(str, t->objName) != 0) {
			continue;
		}

		/* Verify if key matches the index */
		if (verifyIndexMatch(t, 0, 0, N2LKEY, skey) == 0 ||
			verifyIndexMatch(t, 0, 0, N2LIPKEY, skey) == 0)
			continue;

		/* Check the writespecs */
		if (t->objectDN->write.base == 0) {
			logmsg(MSG_NOTIMECHECK, LOG_INFO,
				"%s: No baseDN in writespec. Write disabled "
				"for %s (%s)", myself, t->dbId, map);
			continue;
		}

		/* Check if rulesToLDAP are provided */
		if (t->numRulesToLDAP == 0) {
			logmsg(MSG_NOTIMECHECK, LOG_INFO,
				"%s: No rulesToLDAP. Write disabled for "
				"%s (%s)", myself, t->dbId, map);
			continue;
		}

		/* Set flag to indicate write is enabled */
		flag = 1;

		/* Convert key  and value into an array of rule-values */
		if ((rv = datumToRuleValue(key, value, t, &nv, domain, FALSE,
								&statP)) == 0) {
			logmsg(MSG_NOTIMECHECK, LOG_ERR,
				"%s: Conversion error %d (NIS to name=value "
				"pairs) for NIS data (key=%s, value=%s) "
				"for %s (%s)",
				myself, statP, skey, svalue, t->dbId, map);
			sfree(skey);
			sfree(svalue);

			/* Free full map name */
			sfree(str);

			return (statP);
		}

		/* Convert NIS data to LDAP equivalents for each rule-value */
		for (i = 0; i < nv; i++) {
			/* Verify indexlist with name=value pairs */
			if (verifyIndexMatch(t, 0, &rv[i], 0, 0) == 0)
				break;

			/* Create LDAP request and LDAP name=value pairs */
			if ((ls = createLdapRequest(t, &rv[i],
			    0, 0, NULL, NULL)) == 0) {
				logmsg(MSG_NOTIMECHECK, LOG_ERR,
					"%s: Conversion error (name=value pairs"
					" to LDAP) for NIS data "
					"(key=%s, value=%s) for %s (%s)",
					myself, skey, svalue, t->dbId, map);
				freeRuleValue(rv, nv);
				sfree(skey);
				sfree(svalue);

				/* Free full map name */
				sfree(str);

				return (MAP_CREATE_LDAP_REQUEST_ERROR);
			}
			freeLdapSearch(ls);
			/* printRuleValue(&rv[i]); */
		}

		/* If i < nv then this alternate mapping isn't the one */
		if (i < nv)
			continue;

		/*
		 * Merge rule-values with the same DN so that we have
		 * one ldap write request for each DN
		 */
		nr = nv;
		frv = mergeRuleValueWithSameDN(rv, &nr);
		freeRuleValue(rv, nv);
		if (frv == 0) {
			if (nr == -1) {
				logmsg(MSG_NOTIMECHECK, LOG_ERR,
					"%s: Unable to merge LDAP write "
					"requests to same DN for NIS data "
					"(key=%s, value=%s) for %s (%s)",
					myself, skey, svalue, t->dbId, map);
				statP = MAP_INTERNAL_ERROR;
			} else if (nr == 0) {
				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
					"%s: Cannot generate write DN due to "
					"missing information for NIS data "
					"(key=%s, value=%s) for %s (%s)",
					myself, skey, svalue, t->dbId, map);
				statP = MAP_NO_DN;
			}
			sfree(skey);
			sfree(svalue);

			/* Free full map name */
			sfree(str);

			return (statP);
		}

		/* Write to the LDAP server */
		for (collapse = 0, i = 0; i < nr; i++) {
			if ((dn = findVal("dn", &frv[i], mit_ldap)) != 0) {
				if (replace == FALSE) {
					/* ldap add */
					rc = ldapAdd(dn, &frv[i],
						t->objectDN->write.attrs, 0);
				} else {
					/* ldap modify with addFirst set */
					rc = ldapModify(dn, &frv[i],
						t->objectDN->write.attrs, 1);
				}

				/* if we get err=20, collapse and try again */
				if (!collapse &&
					(rc == LDAP_TYPE_OR_VALUE_EXISTS) &&
					(collapseRuleValue(&frv[i]) == 1)) {
					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
						"%s: Ignoring values differing "
						"in case from NIS data (key=%s,"
						" value=%s) for (dn: %s) for "
						"%s (%s)", myself, skey,
						svalue, dn, t->dbId, map);
					collapse = 1;
					i--;
					continue;
				}

				collapse = 0;
				if (rc != LDAP_SUCCESS) {
					/* Log error */
					logmsg(MSG_NOTIMECHECK, LOG_ERR,
						"%s: %s error %d (%s) for "
						"(dn: %s) for NIS data "
						"(key=%s, value=%s) "
						"for %s (%s)",
						myself, (replace == TRUE) ?
						"ldapModify" : "ldapAdd", rc,
						ldap_err2string(rc), dn, skey,
						svalue, t->dbId, map);

					/* Dumping failed call may be useful */
					/* printRuleValue(&frv[i]); */

					/*
					 * Return the error code and let wrapper
					 * sort out if mapping should continue
					 * or abort.
					 */
					statP = rc;
					sfree(skey);
					sfree(svalue);
					freeRuleValue(frv, nr);

					/* Free full map name */
					sfree(str);

					return (statP);
				}
			}
		}

		freeRuleValue(frv, nr);
	}

	sfree(skey);
	sfree(svalue);

	/* Free full map name */
	sfree(str);

	return ((flag)?SUCCESS:MAP_WRITE_DISABLED);
}
示例#16
0
文件: StatMerge.cpp 项目: subnr01/MPC
int* StatMerge::doSegmentation(int* ch1, int* ch2, int* ch3, double p)
{
	// first, build the edge array
	for(int x = 0; x < iWidth - 1; ++x)
	{
		for(int y = 0; y < iHeight - 1; ++y)
		{
			int p1 = (y * iWidth) + x;
			int p2 = p1 + 1;
			int p3 = p1 + iWidth;

			// vertical edge
			edgeArray[p1 * 2].p1 = p1;
			edgeArray[p1 * 2].p2 = p2;
			edgeArray[p1 * 2].val = findVal(ch1,ch2,ch3,p1,p2);

			// horizontal edge
			edgeArray[(p1 * 2)+1].p1 = p1;
			edgeArray[(p1 * 2)+1].p2 = p3;
			edgeArray[(p1 * 2)+1].val = findVal(ch1,ch2,ch3,p1,p3);
		}
	}

	// we have to handle the very bottom and right differently
	int y = iHeight - 1;
	int x = 0;
	for(x = 0; x < iWidth - 1; ++x)
	{
		int p1 = (y * iWidth) + x;
		int p2 = p1 + 1;

		// vertical edge
		edgeArray[p1 * 2].p1 = p1;
		edgeArray[p1 * 2].p2 = p2;
		edgeArray[p1 * 2].val = findVal(ch1,ch2,ch3,p1,p2);
	}

	y = 0;
	x = iWidth - 1;
	for(y = 0; y < iHeight - 1; ++y)
	{
		int p1 = (y * iWidth) + x;
		int p2 = p1 + iWidth;

		// horizontal edge
		edgeArray[(p1 * 2)+1].p1 = p1;
		edgeArray[(p1 * 2)+1].p2 = p2;
		edgeArray[(p1 * 2)+1].val = findVal(ch1,ch2,ch3,p1,p2);
	}

	// now sort the edges (probably the most expensive part of this entire operation).
	std::sort(edgeArray, edgeArray + (iWidth * iHeight * 2));

	// now let us prepare for the merging!
	uf.reset();
	resetStats(ch1,ch2,ch3);

	double Qg = 255.0 * sqrt(0.5 * log(2.0 / p));

	int p1, p2;
	int p1r, p2r;

	// first, note what our old maximum group number was
	int oldCutoff = maxGroup;
	++maxGroup;

	// first merge: merge unchanged pixels
	for(int i = 0; i < (iWidth*iHeight*2); ++i)
	{
		p1 = edgeArray[i].p1;
		p2 = edgeArray[i].p2;

		if(p1 < 0 || p2 < 0)
			continue;

		// don't merge if one of us has changed or if we're in different groups
		if(changeMask[p1] || changeMask[p2] || (retArray[p1] != retArray[p2]))
			continue;

		//std::cout << "I should never be called!\n";

		// otherwise, merge
		p1r = uf.Find(p1);
		p2r = uf.Find(p2);

		uf.UFUnion(p1r, p2r);
		//statsArray[p1r].merge(statsArray[p2r]);

		// cheat a bit to make sure new groups are formed
		statsArray[p1r].merge(statsArray[p2r], -1, maxGroup);
	}

	// second merge: merge everything again according to statistical rules
	//std::cout << "MaxGroup: " << maxGroup << std::endl;

	
	for(int i = 0; i < (iWidth*iHeight*2); ++i)
	{
		p1 = edgeArray[i].p1;
		p2 = edgeArray[i].p2;

		if(p1 < 0 || p2 < 0)
			continue;

		p1r = uf.Find(p1);
		p2r = uf.Find(p2);

		// (curLabels[p1]==curLabels[p2])
		// if we aren't already merged and the stats indicate we should, merge
		if(p1r != p2r && (false || shouldMerge(statsArray[p1r], statsArray[p2r], Qg)))
		{
			uf.UFUnion(p1r, p2r);

			// this merge call makes sure that new groups merge into old ones and that
			// new groups are properly created
			statsArray[p1r].merge(statsArray[p2r], oldCutoff, maxGroup);
		}
	}

	// now to fill in the return array thingy...
	int curR;
	int curSize;
	for(int curP = 0; curP < iWidth * iHeight; ++curP)
	{
		curR = uf.Find(curP);
		//curG = statsArray[curR].G;
		//retArray[curP] = changeMask[curR];
		curSize = statsArray[curR].N;
		sizeArray[curP] = curSize;
		retArray[curP] = curR;
	}

	return retArray;
}