home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume2 / basic / part2 / newbs / num_ins.c < prev    next >
Encoding:
Text File  |  1986-11-30  |  3.3 KB  |  140 lines

  1. /* int_in() -- tokenizer routine for inputting a number.
  2.  * int_in() returns a pointer to a static data area.  This area gets 
  3.  * overwritten with each call to int_in so use the data before calling
  4.  * int_in() again.
  5.  */
  6. char * int_in()
  7. {
  8.     register char c,*s;
  9.     static char text[20];
  10.  
  11.     s = &text[0];
  12.  
  13. /* beginning state, skip junk until either '-' or ['0'-'9'] comes along */
  14.  
  15. l1: c=input();
  16.     if(c>='0' && c<='9') goto l3;
  17.     else if(c == '-') goto l2;
  18.     else {
  19.     if(c=='\n' || c=='\0') rdlin(bsin);
  20.     goto l1;
  21.     }
  22.  
  23. /* skipped junk, seen '-', gather it and make sure next char is a digit */
  24.  
  25. l2: *s++ = c;
  26.     c=input();
  27.     if(c==' ' || c=='\t') goto l2; /* allow white between sign and digit */
  28.     else if(c>='0' && c<='9') goto l3;
  29.     else { /* seen something not allowed. */
  30.     s = &text[0];
  31.     printf("\n\007??");
  32.     goto l1; /* restart machine */
  33.     }
  34.  
  35. /* skipped junk, seen a digit, gather until a non-digit appears */
  36.  
  37. l3: *s++ = c;
  38.     c=input();
  39.     if(c>='0' && c<='9') goto l3;
  40.     else {
  41.     /* have reached successful conclusion to machine. */
  42.     unput(c);
  43.     *s++ = '\0';
  44.     return(text);
  45.     }
  46. }
  47.  
  48. /* real_in() -- read in a floating point number using input().
  49.  *
  50.  * real_in() returns a pointer to a static data area.  This data area
  51.  * gets overwritten with each call to real_in(), so use it quickly.
  52.  */
  53. char *real_in()
  54. {
  55.     register char *s,c;
  56.     static char bf[30];
  57.  
  58.     s = &bf[0];
  59.  
  60. /* starting state.  loops back until something interesting seen */
  61.  
  62. state1:    c=input();
  63.     if(c == '-') goto state3;
  64.     else if(c>='0' && c<='9') goto state2;
  65.     else if(c == '.') goto state4;
  66.     else {
  67.         if(c == '\0') return(0);
  68.         /* else */
  69.         if(c == '\n') rdlin(bsin);
  70.         goto state1;
  71.     }
  72.  
  73. /* seen ([sign] dig). loop back for digs, looking for (.|e|E) */
  74.  
  75. state2: *s++ = c;
  76.     c=input();
  77.     if(c>='0' && c<='9') goto state2;
  78.     else if(c=='e' || c=='E') goto state6;
  79.     else if(c == '.') goto state4;
  80.     else goto state9;    /* done */
  81.  
  82. /* seen (sign).  looking for (dig). ignore whitespace. */
  83.  
  84. state3: *s++ = c;
  85. state3_a: c=input();
  86.     if(c==' ' || c=='\t') goto state3_a;
  87.     else if(c>='0' && c<='9') goto state2;
  88.     else if(c == '.') goto state4;
  89.     else goto state10;    /* error, had a sign so we have to have digs. */
  90.  
  91. /* seen ([sign] digs '.').  looking for digs.  done on anything else */
  92.  
  93. state4: *s++ = c;
  94.     c=input();
  95.     if(c>='0' && c<='9') goto state5;
  96.     else goto state9;    /* done */
  97.  
  98. /* seen ([sign] digs '.' dig).  looking for (dig|e|E). done on anything else */
  99.  
  100. state5:    *s++ = c;
  101.     c=input();
  102.     if(c=='e' || c=='E') goto state6;
  103.     else if(c>='0' && c<='9') goto state5;
  104.     else goto state9;
  105.  
  106. /* seen ([sign] digs '.' digs (e|E)). looking for sign or digs, else error. */
  107.  
  108. state6: *s++ = c;
  109.     c=input();
  110.     if(c=='+' || c=='-') goto state7;
  111.     else if(c>='0' && c<='9') goto state8;
  112.     else goto state10;    /* error */
  113.  
  114. /* seen ([sign] digs '.' digs (e|E) sign). looking for digs, else error. */
  115.  
  116. state7: *s++ = c;
  117.     c=input();
  118.     if(c>='0' && c<='9') goto state8;
  119.     else goto state10;    /* error */
  120.  
  121. /* seen ([sign] digs '.' digs (e|E) [sign] dig). looking for digs. */
  122.  
  123. state8: *s++ = c;
  124.     c=input();
  125.     if(c>='0' && c<='9') goto state8;
  126.     else goto state9;    /* done */
  127.  
  128. /* seen a complete number.  machine successfully completed.  whew! */
  129.  
  130. state9: unput(c);    /* might want that later */
  131.     *s++ = '\0';
  132.     return(bf);
  133.  
  134. /* Uh oh.  An error.  Print an error and restart. */
  135.  
  136. state10: printf("\n\007??");
  137.     s = &bf[0];
  138.     goto state1;
  139. }
  140.