home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / courses / prntscan.c < prev    next >
C/C++ Source or Header  |  1992-07-06  |  7KB  |  216 lines

  1. /****************************************************************/
  2. /* Printw() and scanw() routines of the PCcurses package    */
  3. /*                                */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version    */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,    */
  8. /* and therefore consider myself free to make it public domain.    */
  9. /*        Bjorn Larsson (...mcvax!enea!infovax!bl)    */
  10. /****************************************************************/
  11. /*             IMPLEMENTATION NOTE            */
  12. /* These routines make a local copy of their parameter stack,    */
  13. /* assuming at most 5 'double' arguments were passed (== 40    */
  14. /* bytes == 20 int's == 10 long's == 10-20 pointers {depending    */
  15. /* on memory model}, etc). This means the invocation of the    */
  16. /* routines themselves requires at least 80 bytes of stack just    */
  17. /* for the parameters, and the vsprintf() and vscanf() functions    */
  18. /* will require more. Therefore, this module should be compiled    */
  19. /* with stack checking on to avoid stack overflow errors.    */
  20. /****************************************************************/
  21. /* 1.0:    Release:                    870515    */
  22. /* 1.2:    Rcsid[] string for maintenance:            881002    */
  23. /* 1.3:    MSC -W3, Turbo'C' -w -w-pro checkes:        881005    */
  24. /****************************************************************/
  25.  
  26. #include <curses.h>
  27. #include <curspriv.h>
  28. #include <stdarg.h>
  29.  
  30. char _curses_prntscan_rcsid[] = "@(#)prntscan.c v1.3 - 881005";
  31.  
  32. static    int    pblen();        /* gets length of buffer */
  33. static    char    printscanbuf[513];    /* buffer used during I/O */
  34.  
  35. /****************************************************************/
  36. /* Wprintw(win,fmt,args) does a printf() in window 'win'.    */
  37. /****************************************************************/
  38.  
  39. int    wprintw(WINDOW *win, char *fmt, ...)
  40.   {
  41.   va_list args;
  42.  
  43.   va_start(args, fmt);
  44.   vsprintf(printscanbuf,fmt,args);
  45.   va_end(args);
  46.  
  47.   if (waddstr(win,printscanbuf) == ERR)
  48.     return(ERR);
  49.   return(pblen());
  50.   } /* wprintw */
  51.  
  52. /****************************************************************/
  53. /* Printw(fmt,args) does a printf() in stdscr.            */
  54. /****************************************************************/
  55.  
  56. int    printw(char *fmt, ...)
  57.   {
  58.   va_list args;
  59.  
  60.   va_start(args, fmt);
  61.   vsprintf(printscanbuf,fmt,args);
  62.   va_end(args);
  63.  
  64.   if(waddstr(stdscr,printscanbuf) == ERR)
  65.     return(ERR);
  66.   return(pblen());
  67.   } /* printw */
  68.  
  69. /****************************************************************/
  70. /* Mvprintw(fmt,args) moves the stdscr cursor to a new posi-    */
  71. /* tion, then does a printf() in stdscr.            */
  72. /****************************************************************/
  73.  
  74. int    mvprintw(int y, int x, char *fmt, ...)
  75.   {
  76.   va_list args;
  77.  
  78.   if (wmove(stdscr,y,x) == ERR)
  79.     return(ERR);
  80.  
  81.   va_start(args, fmt);
  82.   vsprintf(printscanbuf,fmt,args);
  83.   va_end(args);
  84.  
  85.   if(waddstr(stdscr,printscanbuf) == ERR)
  86.     return(ERR);
  87.   return(pblen());
  88.   } /* mvprintw */
  89.  
  90. /****************************************************************/
  91. /* Mvwprintw(win,fmt,args) moves the window 'win's cursor to    */
  92. /* a new position, then does a printf() in window 'win'.    */
  93. /****************************************************************/
  94.  
  95. int    mvwprintw(WINDOW *win, int y, int x, char *fmt, ...)
  96.   {
  97.   va_list args;
  98.  
  99.   if (wmove(win,y,x) == ERR)
  100.     return(ERR);
  101.  
  102.   va_start(args, fmt);
  103.   vsprintf(printscanbuf,fmt,args);
  104.   va_end(args);
  105.  
  106.   if(waddstr(win,printscanbuf) == ERR)
  107.     return(ERR);
  108.   return(pblen());
  109.   } /* mvwprintw */
  110.  
  111. /****************************************************************/
  112. /* Wscanw(win,fmt,args) gets a string via window 'win', then    */
  113. /* scans the string using format 'fmt' to extract the values    */
  114. /* and put them in the variables pointed to the arguments.    */
  115. /****************************************************************/
  116.  
  117. int wscanw(WINDOW *win, char *fmt, ...)
  118.   {
  119.   int count;
  120.   va_list args;
  121.  
  122.   wrefresh(win);                /* set cursor */
  123.   if (wgetstr(win,printscanbuf) == ERR)        /* get string */
  124.     return(ERR);
  125.  
  126.   va_start(args, fmt);
  127.   count = vscanf(printscanbuf,fmt,args);
  128.   va_end(args);
  129.  
  130.   return(count);
  131.   } /* wscanw */
  132.  
  133. /****************************************************************/
  134. /* Scanw(fmt,args) gets a string via stdscr, then scans the    */
  135. /* string using format 'fmt' to extract the values and put them    */
  136. /* in the variables pointed to the arguments.            */
  137. /****************************************************************/
  138.  
  139. int scanw(char *fmt, ...)
  140.   {
  141.   int count;
  142.   va_list args;
  143.  
  144.   wrefresh(stdscr);                /* set cursor */
  145.   if (wgetstr(stdscr,printscanbuf) == ERR)    /* get string */
  146.     return(ERR);
  147.  
  148.   va_start(args, fmt);
  149.   count = vscanf(printscanbuf,fmt,args);
  150.   va_end(args);
  151.  
  152.   return(count);
  153.   } /* scanw */
  154.  
  155. /****************************************************************/
  156. /* Mvscanw(y,x,fmt,args) moves stdscr's cursor to a new posi-    */
  157. /* tion, then gets a string via stdscr and scans the string    */
  158. /* using format 'fmt' to extract the values and put them in the    */
  159. /* variables pointed to the arguments.                */
  160. /****************************************************************/
  161.  
  162. int mvscanw(int y, int x, char *fmt, ...)
  163.   {
  164.   int count;
  165.   va_list args;
  166.  
  167.   if (wmove(stdscr,y,x) == ERR)
  168.     return(ERR);
  169.   wrefresh(stdscr);                /* set cursor */
  170.   if (wgetstr(stdscr,printscanbuf) == ERR)    /* get string */
  171.     return(ERR);
  172.  
  173.   va_start(args, fmt);
  174.   count = vscanf(printscanbuf,fmt,args);
  175.   va_end(args);
  176.  
  177.   return(count);
  178.   } /* mvscanw */
  179.  
  180. /****************************************************************/
  181. /* Mvwscanw(win,y,x,fmt,args) moves window 'win's cursor to a    */
  182. /* new position, then gets a string via 'win' and scans the    */
  183. /* string using format 'fmt' to extract the values and put them    */
  184. /* in the variables pointed to the arguments.            */
  185. /****************************************************************/
  186.  
  187. int mvwscanw(WINDOW *win, int y, int x, char *fmt, ...)
  188.   {
  189.   int count;
  190.   va_list args;
  191.  
  192.   if (wmove(win,y,x) == ERR)
  193.     return(ERR);
  194.   wrefresh(win);                /* set cursor */
  195.   if (wgetstr(win,printscanbuf) == ERR)        /* get string */
  196.     return(ERR);
  197.  
  198.   va_start(args, fmt);
  199.   count = vscanf(printscanbuf,fmt,args);
  200.   va_end(args);
  201.  
  202.   return(count);
  203.   } /* mvwscanw */
  204.  
  205. /****************************************************************/
  206. /* Pblen() returns the length of the string in printscanbuf.    */
  207. /****************************************************************/
  208.  
  209. static    int pblen()
  210.   {
  211.   char *p = printscanbuf;
  212.   
  213.   while(*p++);
  214.   return(p-printscanbuf-1);
  215.   } /* plben */
  216.