void exec(const char *str) { int32 strn = 0; /* check validity of equation in number of brackets */ int32 brackets = 0; for(;*str!='\0';str++,strn++) { if(*str=='(') brackets++; else if(*str==')') brackets--; } if(brackets!=0) { strcpy(lisp_error,"Invalid Syntax, incorrect number of parentheses."); return; } str -= strn; char *lhs = allocs(strn); char *rhs = allocs(strn); /* here the rhs & lhs are obtained */ { char *sptr = NULL; /* sptr points at const *str */ for(sptr=lhs;*str!='\0'&&*str!='=';str++) *sptr++ = *str; *sptr = '\0'; if(*str=='=') { for(sptr=rhs,str++;*str!='\0';str++) { *sptr++ = *str; *sptr = '\0'; } } else { *rhs = '\0'; } trims(lhs,' '); } if(*rhs=='\0') { /* <- empty rhs, so no assignment involved. */ /* check if rhs is a variable */ int32 *value = NULL; value = getint32(lhs); if(value==NULL) { /* rhs is NOT a variable */ int32 val=0; eval_int32(lhs,&val); printf("\n%ld\n",val); } else printf("%s = %ld\n",lhs,*value); } else { /* rhs exists */ /* check for [=>] exchange directive */ if(*rhs=='>') exchange_vars(lhs,rhs,lisp_error); else { /* variable creation/assignment */ void asgn(char *rhs, char *lhs); asgn(rhs,lhs); } } /* free memeory */ free(lhs); free(rhs); }
// TestSpeed //------------------------------------------------------------------------------ void TestMemPoolBlock::TestSpeed() { const uint32_t numAllocs( 20 * 1000 ); const uint32_t allocSize( 24 ); float time1( 0.0f ); float time2( 0.0f ); // Alloc { Array< void * > allocs( numAllocs, false ); Timer t1; { for ( uint32_t i=0; i<numAllocs; ++i ) { uint32_t * mem = (uint32_t *)ALLOC( allocSize ); allocs.Append( mem ); } for ( uint32_t i=0; i<numAllocs; ++i ) { void * mem = allocs[ i ]; FREE( mem ); } } time1 = t1.GetElapsed(); } // MemPoolBlock { Array< void * > allocs( numAllocs, false ); Timer t2; { MemPoolBlock block( allocSize, 4 ); for ( uint32_t i=0; i<numAllocs; ++i ) { uint32_t * mem = (uint32_t *)block.Alloc( allocSize ); allocs.Append( mem ); } for ( uint32_t i=0; i<numAllocs; ++i ) { void * mem = allocs[ i ]; block.Free( mem ); } } time2 = t2.GetElapsed(); } // output OUTPUT( "Alloc : %2.3fs - %u allocs @ %u allocs/sec\n", time1, numAllocs, (uint32_t)( (float)numAllocs / time1 ) ); OUTPUT( "MemPoolBlock : %2.3fs - %u allocs @ %u allocs/sec\n", time2, numAllocs, (uint32_t)( (float)numAllocs / time2 ) ); }
static Bool add_wrapped_text( TextWrapRec * t, int start, int utfstart, int end, int utfend, int tildeIndex, int * tildePos, int * tildeLPos, int * tildeLine, char *** lArray, int * lSize) { int l = end - start; char *c = nil; if (!( t-> options & twReturnChunks)) { if ( !( c = allocs( l + 1))) return false; memcpy( c, t-> text + start, l); c[ l] = 0; } if ( tildeIndex >= 0 && tildeIndex >= start && tildeIndex < end) { *tildeLine = t-> t_line = t-> count; *tildePos = *tildeLPos = tildeIndex - start; if ( tildeIndex == end - 1) { t-> t_line++; tildeLPos = 0; } } if ( t-> count == *lSize) { char ** n = allocn( char*, *lSize + 16); if ( !n) return false; memcpy( n, *lArray, sizeof( char*) * (*lSize)); *lSize += 16; free( *lArray); *lArray = n; }
// AllocateFromSystemAllocator //------------------------------------------------------------------------------ /*static*/ float TestSmallBlockAllocator::AllocateFromSystemAllocator( const Array< uint32_t > & allocSizes, const uint32_t repeatCount ) { const size_t numAllocs = allocSizes.GetSize(); Array< void * > allocs( numAllocs, false ); Timer timer; for ( size_t r=0; r<repeatCount; ++r ) { // use malloc for ( uint32_t i=0; i<numAllocs; ++i ) { uint32_t * mem = (uint32_t *)malloc( allocSizes[i] ); allocs.Append( mem ); } // use free for ( uint32_t i=0; i<numAllocs; ++i ) { void * mem = allocs[ i ]; free( mem ); } allocs.Clear(); } return timer.GetElapsed(); }
float usedPercentage() const { if (!allocator) return 0; duint totalPx = totalSize.x * totalSize.y; duint usedPx = 0; foreach (Rectanglei const &alloc, allocator->allocs().values()) { usedPx += alloc.width() * alloc.height(); } return float(usedPx) / float(totalPx); }
void exec(char *str) { char *lhs = (char *) allocs(strlen(str)); char *rhs = (char *) allocs(strlen(str)); char *sptr = NULL; /* here the rhs & lhs are obtained */ for(sptr=lhs;*str!='\0'&&*str!='=';str++) *sptr++ = *str; *sptr = '\0'; if(*str=='=') for(sptr=rhs,str++;*str!='\0';str++) { *sptr++ = *str; *sptr = '\0'; } else *rhs = '\0'; printf("%s",lhs); int32 *value= NULL; if(*rhs=='\0') /* rhs is empty */ { /* no assignment involved */ value = getint(lhs); if(value!=NULL) printf(" = %ld\n",*value); } else /* rhs exists */ { int32 rvalue=0; rvalue = atoi(rhs); printf(" = %ld\n",rvalue); value = newint(lhs); *value = rvalue; } free(lhs); free(rhs); }
// TestAllocs //------------------------------------------------------------------------------ void TestMemPoolBlock::TestAllocs() const { const size_t blockSize( 32 ); const size_t blockAlignment( 4 ); MemPoolBlock block( blockSize, blockAlignment ); // allocate every size upto the block size Array< void * > allocs( blockSize + 1, false ); for ( size_t i=0; i<=blockSize; ++i ) { void * mem = block.Alloc( i ); TEST_ASSERT( mem ); TEST_ASSERT( ( (size_t)mem % blockAlignment ) == 0 ); allocs.Append( mem ); } // free them for ( size_t i=0; i<allocs.GetSize(); ++i ) { block.Free( allocs[ i ] ); } }
// AllocateFromSmallBlockAllocator //------------------------------------------------------------------------------ /*static*/ float TestSmallBlockAllocator::AllocateFromSmallBlockAllocator( const Array< uint32_t > & allocSizes, const uint32_t repeatCount, const bool threadSafe ) { const size_t numAllocs = allocSizes.GetSize(); Array< void * > allocs( numAllocs, false ); Timer timer; if ( threadSafe == false ) { SmallBlockAllocator::SetSingleThreadedMode( true ); } for ( size_t r=0; r<repeatCount; ++r ) { // Use ALLOC for ( uint32_t i=0; i<numAllocs; ++i ) { uint32_t * mem = (uint32_t *)ALLOC( allocSizes[i] ); allocs.Append( mem ); } // Use FREE for ( uint32_t i=0; i<numAllocs; ++i ) { void * mem = allocs[ i ]; FREE( mem ); } allocs.Clear(); } if ( threadSafe == false ) { SmallBlockAllocator::SetSingleThreadedMode( false ); } return timer.GetElapsed(); }
void eval_int32(char *str, int32 *value) { /* Expression Evaluation Function (int32) * ======================================== * This function evaluates an infix expression * for an int32 result. First, it translates * the given expression to the postfix * form, and then evaluates the postfix * expression using the peval function. * The result is stored in int32 *value. * -------------------------------------------- * WARNING: *str is subject to slight changes, * so transfer expression to temporary string * before passing it here. */ trims(str,' '); register num32 cur=0; /* count the number of operators */ if(*str=='-') { str++; cur++; } num32 stack_size=0; for(;*str!='\0';str++,cur++) if(*str=='+'||*str=='-'|| *str=='^'||*str=='%'|| *str=='*'||*str=='/'|| *str=='<'||*str=='>'|| *str=='&'||*str=='|'|| *str==')'||*str=='('||( *str=='='&&str[1]=='=')) stack_size++; str -= cur; /* no operators */ if(!stack_size) { sval_int32(str,value); return; } /* stacks for storing postfix expression */ char * const ostack = (char *) malloc ((stack_size*2 + 1) * sizeof(char)); int32 * const values = (int32 *) malloc ((stack_size + 1) * sizeof(int32)); if(ostack==NULL||values==NULL) msg(system_no_mem,3); /* Rules: ostack contains operators(+,-,*..), wheras * values contains numbers. if char *ostack = 0, then * int32 *values is holds a valid number of the expression, * otherwise char *ostack contains a valid math operator. * Based on these rules the expression is designed. */ /* important loop counters... */ register num32 top=0,ival=0,ops=0; /***********************************. | ( infix => postfix ) translation: | `***********************************/ { /* temporary character array */ char * const tmp = allocs(strlen(str)); /* temporary stack to hold operators (+,-,*,..) */ uchar * const cstack = (uchar *) malloc ((stack_size + 1) * sizeof(uchar)+11); if(cstack==NULL) msg(system_no_mem,3); *cstack = '\0'; char prevOp = '\0', negative=0; /* beware: mathematics ahead... */ for(cur=0;;str++) { if(*str=='('&&cur) { /* to handle substring baraces */ register int32 pars = 0; for(;*str!='\0';str++) { if(*str=='(') pars++; if(*str==')') pars--; if(!pars) break; tmp[cur++] = *str; } if(*str=='\0') break; else tmp[cur++] = ')'; continue; } if(*str=='+'||*str=='-'|| *str=='*'||*str=='/'|| *str=='^'||*str=='%'|| *str=='<'||*str=='>'|| *str=='('||*str==')'|| *str=='&'||*str=='|'|| *str=='='||*str=='\0') { /* finalise tmp */ tmp[cur] = '\0'; trims(tmp,' '); for(cur=0;tmp[cur]!='\0';cur++); /* skip vals for opening braces */ if(*str=='(') goto after_vals; if(!cur) { /* skip the translation in case of repition of operators */ if(prevOp==')'||prevOp=='(') goto after_vals; else { if(*str=='-') negative++; prevOp = *str; if(*str=='\0') break; continue; } } /* doubly operators */ if(*str=='='&&str[1]=='=') /* logical == */ *++str='E'; else if(*str=='>'&&str[1]=='=') /* logical >= */ *++str='F'; else if(*str=='<'&&str[1]=='=') /* logical <= */ *++str='G'; else if(*str=='&'&&str[1]=='&') /* logical AND */ *++str='A'; else if(*str=='|'&&str[1]=='|') /* logical OR */ *++str='O'; else if(*str=='*'&&str[1]=='*') /* power */ *++str='P'; else if(*str=='>'&&str[1]=='>') /* bitwise right shift (>>) */ *++str='R'; else if(*str=='<'&&str[1]=='<') /* bitwise left shift (<<) */ *++str='L'; else if(*str=='>'&&str[1]=='<') /* maximum value */ *++str='M'; else if(*str=='+'&&str[1]=='&') /* concatenate */ *++str='C'; /* number insertion: */ sval_int32(tmp,&values[ival++]); if(negative) { values[ival-1]*=-1; negative = 0; } ostack[ops++] = 0; cur = *tmp = 0; after_vals: /* operand insertion: */ if(eval_prior(cstack[top])<eval_prior(*str) ||*str=='(') cstack[++top] = *str; else { for(;top&&cstack[top]!='(';top--) ostack[ops++] = cstack[top]; if(*str!=')') cstack[++top] = *str; else if(cstack[top]=='(') cstack[top--] = 0; } if(*str=='\0') break; prevOp = *str; } else tmp[cur++] = *str; } /* free memory: */ free(cstack); free(tmp); } // printf("\n---[%ld]---\n",ops); // for(cur=0,top=0;cur<ops;cur++) // if(!ostack[cur]) printf(" %ld ",values[top++]); // else printf(" %c ",ostack[cur]); // puts("\n-----------"); /*******************************. | Postfix Expression Evaluator: | `*******************************/ { /* temporary stack to evaluate expression */ int32 * const stack = (int32 *) malloc ((stack_size + 1) * sizeof(int32)); if(stack==NULL) msg(system_no_mem,3); for(cur=0,top=0,ival=0;cur<ops;cur++) { if(!ostack[cur]) /* an operand */ stack[ival++] = values[top++]; else { /* an operator (+,-,*,..)[15] */ switch(ostack[cur]) { /* do the math */ case '+': stack[--ival-1] += stack[ival]; break; case '-': stack[--ival-1] -= stack[ival]; break; case '*': stack[--ival-1] *= stack[ival]; break; case '/': stack[--ival-1] /= stack[ival]; break; case '%': stack[--ival-1] %= stack[ival]; break; /* the logical operators (==,>=,&&,||,..) */ case 'E': stack[ival---2] = stack[ival-1] == stack[ival-2] ? 1 : 0; break; /* == */ case '>': stack[ival---2] = stack[ival-1] < stack[ival-2] ? 1 : 0; break; /* > */ case 'F': stack[ival---2] = stack[ival-1] >= stack[ival-2] ? 1 : 0; break; /* >= */ case '<': stack[ival---2] = stack[ival-1] > stack[ival-2] ? 1 : 0; break; /* < */ case 'G': stack[ival---2] = stack[ival-1] >= stack[ival-2] ? 1 : 0; break; /* <= */ case 'A': stack[ival---2] =(stack[ival-1]>0)&&( stack[ival-2]) ? 1 : 0; break; /* && */ case 'O': stack[ival---2] =(stack[ival-1]>0)||( stack[ival-2]) ? 1 : 0; break; /* || */ /* the bitwise operators (&,|,^^,>>,<<,..) */ case '&': stack[--ival-1] &= stack[ival]; break; case '|': stack[--ival-1] |= stack[ival]; break; case '^': stack[--ival-1] ^= stack[ival]; break; case 'L': stack[ival-1] = /* bitwise left shift */ stack[ival-1] << stack[--ival]; break; case 'R': stack[ival-1] = /* bitwise right shift */ stack[ival-1] >> stack[--ival]; break; /* the special operators (^,+&,><)... */ case 'P': stack[--ival-1] = (int32) pow (stack[ival-2],stack[ival-1]); break; case 'C': concatenate(&stack[ival-1],stack[--ival]); break; case 'M': stack[ival---2] = stack[ival-1] > stack[ival-2] ? stack[ival-1] : stack[ival-2]; break; default: stack[--ival-1] = stack[ival]; } } } /* set value */ *value = stack[0]; free(stack); } /* free memory: */ free(ostack); free(values); }
void c2html(const char *filename, char *err, struct colors *c) { /* <- core function -> here the C source code is tranlated to an HTML web page. */ struct /* common bits */ { unsigned char color : 1; /* color the code */ unsigned char qt1 : 1; /* double quotes */ unsigned char qt2 : 1; /* single quotes */ unsigned char cm1 : 1; /* multi-line comments */ unsigned char cm2 : 1; /* single-line comments */ unsigned char num : 1; /* numerical constants */ unsigned char pre : 1; /* preprocessors */ unsigned char cont : 1; /* preprocessors */ } bits = { 1, 0, 0, 0, 0, 0, 0, 0 }; if(!strlen(filename)) { puts("\nPlease enter a filename."); return; } if(*filename==':') { bits.color = 0; filename++; } else if(*filename=='?') { userdef_colors(c); filename++; } FILE *cfile,*hfile; cfile = hfile = NULL; char htmlfile[STR_MAX]; strcpy(htmlfile,filename); strcat(htmlfile,".html"); if((cfile = fopen(filename,"r"))==NULL) { sprintf(err,"Could not open file: %s",filename); return; } unsigned short int line_length = 0; char input_str[STR_MAX]; printf("\nHow many letters per line? "); fgets(input_str,STR_MAX,stdin); string(input_str); line_length = atoi(input_str); if(line_length <= 1) line_length = 80; printf("\nAnalyzing file: %s ...\n\n",filename); register char move = 0; unsigned int strmax, strnow, lines, pages; unsigned long int charcount = 0; unsigned long int letters = 0; strmax = strnow = lines = pages = 0; for(;move!=EOF;strnow++, charcount++) { move = fgetc(cfile); if(move=='\n') { strmax = strnow>strmax ? strnow:strmax; strnow = 0; lines++; } if(move<128 && move!='\n' && move!= '\r') letters++; } pages = lines/50; pages += lines%50 < 10 ? 0 : 1; printf("No. of pages = %d\n",pages); printf("No. of lines = %d\n",lines); printf("No. of chars = %ld\n",charcount); printf("No. of letters = %ld\n",letters); if(charcount<=1) { strcpy(err,"Too few characters."); fclose(cfile); return; } if((hfile = fopen(htmlfile,"w"))==NULL) { sprintf(err,"Could not create file: %s",htmlfile); fclose(cfile); return; } printf("\nTranslating file: %s ... ",filename); fprintf(hfile,"<HTML><HEAD><TITLE>%s</TITLE></HEAD>\n",filename); fprintf(hfile,"<BODY TEXT=%s BGCOLOR=white><CENTER><H1>%s</H1></CENTER><HR><CODE>\n",c->text,filename); char *wstr = NULL, *wwstr = NULL; if((wstr = (char *) malloc (sizeof(char)*(strmax+1)))==NULL) { strcpy(err,"Out of memory."); fclose(hfile); fclose(cfile); return; } else *wstr = '\0'; unsigned int digits = 0; unsigned int _lines = lines; for(;_lines;digits++) _lines /= 10; char *nstr = NULL; if((nstr = (char *) malloc (sizeof(char)*(digits+1)))==NULL) { strcpy(err,"Out of memory."); free(wstr); fclose(hfile); fclose(cfile); return; } else *nstr = '\0'; fclose(cfile); unsigned int lnum = 0; unsigned register int cnum = 0; cfile = fopen(filename,"r"); /* C keywords */ char keys[33][9] = { "auto","break","case","char", "const","continue","default", "double","do","else","enum", "extern","float","for","goto", "if","int","long","register", "return","short","signed", "sizeof","static","struct", "switch","typedef","union", "unsigned","void","volatile", "while", "inline" }; for(bits.cont=0;fgets(wstr,strmax,cfile)!=NULL;lnum++) { unsigned int remaining = 0; unsigned int split_offset = 0; remainder: /* show translation progress */ char prog[4] = "00%"; double progn = (lnum*100)/lines; if(progn<100) numstr(prog,(int)progn,progn<10?1:2); strcat(prog,"%"); if(strlen(prog)==2) printf("\b\b%s",prog); if(strlen(prog)==3) printf("\b\b\b%s",prog); /* insert line numbers */ short int numlength = 0; if(remaining) goto so_go_on; numstr(nstr,lnum,digits); numlength = strlen(nstr); fprintf(hfile,"<B>%s</B> ",nstr); so_go_on: /* check if the line has been commented off */ if(bits.cm1&&lnum) fprintf(hfile,"<FONT COLOR=%s>",c->comment); /* reset the booleans in bits */ if(bits.cont && bits.pre) fprintf(hfile,"<B>"); else bits.pre = 0; if(bits.cont && bits.qt1) fprintf(hfile,"<FONT COLOR=%s>",c->string); else bits.qt1 = 0; bits.qt2 = 0; bits.cm2 = 0; bits.num = 0; unsigned int line_len = 0; line_len = line_length - numlength; if(remaining) { numstr(nstr,lnum,digits); numlength = strlen(nstr); strnull(nstr); strbluff(nstr," " ,numlength); fprintf(hfile,"%s ",nstr); char *t = allocs(line_len+1); strmid(t,wwstr,split_offset,line_len); strcpy(wstr,t); split_offset += strlen(wstr); remaining -= strlen(wstr); } if(strlen(wstr)>line_len && remaining <= 0) { split_offset = 0; char *t = allocs(line_len+1); strmid(t,wstr,split_offset,line_len); wwstr = clones(wstr); strcpy(wstr,t); split_offset += strlen(wstr); remaining = strlen(wwstr)-line_len; } for(cnum=0;cnum<strlen(wstr);cnum++) { /* color up the keywords */ int keyw; for(keyw=0;keyw<33;keyw++) if(!strncmp(&wstr[cnum],keys[keyw], strlen(keys[keyw]))&&!( wstr[cnum-1]>='A'&& wstr[cnum-1]<='Z')&&!( wstr[cnum-1]>='a'&& wstr[cnum-1]<='z')&&!( wstr[cnum-1]>='0'&& wstr[cnum-1]<='9')&& wstr[cnum-1]!='_' &&(!bits.cm1) &&(!bits.cm2) &&(!bits.qt1) &&(!bits.qt2) &&bits.color) { if(!(wstr[cnum+strlen(keys[keyw])]>='A'&& wstr[cnum+strlen(keys[keyw])]<='Z')&&!( wstr[cnum+strlen(keys[keyw])]>='a'&& wstr[cnum+strlen(keys[keyw])]<='z')&&!( wstr[cnum+strlen(keys[keyw])]>='0'&& wstr[cnum+strlen(keys[keyw])]<='9')&& wstr[cnum+strlen(keys[keyw])]!='_') { fprintf(hfile,"<FONT COLOR=%s>%s</FONT>",c->keyword,keys[keyw]); cnum += strlen(keys[keyw]); } } /* color up the comments */ if(!strncmp(&wstr[cnum],"/*",2) &&!bits.qt1 &&!bits.qt2 &&!bits.cm2 &&bits.color) { fprintf(hfile,"<FONT COLOR=%s>/",c->comment); bits.cm1 = 1; } else if(!strncmp(&wstr[cnum],"*/",2) &&!bits.qt1 &&!bits.qt2 &&!bits.cm2 &&(bits.cm1) &&bits.color) { fprintf(hfile,"*/</FONT>"); cnum++; bits.cm1 = 0; } else if((!strncmp(&wstr[cnum],"//",2)) &&!bits.qt1 &&!bits.qt2 &&!bits.cm2 &&!bits.cm1 &&bits.color) { fprintf(hfile,"<FONT COLOR=%s>/",c->comment); bits.cm2 = 1; } /* color up the strings */ else if(wstr[cnum]==34 &&(!bits.cm2) &&(!bits.cm1) &&(!bits.pre) &&bits.color) { if(wstr[cnum-1]!='\\') { if(!bits.qt1) { fprintf(hfile,"<FONT COLOR=%s>%c",c->string,34); bits.qt1 = 1; } else { fprintf(hfile,"%c</FONT>",34); bits.qt1 = 0; } } } /* color up the chars */ else if(wstr[cnum]==39 &&(!bits.qt1) &&(!bits.cm2) &&(!bits.cm1) &&(!bits.pre) &&bits.color) { if(wstr[cnum-1]!='\\') { if(!bits.qt2) { fprintf(hfile,"<FONT COLOR=%s>%c",c->chr,39); bits.qt2 = 1; } else { fprintf(hfile,"%c</FONT>",39); bits.qt2 = 0; } } } /* color up the numbers */ else if(wstr[cnum]>='0'&& wstr[cnum]<='9'&&!( wstr[cnum-1]>'A'&& wstr[cnum-1]<'Z')&&!( wstr[cnum-1]>'a'&& wstr[cnum-1]<'z')&& wstr[cnum-1]!='_' &&(!bits.cm2) &&(!bits.cm1) &&(!bits.qt1) &&(!bits.qt2) &&(!bits.num) &&(!bits.pre) &&bits.color) { fprintf(hfile,"<FONT COLOR=%s>%c",c->numbers,wstr[cnum]); bits.num = 1; } else if(bits.num /* to end number colouring */ &&((wstr[cnum]<'0') ||(wstr[cnum]>'9'))) { fprintf(hfile,"</FONT>%c",wstr[cnum]); bits.num = 0; } /* color up the preprocessors */ else if(wstr[cnum]=='#' &&(!bits.cm2) &&(!bits.cm1) &&(!bits.qt1) &&(!bits.qt2) &&(!bits.pre) &&(bits.color)) { fprintf(hfile,"<B>#"); bits.pre = 1; } /* replace the C symbols with their HTML equivalent */ else if(wstr[cnum-1]==' '&&wstr[cnum]==' ') fprintf(hfile," "); else if(wstr[cnum]=='\t') fprintf(hfile," "); else if(wstr[cnum]=='<') fprintf(hfile,"<"); else if(wstr[cnum]=='>') fprintf(hfile,">"); else if(wstr[cnum]=='\n') { fprintf(hfile,"</FONT>"); if(wstr[cnum-1]=='\\') bits.cont = 1; else bits.cont = 0; break; } else fprintf(hfile,"%c",wstr[cnum]); } if(bits.pre) fprintf(hfile,"</B>"); /* end font tag */ if(bits.cm1|| bits.cm2|| bits.qt1|| bits.qt2|| bits.num) fprintf(hfile,"</FONT>"); /* end the line */ fprintf(hfile,"<BR>\n"); if(remaining) goto remainder; } fputs("</CODE><HR NOSHADE><FONT FACE=Arial>\n",hfile); fprintf(hfile,"No. of pages = %d<BR>\n",pages); fprintf(hfile,"No. of lines = %d<BR>\n",lines); fprintf(hfile,"No. of chars = %ld<BR>\n",charcount); fprintf(hfile,"No. of letters = %ld\n",letters); fputs("</FONT></BODY></HTML>",hfile); puts("\b\b\b100%\n\nTranslation Complete."); printf("HTML File: %s stored.\n",htmlfile); free(wstr); fclose(hfile); fclose(cfile); }
void eval_long(char *str, long int *value) { /* Expression Evaluation Function (long) * ======================================== * This function evaluates an infix expression * for a long int result. First, it translates * the given expression to the postfix * form, and then evaluates the postfix * expression using the peval function. * The result is stored in int *value. * -------------------------------------------- * WARNING: *str is subject to slight changes, * so transfer expression to temporary string * before passing it here. */ trims(str,' '); register unsigned long int cur=0; /* count the number of operators */ if(*str=='-') { str++; cur++; } unsigned long int stack_size=0; for(;*str!='\0';str++,cur++) if(*str=='+'||*str=='-'|| *str=='^'||*str=='%'|| *str=='*'||*str=='/'|| *str=='<'||*str=='>'|| *str=='&'||*str=='|'|| *str==')'||*str=='(') stack_size++; str -= cur; /* no operators */ if(!stack_size) { vals_long(str,value); return; } /* stacks for storing postfix expression */ char * const ostack = (char *) malloc ((stack_size*2 + 1) * sizeof(char)); long int * const values = (long int *) malloc ((stack_size + 1) * sizeof(long int)); if(ostack==NULL||values==NULL) msg(no_mem,3); /* Rules: ostack contains operators(+,-,*..), wheras * values contains numbers. if char *ostack = 0, then * int *values is holds a valid number of the expression, * otherwise char *ostack contains a valid math operator. * Based on these rules the expression is designed. */ /* important loop counters... */ register unsigned long int top=0,ival=0,ops=0; /***********************************. | ( infix => postfix ) translation: | `***********************************/ { /* temporary character array */ char * const tmp = allocs(strlen(str)); /* temporary stack to hold operators (+,-,*,..) */ unsigned char * const cstack = (unsigned char *) malloc ((stack_size + 1) * sizeof(unsigned char)+11); if(cstack==NULL) msg(no_mem,3); *cstack = '\0'; char prevOp = '\0', negative=0; for(cur=0;;str++) { if(*str=='('&&cur) { register int pars = 0; for(;*str!='\0';str++) { if(*str=='(') pars++; if(*str==')') pars--; if(!pars) break; tmp[cur++] = *str; } if(*str=='\0') break; else tmp[cur++] = ')'; continue; } if(*str=='+'||*str=='-'|| *str=='*'||*str=='/'|| *str=='^'||*str=='%'|| *str=='<'||*str=='>'|| *str=='('||*str==')'|| *str=='&'||*str=='|'|| *str=='\0') { /* finalise tmp */ tmp[cur] = '\0'; trims(tmp,' '); for(cur=0;tmp[cur]!='\0';cur++); /* skip vals for opening braces */ if(*str=='(') goto after_vals; if(!cur) { /* skip the translation in case of repition of operators */ if(prevOp==')'||prevOp=='(') goto after_vals; else { if(prevOp=='-') negative++; prevOp = *str; if(*str=='\0') break; continue; } } /* bitwise and (&) */ if(*str=='&'&&str[1]=='&') *++str='A'; /* bitwise or (|) */ if(*str=='|'&&str[1]=='|') *++str='O'; /* number insertion: */ vals_long(tmp,&values[ival++]); if(negative) { values[ival-1]*=-1; negative = 0; } ostack[ops++] = 0; cur = *tmp = 0; after_vals: /* opernad insertion: */ if(eval_prior(cstack[top])<eval_prior(*str) ||*str=='(') cstack[++top] = *str; else { for(;top&&cstack[top]!='(';top--) ostack[ops++] = cstack[top]; if(*str!=')') cstack[++top] = *str; else if(cstack[top]=='(') cstack[top--] = 0; } if(*str=='\0') break; prevOp = *str; } else tmp[cur++] = *str; } /* free memory: */ free(cstack); free(tmp); } printf("\n---[%ld]---\n",ops); for(cur=0,top=0;cur<ops;cur++) if(!ostack[cur]) printf(" %ld ",values[top++]); else printf(" %c ",ostack[cur]); puts("\n-----------"); /*******************************. | Postfix Expression Evaluator: | `*******************************/ { /* temporary stack to evaluate expression */ long int * const stack = (long int *) malloc ((stack_size + 1) * sizeof(long int)); if(stack==NULL) msg(no_mem,3); for(cur=0,top=0,ival=0;cur<ops;cur++) { if(!ostack[cur]) /* an operand */ stack[ival++] = values[top++]; else { /* an operator (+,-,*,..) */ switch(ostack[cur]) { /* do the math */ case '+': stack[--ival-1] += stack[ival]; break; case '-': stack[--ival-1] -= stack[ival]; break; case '*': stack[--ival-1] *= stack[ival]; break; case '/': stack[--ival-1] /= stack[ival]; break; case '%': stack[--ival-1] %= stack[ival]; break; case 'A': stack[--ival-1] &= stack[ival]; break; case 'O': stack[--ival-1] |= stack[ival]; break; case '<': stack[ival-1] = /* bitwise left shift */ stack[ival-1] << stack[--ival]; break; case '>': stack[ival-1] = /* bitwise right shift */ stack[ival-1] >> stack[--ival]; break; case '^': stack[--ival-1] = (long int) pow (stack[ival-2],stack[ival-1]); break; case '&': concatenate(&stack[ival-1],stack[--ival]); break; default: stack[--ival-1] = stack[ival]; } } } /* set value */ *value = stack[0]; free(stack); } /* free memory: */ free(ostack); free(values); }
void exec(const char *str) { long int strn = 0; /* check validity of equation in number of brackets */ long int brackets = 0; for(;*str!='\0';str++,strn++) { if(*str=='(') brackets++; else if(*str==')') brackets--; } if(brackets!=0) { printf("\nError: Invalid Syntax, incorrect number of parentheses.\n"); return; } str -= strn; char *lhs = allocs(strn); char *rhs = allocs(strn); char *sptr = NULL; /* here, sptr points at const *str */ /* here the rhs & lhs are obtained */ for(sptr=lhs;*str!='\0'&&*str!='=';str++) *sptr++ = *str; *sptr = '\0'; if(*str=='=') { for(sptr=rhs,str++;*str!='\0';str++) { *sptr++ = *str; *sptr = '\0'; } } else { *rhs = '\0'; } trims(lhs,' '); /* here onwards, sptr is used as an error descrptor string. */ sptr = (char *) allocs(strn+64); *sptr = '\0'; if(*rhs=='\0') { /* <- empty rhs, so no assignment involved. */ /* check if rhs is a variable */ long int *value = NULL; value = getint(lhs); if(value==NULL) { /* rhs is NOT a variable */ long val=0; eval_long(lhs,&val); printf("\n%ld\n",val); } else printf("%s = %ld\n",lhs,*value); } else { /* rhs exists */ /* check for [=>] exchange directive */ if(*rhs=='>') { exchange_vars(lhs,rhs,sptr); if(strlen(sptr)) printf("\nError: %s\n",sptr); } else /* variable creation/assignment */ { void asgn(char *rhs, char *lhs, char *err); asgn(rhs,lhs,sptr); if(strlen(sptr)) printf("\nError: %s\n",sptr); } } /* free memeory */ free(sptr); free(lhs); free(rhs); }
static SV * new_av( PMenuItemReg m, int level) { AV * glo; if ( m == nil) return nilSV; glo = newAV(); while ( m) { AV * loc = newAV(); if ( !m-> flags. divider) { if ( m-> variable) { /* has name */ SV * sv; int shift = ( m-> flags. checked ? 1 : 0) + ( m-> flags. disabled ? 1 : 0); if ( shift > 0) { /* has flags */ int len = (int) strlen( m-> variable); char * name = allocs( len + shift); if ( name) { int slen = len + shift; memcpy( name + shift, m-> variable, len); if ( m-> flags. disabled) name[ --shift] = '-'; if ( m-> flags. checked) name[ --shift] = '*'; if ( m-> flags. autotoggle) name[ --shift] = '@'; sv = newSVpv( name, slen); } else sv = newSVpv( m-> variable, len); } else /* has name but no flags */ sv = newSVpv( m-> variable, 0); if ( m-> flags. utf8_variable) SvUTF8_on( sv); av_push( loc, sv); } else { /* has flags but no name - autogenerate */ int len; char buffer[20]; len = sprintf( buffer, "%s%s%s#%d", m-> flags. disabled ? "-" : "", m-> flags. checked ? "*" : "", m-> flags. autotoggle ? "@" : "", m-> id); av_push( loc, newSVpv( buffer, ( STRLEN) len)); } if ( m-> bitmap) { if ( PObject( m-> bitmap)-> stage < csDead) av_push( loc, newRV( SvRV((( PObject)( m-> bitmap))-> mate))); else av_push( loc, newSVpv( "", 0)); } else { SV * sv = newSVpv( m-> text, 0); if ( m-> flags. utf8_text) SvUTF8_on( sv); av_push( loc, sv); } if ( m-> accel) { SV * sv = newSVpv( m-> accel, 0); av_push( loc, sv); if ( m-> flags. utf8_accel) SvUTF8_on( sv); } else { av_push( loc, newSVpv( "", 0)); } av_push( loc, newSViv( m-> key)); if ( m-> down) { av_push( loc, new_av( m-> down, level + 1)); } else if ( m-> code) { av_push( loc, newSVsv( m-> code)); } else if ( m-> perlSub) { SV * sv = newSVpv( m-> perlSub, 0); if ( m-> flags. utf8_perlSub) SvUTF8_on( sv); av_push( loc, sv); } else { av_push( loc, newSVpv( "", 0)); } if ( m-> data) av_push( loc, newSVsv( m-> data)); } else { /* divider */ if ( m-> variable) { SV * sv = newSVpv( m-> variable, 0); if ( m-> flags. utf8_perlSub) SvUTF8_on( sv); av_push( loc, sv); } else { int len; char buffer[20]; len = sprintf( buffer, "#%d", m-> id); av_push( loc, newSVpv( buffer, ( STRLEN) len)); } } av_push( glo, newRV_noinc(( SV *) loc)); m = m-> next; } return newRV_noinc(( SV *) glo); }