home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  4.5 KB  |  227 lines

  1. /* 
  2. ** <<ACE>> linked library module: DOS and Intuition window PRINT code. 
  3. ** Copyright (C) 1998 David Benn
  4. ** 
  5. ** This program is free software; you can redistribute it and/or
  6. ** modify it under the terms of the GNU General Public License
  7. ** as published by the Free Software Foundation; either version 2
  8. ** of the License, or (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. **
  19. ** Author: David J Benn
  20. **   Date: 9th,10th,31st October 1993,
  21. **       1st November 1993,
  22. **       24th December 1993,
  23. **       17th September 1994
  24. */
  25.  
  26. #include <intuition/intuition.h>
  27. #include <exec/types.h>
  28.  
  29. #define LF    0L
  30. #define TAB    1L
  31. #define SPC    2L
  32. #define QUN    3L
  33.  
  34. #define FontX    (ULONG)(RPort->Font->tf_XSize)
  35. #define FontY    (ULONG)(RPort->Font->tf_YSize)
  36.  
  37. #define currX    (ULONG)(RPort->cp_x)
  38. #define currY    (ULONG)(RPort->cp_y)
  39.  
  40. /* external variables */
  41. extern BYTE     IntuiMode;
  42. extern ULONG     stdout;
  43. extern struct     RastPort *RPort;
  44. extern struct   Window     *Wdw;
  45.  
  46. /* external functions */
  47. extern ULONG     stringlength();
  48. extern void     beep();
  49. extern void     scroll_screen();
  50.     
  51. /* functions */
  52. void Ucodeprint(code)
  53. long code;
  54. {
  55. char qunmark = '?';
  56. char space = ' ';
  57.  
  58. /* print LF, TAB, SPACE (etc) to DOS or Intuition window */
  59.  
  60.     if (IntuiMode == 0)
  61.     {
  62.         /* DOS window */
  63.         switch(code)
  64.         {
  65.          case LF    : putchar('\n'); break;
  66.          case TAB    : putchar(9); break;
  67.          case SPC    : putchar(' '); break;
  68.          case QUN    : putchar('?'); break;
  69.         }        
  70.     }
  71.     else
  72.     {
  73.         /* Intuition window */    
  74.         switch(code)
  75.         {
  76.          case LF     : printsLF();     break;
  77.          case TAB     : printsTAB();     break;
  78.          case SPC    : Text(RPort,&space,1); break;
  79.          case QUN    : Text(RPort,&qunmark,1); break;
  80.         }
  81.     }        
  82. }
  83.     
  84. BOOL special_character(ch)
  85. char ch;
  86. {
  87. /* return true or false depending upon whether "ch" is
  88.    in the range 0..31 and needs to be handled specially 
  89.    by ACE ala AmigaBASIC.
  90. */
  91.  
  92.     if (ch==7 || ch==8 || ch==9 || ch==10 || ch==12 || ch==13)
  93.         return(TRUE);
  94.     else
  95.         return(FALSE);
  96. }
  97.  
  98. void act_on_special_character(ch)
  99. char ch;
  100. {
  101. /* duplicate the effect that "ch" would have in
  102.    an AmigaBASIC window. 
  103. */
  104.  
  105.     switch(ch)
  106.     {
  107.         /* BELL */
  108.         case 7 :     beep(); 
  109.                 break;
  110.  
  111.         /* BS */
  112.         case 8 :    if (currX >= FontX)
  113.                 {
  114.                   Move(RPort,currX-FontX,currY);
  115.                   Text(RPort," ",1);
  116.                   Move(RPort,currX-FontX,currY);
  117.                 }
  118.                 break;
  119.  
  120.         /* TAB */
  121.         case 9 :     Text(RPort,"    ",4);
  122.                 break;
  123.  
  124.         /* LF */
  125.         case 10 :    Move(RPort,0L,currY+FontY);
  126.                 scroll_screen();
  127.                 break;
  128.  
  129.         /* CLS */
  130.         case 12 :     Move(RPort,0,0);
  131.                 ClearScreen(RPort);
  132.                 Move(RPort,currX,currY+FontY-2);
  133.                 break;
  134.  
  135.         /* CR */
  136.         case 13 :    Move(RPort,0L,currY);
  137.                 break;
  138.     }
  139. }
  140.  
  141. void Ustringprint(str)
  142. char *str;
  143. {
  144. /* print string to DOS or Intuition window */
  145. char  *first,*last;
  146. BOOL  special;
  147. ULONG length;
  148.  
  149.     if (IntuiMode == 0)
  150.     {
  151.         /* DOS window */
  152.         Write(stdout,str,stringlength(str));        
  153.     }
  154.     else
  155.     {
  156.         /* Intuition window */
  157.         first = str;
  158.         last = str;
  159.  
  160.         do
  161.         {
  162.         
  163.             /* look for special character or end of string */
  164.             while (!(special = special_character(*last)) &&
  165.                                *last != '\0') ++last;
  166.             
  167.             /* print partial string, then special character */ 
  168.             if (special)
  169.             {
  170.                 length = last-first;
  171.                 if (length > 0) Text(RPort,first,length);
  172.                 act_on_special_character(*last);
  173.                 ++last;
  174.                 first = last;    
  175.             }
  176.             else
  177.             {
  178.                 /* print whole/rest of string! */
  179.                 length = last-first;
  180.                 if (length > 0) Text(RPort,first,length);
  181.             }
  182.         }
  183.         while (*last != '\0');
  184.     }        
  185. }
  186.  
  187. void Ushortprint(num)
  188. long num;
  189. {
  190. static char buf[40];
  191. /* print short integer to DOS or Intuition window */
  192.  
  193.     /* convert to string */
  194.     if (num >= 0) 
  195.        sprintf(buf," %ld",num);  /* leading space if num >= 0 */
  196.     else
  197.        sprintf(buf,"%ld",num);
  198.     
  199.     /* display number as string */
  200.     Ustringprint(buf);
  201. }
  202.  
  203. void Ulongprint(num)
  204. long num;
  205. {
  206. static char buf[40];
  207. /* print long integer to DOS or Intuition window */
  208.  
  209.     /* convert to string */
  210.     if (num >= 0) 
  211.        sprintf(buf," %ld",num);    /* leading space if num >= 0 */
  212.     else
  213.        sprintf(buf,"%ld",num);
  214.     
  215.     /* display number as string */
  216.     Ustringprint(buf);
  217. }
  218.  
  219. void Usingleprint(num)
  220. float num;
  221. {
  222. /* print single-precision float to DOS or Intuition window */
  223.  
  224.     /* display number as string */    
  225.     Ustringprint(strsingle(num));
  226. }
  227.