home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / lib / objcol2 / src / vircon.c < prev   
Encoding:
C/C++ Source or Header  |  1995-08-12  |  5.1 KB  |  320 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <string.h>
  4. #include <egb.h>
  5. #include "symbol.h"
  6.  
  7. #define    ON        1
  8. #define    OFF        0
  9. #define    S_INT    1
  10. #define    US_INT    2
  11. #define    L_INT    3
  12. #define    UL_INT    4
  13. #define    CHAR    5
  14. #define    POINTER    6
  15. #define    ERR        1
  16. #define    NOERR    0
  17.  
  18. static char    *egbwork;
  19. static int    basex,basey;
  20. static int    fcolor,bcolor;
  21. static int    line,wide;
  22. static int    cx,cy;
  23. static char    *his[30];
  24. static int    hisofs;
  25. static char hisdata[30][81];
  26. static va_list    ap;
  27.  
  28. typedef union
  29. {
  30.     short int    s;
  31.     long int    l;
  32.     unsigned short int    us;
  33.     unsigned long int    ul;
  34.     char    c;
  35.     void    *p;
  36. } VALUE;
  37.  
  38. static void    waitVsync()
  39. {
  40.     char    para[16];
  41.     DWORD( para + 0 ) = 0;
  42.     EGB_palette( egbwork, 1, para );
  43.     return;
  44. }
  45.  
  46. static void    boxpaint()
  47. {
  48.     EGB_writeMode( egbwork, 0 );
  49.     EGB_foreColor( egbwork, bcolor );
  50.     EGB_paintColor( egbwork, bcolor );
  51.     EGB_paintMode( egbwork, 0x22 );
  52.     waitVsync();
  53.     EGB_box( egbwork, basex, basey, basex+wide*8, basey+line*16 );
  54.     return;
  55. }
  56.  
  57. void    VC_cls()
  58. {
  59.     boxpaint();
  60.     cx = cy = 0;
  61.     return;
  62. }
  63.  
  64. void    VC_setWriteColor( int c )
  65. {
  66.     fcolor = c;
  67.     return;
  68. }
  69.  
  70. void    VC_setBackColor( int c )
  71. {
  72.     bcolor = c;
  73.     return;
  74. }
  75.  
  76. void    VC_locate( int x, int y )
  77. {
  78.     cx = x;
  79.     cy = y;
  80.     return;
  81. }
  82.  
  83. void    VC_init( char *work, int bx, int by, int w, int l, int fc, int bc )
  84. {
  85.     int        i;
  86.     egbwork = work;
  87.     basex = bx;
  88.     basey = by;
  89.     wide = w;
  90.     line = l;
  91.     for( i=0; i<30; i++ )
  92.         his[i] = hisdata[i];
  93.     hisofs = 0;
  94.     bcolor = bc;
  95.     fcolor = fc;
  96.     cx = cy = 0;
  97.     boxpaint();
  98.     for( i=0; i<line; i++ )
  99.         *(hisdata[i]) = 0;
  100.  
  101.     return;
  102. }
  103.  
  104. static void    nextline()
  105. {
  106.     char    para[128];
  107.     int        i,j;
  108.  
  109.     ++ cy;
  110.     cx = 0;
  111.  
  112.     *(his[hisofs]) = 0;
  113.     his[hisofs] = hisdata[hisofs];
  114.     hisofs = (++ hisofs) % line;
  115.     *(his[hisofs]) = 0;
  116.     if( cy >= line )
  117.     {
  118.         -- cy;
  119.         boxpaint();
  120.         EGB_foreColor( egbwork, fcolor );
  121.         EGB_paintMode( egbwork, 0x02 );
  122.         j = hisofs;
  123.         for( i=0; i<line; i++ )
  124.         {
  125.             j = (++ j) % line;
  126.             symbol( egbwork, basex, basey + i * 16 + 15, his[j] );
  127.         }
  128.     }
  129.     return;
  130. }
  131.  
  132. static void    VC_puts( char *p, int l )
  133. {
  134.     char    para[128];
  135.     int        i,j;
  136.  
  137.     if( l == 0 )
  138.         return;
  139.     i = strlen( p );
  140.     if( l == -1 || i<l )
  141.         l = i;
  142.     while( cx+l > wide )
  143.     {
  144.         i = wide - cx;
  145.         VC_puts( p, i );
  146.         l -= i;
  147.         p += i;
  148.         nextline();
  149.     }
  150.     for( i=0; i<l; i++ )
  151.     {
  152.         if( *(p+i) == '\n' )
  153.         {
  154.             VC_puts( p, i );
  155.             l -= i+1;
  156.             p += i+1;
  157.             i = -1;
  158.             nextline();
  159.         }
  160.     }
  161.     EGB_foreColor( egbwork, fcolor );
  162.     EGB_paintMode( egbwork, 0x02 );
  163.     strncpy( para+6, p, l );
  164.     *(para+6+l) = 0;
  165.     strncpy( his[hisofs], p, l );
  166.     his[hisofs] += l;
  167.     symbol( egbwork, basex + cx * 8, basey + cy * 16 + 15, para+6 );
  168.     cx += l;
  169.     return;
  170. }
  171.  
  172. static int    valueToStrings( char *c )
  173. {
  174.     int        type,i,j;
  175.     VALUE    val,d;
  176.  
  177.     type = va_arg( ap, int );
  178.     switch( type )
  179.     {
  180.         case S_INT:    
  181.             val.s = va_arg( ap, short int );
  182.             if( val.s < 0 )
  183.             {
  184.                 *c++ = '-';
  185.                 val.s = -val.s;
  186.             }
  187.             d.s = 10000;
  188.             while( d.s )
  189.             {
  190.                 if( val.s >= d.s || d.s == 1 )
  191.                 {
  192.                     i = ( val.s % ( d.s*10 ) ) / d.s;
  193.                     *c++ = '0' + i;
  194.                 }
  195.                 d.s /= 10;
  196.             }
  197.             break;
  198.         case US_INT:
  199.             val.us = va_arg( ap, unsigned short int );
  200.             d.us = 10000;
  201.             while( d.us )
  202.             {
  203.                 if( val.us >= d.s || d.us == 1 )
  204.                 {
  205.                     i = ( val.us % ( d.us*10 ) ) / d.us;
  206.                     *c++ = '0' + i;
  207.                 }
  208.                 d.us /= 10;
  209.             }
  210.             break;
  211.         case L_INT:
  212.             val.l = va_arg( ap, long int );
  213.             if( val.l < 0 )
  214.             {
  215.                 *c++ = '-';
  216.                 val.l = -val.l;
  217.             }
  218.             d.l = 1000000000;
  219.             while( d.l )
  220.             {
  221.                 if( val.l >= d.l || d.l == 1 )
  222.                 {
  223.                     i = ( val.l % ( d.l*10 ) ) / d.l;
  224.                     *c++ = '0' + i;
  225.                 }
  226.                 d.l /= 10;
  227.             }
  228.             break;
  229.         case UL_INT:
  230.             val.ul = va_arg( ap, unsigned long int );
  231.             d.ul = 1000000000;
  232.             while( d.ul )
  233.             {
  234.                 if( val.ul >= d.ul || d.ul == 1 )
  235.                 {
  236.                     i = ( val.ul % ( d.ul*10 ) ) / d.ul;
  237.                     *c++ = '0' + i;
  238.                 }
  239.                 d.ul /= 10;
  240.             }
  241.             break;
  242.         case CHAR:
  243.             val.c = va_arg( ap, char );
  244.             d.c = 100;
  245.             while( d.c )
  246.             {
  247.                 if( val.c >= d.c || d.c == 1 )
  248.                 {
  249.                     i = ( val.c % ( d.c*10 ) ) / d.c;
  250.                     *c++ = '0' + i;
  251.                 }
  252.                 d.c /= 10;
  253.             }
  254.             break;
  255.         default:
  256.             return ERR;
  257.     }
  258.     *c = 0;
  259.     return NOERR;
  260. }
  261.  
  262. int        VC_printf( char *fmt, ... )
  263. {
  264.     int        type;
  265.     int        comflag = OFF;
  266.     VALUE    val;
  267.     char    c[16];
  268.     int        bufl = 0;
  269.  
  270.     va_start( ap, fmt );
  271. /*    {FILE *fp;fp=fopen("out","a");fprintf(fp,"wide=%d\n",wide);fclose(fp);}*/
  272.  
  273.     while( *fmt )
  274.     {
  275.         if( *fmt == '%' )
  276.         {
  277.             ++fmt;
  278.             switch( *fmt )
  279.             {
  280.                 case 'd':
  281.                     VC_puts( fmt-bufl-1, bufl );
  282.                     bufl = 0;
  283.                     if( valueToStrings( c ) )
  284.                         return ERR;
  285.                     VC_puts( c, -1 );
  286.                     comflag = ON;
  287.                     break;
  288.                 case 's':
  289.                     VC_puts( fmt-bufl-1, bufl );
  290.                     bufl = 0;
  291.                     type = va_arg( ap, int );
  292.                     if( type != POINTER )
  293.                         return ERR;
  294.                     val.p = va_arg( ap, void * );
  295.                     VC_puts( val.p, -1 );
  296.                     comflag = ON;
  297.                     break;
  298.             }
  299.             if( comflag == ON )
  300.             {
  301.                 comflag = OFF;
  302.                 ++fmt;
  303.                 continue;
  304.             }
  305.         }
  306.         if( *fmt == '\n' )
  307.         {
  308.             VC_puts( fmt-bufl, bufl );
  309.             bufl = -1;
  310.             nextline();
  311.         }
  312.         ++fmt;
  313.         ++bufl;
  314.     }
  315.     VC_puts( fmt-bufl, bufl );
  316.     va_end( ap );
  317.     return;
  318. }
  319.  
  320.