void bigendian(FILE * fi,bool BOMfound) { if(BOMfound) { if(type[0]) { be = true; if(!strcmp(type,"BE")) result = 1; } else { printf("Byte order: big endian\n"); } } else if(!type[0]) printf("Unkown byte order. Assuming big endian.\n"); int kar; int s; while((kar = fgetc(fi)) != EOF) { s = kar << 8; if((kar = fgetc(fi)) == EOF) break; s += kar; surrogate(s); } printmb(BOMfound); }
void bigendian(FILE * fi,FILE * fo) { int kar; int s; while((kar = fgetc(fi)) != EOF) { s = kar << 8; if((kar = fgetc(fi)) == EOF) break; s += kar; surrogate(s,fo); } }
void littleendian(FILE * fi) { int kar; int s; while((kar = fgetc(fi)) != EOF) { s = kar; if((kar = fgetc(fi)) == EOF) break; s += kar << 8; surrogate(s); } printmb(true); }
/* static */ bool PluginAsyncSurrogate::Create(PluginModuleParent* aParent, NPMIMEType aPluginType, NPP aInstance, uint16_t aMode, int16_t aArgc, char* aArgn[], char* aArgv[]) { nsRefPtr<PluginAsyncSurrogate> surrogate(new PluginAsyncSurrogate(aParent)); if (!surrogate->Init(aPluginType, aInstance, aMode, aArgc, aArgn, aArgv)) { return false; } PluginAsyncSurrogate* rawSurrogate = nullptr; surrogate.forget(&rawSurrogate); aInstance->pdata = static_cast<PluginDataResolver*>(rawSurrogate); return true; }
/* static */ NPError PluginAsyncSurrogate::NPP_Destroy(NPP aInstance, NPSavedData** aSave) { PluginAsyncSurrogate* rawSurrogate = Cast(aInstance); MOZ_ASSERT(rawSurrogate); PluginModuleParent* module = rawSurrogate->GetParent(); if (module && !module->IsInitialized()) { // Take ownership of pdata's surrogate since we're going to release it nsRefPtr<PluginAsyncSurrogate> surrogate(dont_AddRef(rawSurrogate)); aInstance->pdata = nullptr; // We haven't actually called NPP_New yet, so we should remove the // surrogate for this instance. bool removeOk = module->RemovePendingSurrogate(surrogate); MOZ_ASSERT(removeOk); if (!removeOk) { return NPERR_GENERIC_ERROR; } surrogate->mInitCancelled = true; return NPERR_NO_ERROR; } return rawSurrogate->NPP_Destroy(aSave); }
int partition(int nodenum, pNode splitnode, double *sumrisk, int n1, int n2, int minsize, int split_Rule, double alpha, int bucketnum, int bucketMax, double train_to_est_ratio) { pNode me; double tempcp; int i, j, k; double tempcp2; double left_risk, right_risk; int left_split, right_split; double twt, ttr; int nleft, nright; int n; int min_node_size = minsize; FILE* fptr; me = splitnode; n = n2 - n1; /* total number of observations */ me->id = nodenum; //#ifdef DEBUG //fptr=fopen("C:\\Users\\vikasr\\Documents\\debug_text.txt","w"); //fprintf(fptr,"test print\n"); //fclose(fptr); R_FlushConsole(); //Rprintf("test print\n"); //R_ShowMessage("R_show_message\n"); //#endif if (nodenum > 1) { twt = 0; ttr = 0; k = 0; for (i = n1; i < n2; i++) { j = ct.sorts[0][i]; /* any variable would do, use first */ if (j < 0) j = -(1 + j); /* if missing, value = -(1+ true index) */ ct.wtemp[k] = ct.wt[j]; ct.trtemp[k] = ct.treatment[j]; ct.ytemp[k] = ct.ydata[j]; twt += ct.wt[j]; ttr += ct.treatment[j] * ct.wt[j]; k++; } if (split_Rule == 1) { // tot (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, ct.propensity); } else if (split_Rule == 2) { // ct (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } else if (split_Rule == 3) { // fit (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } else if (split_Rule == 4) { //tstats (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } else if (split_Rule == 5) { // totD (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, ct.propensity); } else if (split_Rule == 6) { // CTD (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } else if (split_Rule == 7) { //fitD (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } else if (split_Rule == 8) { //tstatsD (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } else if (split_Rule == 9) { // user (temporarily set as CT) (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } else if (split_Rule == 10) { // userD (temporarily set as CTD) (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); }else if (split_Rule == 11) { // policy (temporarily set as CTD) (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); }else if (split_Rule == 12) { // policyD (temporarily set as CTD) (*ct_eval) (n, ct.ytemp, me->response_est, me->controlMean, me->treatMean, &(me->risk), ct.wtemp, ct.trtemp, ct.max_y, alpha, train_to_est_ratio); } me->num_obs = n; me->sum_wt = twt; me->sum_tr = ttr; tempcp = me->risk; if (tempcp > me->complexity) tempcp = me->complexity; } else tempcp = me->risk; /* * Can I quit now ? */ if (me->num_obs < ct.min_split || tempcp <= ct.alpha || nodenum > ct.maxnode) { me->complexity = ct.alpha; *sumrisk = me->risk; /* * make sure the split doesn't have random pointers to somewhere * i.e., don't trust that whoever allocated memory set it to zero */ me->leftson = (pNode) NULL; me->rightson = (pNode) NULL; me->primary = (pSplit) NULL; me->surrogate = (pSplit) NULL; return 0; } /* * Guess I have to do the split */ bsplit(me, n1, n2, min_node_size, split_Rule, alpha, bucketnum, bucketMax, train_to_est_ratio); if (!me->primary) { /* * This is rather rare -- but I couldn't find a split worth doing */ me->complexity = ct.alpha; me->leftson = (pNode) NULL; me->rightson = (pNode) NULL; me->primary = (pSplit) NULL; me->surrogate = (pSplit) NULL; *sumrisk = me->risk; return 0; } #ifdef DEBUG print_tree(me, 4); #endif if (ct.maxsur > 0) surrogate(me, n1, n2); else me->surrogate = (pSplit) NULL; nodesplit(me, nodenum, n1, n2, &nleft, &nright); /* * split the leftson */ me->leftson = (pNode) CALLOC(1, nodesize); (me->leftson)->parent = me; (me->leftson)->complexity = tempcp - ct.alpha; left_split = partition(2 * nodenum, me->leftson, &left_risk, n1, n1 + nleft, min_node_size, split_Rule, alpha, bucketnum, bucketMax, train_to_est_ratio); /* * Update my estimate of cp, and split the right son. */ tempcp = (me->risk - left_risk) / (left_split + 1); tempcp2 = (me->risk - (me->leftson)->risk); if (tempcp < tempcp2) tempcp = tempcp2; if (tempcp > me->complexity) tempcp = me->complexity; me->rightson = (pNode) CALLOC(1, nodesize); (me->rightson)->parent = me; (me->rightson)->complexity = tempcp - ct.alpha; right_split = partition(1 + 2 * nodenum, me->rightson, &right_risk, n1 + nleft, n1 + nleft + nright, min_node_size, split_Rule, alpha, bucketnum, bucketMax, train_to_est_ratio); /* * Now calculate my actual C.P., which depends on children nodes, and * on grandchildren who do not collapse before the children. * The calculation is done assuming that I am the top node of the * whole tree, an assumption to be fixed up later. */ tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); /* Who goes first -- minimum of tempcp, leftson, and rightson */ if ((me->rightson)->complexity > (me->leftson)->complexity) { if (tempcp > (me->leftson)->complexity) { /* leftson collapses first */ left_risk = (me->leftson)->risk; left_split = 0; tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (tempcp > (me->rightson)->complexity) { /* right one goes too */ right_risk = (me->rightson)->risk; right_split = 0; } } } else if (tempcp > (me->rightson)->complexity) { /* right hand child goes first */ right_split = 0; right_risk = (me->rightson)->risk; tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (tempcp > (me->leftson)->complexity) { /* left one goes too */ left_risk = (me->leftson)->risk; left_split = 0; } } me->complexity = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (me->complexity <= ct.alpha) { /* * All was in vain! This node doesn't split after all. */ free_tree(me, 0); *sumrisk = me->risk; for (i = n1; i < n2; i++) { j = ct.sorts[0][i]; if (j < 0) j = -(1 + j); ct.which[j] = nodenum; /* revert to the old nodenumber */ } return 0; /* return # of splits */ } else { *sumrisk = left_risk + right_risk; return left_split + right_split + 1; } }
int main(int argc,char * argv[]) { int b1,b2,b3 = 0; FILE * fi = stdin; if(argc > 1) { fi = fopen(argv[1],"rb"); } if(argc > 2) { type = argv[2]; char * valid[] = {"LE" ,"BE" ,"UCS-2" ,"UCS-2-LE" ,"UCS-2-BE" ,"BIN" ,"UTF-16" ,"UTF-16-LE" ,"UTF-16-BE" ,"ASCII" ,"ISO" ,"UTF-8" ,"UTF-8-BOM" }; int i; for(i = sizeof(valid)/sizeof(valid[0]);--i >= 0;) if(!strcmp(type,valid[i])) break; if(i < 0) { printf("second argument must be one of:\n"); for(i = 0;i < sizeof(valid)/sizeof(valid[0]);++i) printf("%s\n",valid[i]); return 1; } } if(!fi) { printf("Cannot open input %s\n",argv[1]); return 1; } makeDuples(); b1 = fgetc(fi); b2 = fgetc(fi); if(b1 == 0xff && b2 == 0xfe) { if(type[0]) { le = true; if(!strcmp(type,"LE")) result = 1; } else { printf("Byte order: little endian\n"); } littleendian(fi); } else if(b1 == 0xfe && b2 == 0xff) { bigendian(fi,true); } else if(b1 == 0 || b2 == 0) // No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2); bigendian(fi,false); } else { FILE * ftempInput = tmpfile(); b3 = fgetc(fi); if(b3 == 0)// No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2); surrogate((b3 << 8) + fgetc(fi)); bigendian(fi,false); } else { if(b1 == 0xEF && b2 == 0xBB && b3 == 0xBF) // BOM found, probably UTF8 { ;// remove BOM } else { fputc(b1,ftempInput); fputc(b2,ftempInput); fputc(b3,ftempInput); b1 = b2 = b3 = 0; } int k; bool zeroFound = false; while((k = fgetc(fi)) != EOF) { if(k == 0) zeroFound = true; fputc(k,ftempInput); } rewind(ftempInput); if(zeroFound) { if(b1 && b2 && b3) { surrogate((b1 << 8) + b2); surrogate((b3 << 8) + fgetc(ftempInput)); } bigendian(ftempInput,false); } else { bool bom = false; if(b1 && b2 && b3) { if(type[0]) { bom = true; } else printf("BOM found, but not UTF-16. (UTF-8 file created in Windows?)\n"); } if(UTF8(ftempInput)) { utf8 = true; if(ascii) { if(type[0]) { if( !bom && (!strcmp(type,"ASCII") || !strcmp(type,"ISO")) || !strcmp(type,"UTF-8") || bom && !strcmp(type,"UTF-8-BOM") ) result = 1; } else { printf("encoding: ASCII (subset of UTF-8 and all ISO-8859 encodings)\n"); } } else { if(type[0]) { if(!strcmp(type,"UTF-8") || bom && !strcmp(type,"UTF-8-BOM")) result = 1; } else { printf("encoding: UTF-8\n"); } } } else { int c = 0; while((k = fgetc(ftempInput)) != EOF) getbyte(k); if(type[0]) { if(!bom && (!strcmp(type,"ASCII") || !strcmp(type,"ISO"))) result = 1; } else { printf("encoding: 8-bits\n"); } } } } } if(fi != stdin) fclose(fi); if(!type[0]) fprintf(stderr,"%s",report()); if(!utf8 && ascii) if(!type[0]) printf("File could have been encoded in ASCII!\n"); deleteDuples(); if(type[0]) { printf("[%d]\t%s\n",result,argv[1]); } return 0; }
int partition(int nodenum, pNode splitnode, double *sumrisk, int n1, int n2) { pNode me; double tempcp; int i, j, k; double tempcp2; double left_risk, right_risk; int left_split, right_split; double twt; int nleft, nright; int n; me = splitnode; n = n2 - n1; /* total number of observations */ if (nodenum > 1) { twt = 0; k = 0; for (i = n1; i < n2; i++) { j = rp.sorts[0][i]; /* any variable would do, use first */ if (j < 0) j = -(1 + j); /* if missing, value = -(1+ true index) */ rp.wtemp[k] = rp.wt[j]; rp.ytemp[k] = rp.ydata[j]; twt += rp.wt[j]; k++; } (*rp_eval) (n, rp.ytemp, me->response_est, &(me->risk), rp.wtemp); me->num_obs = n; me->sum_wt = twt; tempcp = me->risk; if (tempcp > me->complexity) tempcp = me->complexity; } else tempcp = me->risk; /* * Can I quit now ? */ if (me->num_obs < rp.min_split || tempcp <= rp.alpha || nodenum > rp.maxnode) { me->complexity = rp.alpha; *sumrisk = me->risk; /* * make sure the split doesn't have random pointers to somewhere * i.e., don't trust that whoever allocated memory set it to zero */ me->leftson = (pNode) NULL; me->rightson = (pNode) NULL; me->primary = (pSplit) NULL; me->surrogate = (pSplit) NULL; return 0; } /* * Guess I have to do the split */ bsplit(me, n1, n2); if (!me->primary) { /* * This is rather rare -- but I couldn't find a split worth doing */ me->complexity = rp.alpha; me->leftson = (pNode) NULL; me->rightson = (pNode) NULL; me->primary = (pSplit) NULL; me->surrogate = (pSplit) NULL; *sumrisk = me->risk; return 0; } #ifdef DEBUG print_tree(me, 2); #endif if (rp.maxsur > 0) surrogate(me, n1, n2); else me->surrogate = (pSplit) NULL; nodesplit(me, nodenum, n1, n2, &nleft, &nright); /* * split the leftson */ me->leftson = (pNode) CALLOC(1, nodesize); (me->leftson)->complexity = tempcp - rp.alpha; left_split = partition(2 * nodenum, me->leftson, &left_risk, n1, n1 + nleft); /* * Update my estimate of cp, and split the right son. */ tempcp = (me->risk - left_risk) / (left_split + 1); tempcp2 = (me->risk - (me->leftson)->risk); if (tempcp < tempcp2) tempcp = tempcp2; if (tempcp > me->complexity) tempcp = me->complexity; me->rightson = (pNode) CALLOC(1, nodesize); (me->rightson)->complexity = tempcp - rp.alpha; right_split = partition(1 + 2 * nodenum, me->rightson, &right_risk, n1 + nleft, n1 + nleft + nright); /* * Now calculate my actual C.P., which depends on children nodes, and * on grandchildren who do not collapse before the children. * The calculation is done assuming that I am the top node of the * whole tree, an assumption to be fixed up later. */ tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); /* Who goes first -- minimum of tempcp, leftson, and rightson */ if ((me->rightson)->complexity > (me->leftson)->complexity) { if (tempcp > (me->leftson)->complexity) { /* leftson collapses first */ left_risk = (me->leftson)->risk; left_split = 0; tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (tempcp > (me->rightson)->complexity) { /* right one goes too */ right_risk = (me->rightson)->risk; right_split = 0; } } } else if (tempcp > (me->rightson)->complexity) { /* right hand child goes first */ right_split = 0; right_risk = (me->rightson)->risk; tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (tempcp > (me->leftson)->complexity) { /* left one goes too */ left_risk = (me->leftson)->risk; left_split = 0; } } me->complexity = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (me->complexity <= rp.alpha) { /* * All was in vain! This node doesn't split after all. */ free_tree(me, 0); *sumrisk = me->risk; for (i = n1; i < n2; i++) { j = rp.sorts[0][i]; if (j < 0) j = -(1 + j); rp.which[j] = nodenum; /* revert to the old nodenumber */ } return 0; /* return # of splits */ } else { *sumrisk = left_risk + right_risk; return left_split + right_split + 1; } }
int main(int argc,char * argv[]) { int b1,b2,b3 = 0; FILE * fi = stdin; FILE * fo = stdout; if(argc > 1) { fi = fopen(argv[1],"rb"); if(argc > 2) fo = fopen(argv[2],"wb"); } if(!fi || !fo) return 1; makeDuples(); b1 = fgetc(fi); b2 = fgetc(fi); if(b1 == 0xff && b2 == 0xfe) littleendian(fi,fo); else if(b1 == 0xfe && b2 == 0xff) bigendian(fi,fo); else if(b1 == 0 || b2 == 0) // No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2,fo); bigendian(fi,fo); } else { FILE * ftempInput = tmpfile(); b3 = fgetc(fi); if(b3 == 0)// No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2,fo); surrogate((b3 << 8) + fgetc(fi),fo); bigendian(fi,fo); } else { if(b1 == 0xEF && b2 == 0xBB && b3 == 0xBF) // BOM found, probably UTF8 ; // remove BOM else { fputc(b1,ftempInput); fputc(b2,ftempInput); fputc(b3,ftempInput); b1 = b2 = b3 = 0; } int k; bool zeroFound = false; while((k = fgetc(fi)) != EOF) { if(k == 0) zeroFound = true; fputc(k,ftempInput); } rewind(ftempInput); if(zeroFound) { if(b1 && b2 && b3) { surrogate((b1 << 8) + b2,fo); surrogate((b3 << 8) + fgetc(ftempInput),fo); } bigendian(ftempInput,fo); } else if(!UTF8(ftempInput,fo)) { rewind(ftempInput); if(b1 && b2 && b3) // "BOM" found, but not in UTF8 file! { // write "BOM" fputc(b1,fo); fputc(b2,fo); fputc(b3,fo); } copy(ftempInput,fo); } } } if(fi != stdin) fclose(fi); if(fo != stdout) fclose(fo); fprintf(stderr,"%s",report()); deleteDuples(); return 0; }