URI:
       tchan.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tchan.c (1025B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <draw.h>
            4 
            5 static char channames[] = "rgbkamx";
            6 char*
            7 chantostr(char *buf, u32int cc)
            8 {
            9         u32int c, rc;
           10         char *p;
           11 
           12         if(chantodepth(cc) == 0)
           13                 return nil;
           14 
           15         /* reverse the channel descriptor so we can easily generate the string in the right order */
           16         rc = 0;
           17         for(c=cc; c; c>>=8){
           18                 rc <<= 8;
           19                 rc |= c&0xFF;
           20         }
           21 
           22         p = buf;
           23         for(c=rc; c; c>>=8) {
           24                 *p++ = channames[TYPE(c)];
           25                 *p++ = '0'+NBITS(c);
           26         }
           27         *p = 0;
           28 
           29         return buf;
           30 }
           31 
           32 u32int
           33 strtochan(char *s)
           34 {
           35         char *p, *q;
           36         u32int c;
           37         int t, n;
           38 
           39         c = 0;
           40         p=s;
           41         while(*p && isspace((uchar)*p))
           42                 p++;
           43 
           44         while(*p && !isspace((uchar)*p)){
           45                 if((q = strchr(channames, p[0])) == nil)
           46                         return 0;
           47                 t = q-channames;
           48                 if(p[1] < '0' || p[1] > '9')
           49                         return 0;
           50                 n = p[1]-'0';
           51                 c = (c<<8) | __DC(t, n);
           52                 p += 2;
           53         }
           54         return c;
           55 }
           56 
           57 int
           58 chantodepth(u32int c)
           59 {
           60         int n;
           61 
           62         for(n=0; c; c>>=8){
           63                 if(TYPE(c) >= NChan || NBITS(c) > 8 || NBITS(c) <= 0)
           64                         return 0;
           65                 n += NBITS(c);
           66         }
           67         if(n==0 || (n>8 && n%8) || (n<8 && 8%n))
           68                 return 0;
           69         return n;
           70 }