home *** CD-ROM | disk | FTP | other *** search
- /* int_in() -- tokenizer routine for inputting a number.
- * int_in() returns a pointer to a static data area. This area gets
- * overwritten with each call to int_in so use the data before calling
- * int_in() again.
- */
- char * int_in()
- {
- register char c,*s;
- static char text[20];
-
- s = &text[0];
-
- /* beginning state, skip junk until either '-' or ['0'-'9'] comes along */
-
- l1: c=input();
- if(c>='0' && c<='9') goto l3;
- else if(c == '-') goto l2;
- else {
- if(c=='\n' || c=='\0') rdlin(bsin);
- goto l1;
- }
-
- /* skipped junk, seen '-', gather it and make sure next char is a digit */
-
- l2: *s++ = c;
- c=input();
- if(c==' ' || c=='\t') goto l2; /* allow white between sign and digit */
- else if(c>='0' && c<='9') goto l3;
- else { /* seen something not allowed. */
- s = &text[0];
- printf("\n\007??");
- goto l1; /* restart machine */
- }
-
- /* skipped junk, seen a digit, gather until a non-digit appears */
-
- l3: *s++ = c;
- c=input();
- if(c>='0' && c<='9') goto l3;
- else {
- /* have reached successful conclusion to machine. */
- unput(c);
- *s++ = '\0';
- return(text);
- }
- }
-
- /* real_in() -- read in a floating point number using input().
- *
- * real_in() returns a pointer to a static data area. This data area
- * gets overwritten with each call to real_in(), so use it quickly.
- */
- char *real_in()
- {
- register char *s,c;
- static char bf[30];
-
- s = &bf[0];
-
- /* starting state. loops back until something interesting seen */
-
- state1: c=input();
- if(c == '-') goto state3;
- else if(c>='0' && c<='9') goto state2;
- else if(c == '.') goto state4;
- else {
- if(c == '\0') return(0);
- /* else */
- if(c == '\n') rdlin(bsin);
- goto state1;
- }
-
- /* seen ([sign] dig). loop back for digs, looking for (.|e|E) */
-
- state2: *s++ = c;
- c=input();
- if(c>='0' && c<='9') goto state2;
- else if(c=='e' || c=='E') goto state6;
- else if(c == '.') goto state4;
- else goto state9; /* done */
-
- /* seen (sign). looking for (dig). ignore whitespace. */
-
- state3: *s++ = c;
- state3_a: c=input();
- if(c==' ' || c=='\t') goto state3_a;
- else if(c>='0' && c<='9') goto state2;
- else if(c == '.') goto state4;
- else goto state10; /* error, had a sign so we have to have digs. */
-
- /* seen ([sign] digs '.'). looking for digs. done on anything else */
-
- state4: *s++ = c;
- c=input();
- if(c>='0' && c<='9') goto state5;
- else goto state9; /* done */
-
- /* seen ([sign] digs '.' dig). looking for (dig|e|E). done on anything else */
-
- state5: *s++ = c;
- c=input();
- if(c=='e' || c=='E') goto state6;
- else if(c>='0' && c<='9') goto state5;
- else goto state9;
-
- /* seen ([sign] digs '.' digs (e|E)). looking for sign or digs, else error. */
-
- state6: *s++ = c;
- c=input();
- if(c=='+' || c=='-') goto state7;
- else if(c>='0' && c<='9') goto state8;
- else goto state10; /* error */
-
- /* seen ([sign] digs '.' digs (e|E) sign). looking for digs, else error. */
-
- state7: *s++ = c;
- c=input();
- if(c>='0' && c<='9') goto state8;
- else goto state10; /* error */
-
- /* seen ([sign] digs '.' digs (e|E) [sign] dig). looking for digs. */
-
- state8: *s++ = c;
- c=input();
- if(c>='0' && c<='9') goto state8;
- else goto state9; /* done */
-
- /* seen a complete number. machine successfully completed. whew! */
-
- state9: unput(c); /* might want that later */
- *s++ = '\0';
- return(bf);
-
- /* Uh oh. An error. Print an error and restart. */
-
- state10: printf("\n\007??");
- s = &bf[0];
- goto state1;
- }
-