home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / catalogs / mtchplay.zip / MP_UTILS.C < prev    next >
Text File  |  1993-02-13  |  4KB  |  184 lines

  1. /* MP_UTILS.C */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <ctype.h>
  6. #include <dos.h>
  7. #include "defines.h"
  8.  
  9. extern PLAYER player[MAXFL][MAXSZ];
  10. extern int NP[], order[];
  11. extern char mark[];
  12. extern int no_space;
  13.  
  14. /* function prototypes */
  15. void
  16.   error_exit(char *),
  17.   hide_cursor(void),
  18.   showcursor(void);
  19.  
  20. int
  21.   find(char *, int *),
  22.   findn(char *, int *, int *),
  23.   Getint(int, char *, int *),
  24.   getkey(void),
  25.   get_string(char *, char *, char *, int, int);
  26.  
  27. /* ---------------------------------------------------------------------- */
  28. void
  29. error_exit(char *msg)
  30. {
  31.    cputs(msg);
  32.    showcursor();
  33.    exit(1);
  34. }
  35.  
  36. /* ---------------------------------------------------------------------- */
  37. int
  38. find(char *pname, int *flight)  /* find flight and index of player 'pname' */
  39. {
  40.   int m,k;
  41.  
  42.   for (k=0; k<MAXFL; k++) {
  43.     if (NP[k] == 0) continue;
  44.     for (m=0; m<NP[k]; m++) {
  45.       if (strcmp(player[k][m].name,pname) == 0) {
  46.         *flight = k;
  47.         return m;
  48.       }
  49.     }
  50.   }
  51.   return -1;
  52. }
  53.  
  54. /* ---------------------------------------------------------------------- */
  55. /* find up to 10 players with names starting 'pname*', put in flt[],index[] */
  56. int
  57. findn(char *pname, int flt[], int index[])
  58. {  
  59.   int m, j=-1, k, n = strlen(pname);
  60.  
  61.   for (k=0; k<MAXFL; k++) {
  62.     if (NP[k] == 0) continue;
  63.     for (m=0; m<NP[k]; m++) {
  64.       if (strncmpi(player[k][m].name, pname, n) == 0) {
  65.         if (++j > 9)
  66.           return -2;
  67.         flt[j] = k;
  68.         index[j] = m;
  69.       }
  70.     }
  71.   }
  72.   return j;
  73. }
  74.  
  75. /* ---------------------------------------------------------------------- */
  76. /*     extract n pos int values from string s, put them in array vi[]     */
  77. int
  78. Getint(int n, char *s, int vi[])
  79. {
  80.   int cnt=0;
  81.   char *p;
  82.  
  83.   if (n == 0) return cnt;
  84.  
  85.   p = s;
  86.   while (cnt < n && *p)
  87.     {
  88.      while (*p && !isdigit(*p)) p++;
  89.      if (*p) {
  90.        vi[cnt] = atoi(p);
  91.        while (isdigit(*p)) p++;
  92.        cnt++;
  93.      }
  94.     }
  95.   return cnt;
  96. }
  97.  
  98. /* ---------------------------------------------------------------------- */
  99.                  /* basic input loop feeder */
  100. int 
  101. getkey(void)
  102. {
  103.  int k;
  104.  
  105.  k = getch();
  106.  if (k == 0) k = -getch();
  107.  return k;
  108. }
  109.  
  110. /* ---------------------------------------------------------------------- */
  111. int
  112. get_string(char *prompt, char *dest, char *mask, int col, int row)
  113. {
  114.    int i, c, pos=0, maxpos=strlen(mask);
  115.  
  116.    if (maxpos == 0) maxpos = 20;     /* set default string length */
  117.    for (i=0; i<maxpos; i++)
  118.      if (mask[i] == '-') dest[i] = '-'; else dest[i] = 0;
  119.  
  120.    if (strlen(prompt)) {
  121.      gotoxy(col-strlen(prompt)-2, row);
  122.      cputs(prompt); cputs(": ");
  123.    }
  124.    else
  125.      gotoxy(col,row);
  126.    cputs(mask);
  127.    gotoxy(col,row); showcursor();
  128.    while (kbhit())
  129.      getch();                    /* clear kb buffer */
  130.    while (1) {
  131.      c = getkey();
  132.      if (c == ' ' && no_space)
  133.        continue;
  134.      if (isprint(c) && pos<maxpos) {
  135.        cprintf("%c",c);
  136.        dest[pos] = c;
  137.        while (pos++ < maxpos && mask[pos] == '-')
  138.          ;
  139.        gotoxy(col+pos, row);
  140.      }
  141.      else if (c==8 && pos>0) {
  142.        while (pos-- > 0 && mask[pos] == '-')
  143.          ;
  144.        gotoxy(col+pos, row);
  145.        if (mask[0])
  146.          cputs(mark);          /* if using input mask, restore when bk */
  147.        else
  148.          putch(' ');
  149.        gotoxy(col+pos, row);
  150.      }
  151.      else if (c == CR || c == ESC || c == END)  break;
  152.    }
  153.    hide_cursor();
  154.    for (i=pos; i<maxpos; i++)
  155.      cputs(" ");
  156.    if (c == CR || c == END)
  157.      dest[pos] = '\0';
  158.    else
  159.      dest[0] = '\0';
  160.    if (c == END)
  161.      return -strlen(dest);
  162.    return strlen(dest);
  163. }
  164.  
  165. /* ------------------------- hide the cursor -------------------------- */
  166. void
  167. hide_cursor()
  168. {
  169.   _AH = 1;
  170.   _CH = 0x20;
  171.   geninterrupt(0x10);
  172. }
  173.  
  174. /* ------------------------- show the cursor ------------------------------ */
  175. void
  176. showcursor()
  177. {
  178.   _AX = 0x0100;
  179.   _BX = 0;
  180.   _CX = 0x0c0d;
  181.   geninterrupt(0x10);
  182. }
  183.  
  184.