//-----Main------// int main(void) { //RN_test_basic(); float Ntry=10000000; int Nbin = 10; int i; int R; int arrray_sz=10 int tally[array_sz]; for(i=0; i<Ntry; i++) { R=rand(); Nbin = 1 + floor(Nbin*R); tally(bin) = tally(bin) +1; if(mod(i,Ntry)==0) { //print tally() array int j; for(j=0;j<array_sz; j++) { printf('For bin %d the number of rands is %d',j,tally(j)); } } } }
void partFound(GMimePart* part, gpointer tallied_length) { const GMimeContentType* ct = 0; const gchar* content = 0; int contentLen = 0; int i = 0; int n = *(int*)tallied_length; ct = g_mime_part_get_content_type(part); /* printf("%s/%s\n", ct->type, ct->subtype); */ if (ct != 0 && strncasecmp(ct->type, textType, textTypeLen) == 0 ) { if (strncasecmp(ct->subtype, plainSubType, plainSubTypeLen) == 0) { content = g_mime_part_get_content(part, &contentLen); n += tally(content, 0, contentLen, n, 0); } else if (strncasecmp(ct->subtype, htmlSubType, htmlSubTypeLen) == 0) { gchar curChar = '\000'; int beg = 0; content = g_mime_part_get_content(part, &contentLen); for (i=0; i<contentLen; ++i) { curChar = content[i]; if (curChar == '<') { if (i != beg) n += tally(content, beg, i, n, 0); } if (curChar == '>') beg = i+1; } if (i != beg) n += tally(content, beg, i, n, 0); } } *((int*)tallied_length) = n; }
int main() { while ( getch() != 'q') tally(); return 0; }
/* * information from a gmon.out file is in two parts: * an array of sampling hits within pc ranges, * and the arcs. */ void getpfile(const char *filename) { FILE *pfile; struct rawarc arc; pfile = openpfile(filename); readsamples(pfile); /* * the rest of the file consists of * a bunch of <from,self,count> tuples. */ while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) { # ifdef DEBUG if ( debug & SAMPLEDEBUG ) { printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" , arc.raw_frompc , arc.raw_selfpc , arc.raw_count ); } # endif /* DEBUG */ /* * add this arc */ tally( &arc ); } fclose(pfile); }
nialptr to_real(nialptr x) { nialptr z, xi; nialint i, t = tally(x); int v = valence(x); double r = 0.0; /* create the result container */ z = new_create_array(realtype, v, 0, shpptr(x, v)); for (i = 0; i < t; i++) { int k; xi = fetch_array(x, i); k = kind(xi); if (k == realtype) r = *pfirstreal(xi); else /* must be boolean or integer */ if (k == inttype) r = 1.0 * intval(xi); else if (k == booltype) r = 1.0 * boolval(xi); store_real(z, i, r); } return (z); }
void isetprompt(void) { nialptr z; int t; z = apop(); if (kind(z) == phrasetype || kind(z) == chartype) { /* return old prompt */ apush(makephrase(prompt)); /* get new prompt */ if (kind(z) == phrasetype) { t = tknlength(z); if (t > MAXPROMPTSIZE) goto spout; strcpy(prompt, pfirstchar(z)); } else if (kind(z) == chartype) { t = tally(z); if (t > MAXPROMPTSIZE) goto spout; strcpy(prompt, pfirstchar(z)); } } else buildfault("arg to setprompt must be string or phrase"); freeup(z); return; spout: freeup(apop()); /* old prompt already stacked */ buildfault("prompt too long"); }
/** * Initializes all the elements in the EndResearch screen. * @param game Pointer to the core game. * @param base Pointer to the base to get info from. * @param possibilities List of newly possible ResearchProject */ NewPossibleResearchState::NewPossibleResearchState(Base * base, const std::vector<RuleResearch *> & possibilities) : _base(base) { _screen = false; // Create objects _window = new Window(this, 288, 180, 16, 10); _btnOk = new TextButton(160, 14, 80, 149); _btnResearch = new TextButton(160, 14, 80, 165); _txtTitle = new Text(288, 40, 16, 20); _lstPossibilities = new TextList(250, 96, 35, 50); // Set palette setInterface("geoResearch"); add(_window, "window", "geoResearch"); add(_btnOk, "button", "geoResearch"); add(_btnResearch, "button", "geoResearch"); add(_txtTitle, "text1", "geoResearch"); add(_lstPossibilities, "text2", "geoResearch"); centerAllSurfaces(); // Set up objects _window->setBackground(_game->getMod()->getSurface("BACK05.SCR")); _btnOk->setText(tr("STR_OK")); _btnOk->onMouseClick((ActionHandler)&NewPossibleResearchState::btnOkClick); _btnOk->onKeyboardPress((ActionHandler)&NewPossibleResearchState::btnOkClick, Options::keyCancel); _btnResearch->setText(tr("STR_ALLOCATE_RESEARCH")); _btnResearch->onMouseClick((ActionHandler)&NewPossibleResearchState::btnResearchClick); _btnResearch->onKeyboardPress((ActionHandler)&NewPossibleResearchState::btnResearchClick, Options::keyOk); _txtTitle->setBig(); _txtTitle->setAlign(ALIGN_CENTER); _lstPossibilities->setColumns(1, 250); _lstPossibilities->setBig(); _lstPossibilities->setAlign(ALIGN_CENTER); _lstPossibilities->setScrolling(true, 0); size_t tally(0); for (std::vector<RuleResearch *>::const_iterator iter = possibilities.begin(); iter != possibilities.end(); ++iter) { bool liveAlien = (*iter)->needItem() && _game->getMod()->getUnit((*iter)->getName()) != 0; if (!_game->getSavedGame()->wasResearchPopped(*iter) && (*iter)->getRequirements().empty() && !liveAlien) { _game->getSavedGame()->addPoppedResearch((*iter)); _lstPossibilities->addRow (1, tr((*iter)->getName()).c_str()); } else { tally++; } } if (!(tally == possibilities.size() || possibilities.empty())) { _txtTitle->setText(tr("STR_WE_CAN_NOW_RESEARCH")); } }
main(int argc,char **argv) { if (read(0,key,sizeof key) < sizeof key) return 111; AES_set_encrypt_key(key,128,&expanded); /* preload */ m5_setpid(lock); AES_encrypt(packet,workarea,&expanded); m5_setpid(unlock); int power2=4; int j; size = 16; struct pollfd p; long long inputs = 0; // study; for (;;){ for (;;){ if (size < 16) continue; if (size > sizeof packet) continue; for (j = 0; j < size; ++j) packet[j] = random(); j = 0; n[j] = packet[j]; int i; for (i = 0;i < 48;++i) out[i] = 0; i = 0; out[i] = packet[i]; for (i = 0;i < 16;++i) workarea[i] = packet[i]; m5_setpid(lock); *(unsigned long long *)(out + 32) = rdtsc(); AES_encrypt(packet,workarea,&expanded); /* a real server would now check AES-based authenticator, */ /* process legitimate packets, and generate useful output */ *(unsigned long long *) (out + 40) = rdtsc(); m5_setpid(unlock); i = 0; out[16 + i] = scrambledzero[i]; unsigned long long timing; timing = *(unsigned long long *) (out + 40); timing -= *(unsigned long long *) (out + 32); tally(timing);break; } ++inputs; if (timetoprint(inputs)){ printpatterns(); } if (power2 == 17){ return 1; } } }
/** * Initializes all the elements in the EndResearch screen. * @param game Pointer to the core game. * @param base Pointer to the base to get info from. * @param possibilities List of newly possible ResearchProject */ NewPossibleResearchState::NewPossibleResearchState(Game * game, Base * base, const std::vector<RuleResearch *> & possibilities) : State (game), _base(base) { _screen = false; // Create objects _window = new Window(this, 288, 180, 16, 10); _btnOk = new TextButton(160, 14, 80, 149); _btnResearch = new TextButton(160, 14, 80, 165); _txtTitle = new Text(288, 40, 16, 20); _lstPossibilities = new TextList(288, 80, 16, 56); // Set palette _game->setPalette(_game->getResourcePack()->getPalette("BACKPALS.DAT")->getColors(Palette::blockOffset(1)), Palette::backPos, 16); add(_window); add(_btnOk); add(_btnResearch); add(_txtTitle); add(_lstPossibilities); // Set up objects _window->setColor(Palette::blockOffset(15)-1); _window->setBackground(_game->getResourcePack()->getSurface("BACK05.SCR")); _btnOk->setColor(Palette::blockOffset(8)+5); _btnOk->setText(_game->getLanguage()->getString("STR_OK")); _btnOk->onMouseClick((ActionHandler)&NewPossibleResearchState::btnOkClick); _btnResearch->setColor(Palette::blockOffset(8)+5); _btnResearch->setText(_game->getLanguage()->getString("STR_ALLOCATE_RESEARCH")); _btnResearch->onMouseClick((ActionHandler)&NewPossibleResearchState::btnResearchClick); _txtTitle->setColor(Palette::blockOffset(15)-1); _txtTitle->setBig(); _txtTitle->setAlign(ALIGN_CENTER); _lstPossibilities->setColor(Palette::blockOffset(8)+10); _lstPossibilities->setColumns(1, 288); _lstPossibilities->setBig(); _lstPossibilities->setAlign(ALIGN_CENTER); size_t tally(0); for(std::vector<RuleResearch *>::const_iterator iter = possibilities.begin (); iter != possibilities.end (); ++iter) { if((*iter)->getRequirements().size() == 0 && (*iter)->getStringTemplate().size() == 0 ) { _lstPossibilities->addRow (1, _game->getLanguage()->getString((*iter)->getName ()).c_str()); } else { tally++; } } if (!(tally == possibilities.size () || possibilities.empty ())) { _txtTitle->setText(_game->getLanguage()->getString("STR_WE_CAN_NOW_RESEARCH")); } }
nialptr arithconvert(nialptr x, int *newk) { int k, ki, changed; nialint i, t = tally(x); nialptr z, x0, xi; if (t == 0) { /* x is empty */ *newk = kind(x); return (x); } /* set k based on kind of first item */ x0 = fetch_array(x, 0); /* first item */ k = kind(x0); /* loop to find maximum kind in x */ for (i = 1; i < t; i++) { xi = fetch_array(x, i); ki = kind(xi); if (ki > k) k = ki; } /* convert to highest kind */ changed = true; if (homotype(k) && k != chartype) { switch (k) { case inttype: z = to_int(x); break; case realtype: z = to_real(x); break; default: z = x; changed = false; break; } } else { z = x; changed = false; } if (changed) /* argument has been converted */ freeup(x); *newk = k; return (z); }
void run(int iterations, int burn, int thin) { if(logp()==-std::numeric_limits<double>::infinity()) { throw std::logic_error("ERROR: cannot start from a logp of -Inf."); } for(int i = 1; i <= (iterations + burn); i++) { step(); if(i > burn && (i % thin == 0)) { tally(); } } }
void iloaddefs(void) { nialptr nm, x = apop(); int mode; /* get the file name as a Nial array */ if (atomic(x) || kind(x) == chartype) nm = x; else if (kind(x) == atype) nm = fetch_array(x, 0); else { buildfault("invalid file name"); freeup(x); return; } mode = 0; /* default to silent mode */ if (kind(x) == atype && tally(x) == 2) { /* argument has a mode filed, select it */ nialptr it = fetch_array(x, 1); if (kind(it) == inttype) mode = intval(it); if (kind(it) == booltype) mode = boolval(it); } /* try to put filename into gcharbuf */ if (!ngetname(nm, gcharbuf)) { buildfault("invalid file name"); freeup(x); } else { /* check the extension as .ndf */ check_ext(gcharbuf, ".ndf",NOFORCE_EXTENSION); freeup(x); /* do freeup here so file name doesn't show in iusedspace */ /* load the definition file */ if (loaddefs(true, gcharbuf, mode)) { apush(Nullexpr); } else buildfault(errmsgptr); /* this is safe since call is from iloaddefs */ } #ifdef DEBUG memchk(); #endif }
int main(int argc, char *argv[]) { if (argc<2) {printf("Please specify output file.\n"); exit(1);} else strcpy(fname,argv[1]); // TALLY X PER CONTRACTED INTEGRAL init(); while (!feof(fptr)) { read; w=(l+1)*(l+1)*cart(a)*cart(b); tally(); } printstat("X statistics per CONTRACTED INTEGRAL"); // TALLY X PER INTPACK CALL init(); w=1; while (!feof(fptr)) { read; tally(); } printstat("X statistics per INTPACK CALL"); printf("Total X = %e\n",S); }
nialptr testfaults(nialptr x, nialptr stdfault) { int found = false; nialint t = tally(x), i = 0; nialptr xi = Null, z; /* find first fault */ while (!found && i < t) { xi = fetch_array(x, i++); if (kind(xi) == chartype || kind(xi) == phrasetype) return (stdfault); /* result is stdfault if other literal types occur */ found = kind(xi) == faulttype; } if (!found) return (stdfault); /* used in "and" and "or" in cases like "and o -12" */ z = xi; #ifdef V4AT if (stdfault==Logical) { /* Logical faults are different in V4 */ i = 0; /* scan all the items */ /* find other faults and compare */ while (i < t) { xi = fetch_array(x, i++); if ((kind(xi) == faulttype && z != xi) || kind(xi) != faulttype) /* result is stdfault if fault value changes or other type */ return (stdfault); } } else #endif /* find other faults and compare */ { while (i < t) { xi = fetch_array(x, i++); if ((kind(xi) == faulttype && z != xi) || kind(xi) == chartype || kind(xi) == phrasetype) /* result is stdfault if fault value changes or other literal type */ return (stdfault); } } freeup(stdfault); /* since this is a temporary and not used */ return (z); }
nialptr bool_to_real(nialptr x) { nialint i, t = tally(x); nialptr z; double *pz; int v = valence(x); z = new_create_array(realtype, v, 0, shpptr(x, v)); pz = pfirstreal(z); /* safe */ for (i = 0; i < t; i++) *pz++ = 1.0 * fetch_bool(x, i); freeup(x); return (z); }
int convert(nialptr * x, int *kx, int k) { if (numeric(k)) { nialptr z = Null; int v = valence(*x); nialint i, t = tally(*x); switch (k) { case inttype: /* must be converting a booltype array */ { nialint *zptr; z = new_create_array(inttype, v, 0, shpptr(*x, v)); zptr = pfirstint(z); /* safe */ for (i = 0; i < t; i++) *zptr++ = fetch_bool(*x, i); } break; case realtype: /* converting a boolean or integer array */ { double *zptr; z = new_create_array(realtype, v, 0, shpptr(*x, v)); zptr = pfirstreal(z); /* safe */ if (*kx == booltype) { for (i = 0; i < t; i++) *zptr++ = (double) fetch_bool(*x, i); } else { nialint *xptr = pfirstint(*x); /* safe */ for (i = 0; i < t; i++) *zptr++ = (double) *xptr++; } } break; } freeup(*x); *x = z; *kx = kind(z); return true; } return false; }
nialptr int_to_real(nialptr x) { nialint i, *px, t = tally(x); nialptr z; double *pz; int v = valence(x); z = new_create_array(realtype, v, 0, shpptr(x, v)); px = pfirstint(x); /* safe */ pz = pfirstreal(z); /* safe */ for (i = 0; i < t; i++) *pz++ = 1.0 * *px++; freeup(x); return (z); }
nialptr boolstoints(nialptr x) { nialptr z; nialint i, t = tally(x), *ptrz; int v = valence(x); z = new_create_array(inttype, v, 0, shpptr(x, v)); ptrz = pfirstint(z); /* safe */ for (i = 0; i < t; i++) { int xi; xi = fetch_bool(x, i); *ptrz++ = xi; } return (z); }
static nialptr to_int(nialptr x) { nialptr z, xi; nialint i, t = tally(x); int v = valence(x); /* create the result container */ z = new_create_array(inttype, v, 0, shpptr(x, v)); for (i = 0; i < t; i++) { xi = fetch_array(x, i); if (kind(xi) == inttype) { copy1(z, i, xi, 0); } else /* type must be boolean */ store_int(z, i, boolval(xi)); } return (z); }
int putDownNetwork(SimView *view, short mapH, short mapV) { int tile = Map[mapH][mapV] & LOMASK; if ((TotalFunds > 0) && tally(tile)) { Map[mapH][mapV] = tile = 0; Spend(1); } if (tile == 0) { if ((TotalFunds - CostOf[view->tool_state]) >= 0) { Map[mapH][mapV] = TELEBASE | CONDBIT | BURNBIT | BULLBIT | ANIMBIT; Spend(CostOf[view->tool_state]); return 1; } else { return -2; } } else { return -1; } }
int tally_string(const char *string, int tallied_length) { return tally(string, 0, strlen(string), tallied_length, 0); }
int loaddefs(int fromfile, char *fname, int mode) { nialptr ts; int repeatloop, keepreading, nolines, inremark, linecnt; FILE *f1 = NULL; /* initialized to avoid complaint */ int errorsfound; if (fromfile) { f1 = openfile(fname, 'r', 't'); if (f1 == OPENFAILED) return (false); pushsysfile(f1); } /* a loaddefs always affects the global environment. We reset current_env to relect this. The code to restore the environment is below. This must be saved on the stack, otherwise it can get thrown away since it may only be owned by a transient definition value. The following example failed before I protected this on the stack: retry is { host 'vi bug.ndf'; loaddefs"bug l } where this definition was in the file bug.ndf. */ apush(current_env); current_env = Null; ts = topstack; /* to monitor stack growth on each action */ errorsfound = 0; /* reset parse error counter */ repeatloop = true; linecnt = 0; /* loop to pick up groups of lines */ while (repeatloop) { /* continue as long as their are line groups */ /* test on each circuit if an interrupt signal has been posted */ #ifdef USER_BREAK_FLAG if (fromfile) checksignal(NC_CS_NORMAL); #endif inremark = false; nolines = 0; keepreading = true; /* loop to pick up lines until a whitespace line occurs */ while (keepreading) { if (fromfile) { /* reading a line from the file */ readfileline(f1, (mode ? 2 : 0)); /* mode==2 only in a loaddefs */ /* readfileline places result on the stack */ if (top == Eoffault) { apop(); /* to remove the end of file marker */ repeatloop = false; break; /* to end read loop */ } } else { /* select a line from array defsndf loadded from defstbl.h */ char *line; line = defsndf[linecnt++]; if (linecnt == NOLINES) { repeatloop = false; keepreading = false; /* to end read loop */ } mkstring(line); /* convert the line to a Nial string and push it */ } if (nolines == 0) { /* check first line of group for a remark */ char firstchar; int i = 0; /* loop to skip blanks */ while (i < tally(top) && fetch_char(top, i) <= BLANK) i++; /* note whether first char is "#" */ firstchar = fetch_char(top, i); if (tally(top)) inremark = firstchar == HASHSYMBOL; else inremark = false; } /* if the line is all while space then we are at the end of a group */ if (top == Null || allwhitespace(pfirstchar(top))) { keepreading = false; freeup(apop()); /* to get rid of the empty line */ } else /* count the line on the stack */ nolines++; } /* we have a group of lines to process */ if (nolines > 0) { mklist(nolines); /* create a list of lines and link them*/ ilink(); if (inremark) { freeup(apop()); /* remarks are ignored */ } else { /* carry out the actions of the main loop */ iscan(); parse(true); /* check whether parse produced an error */ if (kind(top) == faulttype) { if (top != Nullexpr) { errorsfound++; if (mode == 0) { /* show error message */ apush(top); ipicture(); show(apop()); } } } /* evaluate the parse tree, if it is a fault, it is the value returned */ ieval(); #ifdef DEBUG memchk(); #endif if (mode) { /* show the result */ if (top != Nullexpr) { ipicture(); show(apop()); } else apop(); /* the Nullexpr */ } else freeup(apop()); /* free because it might not be Nullexpr */ } if (mode) { /* now display empty line */ writechars(STDOUT, "", (nialint) 0, true); if (keeplog && f1 == STDIN) writelog("", 0, true); } } /* check that the stack hasn't grown */ if (ts != topstack) { while (ts != topstack) freeup(apop()); exit_cover(NC_STACK_GROWN_I); } } /* done reading groups of lines */ if (fromfile) { closefile(f1); popsysfile(); } /* restore the current_env */ current_env = apop(); if (errorsfound > 0) nprintf(OF_NORMAL_LOG, "errors found: %d\n", errorsfound); return (true); }
state_name tally_star(void * dfaSet, StateMachine * stateMachine, Message * m, char * from) { return tally(broadcast_lists[1], broadcast_list_host_count[1], (char*)(&(stateMachine->app_state)), from); }
void wsload(FILE * f1, int user) { nialptr addr, cnt, lastfree, nextaddr; char stampcheck[256]; /* read global structure */ testrderr(readblock(f1, (char *) &G, (long) sizeof G, false, 0L, 0)); /* nprintf(OF_DEBUG,"wssize read in is %ld\n",wssize); */ /* check workspace stamp */ #ifdef DEBUG strcpy(stampcheck, systemname); strcat(stampcheck, nialversion); strcat(stampcheck, " (debug)"); #else strcpy(stampcheck, systemname); strcat(stampcheck, nialversion); #endif if (strcmp(stampcheck, wsstamp) != 0) { if (user) exit_cover(NC_USER_WS_WRONG_VERSION_W); else exit_cover(NC_WS_WRONG_VERSION_S); } /* check that the new workspace will fit in the current memory */ /* printf("wssize %d memsize %d instartup %d\n",wssize,memsize,instartup); */ if (wssize + MINHEAPSPACE > memsize) { if (expansion) expand_heap(wssize - memsize); else { if (interpreter_running) { nprintf(OF_MESSAGE, "Not enough memory to load the workspace\n"); exit_cover(NC_MEM_EXPAND_FAILED_W); } else { nprintf(OF_NORMAL, "Unable to load the workspace\n"); longjmp(init_buf, NC_MEM_EXPAND_NOT_TRIED_F); } } } /* set the link to the first free block */ fwdlink(freelisthdr) = firstfree; /* read memory blocks */ lastfree = freelisthdr; nextaddr = membase; testrderr(readblock(f1, (char *) &cnt, (long) sizeof cnt, false, 0L, 0)); testrderr(readblock(f1, (char *) &addr, (long) sizeof addr, false, 0L, 0)); while (cnt != 0) { /* nprintf(OF_DEBUG,"reading addr %d cnt %d\n",addr,cnt); */ testrderr(readblock(f1, (char *) &mem[addr], (long) (bytespu * cnt), false, 0L, 0)); nextaddr = addr + cnt; /* get info for next block */ testrderr(readblock(f1, (char *) &cnt, (long) sizeof cnt, false, 0L, 0)); if (cnt != 0) { /* still blocks to read */ testrderr(readblock(f1, (char *) &addr, (long) sizeof addr, false, 0L, 0)); /* set up free block between used blocks */ fwdlink(lastfree) = nextaddr; blksize(nextaddr) = addr - nextaddr; /* nprintf(OF_DEBUG,"inserting freeblock %d size %d\n",nextaddr,blksize(nextaddr)); */ bcklink(nextaddr) = lastfree; set_freetag(nextaddr); /* sets free tag */ set_endinfo(nextaddr); lastfree = nextaddr; } } if (nextaddr < memsize) { /* add in last free block */ fwdlink(lastfree) = nextaddr; blksize(nextaddr) = memsize - nextaddr; /* nprintf(OF_DEBUG,"adding last freeblock %d size %d\n",nextaddr,blksize(nextaddr)); */ bcklink(nextaddr) = lastfree; set_freetag(nextaddr); set_endinfo(nextaddr); fwdlink(nextaddr) = TERMINATOR; /* to complete the chain */ } else fwdlink(lastfree) = TERMINATOR; /* to complete the chain */ /* reset atomtbl stuff */ atomtblsize = tally(atomtblbase); atomtbl = pfirstitem(atomtblbase); #ifdef DEBUG memchk(); #endif closefile(f1); return; fail: closefile(f1); nprintf(OF_NORMAL_LOG, "workspace failed to read correctly\n"); exit_cover(NC_WS_LOAD_ERR_F); }
short check6x6(SimView *view, short mapH, short mapV, short base, short tool) { register short rowNum, columnNum; short h, v; short holdMapH; short xMap, yMap; short flag; short tileValue; short cost = 0; mapH--; mapV--; if ((mapH < 0) || (mapH > (WORLD_X - 6)) || (mapV < 0) || (mapV > (WORLD_Y - 6))) return -1; h = xMap = holdMapH = mapH; v = yMap = mapV; flag = 1; for (rowNum = 0; rowNum <= 5; rowNum++) { mapH = holdMapH; for (columnNum = 0; columnNum <= 5; columnNum++) { tileValue = Map[mapH++][mapV] & LOMASK; if (autoBulldoze) { /* if autoDoze is enabled, add up the cost of bulldozed tiles */ if (tileValue != 0) if (tally(tileValue)) { cost++; } else { flag = 0; } } else { /* check and see if the tile is clear or not */ if (tileValue != 0) { flag = 0; } } } mapV++; } if (flag == 0) { return -1; } cost += CostOf[tool]; if ((TotalFunds - cost) < 0) { return -2; } if ((Players > 1) && (OverRide == 0) && (cost >= Expensive) && (view != NULL) && (view->super_user == 0)) { return -3; } /* take care of the money situtation here */ Spend(cost); UpdateFunds(); mapV = v; holdMapH = h; for (rowNum = 0; rowNum <= 5; rowNum++) { mapH = holdMapH; for (columnNum = 0; columnNum <= 5; columnNum++) { if (columnNum == 1 && rowNum == 1) { Map[mapH++][mapV] = base + BNCNBIT + ZONEBIT; } else { Map[mapH++][mapV] = base + BNCNBIT; } base++; } mapV++; } check6x6border(xMap, yMap); return 1; }
void ifromraw(void) { nialptr z = apop(); nialptr res; int slen; int totype; int dataerror = 0; int numelements; nialptr bools; if (tally(z) != 2) { apush(makefault("?Must supply a type and a value to fromraw")); freeup(z); return; } totype = kind(fetch_array(z, 0)); if (totype == atype) { apush(makefault("?fromraw cannot convert to a nested array")); freeup(z); return; } bools = fetch_array(z, 1); if ((kind(bools) != booltype)) { apush(makefault("?Must supply a Boolean value to fromraw")); freeup(z); return; } /* no conversion needed */ if (totype == booltype) { apush(bools); freeup(z); return; } /* compute number of elements by type and validate length of bools */ slen = 0; switch (totype) { case inttype: numelements = tally(bools) / sizeof(int); if (tally(bools) % sizeof(int) != 0) dataerror = 1; break; case realtype: numelements = tally(bools) / sizeof(double); if (tally(bools) % sizeof(double) != 0) dataerror = 1; break; case phrasetype: case faulttype: slen = tally(bools) / sizeof(char); numelements = 1; case chartype: numelements = tally(bools) / sizeof(char); break; } numelements /= 8; if (dataerror) { apush(makefault("?fromraw must have correct multiple of bools for desired datatype")); freeup(z); return; } /* create the result copy */ res = new_create_array(totype, 1, slen, &numelements); /* do the data copy */ switch (totype) { case inttype: memcpy(pfirstchar(res), pfirstchar(bools), tally(bools) / 8); break; case realtype: memcpy_fwo(pfirstchar(res), pfirstchar(bools), tally(bools) / 8, MCPY_FROMRAW); break; /* Have to use strncpy here to preserve the data */ case chartype: case phrasetype: case faulttype: memcpy_fbo(pfirstchar(res), pfirstchar(bools), tally(bools) / 8, MCPY_FROMRAW); break; } if (totype == chartype) *(pfirstchar(res) + numelements) = '\0'; apush(res); freeup(z); return; }
void itoraw(void) { nialptr z = apop(); nialptr res; int bits = 0; /* can only convert non-nested arrays */ if (kind(z) == atype) { apush(makefault("?Must supply a non-nested array value to toraw")); freeup(z); return; } /* if we are given a bool array then just return it */ if (kind(z) == booltype) { apush(z); return; } /* for the remaining types, calculate the number of bytes */ switch (kind(z)) { case inttype: bits = tally(z) * sizeof(nialint); break; case realtype: bits = tally(z) * sizeof(double); break; case chartype: bits = tally(z); break; case phrasetype: case faulttype: bits = strlen(pfirstchar(z)); break; } /* from bytes to bits */ bits *= 8; /* Create the new array */ res = new_create_array(booltype, 1, 0, &bits); /* This assumes the same order of bools and a bool array shows */ /* do the data copy */ switch (kind(z)) { case inttype: memcpy(pfirstchar(res), pfirstchar(z), bits / 8); break; case realtype: memcpy_fwo(pfirstchar(res), pfirstchar(z), bits / 8, MCPY_TORAW); break; /* Have to use strncpy here to preserve the data */ case chartype: case phrasetype: case faulttype: memcpy_fbo(pfirstchar(res), pfirstchar(z), bits / 8, MCPY_TORAW); break; } /* push the result and leave */ apush(res); freeup(z); return; }
void iinnerproduct() { nialptr z, a, b, x; int va, vb, vx, replicatea, replicateb; nialint m, n, bn, p, i, j, k, sh[2]; double sum, *ap, *bp, *xp; z = apop(); if (tally(z) != 2) { buildfault("arg to innerproduct not a pair"); freeup(z); return; } splitfb(z, &a, &b); va = valence(a); vb = valence(b); if (va > 2 || vb > 2) { buildfault("incorrect valence in innerproduct"); freeup(a); freeup(b); freeup(z); return; } /* ensure b is realtype */ switch (kind(b)) { case booltype: b = bool_to_real(b); break; case inttype: b = int_to_real(b); break; case realtype: break; case chartype: case phrasetype: case faulttype: { buildfault("second arg not numeric type in innerproduct"); freeup(a); freeup(b); freeup(z); return; } case atype: { nialint i = 0; if (!simple(b)) { buildfault("arg not simple in innerproduct"); freeup(a); freeup(b); freeup(z); return; } while (i < tally(b)) { if (!numeric(kind(fetch_array(b, i)))) { buildfault("second arg not numeric type in innerproduct"); freeup(a); freeup(b); freeup(z); return; } i++; } b = to_real(b); /* safe because freeup(z) will clear old b */ } } /* ensure a is realtype */ switch (kind(a)) { case booltype: a = bool_to_real(a); break; case inttype: a = int_to_real(a); break; case realtype: break; case chartype: case phrasetype: case faulttype: { buildfault("first arg not numeric type in innerproduct"); freeup(a); freeup(b); freeup(z); return; } case atype: { nialint i = 0; if (!simple(a)) { buildfault("first arg not simple in innerproduct"); freeup(a); freeup(b); freeup(z); return; } while (i < tally(a)) { if (!numeric(kind(fetch_array(a, i)))) { buildfault("first arg not numeric type in innerproduct"); freeup(a); freeup(b); freeup(z); return; } i++; } a = to_real(a); /* safe becasue freeup of z clears old a */ } } if (va == 0) { m = 1; n = 1; } else if (va == 1) { m = 1; n = pickshape(a, 0); } else { m = pickshape(a, 0); n = pickshape(a, 1); } if (vb == 0) { bn = 1; p = 1; } else if (vb == 1) { bn = pickshape(b, 0); p = 1; } else { bn = pickshape(b, 0); p = pickshape(b, 1); } replicatea = (va == 0); replicateb = (vb == 0); if (!(replicatea || replicateb) && n != bn) { buildfault("conform error in innerproduct"); freeup(a); freeup(b); freeup(z); return; } /* get valence for the result */ vx = (va <= 1 ? (vb <= 1 ? 0 : 1) : (vb <= 1 ? 1 : 2)); if (vx == 2) { sh[0] = m; sh[1] = p; } else if (vx == 1) sh[0] = (va == 2 ? m : p); /* if vx==0 then sh is not used */ /* allocate space for the result matrix */ x = new_create_array(realtype, vx, 0, sh); ap = pfirstreal(a); /* safe: no allocations */ bp = pfirstreal(b); /* safe: no allocations */ xp = pfirstreal(x); /* safe: no allocations */ /* type of loop chosen on the kind of ip being done */ if (vx == 2) { /* matrix - matrix */ for (i = 0; i < m; i++) { for (j = 0; j < p; j++) { sum = 0.; for (k = 0; k < n; k++) sum += *(ap + (n * i + k)) * *(bp + (p * k + j)); *(xp + (p * i + j)) = sum; } checksignal(NC_CS_NORMAL); } } else if (va == 2) { /* matrix - vector */ for (i = 0; i < m; i++) { sum = 0.; if (replicateb) for (k = 0; k < n; k++) sum += *(ap + (n * i + k)) * *bp; else for (k = 0; k < n; k++) sum += *(ap + (n * i + k)) * *(bp + k); *(xp + i) = sum; } } else if (vb == 2) { /* vector - matrix */ for (j = 0; j < p; j++) { sum = 0.; if (replicatea) for (k = 0; k < bn; k++) sum += *ap * *(bp + (p * k + j)); else for (k = 0; k < bn; k++) sum += *(ap + k) * *(bp + (p * k + j)); *(xp + j) = sum; } } else { /* vector - vector */ sum = 0.; if (replicatea) for (k = 0; k < bn; k++) sum += *ap * *(bp + k); else if (replicateb) for (k = 0; k < n; k++) sum += *(ap + k) * *bp; else for (k = 0; k < n; k++) sum += *(ap + k) * *(bp + k); *xp = sum; } apush(x); freeup(a); freeup(b); freeup(z); }
int main(int argc, char ** argv) { mode = program_mode(argv[0]); /* Initialise settings from configuration files. */ dvswitch_read_config(handle_config); /* Parse arguments. */ int opt; while ((opt = getopt_long(argc, argv, "c:h:p:i:t", options, NULL)) != -1) { switch (opt) { case 'c': free(firewire_card); firewire_card = strdup(optarg); break; case 'F': mode = mode_firewire; break; case 'V': mode = mode_v4l2; break; case 'h': free(mixer_host); mixer_host = strdup(optarg); break; case 'p': free(mixer_port); mixer_port = strdup(optarg); break; case 't': do_tally = 1; break; case 'i': { long id = strtoul(optarg, NULL, 10); mixer_id = (id < 255) ? id-1 : 255; } break; case 'H': /* --help */ usage(argv[0]); return 0; default: usage(argv[0]); return 2; } } if (optind != argc) { free(device_name); device_name = strdup(argv[optind++]); } if (!mixer_host || !mixer_port) { fprintf(stderr, "%s: mixer hostname and port not defined\n", argv[0]); return 2; } if (optind != argc) { fprintf(stderr, "%s: excess argument \"%s\"\n", argv[0], argv[optind]); usage(argv[0]); return 2; } if (mode == mode_firewire) { if (device_name) printf("INFO: Reading from Firewire device %s\n", device_name); else if (firewire_card) printf("INFO: Reading from Firewire card %s\n", firewire_card); else printf("INFO: Reading from first Firewire card with camera\n"); } else if (mode == mode_v4l2) { if (!device_name) device_name = "/dev/video"; printf("INFO: Reading from V4L2 device %s\n", device_name); } else { fprintf(stderr, "%s: mode not defined (Firewire or V4L2)\n", argv[0]); return 2; } /* Connect to the mixer, set that as stdout, and run dvgrab. */ printf("INFO: Connecting to %s:%s\n", mixer_host, mixer_port); int sock = create_connected_socket(mixer_host, mixer_port); assert(sock >= 0); /* create_connected_socket() should handle errors */ if (write(sock, do_tally ? GREETING_ACT_SOURCE : GREETING_SOURCE, GREETING_SIZE-1) != GREETING_SIZE-1) { perror("ERROR: write"); exit(1); } if (write(sock, &mixer_id, sizeof(mixer_id)) != sizeof(mixer_id)) { perror("ERROR: write"); exit(1); } if (do_tally) { fflush(NULL); int child_pid = fork(); if (child_pid < 0) { perror("ERROR: fork"); return 1; } if (child_pid == 0) { tally(sock); _exit(0); } } if (dup2(sock, STDOUT_FILENO) < 0) { perror("ERROR: dup2"); return 1; } close(sock); char * dvgrab_argv[7]; char ** argp = dvgrab_argv; *argp++ = "dvgrab"; if (mode == mode_v4l2) *argp++ = "-v4l2"; if (device_name) { *argp++ = "-input"; *argp++ = device_name; } else if (firewire_card) { *argp++ = "-card"; *argp++ = firewire_card; } *argp++ = "-noavc"; *argp++ = "-"; *argp = NULL; assert(argp < dvgrab_argv + sizeof(dvgrab_argv) / sizeof(dvgrab_argv[0])); execvp("dvgrab", dvgrab_argv); perror("ERROR: execvp"); return 1; }
void iregisterdllfun(void) { int i, index; ResType argtypes[MAX_ARGS]; /* list of types of arguments */ bool vararg[MAX_ARGS]; /* is the argument a variable arg */ bool ispointer[MAX_ARGS]; /* is the argument a pointer */ int varargcount = 0; /* number of variable args */ int ispointercount = 0; /* number of pointer args */ nialptr z = apop(); char *nialname; /* names used by calldllfun */ char *dllname; /* the real name of the function */ /* in the DLL file */ char *library; /* name of the DLL file */ ResType resulttype; /* the type of the result */ nialptr nargtypes; /* the arg type array */ int argcount; int j; int sz; nialptr current; /* usually hold the current entry in the dlllist */ int is_register; /* if we have 5 args we are registering a function */ if ((tally(z) == 5) && (kind(z) == atype)) is_register = 1; /* register mode */ else /* only one arg and it is a char or a phrase, we are deleting the fun */ if ((kind(z) == chartype) || (kind(z) == phrasetype)) is_register = 0; /* delete mode */ else { /* error mode */ apush(makefault("?Incorrect number of arguments to registerdllfun")); freeup(z); return; } if (is_register) { /* The Nial level name for the DLL function */ STRING_CHECK(z, 0) nialname = STRING_GET(z, 0); /* The internal DLL name for the function */ STRING_CHECK(z, 1) dllname = STRING_GET(z, 1); /* The name of the library file */ STRING_CHECK(z, 2) library = STRING_GET(z, 2); /* The name of the result type */ STRING_CHECK(z, 3) resulttype = StringToTypeID(STRING_GET(z, 3)); /* did we find an unrecognized result type? */ if (resulttype < 0) { apush(makefault("?Return type not recognized")); freeup(z); return; } if (kind(fetch_array(z, 4)) != atype) { apush(makefault("?Argument must be a list of strings or phrases")); freeup(z); return; } nargtypes = fetch_array(z, 4); argcount = tally(nargtypes); /* Check each of the argument type */ for (j = 0; j < argcount; j++) STRING_CHECK_FREEUP(nargtypes, j, z) /* create an integer list of argument types from the phrase/string list */ for (i = 0; i < argcount; i++) { char *tmp; tmp = pfirstchar(fetch_array(nargtypes, i)); /* safe: no allocation */ argtypes[i] = StringToTypeID(tmp); /* the ith argument name was not recognized */ if (argtypes[i] < 0) { char stmp[256]; wsprintf(stmp, "?Type \"%s\" for argument %d not recognized", tmp, i + 1); apush(makefault(stmp)); freeup(z); return; } /* set the vararg and ispointer flags for this arg */ vararg[i] = IsVarArg(tmp); ispointer[i] = IsPointer(tmp); /* keep count of these special args */ if (vararg[i]) varargcount++; if (ispointer[i]) ispointercount++; } /* NEW workspace Version */ /* If the list does not yet exist, then create a one element list here */ if (tally(dlllist) == 0) { nialptr tmp = create_new_dll_entry; /* build a empty entry */ setup_dll_entry(tmp) /* fill it with empty data */ apush(tmp); isolitary(); /* make it a list */ decrrefcnt(dlllist); freeup(dlllist); dlllist = apop(); incrrefcnt(dlllist); index = 0; } else { int pos; /* does the requested name already exist in out list? */ if ((pos = inlist(nialname)) >= 0) { /* yes it's here already, so note its position, and free the old * entry */ index = pos; freeEntry(index); } else { /* if we got here, then we need to create a new entry and add it to * and existing dlllist */ nialptr tmp = create_new_dll_entry; setup_dll_entry(tmp) decrrefcnt(dlllist); append(dlllist, tmp); dlllist = apop(); incrrefcnt(dlllist); index = tally(dlllist) - 1; /* this is the location of the new entry */ } } /* grab the entry to work on */ current = fetch_array(dlllist, index); /* fill in data */ set_handle(current, NULL); set_nialname(current, nialname); set_dllname(current, dllname); set_callingconvention(current, (dllname[0] == '_' ? C_CALL : PASCAL_CALL)); set_library(current, library); set_isloaded(current, false); set_resulttype(current, resulttype); set_argcount(current, argcount); set_varargcount(current, varargcount); set_ispointercount(current, ispointercount); sz = argcount; replace_array(current, 10, (sz == 0 ? Null : new_create_array(inttype, 1, 0, &sz))); for (j = 0; j < sz; j++) set_argtypes(current, j, argtypes[j]); replace_array(current, 11, (sz == 0 ? Null : new_create_array(booltype, 1, 0, &sz))); for (j = 0; j < sz; j++) set_ispointer(current, j, ispointer[j]); replace_array(current, 14, (sz == 0 ? Null : new_create_array(booltype, 1, 0, &sz))); for (j = 0; j < sz; j++) set_vararg(current, j, vararg[j]); } else { /* delete entry code */