home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / misc / amigem.lha / amigem / exec / rawdofmt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  4.1 KB  |  172 lines

  1. #include <exec/execbase.h>
  2. #include <dos/dos.h>
  3. #include <amigem/machine.h>
  4.  
  5. #include <amigem/fd_lib.h>
  6. #define LIBBASE struct ExecBase *SysBase
  7.  
  8. #define CBUFSIZE (sizeof(ULONG)*BITSPERBYTE*301/1000)
  9.  
  10. /* Two functions from machine.c */
  11. struct dm10
  12. {
  13.   ULONG q;
  14.   ULONG r;
  15. };
  16.  
  17. APTR stuffChar(void (*PutChProc)(),ULONG character,APTR PutChData);
  18. struct dm10 DivMod10(ULONG value);
  19.  
  20. FD4(87,APTR,RawDoFmt,STRPTR FormatString,A0,APTR DataStream,A1,void (*PutChProc)(),A2,APTR PutChData,A3)
  21. {
  22.   while(*FormatString)
  23.   {
  24.     if(*FormatString=='%')
  25.     {
  26.       int left=0,fill=' ',larg=0,minus=0;
  27.       ULONG i,width=0,minwidth=0,maxwidth=~0;
  28.       UBYTE cbuf[CBUFSIZE];
  29.       UBYTE *buf;
  30.       
  31.       FormatString++;
  32.  
  33.       if(*FormatString=='-')
  34.         left=*FormatString++;
  35.       if(*FormatString=='0')
  36.         fill=*FormatString++;
  37.  
  38.       while(*FormatString>='0'&&*FormatString<='9')
  39.         minwidth=minwidth*10+(*FormatString++-'0');
  40.  
  41.       if(*FormatString=='.')
  42.         FormatString++;
  43.  
  44.       if(*FormatString>='0'&&*FormatString<='9')
  45.       {
  46.         maxwidth=0;
  47.         do
  48.           maxwidth=maxwidth*10+(*FormatString++-'0');
  49.         while(*FormatString>='0'&&*FormatString<='9');
  50.       }
  51.  
  52.       if(*FormatString=='l')
  53.         larg=*FormatString++;
  54.       
  55.       switch(*FormatString++)
  56.       {
  57.         case 'b':
  58.           {
  59.             UBYTE *buffer;
  60.             buf=buffer=(UBYTE *)BSTR2C(*(*(BPTR **)&DataStream)++);
  61.             while(*buffer++)
  62.               width++;
  63.             if(width>maxwidth)
  64.               width=maxwidth;
  65.       }
  66.           break;
  67.         case 'd':
  68.           {
  69.             LONG n;
  70.             if(larg)
  71.               n=*(*(ULONG **)&DataStream)++;
  72.             else
  73.               n=*(*(UWORD **)&DataStream)++;
  74.             if(n<0)
  75.             {
  76.               minus=1;
  77.               n=-n;
  78.               width++;
  79.         }
  80.             buf=&cbuf[CBUFSIZE];
  81.             do
  82.             {
  83.               struct dm10 r;
  84.               r=DivMod10(n);
  85.               *--buf=r.r+'0';
  86.               n=r.q;
  87.               width++;
  88.         }while(n);
  89.       }
  90.           break;
  91.         case 'u':
  92.           {
  93.             ULONG n;
  94.             if(larg)
  95.               n=*(*(ULONG **)&DataStream)++;
  96.             else
  97.               n=*(*(UWORD **)&DataStream)++;
  98.             buf=&cbuf[CBUFSIZE];
  99.             do
  100.             {
  101.               struct dm10 r;
  102.               r=DivMod10(n);
  103.               *--buf=r.r+'0';
  104.               n=r.q;
  105.               width++;
  106.         }while(n);
  107.           }
  108.           break;
  109.         case 'x':
  110.           {
  111.             ULONG n;
  112.             if(larg)
  113.               n=*(*(ULONG **)&DataStream)++;
  114.             else
  115.               n=*(*(UWORD **)&DataStream)++;
  116.             buf=&cbuf[CBUFSIZE];
  117.             do
  118.             {
  119.               *--buf="0123456789abcdef"[n&15];
  120.               n>>=4;
  121.               width++;
  122.         }while(n);
  123.           }
  124.           break;
  125.         case 's':
  126.           {
  127.             UBYTE *buffer;
  128.             buf=buffer=*(*(UBYTE ***)&DataStream)++;
  129.             while(*buffer++)
  130.               width++;
  131.             if(width>maxwidth)
  132.               width=maxwidth;
  133.       }
  134.           break;
  135.         case 'c':
  136.           {
  137.             buf=cbuf;
  138.             width++;
  139.             if(larg)
  140.               *buf=*(*(ULONG **)&DataStream)++;
  141.             else
  142.               *buf=*(*(UWORD **)&DataStream)++;
  143.       }
  144.           break;
  145.         case '\0':
  146.           FormatString--;
  147.           buf=NULL;
  148.           break;
  149.         default: /* case '%': */
  150.           buf=FormatString-1;
  151.           width++;
  152.           break;
  153.       }
  154.       if(minus&&fill==' ')    /* Print sign */
  155.         PutChData=stuffChar(PutChProc,'-',PutChData);
  156.       if(!left)            /* Pad left */
  157.         for(i=width+minus;i<minwidth;i++)
  158.           PutChData=stuffChar(PutChProc,fill,PutChData);
  159.       if(minus&&fill!=' ')    /* Print sign */
  160.         PutChData=stuffChar(PutChProc,'-',PutChData);
  161.       for(i=0;i<width;i++)    /* Print body */
  162.         PutChData=stuffChar(PutChProc,*buf++,PutChData);
  163.       if(left)            /* Pad right */
  164.         for(i=width+minus;i<minwidth;i++)
  165.           PutChData=stuffChar(PutChProc,' ',PutChData);
  166.     }else
  167.       PutChData=stuffChar(PutChProc,*FormatString++,PutChData);
  168.   }
  169.   PutChData=stuffChar(PutChProc,0,PutChData);
  170.   return DataStream;
  171. }
  172.