home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 273_01 / getfield.cc < prev    next >
Text File  |  1988-04-28  |  5KB  |  192 lines

  1. #include <tcutil.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. get_field(int row, int col, char *str, int sleng, int attr, int dleng, char *mask)
  6. /*
  7. ┌────────────────────────────────────────────────────────────────────┐
  8. │Purpose: To provide for formatted field input capability.           │
  9. │                                                                    │
  10. │ Inputs: row = row to read field from                               │
  11. │         col = col to read field from                               │
  12. │         *str = pointer to char string to store field into.         │
  13. │         sleng = leng of string pointed to by *str.                 │
  14. │         attr = attribute used to display field.                    │
  15. │         dleng = length of display area.                            │
  16. │         *mask = pointer to mask string.                            │
  17. │         (THE MASK STRING MUST BE THE SAME LENGTH AS IS SPECIFIED   │
  18. │          IN THE SLENG PARAMETER!!!!  THE MASK CONSISTS OF THE      │
  19. │          FOLLOWING SPECIAL CHARACTERS.                             │
  20. │          A = allow alpha characters only in this position.         │
  21. │          N = allow numeric characters only in this position.       │
  22. │      blank = allow any characters in this position.                │
  23. │          # = force the enter key when the user hits this position. │
  24. │                                                                    │
  25. │          ANY OTHER CHARACTER IN THE MASK FIELD WILL BE DISPLAYED   │
  26. │          ON THE SCREEN AND INPUT WILL NOT BE ALLOWED IN THAT       │
  27. │          POSITION. NOTE!!!!!!!!!! THE A AND N MASK CHARACTERS      │
  28. │          MUST BE UPPER CASE IN ORDER TO BE RECOGNIZED!!!!!!!!!!!!!!│
  29. │                                                                    │
  30. │Outputs: None.                                                      │
  31. │                                                                    │
  32. │                                                                    │
  33. │ Return: An integer value indicating the key that was pressed to    │
  34. │         terminate the entry of the field.  I.E. the enter key      │
  35. │         or the ESC key.                                            │
  36. │                                                                    │
  37. └────────────────────────────────────────────────────────────────────┘
  38. */
  39. {
  40.     int ch, ch2, idx=0, tslen=sleng, tdlen=dleng, orow, ocol, ccol;
  41.     int lcol, odx=0, lidx, term_ch=0, x, tidx, fidx;
  42.     int cur_stat, skip=0;
  43.     char mch, backup=0;
  44.  
  45.  
  46.     locate(row,col);
  47.     cur_stat = get_cur(&orow,&ocol);
  48.     show_cur(1);
  49.     rcolor(row,col,attr,dleng);
  50.     ccol=ocol;
  51.     lcol=ocol + tdlen -1;
  52.     strcpy(str,mask);
  53.     for(x=0; x <= sleng; x++)  {
  54.         switch (*(str + x)) {
  55.             case 'A':
  56.             case 'X':
  57.             case 'N':
  58.             case '#':
  59.                 *(str + x) = ' ';
  60.         }
  61.     }
  62.     writef_n(row,col,attr,str,dleng);
  63.  
  64.     while(1) {
  65.         mch=*(mask + idx);
  66.         if(!idx) {
  67.             if(mch == 'A' || mch == ' ' || mch == 'N') {
  68.                 idx=idx;
  69.             }
  70.             else {
  71.                 idx++; ccol++; tslen--; skip=1;
  72.             }
  73.         }
  74.         else skip=0;
  75.  
  76.         if(!skip) ch=get_xa();
  77.         if(!skip) {
  78.             if(isprint(ch)) ch2=0;
  79.             else ch2=ch;
  80.         }
  81.  
  82.         if(!skip) {
  83.             switch(ch2) {
  84.                 case 0:
  85.                     if(mch == 'A') {
  86.                         if(!isalpha(ch)) {
  87.                             beep(1600,5);
  88.                             break;
  89.                         }
  90.                     }
  91.                     if(mch == 'N') {
  92.                         if(!isdigit(ch)) {
  93.                             beep(1600,5);
  94.                             break;
  95.                         }
  96.                     }
  97.                     *(str + idx) = ch;
  98.                     idx++; ccol++; tslen--;
  99.                     break;
  100.                 case K_ENTER:
  101.                 case K_ESC:
  102.                     term_ch=ch;
  103.                     goto end_it;
  104.                 case K_TAB:
  105.                 case K_RIGHT:
  106.                     idx++; ccol++; tslen--;
  107.                     break;
  108.                 case K_HOME:
  109.                     idx=odx=0;
  110.                     tslen=sleng;
  111.                     ccol=ocol;
  112.                     break;
  113.                 case K_END:
  114.                     for(idx=sleng - 1; idx >=0 ;idx--) {
  115.                         if(*(str + idx) != ' ') {
  116.                             idx++;
  117.                             break;
  118.                         }
  119.                     }
  120.                     tslen = sleng - idx;
  121.                     if(idx + 1 <= dleng) {
  122.                         odx = 0;
  123.                         ccol = ocol + idx;
  124.                     }
  125.                     else {
  126.                         odx = idx - dleng + 1;
  127.                         ccol = lcol;
  128.                     }
  129.                     break;
  130.                 case K_BACKSPACE:
  131.                     backup=1;
  132.                     if(idx) idx--;
  133.                     mch = *(mask + idx);
  134.                     tslen++;
  135.                     ccol--;
  136.                     if(mch == 'A' || mch == 'X' || mch == 'N')
  137.                         *(str + idx) = ' ';
  138.                     break;
  139.                 case K_LEFT:
  140.                     backup=1;
  141.                     idx--; tslen++;
  142.                     ccol--;
  143.                     break;
  144.                 case K_CEND:
  145.                     for(x=idx; x < sleng; x++)
  146.                         *(str + x) = ' ';
  147.                     break;
  148.                 default:
  149.                     break;
  150.                     /*term_ch=ch;*/
  151.                     /*goto end_it;*/
  152.             }
  153.         }
  154.         mch = *(mask + idx);
  155.         while(idx && tslen && (mch != 'A' && mch != ' ' && mch != 'N' && mch != '#')) {
  156.             if(backup) {
  157.                 idx--; ccol--; tslen++;
  158.             }
  159.             else {
  160.             idx++; ccol++; tslen--;
  161.             }
  162.             mch = *(mask + idx);
  163.         }
  164.         backup=0;
  165.         if(tslen > sleng) tslen=sleng;
  166.         if(tslen < 0) tslen=0;
  167.         if(idx < 0) idx=0;
  168.         if(idx > sleng - 1) idx=sleng-1;
  169.         if(ccol > lcol) {
  170.             ccol=lcol;
  171.             odx++;
  172.         }
  173.         if(ccol < ocol) {
  174.             ccol=ocol;
  175.             odx--;
  176.         }
  177.         if(odx < 0) odx=0;
  178.         if(odx > (sleng-dleng)) odx=sleng-dleng;
  179.         writef_n(orow,ocol,attr,str+odx,dleng);
  180.         locate(orow,ccol);
  181.         if(mch == '#') {
  182.             term_ch = 0x00;
  183.             goto end_it;
  184.         }
  185.     }
  186. end_it:
  187.     trim_r(str);
  188.     if(!term_ch) term_ch=0x0d;
  189.     if(cur_stat) hide_cur();
  190.     return(term_ch);
  191. }
  192.