home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / pccurs14.zoo / pccurses.4 / prntscan.c < prev    next >
C/C++ Source or Header  |  1990-01-20  |  9KB  |  273 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 (bl@infovox.se)    */
  10. /****************************************************************/
  11. /*             IMPLEMENTATION NOTE 1            */
  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 invokation of the    */
  16. /* routines themself require at least 80 bytes of stack just    */
  17. /* for the parameters, and the sprintf() and sscanf() functions    */
  18. /* will require more. Therefore, this module should be compiled    */
  19. /* with stack checking on to avoid stack overflow errors.    */
  20. /****************************************************************/
  21. /*             IMPLEMENTATION NOTE 2            */
  22. /* This curses version is also used in a special environment    */
  23. /* with a 68000 CPU. The 68K compiler used has a bug in the    */
  24. /* standard library, which means that sprintf will not print    */
  25. /* newlines right. Therefore a workaround has been included in    */
  26. /* this file, conditionalized by '#if BUG68K'. This does not    */
  27. /* affect the PC version in any way, except the source is a    */
  28. /* little more obscure...                    */
  29. /****************************************************************/
  30. /* 1.4:  Use of short wherever possible. Portability        */
  31. /*     improvements. 68K bug workaround:        900114    */
  32. /* 1.3:     MSC -W3, Turbo'C' -w -w-pro checkes:        881005    */
  33. /* 1.2:     Rcsid[] string for maintenance:        881002    */
  34. /* 1.0:     Release:                    870515    */
  35. /****************************************************************/
  36.  
  37. #include <curses.h>
  38. #include <curspriv.h>
  39.  
  40. char _curses_prntscan_rcsid[] = "@(#)prntscan.c   v.1.4  - 900114";
  41.  
  42. static    int    pblen();        /* gets length of buffer */
  43.  
  44. #if BUG68K
  45. static    void    setnl();        /* conv nl -> CTRL-\ */
  46. static    void    getnl();        /* conv CTRL-\ -> nl */
  47. #endif
  48.  
  49. static    char    printscanbuf[513];    /* buffer used during I/O */
  50.  
  51. /****************************************************************/
  52. /* Wprintw(win,fmt,args) does a printf() in window 'win'.    */
  53. /****************************************************************/
  54.  
  55. int    wprintw(win,fmt,A1,A2,A3,A4,A5)
  56.   WINDOW    *win;
  57.   char        *fmt;
  58.   double     A1,A2,A3,A4,A5;
  59.   {
  60. #if BUG68K
  61.   setnl(fmt);
  62.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  63.   getnl(fmt);
  64.   getnl(printscanbuf);
  65. #else
  66.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  67. #endif
  68.   if (waddstr(win,printscanbuf) == ERR)
  69.     return(ERR);
  70.   return(pblen());
  71.   } /* wprintw */
  72.  
  73. /****************************************************************/
  74. /* Printw(fmt,args) does a printf() in stdscr.            */
  75. /****************************************************************/
  76.  
  77. int    printw(fmt,A1,A2,A3,A4,A5)
  78.   char        *fmt;
  79.   double     A1,A2,A3,A4,A5;
  80.   {
  81. #if BUG68K
  82.   setnl(fmt);
  83.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  84.   getnl(fmt);
  85.   getnl(printscanbuf);
  86. #else
  87.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  88. #endif
  89.   if(waddstr(stdscr,printscanbuf) == ERR)
  90.     return(ERR);
  91.   return(pblen());
  92.   } /* printw */
  93.  
  94. /****************************************************************/
  95. /* Mvprintw(fmt,args) moves the stdscr cursor to a new posi-    */
  96. /* tion, then does a printf() in stdscr.            */
  97. /****************************************************************/
  98.  
  99. int    mvprintw(y,x,fmt,A1,A2,A3,A4,A5)
  100.   int         y;
  101.   int         x;
  102.   char        *fmt;
  103.   double     A1,A2,A3,A4,A5;
  104.   {
  105.   if (wmove(stdscr,y,x) == ERR)
  106.     return(ERR);
  107. #if BUG68K
  108.   setnl(fmt);
  109.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  110.   getnl(fmt);
  111.   getnl(printscanbuf);
  112. #else
  113.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  114. #endif
  115.   if(waddstr(stdscr,printscanbuf) == ERR)
  116.     return(ERR);
  117.   return(pblen());
  118.   } /* mvprintw */
  119.  
  120. /****************************************************************/
  121. /* Mvwprintw(win,fmt,args) moves the window 'win's cursor to    */
  122. /* a new position, then does a printf() in window 'win'.    */
  123. /****************************************************************/
  124.  
  125. int    mvwprintw(win,y,x,fmt,A1,A2,A3,A4,A5)
  126.   WINDOW    *win;
  127.   int         y;
  128.   int         x;
  129.   char        *fmt;
  130.   double     A1,A2,A3,A4,A5;
  131.   {
  132.   if (wmove(win,y,x) == ERR)
  133.     return(ERR);
  134. #if BUG68K
  135.   setnl(fmt);
  136.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  137.   getnl(fmt);
  138.   getnl(printscanbuf);
  139. #else
  140.   sprintf(printscanbuf,fmt,A1,A2,A3,A4,A5);
  141. #endif
  142.   if(waddstr(win,printscanbuf) == ERR)
  143.     return(ERR);
  144.   return(pblen());
  145.   } /* mvwprintw */
  146.  
  147. /****************************************************************/
  148. /* Wscanw(win,fmt,args) gets a string via window 'win', then    */
  149. /* scans the string using format 'fmt' to extract the values    */
  150. /* and put them in the variables pointed to the arguments.    */
  151. /****************************************************************/
  152.  
  153. int wscanw(win,fmt,A1,A2,A3,A4,A5)
  154.   WINDOW    *win;
  155.   char        *fmt;
  156.   double     A1,A2,A3,A4,A5;        /* really pointers */
  157.   {
  158.   wrefresh(win);                /* set cursor */
  159.   if (wgetstr(win,printscanbuf) == ERR)        /* get string */
  160.     return(ERR);
  161.   return(sscanf(printscanbuf,fmt,A1,A2,A3,A4,A5));
  162.   } /* wscanw */
  163.  
  164. /****************************************************************/
  165. /* Scanw(fmt,args) gets a string via stdscr, then scans the    */
  166. /* string using format 'fmt' to extract the values and put them    */
  167. /* in the variables pointed to the arguments.            */
  168. /****************************************************************/
  169.  
  170. int scanw(fmt,A1,A2,A3,A4,A5)
  171.   char        *fmt;
  172.   double     A1,A2,A3,A4,A5;        /* really pointers */
  173.   {
  174.   wrefresh(stdscr);                /* set cursor */
  175.   if (wgetstr(stdscr,printscanbuf) == ERR)    /* get string */
  176.     return(ERR);
  177.   return(sscanf(printscanbuf,fmt,A1,A2,A3,A4,A5));
  178.   } /* scanw */
  179.  
  180. /****************************************************************/
  181. /* Mvscanw(y,x,fmt,args) moves stdscr's cursor to a new posi-    */
  182. /* tion, then gets a string via stdscr and scans the string    */
  183. /* using format 'fmt' to extract the values and put them in the    */
  184. /* variables pointed to the arguments.                */
  185. /****************************************************************/
  186.  
  187. int mvscanw(y,x,fmt,A1,A2,A3,A4,A5)
  188.   int         y;
  189.   int         x;
  190.   char        *fmt;
  191.   double     A1,A2,A3,A4,A5;        /* really pointers */
  192.   {
  193.   if (wmove(stdscr,y,x) == ERR)
  194.     return(ERR);
  195.   wrefresh(stdscr);                /* set cursor */
  196.   if (wgetstr(stdscr,printscanbuf) == ERR)    /* get string */
  197.     return(ERR);
  198.   return(sscanf(printscanbuf,fmt,A1,A2,A3,A4,A5));
  199.   } /* mvscanw */
  200.  
  201. /****************************************************************/
  202. /* Mvwscanw(win,y,x,fmt,args) moves window 'win's cursor to a    */
  203. /* new position, then gets a string via 'win' and scans the    */
  204. /* string using format 'fmt' to extract the values and put them    */
  205. /* in the variables pointed to the arguments.            */
  206. /****************************************************************/
  207.  
  208. int mvwscanw(win,y,x,fmt,A1,A2,A3,A4,A5)
  209.   WINDOW    *win;
  210.   int         y;
  211.   int         x;
  212.   char        *fmt;
  213.   double     A1,A2,A3,A4,A5;        /* really pointers */
  214.   {
  215.   if (wmove(win,y,x) == ERR)
  216.     return(ERR);
  217.   wrefresh(win);                /* set cursor */
  218.   if (wgetstr(win,printscanbuf) == ERR)        /* get string */
  219.     return(ERR);
  220.   return(sscanf(printscanbuf,fmt,A1,A2,A3,A4,A5));
  221.   } /* mvwscanw */
  222.  
  223. /****************************************************************/
  224. /* Pblen() returns the length of the string in printscanbuf.    */
  225. /****************************************************************/
  226.  
  227. static    int pblen()
  228.   {
  229.   char *p = printscanbuf;
  230.   
  231.   while(*p++);
  232.   return((int) (p - printscanbuf - 1));
  233.   } /* plben */
  234.  
  235. #if BUG68K
  236. /****************************************************************/   
  237. /* This function circumvents a problem in the 68000 C library:    */
  238. /* If the standard sprintf is used, it will ignore any newlines    */
  239. /* in the format string. Therefore this routine changes the    */
  240. /* newlines to CTRL-\ characters, to be restored later by the    */
  241. /* getnl() function.                        */
  242. /****************************************************************/
  243.  
  244. static    void setnl(fmt)
  245.   char    *fmt;
  246.   {
  247.   while (*fmt)
  248.     {
  249.     if (*fmt == '\n')
  250.       *fmt = 0x1c;
  251.     fmt++;
  252.     } /* while */
  253.   } /* setnl */
  254.  
  255. /****************************************************************/
  256. /* This function circumvents a problem in the 68000 C library:    */
  257. /* If the standard sprintf is used, it will ignore any newlines    */
  258. /* in the format string. Therefore this routine changes CTRL-\    */
  259. /* characters (already set by setnl()) back to newlines.    */
  260. /****************************************************************/
  261.  
  262. static    void getnl(fmt)
  263.   char    *fmt;
  264.   {
  265.   while (*fmt)
  266.     {
  267.     if (*fmt == 0x1c)
  268.       *fmt = '\n';
  269.     fmt++;
  270.     } /* while */
  271.   } /* getnl */
  272. #endif
  273.