int main() { clrscr(); int a[10],i,j,k; printf("Vvedit 10 chisel:"); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=0,j=0,k=0;i<10;i++) { if(a[i]%2==0) {putinstack(p1,a[i],j); j++;} else {putinstack(p2,a[i],k); k++;} } puts("\nParni:"); for(i=0;i<10;i++) printf("%d",viewstack(p1,i)); puts("\n\nNeparni:"); for(i=0;i<10;i++) printf("%d",viewstack(p2,i)); clearstack(p1); clearstack(p2); getch(); return 0; }
// // sequence::clear // // empty the entire sequence, clear undo/redo history etc // bool sequence::clear () { span *sptr, *tmp; // delete all spans in the sequence for(sptr = head->next; sptr != tail; sptr = tmp) { tmp = sptr->next; delete sptr; } // re-link the head+tail head->next = tail; tail->prev = head; // delete everything in the undo/redo stacks clearstack(undostack); clearstack(redostack); // delete all memory-buffers for(size_t i = 0; i < buffer_list.size(); i++) { delete[] buffer_list[i]->buffer; delete buffer_list[i]; } buffer_list.clear(); sequence_length = 0; return true; }
// // sequence::clear // // empty the entire sequence, clear undo/redo history etc // bool sequence::clear () { span *sptr, *tmp; // delete all spans in the sequence for(sptr = head->next; sptr != tail; sptr = tmp) { tmp = sptr->next; delete sptr; } // re-link the head+tail head->next = tail; tail->prev = head; // delete everything in the undo/redo stacks clearstack(undostack); clearstack(redostack); // delete all memory-buffers for(size_t i = 0; i < buffer_list.size(); i++) { //delete[] buffer_list[i]->buffer; delete buffer_list[i]; } buffer_list.clear(); sequence_length = 0; can_quicksave = false; // make sure to setup all the filedescriptors again to 'empty'! init(); return true; }
void evalinit(void) { lexemes = getmem(LEX_MAX); symtable = getmem(SYM_MAX * sizeof(struct Entry)); lexbuf = getmem(MAX_VNAME+1); stack = getmem(STACK_MAX * sizeof(struct StackItem)); clearstack(); }
/******************** reverse Polish calculater *******************/ int main(void) { int type; double op2; char s[MAXOP]; while((type = getop(s)) != EOF) { switch(type){ case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if(op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case'%': op2 = pop(); if(op2 != 0.0) push(fmod(pop(), op2)); else printf("error: zero divisor\n"); case'?': showTop(); break; case'#': duplicate(); break; case'~': swapitems(); case'!': clearstack(); break; case'\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }
enum ValueType evaluate(char *string, unsigned int *intv, char **strv) { while(*string == ' ') { string++; } evalstring = string; /* catch common error: &hex instead of 0xhex */ if(*string == '&') { error("Hex numbers must be prefixed with `0x'"); return V_ERROR; } eval_error = 0; clearstack(); input = string; lastchar = -1; lastentry = 0; done = 0; t = egettok(); expr(1); if(t != DONE) { error("Illegal expression"); return V_ERROR; } if(stackpos != 1) { error("Illegal expression: too many/few stack entries"); } else { if(eval_error) return V_ERROR; if(stack[0].tok == NUM) { *intv = stack[0].val; return V_INT; } else if(stack[0].tok == STRING) { *strv = (symtable[(stack[0].val)].name) ? (symtable[(stack[0].val)].name) : "<NULL>"; return V_STRING; } else { error("Expression evaluates to bad result"); return V_ERROR; } } }
/* Puts new item onto directory stack. */ static int addstack(char *uname, struct dir *dir, int pidx) { struct dsitem *ds; size_t l, u; struct dsitem *tmpds = NULL; /* check if we have some space on stack... */ if (dsidx >= dssize) { dssize += DS_BSIZE; tmpds = realloc(dstack, dssize * sizeof(struct dsitem)); if (tmpds == NULL) { clearstack(); free(dstack); return -1; } dstack = tmpds; } /* Put new element. Allocate and copy lname and path. */ ds = dstack + dsidx++; ds->ds_did = dir->d_did; ds->ds_checked = 0; return 0; }
/******************** reverse Polish calculater *******************/ int main(void) { int type; double op2; char s[MAXOP]; struct varType var[MAXVARS]; clearstack(var); while((type = getop(s)) != EOF) { switch(type){ case NUMBER: push(atof(s)); break; case IDENTIFIER: mathname(s); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if(op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case'%': op2 = pop(); if(op2 != 0.0) push(fmod(pop(), op2)); else printf("error: zero divisor\n"); case'?': showTop(); break; case'#': duplicate(); break; case'~': swapitems(); case'!': clearstack(var); break; case'\n': printf("\t%.8g\n", pop()); break; case ENDSTRING: break; case'=': pop(); var[pos].val = pop(); last.val = var[pos].val; push(last.val); break; case'<': printf("The last variable used was: %s (value == %g)\n", last.name, last.val); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }
static int catsearch(struct vol *vol, struct dir *dir, int rmatches, uint32_t *pos, char *rbuf, uint32_t *nrecs, int *rsize, int ext) { static u_int32_t cur_pos; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */ static DIR *dirpos; /* UNIX structure describing currently opened directory. */ struct dir *currentdir; /* struct dir of current directory */ int cidx, r; struct dirent *entry; int result = AFP_OK; int ccr; struct path path; char *vpath = vol->v_path; char *rrbuf = rbuf; time_t start_time; int num_rounds = NUM_ROUNDS; int cwd = -1; int error; int unlen; if (*pos != 0 && *pos != cur_pos) { result = AFPERR_CATCHNG; goto catsearch_end; } /* FIXME: Category "offspring count ! */ /* We need to initialize all mandatory structures/variables and change working directory appropriate... */ if (*pos == 0) { clearstack(); if (dirpos != NULL) { closedir(dirpos); dirpos = NULL; } if (addstack(vpath, dir, -1) == -1) { result = AFPERR_MISC; goto catsearch_end; } /* FIXME: Sometimes DID is given by client ! (correct this one above !) */ } /* Save current path */ if ((cwd = open(".", O_RDONLY)) < 0) { result = AFPERR_MISC; goto catsearch_end; } /* So we are beginning... */ start_time = time(NULL); while ((cidx = reducestack()) != -1) { if ((currentdir = dirlookup(vol, dstack[cidx].ds_did)) == NULL) { result = AFPERR_MISC; goto catsearch_end; } LOG(log_debug, logtype_afpd, "catsearch: current struct dir: \"%s\"", cfrombstr(currentdir->d_fullpath)); error = movecwd(vol, currentdir); if (!error && dirpos == NULL) dirpos = opendir("."); if (dirpos == NULL) dirpos = opendir(bdata(currentdir->d_fullpath)); if (error || dirpos == NULL) { switch (errno) { case EACCES: dstack[cidx].ds_checked = 1; continue; case EMFILE: case ENFILE: case ENOENT: result = AFPERR_NFILE; break; case ENOMEM: case ENOTDIR: default: result = AFPERR_MISC; } /* switch (errno) */ goto catsearch_end; } while ((entry = readdir(dirpos)) != NULL) { (*pos)++; if (!check_dirent(vol, entry->d_name)) continue; LOG(log_debug, logtype_afpd, "catsearch(\"%s\"): dirent: \"%s\"", cfrombstr(currentdir->d_fullpath), entry->d_name); memset(&path, 0, sizeof(path)); path.u_name = entry->d_name; if (of_stat(vol, &path) != 0) { switch (errno) { case EACCES: case ELOOP: case ENOENT: continue; case ENOTDIR: case EFAULT: case ENOMEM: case ENAMETOOLONG: default: result = AFPERR_MISC; goto catsearch_end; } } switch (S_IFMT & path.st.st_mode) { case S_IFDIR: /* here we can short cut ie if in the same loop the parent dir wasn't in the cache ALL dirsearch_byname will fail. */ unlen = strlen(path.u_name); path.d_dir = dircache_search_by_name(vol, currentdir, path.u_name, unlen); if (path.d_dir == NULL) { /* path.m_name is set by adddir */ if ((path.d_dir = dir_add(vol, currentdir, &path, unlen)) == NULL) { result = AFPERR_MISC; goto catsearch_end; } } path.m_name = cfrombstr(path.d_dir->d_m_name); if (addstack(path.u_name, path.d_dir, cidx) == -1) { result = AFPERR_MISC; goto catsearch_end; } break; case S_IFREG: path.d_dir = currentdir; break; default: continue; } ccr = crit_check(vol, &path); /* bit 0 means that criteria has been met */ if ((ccr & 1)) { r = rslt_add ( vol, &path, &rrbuf, ext); if (r == 0) { result = AFPERR_MISC; goto catsearch_end; } *nrecs += r; /* Number of matches limit */ if (--rmatches == 0) goto catsearch_pause; /* Block size limit */ if (rrbuf - rbuf >= 448) goto catsearch_pause; } /* MacOS 9 doesn't like servers executing commands longer than few seconds */ if (--num_rounds <= 0) { if (start_time != time(NULL)) { result=AFP_OK; goto catsearch_pause; } num_rounds = NUM_ROUNDS; } } /* while ((entry=readdir(dirpos)) != NULL) */ closedir(dirpos); dirpos = NULL; dstack[cidx].ds_checked = 1; } /* while (current_idx = reducestack()) != -1) */ /* We have finished traversing our tree. Return EOF here. */ result = AFPERR_EOF; goto catsearch_end; catsearch_pause: cur_pos = *pos; save_cidx = cidx; catsearch_end: /* Exiting catsearch: error condition */ *rsize = rrbuf - rbuf; if (cwd != -1) { if ((fchdir(cwd)) != 0) { LOG(log_debug, logtype_afpd, "error chdiring back: %s", strerror(errno)); } close(cwd); } return result; } /* catsearch() */
// // sequence::erase_worker // bool sequence::erase_worker (size_w index, size_w length, action act) { span *sptr; span_range oldspans; span_range newspans; span_range *event; size_w spanindex; size_w remoffset; size_w removelen; bool append_spanrange; debug("Erasing: idx=%d len=%d\n", index, length); // make sure we stay within the range of the sequence if(length == 0 || length > sequence_length || index > sequence_length - length) return false; // find the span that the deletion starts at if((sptr = spanfromindex(index, &spanindex)) == 0) return false; // work out the offset relative to the start of the *span* remoffset = index - spanindex; removelen = length; // // can we optimize? // // special-case 1: 'forward-delete' // erase+replace operations will pass through here // if(index == spanindex && can_optimize(act, index)) { event = stackback(undostack, act == action_replace ? 1 : 0); event->length += length; append_spanrange = true; if(frag2 != 0) { if(length < frag2->length) { frag2->length -= length; frag2->offset += length; sequence_length -= length; return true; } else { if(act == action_replace) stackback(undostack, 0)->last = frag2->next; removelen -= sptr->length; sptr = sptr->next; deletefromsequence(&frag2); } } } // // special-case 2: 'backward-delete' // only erase operations can pass through here // else if(index + length == spanindex + sptr->length && can_optimize(action_erase, index+length)) { event = undostack.back(); event->length += length; event->index -= length; append_spanrange = false; if(frag1 != 0) { if(length < frag1->length) { frag1->length -= length; frag1->offset += 0; sequence_length -= length; return true; } else { removelen -= frag1->length; deletefromsequence(&frag1); } } } else { append_spanrange = true; frag1 = frag2 = 0; if((event = initundo(index, length, act)) == 0) return false; } // // general-case 2+3 // clearstack(redostack); // does the deletion *start* mid-way through a span? if(remoffset != 0) { // split the span - keep the first "half" newspans.append(new span(sptr->offset, remoffset, sptr->buffer)); frag1 = newspans.first; // have we split a single span into two? // i.e. the deletion is completely within a single span if(remoffset + removelen < sptr->length) { // make a second span for the second half of the split newspans.append(new span( sptr->offset + remoffset + removelen, sptr->length - remoffset - removelen, sptr->buffer) ); frag2 = newspans.last; } removelen -= min(removelen, (sptr->length - remoffset)); // archive the span we are going to replace oldspans.append(sptr); sptr = sptr->next; } // we are now on a proper span boundary, so remove // any further spans that the erase-range encompasses while(removelen > 0 && sptr != tail) { // will the entire span be removed? if(removelen < sptr->length) { // split the span, keeping the last "half" newspans.append(new span( sptr->offset + removelen, sptr->length - removelen, sptr->buffer) ); frag2 = newspans.last; } removelen -= min(removelen, sptr->length); // archive the span we are replacing oldspans.append(sptr); sptr = sptr->next; } // for replace operations, update the undo-event for the // insertion so that it knows about the newly removed spans if(act == action_replace && !oldspans.boundary) stackback(undostack, 0)->last = oldspans.last->next; swap_spanrange(&oldspans, &newspans); sequence_length -= length; if(append_spanrange) event->append(&oldspans); else event->prepend(&oldspans); return true; }
// // sequence::insert_worker // bool sequence::insert_worker (size_w index, const seqchar *buf, size_w length, action act) { span * sptr; size_w spanindex; size_t modbuf_offset; span_range newspans; size_w insoffset; if(index > sequence_length) return false; // find the span that the insertion starts at if((sptr = spanfromindex(index, &spanindex)) == 0) return false; // ensure there is room in the modify buffer... // allocate a new buffer if necessary and then invalidate span cache // to prevent a span using two buffers of data if(!import_buffer(buf, length, &modbuf_offset)) return false; debug("Inserting: idx=%d len=%d %.*s\n", index, length, length, buf); clearstack(redostack); insoffset = index - spanindex; // special-case #1: inserting at the end of a prior insertion, at a span-boundary if(insoffset == 0 && can_optimize(act, index)) { // simply extend the last span's length span_range *event = undostack.back(); sptr->prev->length += length; event->length += length; } // general-case #1: inserting at a span boundary? else if(insoffset == 0) { // // Create a new undo event; because we are inserting at a span // boundary there are no spans to replace, so use a "span boundary" // span_range *oldspans = initundo(index, length, act); oldspans->spanboundary(sptr->prev, sptr); // allocate new span in the modify buffer newspans.append(new span( modbuf_offset, length, modifybuffer_id) ); // link the span into the sequence swap_spanrange(oldspans, &newspans); } // general-case #2: inserting in the middle of a span else { // // Create a new undo event and add the span // that we will be "splitting" in half // span_range *oldspans = initundo(index, length, act); oldspans->append(sptr); // span for the existing data before the insertion newspans.append(new span( sptr->offset, insoffset, sptr->buffer) ); // make a span for the inserted data newspans.append(new span( modbuf_offset, length, modifybuffer_id) ); // span for the existing data after the insertion newspans.append(new span( sptr->offset + insoffset, sptr->length - insoffset, sptr->buffer) ); swap_spanrange(oldspans, &newspans); } sequence_length += length; return true; }
void sequence::clear_undo() { clearstack(undostack); clearstack(redostack); }
/* * Checks standard hand for a winner (not a war hand) */ static void checkhandwinner(void) { int i; int hcnp = 0; int hcnpt = 0; for (wpln = 0; wpln < currentwarplayercount; wpln++) { if (warinprogress == wplayeratwar[wpln]) { if ((wplayercardplayed[wpln] % 13) > hcnp) { hcnp = (wplayercardplayed[wpln] % 13); hcnpt = 1; } else if ((wplayercardplayed[wpln] % 13) == hcnp) { hcnpt++; } } } if (hcnpt == 1) { for (wpln = 0; wpln < currentwarplayercount; wpln++) { if (warinprogress == wplayeratwar[wpln]) { if ((wplayercardplayed[wpln] % 13) == hcnp) { if (warinprogress == 1) { irc_chanprivmsg (ws_bot, warroom, "\0037%s\0039 wins the \0034WAR\0039.", wplayernick[wpln]); } else { irc_chanprivmsg (ws_bot, warroom, "\0037%s\0039 takes the hand.", wplayernick[wpln]); } for (i = 0; i < wstackcardscurrent; i++) { wplayercardsinhand[wpln][wplayercardstotal[wpln]]= wstackcards[i]; wplayercardstotal[wpln]++; } clearstack(); wpln= currentwarplayercount; } } } for (wpln = 0; wpln < currentwarplayercount; wpln++) { wplayeratwar[wpln]= 0; } warinprogress= 0; } else { irc_chanprivmsg (ws_bot, warroom, "\0034WAR DECLARED"); for (wpln = 0; wpln < currentwarplayercount; wpln++) { if (warinprogress == wplayeratwar[wpln]) { wplayeratwar[wpln] = 0; if ((wplayercardplayed[wpln] % 13) == hcnp) { wplayeratwar[wpln]= 1; if (wplayercardstotal[wpln] < 3) { irc_chanprivmsg (ws_bot, warroom, "\0037%s\0038 Surrenders\0039 (Insufficient Cards)", wplayernick[wpln]); hcnpt--; removewar(wplayernick[wpln]); if (currentwarplayercount < 2) { wpln= currentwarplayercount; return; } } } } } warinprogress= 1; } currentplayer= 0; playershufflecards(); askplaycard(); }