/** get next token/lexem */ int gettoken(void) { int c, getch(void); void ungetch(int); char *p = token; /** skip whitespaces */ while ((c = getch()) == ' ' || c == '\t') ; /** collect '(' and "()" tokens */ if (c == '(') { if ((c = getch()) == ')') { strcpy(token, "()"); return tokentype = PARENS; } else { ungetch(c); return tokentype = '('; } } /** collect "[SIZE]" tokens */ else if (c == '[') { for (*p++ = c; (*p++ = getch()) != ']'; ) ; *p = '\0'; return tokentype = BRACKETS; } /** collect "NAME" tokens */ else if (isalpha(c)) { for (*p++ = c; isalnum(c = getch()); ) *p++ = c; *p = '\0'; ungetch(c); return tokentype = NAME; } /** collect everything else */ else return tokentype = c; }
int get_token(void) { int c, getch(void); void ungetch(int); char *p = token; while ((c = getch()) == ' ' || c == '\t') ; if (c == '(') { if ((c = getch()) == ')') { strcpy(token, "()"); return tokentype = PARENS; } else { ungetch(c); return tokentype = '('; } } else if (c == '[') { for (*p++ = c; (*p++ = getch()) != ']'; ) ; *p = '\0'; return tokentype = BRACKETS; } else if (isalpha(c)) { for (*p++ = c; isalnum(c = getch()); ) *p++ = c; *p = '\0'; ungetch(c); return tokentype = NAME; } else return tokentype = c; }
int main(int argc, char *argv[]) { char word[MAXWORD]; char word2[MAXWORD]; int i; struct nlist *np; struct tnode *root; root = NULL; while (getword(word, MAXWORD) != EOF) { if (strcmp(word, "#define") == 0 && getword(word, MAXWORD) != EOF && isalpha(word[0]) && getword(word2, MAXWORD) != EOF) { install(word, word2); } else if (isalpha(word[0])) { if ((np = lookup(word)) != NULL) { i = strlen(np->defn) - 1; while (i >= 0) { ungetch(np->defn[i]); i--; } } else { root = addtree(root, word); } } } /* INPUT: #define a x #define x e a x x c v b a q e OUTPUT: 1 b 1 c 5 e 1 q 1 v */ treeprint(root); return 0; }
bool QFile::open( int m, FILE *f ) { if ( isOpen() ) { #if defined(QT_CHECK_RANGE) qWarning( "QFile::open: File already open" ); #endif return FALSE; } init(); setMode( m &~IO_Raw ); setState( IO_Open ); fh = f; ext_f = TRUE; struct stat st; ::fstat( fileno(fh), &st ); #if defined(QT_LARGEFILE_SUPPORT) #if !defined(QT_ABI_QT4) off_t tmp = ftello( fh ); ioIndex = tmp > UINT_MAX ? UINT_MAX : (Offset)tmp; #else ioIndex = (Offset)ftello( fh ); #endif #else ioIndex = (Offset)ftell( fh ); #endif if ( (st.st_mode & S_IFMT) != S_IFREG || f == stdin ) { //stdin is non seekable // non-seekable setType( IO_Sequential ); length = INT_MAX; ioIndex = 0; } else { #if defined(QT_LARGEFILE_SUPPORT) && !defined(QT_ABI_QT4) length = st.st_size > UINT_MAX ? UINT_MAX : (Offset)st.st_size; #else length = (Offset)st.st_size; #endif if ( !(flags()&IO_Truncate) && length == 0 && isReadable() ) { // try if you can read from it (if you can, it's a sequential // device; e.g. a file in the /proc filesystem) int c = getch(); if ( c != -1 ) { ungetch(c); setType( IO_Sequential ); length = INT_MAX; ioIndex = 0; } resetStatus(); } } return TRUE; }
int getint(int *pn) { /* Get next integer from input into *pn. */ int c; int sign; while (isspace(c = getch())) continue; if (!isdigit(c) && c != EOF && c != '+' && c != '-') { printf("%c is not a number or sign\n", c); ungetch(c); return 0; } sign = (c == '-') ? -1 : 1; if (c == '+' || c == '-') { c = getch(); if (!isdigit(c)) { /* push both characters back on input */ ungetch((sign == 1) ? '+' : '-'); ungetch(c); return 0; } } for (*pn = 0; isdigit(c); c = getch()) *pn = 10 * *pn + (c - '0'); *pn *= sign; if (c != EOF) ungetch(c); return c; }
/*gettop: get next character or operand */ int getop(char s[]) { int i, c; double r; while ( (s[0] = c = getch() ) == ' ' || c == '\t') ; s[1] = '\0'; i = 0; if (!isdigit(c) && c != '.' && c != '-') return c; /* not a number */ if (c == '-') /* section to check negative values */ if (isdigit(c = getch()) || c == '.') /* if a negative symbol detects */ s[++i] = c; /* negative number*/ /* check whether next character is a number or a '.' if so */ else { /* that is negative number & that will be stored in the array to be returned*/ if (c != EOF) /* else return a negative operator to compute */ ungetch(c); return '-'; } if (isdigit(c)) /* collect integer parts */ while (isdigit(s[++i] = c = getchar())) ; if (c == '.') while (isdigit(s[++i] = c = getchar())); ; s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER; /* if (c == '\n') { i = 1; r = val[--sp]; printf("\t%.8g", r); } */ }
/* getop: get next operator or numeric operand */ int getop(char input[]) { int i, c; while ((input[0] = c = getch()) == ' ' || c == '\t') { ; } input[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-') { return c; /* not a number */ } i = 0; if (c == '-') { if (isdigit(input[++i] = c = getch())) { ; /* negative number */ } else { ungetch(c); /* not a negative, so backup and return negative */ return '-'; } } if (isdigit(c)) { /* collect integer part */ while (isdigit(input[++i] = c = getch())) { ; } } if (c == '.') { /* collect fraction part */ while (isdigit(input[++i] = c = getch())) { ; } } input[i] = '\0'; if (c != EOF) { ungetch(c); } return NUMBER; }
/* getop: получает следующий оператор или операнд */ int getop(char s[]) { int i = 0, c; while ((c = getch()) == ' ' || c == '\t' ) ; s[1] = '\0'; if (!isdigit(c) && c != '.') { if (c == '-') { s[i++] = c; if (!isdigit(c = getch())) { ungetch(c); return s[--i]; /* не число */ } } else { return c; /* не число */ } } /* накапливаем целую часть */ if (isdigit(c)) { s[i++] = c; while (isdigit(s[i++] = c = getch())) ; } /* накапливаем дробную часть */ if (c == '.') { s[i++] = c; while (isdigit(s[i++] = c = getch())) ; } if (!isdigit(s[i - 1])) --i; if (c != EOF) ungetch(c); s[i] = '\0'; return NUMBER; }
// Возвращает строку s во входной поток void ungets(char s[]) { // Узнаём размер int pos; for (pos = 0; s[pos] != '\0' && s[pos] != '\n' && s[pos] != EOF; pos++) ; --pos; // Возвращаем с конца в буфер while (pos >= 0) { ungetch(s[pos]); --pos; } }
/* ungets: push back an entire string onto the input */ void ungets(char s[]) { char c; int i; /* read input line and push back all its characters */ for (i = 0; (c = getchar()) != NEWLINE; i++) ungetch(c); s[i++] = NEWLINE; s[i] = NULLCHAR; /* get pushed back characters, newline and nullchar remain in place */ for (i = i - 2; i >= 0; i--) s[i] = getch(); }
int kbhit(void) { int temp; if (!screen_initialized) init_screen(); wtimeout(stdscr, 0); if ((temp = wgetch(stdscr)) != EOF) { ungetch(temp); nodelay(stdscr, FALSE); return TRUE; } else { nodelay(stdscr, FALSE); return FALSE; } }
char * cgets(char *string) { unsigned len = 0; unsigned int maxlen_wanted; char *sp; int c; /* * Be smart and check for NULL pointer. * Don't know wether TURBOC does this. */ if (!string) return (NULL); maxlen_wanted = (unsigned int) ((unsigned char) string[0]); sp = &(string[2]); /* * Should the string be shorter maxlen_wanted including or excluding * the trailing '\0' ? We don't take any risk. */ while (len < maxlen_wanted - 1) { c = getch(); /* * shold we check for backspace here? * TURBOC does (just checked) but doesn't in cscanf (thats harder * or even impossible). We do the same. */ if (c == '\b') { if (len > 0) { cputs("\b \b"); /* go back, clear char on screen with space and go back again */ len--; sp[len] = '\0'; /* clear the character in the string */ } } else if (c == '\r') { sp[len] = '\0'; break; } else if (c == 0) { /* special character ends input */ sp[len] = '\0'; ungetch(c); /* keep the char for later processing */ break; } else { sp[len] = putch(c); len++; } } sp[maxlen_wanted - 1] = '\0'; string[1] = (char) ((unsigned char) len); return (sp); }
int getword(char *word, int lim) { int c, getch(void); void ungetch(int); char *w = word; while (isspace(c = getch())){ /*if (c=='\n'){ return 0*/; // } ;} if(c==EOF) return EOF; if (isalpha(c)){ *w++ = c; } if (!isalpha(c)) { if(c=='/'){ if((c=getch())=='*'){ while((c=getch())!='*') ; if((c=getch())=='/'){ if((c=getch())=='\n'){ return 1;} } } } if(c=='"'){ while((c=getch())!='"') ; if((c=getch())=='\n') return 1; } } for ( ; --lim > 0; w++) if (!isalnum(*w = getch())&&*w!='_'&&*w!='.') { if(*w=='\n'){ *w='\0'; return 1; } ungetch(*w); break; } *w = '\0'; return 1; }
int comment(void){ int getch(void); void ungetch(int); int c; while((c = getch()) != EOF) if(c == '*'){ if((c = getch()) == '/') break; else ungetch(c); } return c; }
int main(void) { int c; while ((c = getch()) != EOF) { if (c == '\n'); // Do nothing else if (c >= '0' && c <= '9') { ungetch(c); printf("[%c] is a number\n", getch()); } else printf("[%c] is not a number\n", c); } return 0; }
double getdouble(double *pn) { int i = 0, e = 0; double b, c, sign; while ((c = getch()) == ' ' || c == '\t') ; if (!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '\n' && c != '.') c = getdouble(&array[++n]); sign = (c == '-') ? -1 : 1; if (c == '+' || c == '-') { if (isdigit(b = getch())) c = b; else c = getdouble(&array[++n]); } if (c == '\n') { printf("\n"); c = getdouble(&array[++n]); } if (c != EOF) { *pn = 0; i = 10 * i + (c - '0'); while (isdigit(c = getch())) i = 10 * i + (c - '0'); if (c == '.') { while(isdigit(c = getch())) { *pn = 10.0 * *pn + (c - '0'); e--; } } while (e < 0) { *pn *= 0.1; e++; } *pn += i; *pn *= sign; ungetch(c); } return c; }
int POL::gettok (TOKEN *tok) { int c, toktype; int inum; double fnum; int toksiz = MAXTOK; /* maximum length of token string */ while ((c = inchar()) == BLANK || c == TAB) ; ungetch (c); c = lookchar(); toktype = type(c); fnum = 0.0; inum = 0; if (c == BLANK || c == TAB) { /* skip white space */ getblank(tok->tokstr, toksiz); toktype = TT_BLANK; } else if (toktype == LETTER) { toktype = getalpha (tok->tokstr, toksiz); } else if (c == meta.str) { /* quoted string */ getquote (tok->tokstr, toksiz); toktype = TT_STRING; } else if (type(c) == DIGIT || c == PLUS || c == HYPHEN || c == PERIOD) { toktype = getnumber (tok->tokstr, toksiz, &fnum, &inum); } else if (c == EOF) { tok->tokstr[0] = EOS; toktype = TT_EOF; } else { c = inchar(); tok->tokstr[0] = c; tok->tokstr[1] = EOS; toktype = TT_SPECLCHAR; } tok->type = toktype; tok->ready = true; if (tok->type == TT_REAL || tok->type == TT_INT) { tok->fnum = fnum; tok->inum = inum; } else { tok->fnum = 0.0; tok->inum = 0; } return (toktype); }
int getfloat(float *pn) { int c, sign; float power; while(isspace(c=getch() )) ; if( !isdigit(c) && c !=EOF && c !='+' && c!= '-' && c != '.') { ungetch(c); return 0; } sign = (c== '-')? -1:1; if (c== '+' || c == '-') if(!isdigit(c =getch() && c != '.'){ ungetch(c); return 0; } for(*pn =0; isdigit(c); c=getch()) *pn = 10.0* *pn + (c - '0'); if(c == '.') getch(c); for(*pn=0, power =1.0; isdigit(c); c=getch()){ *pn = 10.0 * *pn + (c - '0'); power *= 10.0; } *pn *= (sign/power); if( c != EOF) ungetch(c); return c; }
int gettoken(void) /* return next token */ { int c,getch(void); void ungetch(int); char *p = token; if(prevtoken==YES) { prevtoken = NO; return tokentype; } while((c=getch())==' '||c=='\t'){ ; } if(c=='('){ if((c=getch())==')'){ strcpy(token,"()"); return tokentype=PARENS; }else{ ungetch(c); return tokentype=')'; } }else if(c=='['){ for(*p++=c;(*p++=getch())!=']';) ; *p='\0'; return tokentype = BRACKETS; }else if(isalpha(c)){ for(*p++=c;isalnum(c=getch());){ *p++=c; } *p='\0'; ungetch(c); return tokentype = NAME; }else{ return tokentype=c; } }
/* getop: get next operator or numeric operand */ int getop(char s[]) { int i, c; while ((s[0] = c = getch()) == ' ' || c == '\t') { ; } s[1] = '\0'; if (!isdigit(c) && c != '.') { if (c == COMMAND) { /* a command, read the command till a non-printable character */ for (i = 0; (s[i] = c = getch()) != ' ' && c != '\n' && c != '\t' && c != EOF; ++i) { ; } ungetch(c); /* return the extra character read */ s[i] = '\0'; /* terminate the command string */ return COMMAND; } return c; /* not a number, not a comamnd, this is an operator */ } i = 0; if (isdigit(c)) { /* collect integer part */ while (isdigit(s[++i] = c = getch())) { ; } } if (c == '.') { /* collect fraction part */ while (isdigit(s[++i] = c = getch())) { ; } } s[i] = '\0'; if (c != EOF) { ungetch(c); } return NUMBER; }
/* getop: get next operator or numeric operand*/ int getop(char s[]) { int i, c; int c2; while ((c = getch()) == ' ' || c == '\t') ; s[0] = c; s[1] = '\0'; i = 1; if (!isdigit(c) && c != '.') { if (c == '-'){ c2 = getch(); if (isdigit(c2) || c2 == '.') { s[i++] = c2; } else { ungetch(c2); return c; } } else return c; } while (isdigit(c = getch())) s[i++] = c; if (c == '.') { s[i++] = c; while (isdigit(c = getch())) s[i++] = c; } s[i] = '\0'; // if (c != EOF) ungetch(c); return NUMBER; }
int getword(char *word, int lim, int *lineNum) { int c; char *w = word; while (isspace(c = getch())){ //出现换行符 if ('\n' == c){ (*lineNum)++; } } if (c != EOF && '/' != c){ *w++ = c; } if (!isalpha(c) && '_' != c && '/' != c){ *w = '\0'; return c; } if ((isalpha(c) || '_' == c) && '/' != c){ for (; --lim > 0; w++){ if (!isalnum(*w = getch()) && '_' != *w){ ungetch(*w); break; } } } else if ('/' == c){ if ('*' == (c = getch())){ comment(); } else{ ungetch(c); } } *w = '\0'; return word[0]; }
char * getnextoken(void) { int c; char *bp = tokbuf; for (;;) { if ((c = getch()) < 0) return 0; if (c == ' ' || c == '\t') continue; if (c == '\n') { *bp++ = c; *bp = 0; return tokbuf; } /* start of something */ ungetch(c); break; } for (;;) { if ((c = getch()) < 0) { ungetch(c); if (bp > tokbuf) { *bp = 0; return tokbuf; } return 0; } else if (c == ' ' || c == '\t' || c == '\n') { ungetch(c); *bp = 0; return tokbuf; } *bp++ = c; } return 0; /* not reached */ }
int getint ( int *pn ) { int c = 0; int sign = 0; while ( isspace ( c = getch () ) ) { } if ( ! isdigit ( c ) && c != EOF && c != '+' && c != '-' ) { // ungetch ( c ); return 0; }; sign = ( c == '-' ) ? ( -1 ) : 1; if ( c == '+' || c == '-' ) { c = getch (); if ( ! isdigit ( c ) ) { ungetch ( ( sign == 1 ) ? '+' : '-' ); return 0; } }; for ( *pn = 0; isdigit ( c ); c = getch () ) { *pn = 10 * *pn + ( c - '0' ) ; }; *pn *= sign; if ( c != EOF ) { ungetch ( c ); } return c; }
/* getop: get next operator or numeric operand, return type */ int getop(char s[]) { int i, c, type; i = 0; while ( (s[i] = c = getch()) == ' ' || c == '\t') ; /* skip white space */ if (c == '-') { /* '-' could be operator or negative indicator */ s[++i] = c = getch(); if (!isdigit(c)) { ungetch(c); s[i] = '\0'; return s[0]; } } if (isop(c) || c == '\n') { s[i] = '\0'; return c; } else if (isdigit(c) || c == '.') { /* digit */ type = NUMBER; if (isdigit(c)) /* collect integer part */ while (isdigit(s[++i] = c = getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())) ; } else { /* handles any other input */ while ( (s[++i] = c = getch()) != ' ' && c != '\t' && c != '\n') ; type = optype(s, i-1); } s[i] = '\0'; if (c != EOF) ungetch(c); return type; }
int getint(int *pn) { int c, sign = 1; c = getch(); while(isspace(c)) { c = getch(); } if(c != '+' && c != '-' && !isdigit(c) && c != EOF) { ungetch(c); return 0; } if(c == '+' || c == '-') { if(c == '-') { sign = -1; } c = getch(); if(!isdigit(c)) { ungetch(c); ungetch(sign == 1 ? '+' : '-'); return 0; } } *pn = 0; while(isdigit(c)) { *pn = *pn * 10 + c - '0'; c = getch(); } *pn *= sign; if(c != EOF) { ungetch(c); } return c; }
int main(int argc, char *argv[]) { // awesome code goes here: int c; while ((c = getch()) != EOF) { // if (c >= '0' && c <= '9') printf("%c", c); if (c < '0' && c > '9') ungetch(c); //printf( } return 0; }
/* getfloat - get next floating point value from input into *f * * handle strings of digits with decimal points for now: * * 12.3 -89.0 23435.34301 * */ int getfloat(double *f) { double val, power; int c, sign; while (isspace(c = getch())) /* skip white space */ ; if (!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.') { ungetch(c); return 0; } sign = (c == '-') ? -1 : 1; if (c == '+' || c == '-') { c = getch(); if (!isdigit(c) && c != '.') { ungetch(c); return 0; } } for (val = 0.0; isdigit(c); c = getch()) val = 10.0 * val + (c - '0'); if (c == '.') c = getch(); for (power = 1.0; isdigit(c); c = getch()) { val = 10.0 * val + (c - '0'); power *= 10.0; } *f = sign * val / power; if (c != EOF) ungetch(c); return(c); }
int isAssigner(char s []) { int nextChar; if (s[0] == '-') { nextChar = getch(); if (nextChar != '>') { ungetch(nextChar); return 0; } return 1; } return 0; }
// getword: get word input int getword(char *w, int lim) { int c; char *word = w; while (isspace(c=getch())); if (!isalpha(c) && c != '_') return c; for (*w++=c; w-word<lim-1 && (isalnum(c=getch()) || c=='_'); *w++=c); if (w-word < lim-1) { ungetch(c); c = word[0]; } *w = '\0'; return c; }