chan.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. static char channames[] = "rgbkamx";
  13. char*
  14. chantostr(char *buf, uint32_t cc)
  15. {
  16. uint32_t c, rc;
  17. char *p;
  18. if(chantodepth(cc) == 0)
  19. return nil;
  20. /* reverse the channel descriptor so we can easily generate the string in the right order */
  21. rc = 0;
  22. for(c=cc; c; c>>=8){
  23. rc <<= 8;
  24. rc |= c&0xFF;
  25. }
  26. p = buf;
  27. for(c=rc; c; c>>=8) {
  28. *p++ = channames[TYPE(c)];
  29. *p++ = '0'+NBITS(c);
  30. }
  31. *p = 0;
  32. return buf;
  33. }
  34. /* avoid pulling in ctype when using with drawterm etc. */
  35. static int
  36. isspace(char c)
  37. {
  38. return c==' ' || c== '\t' || c=='\r' || c=='\n';
  39. }
  40. uint32_t
  41. strtochan(char *s)
  42. {
  43. char *p, *q;
  44. uint32_t c;
  45. int t, n, d;
  46. c = 0;
  47. d = 0;
  48. p=s;
  49. while(*p && isspace(*p))
  50. p++;
  51. while(*p && !isspace(*p)){
  52. if((q = strchr(channames, p[0])) == nil)
  53. return 0;
  54. t = q-channames;
  55. if(p[1] < '0' || p[1] > '9')
  56. return 0;
  57. n = p[1]-'0';
  58. d += n;
  59. c = (c<<8) | __DC(t, n);
  60. p += 2;
  61. }
  62. if(d==0 || (d>8 && d%8) || (d<8 && 8%d))
  63. return 0;
  64. return c;
  65. }
  66. int
  67. chantodepth(uint32_t c)
  68. {
  69. int n;
  70. for(n=0; c; c>>=8){
  71. if(TYPE(c) >= NChan || NBITS(c) > 8 || NBITS(c) <= 0)
  72. return 0;
  73. n += NBITS(c);
  74. }
  75. if(n==0 || (n>8 && n%8) || (n<8 && 8%n))
  76. return 0;
  77. return n;
  78. }