util.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <thread.h>
  13. #include <cursor.h>
  14. #include <mouse.h>
  15. #include <keyboard.h>
  16. #include <frame.h>
  17. #include <fcall.h>
  18. #include "dat.h"
  19. #include "fns.h"
  20. void
  21. cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
  22. {
  23. uint8_t *q;
  24. Rune *s;
  25. int j, w;
  26. /*
  27. * Always guaranteed that n bytes may be interpreted
  28. * without worrying about partial runes. This may mean
  29. * reading up to UTFmax-1 more bytes than n; the caller
  30. * knows this. If n is a firm limit, the caller should
  31. * set p[n] = 0.
  32. */
  33. q = (uint8_t*)p;
  34. s = r;
  35. for(j=0; j<n; j+=w){
  36. if(*q < Runeself){
  37. w = 1;
  38. *s = *q++;
  39. }else{
  40. w = chartorune(s, (char*)q);
  41. q += w;
  42. }
  43. if(*s)
  44. s++;
  45. else if(nulls)
  46. *nulls = TRUE;
  47. }
  48. *nb = (char*)q-p;
  49. *nr = s-r;
  50. }
  51. void
  52. error(char *s)
  53. {
  54. fprint(2, "rio: %s: %r\n", s);
  55. if(errorshouldabort)
  56. abort();
  57. threadexitsall("error");
  58. }
  59. void*
  60. erealloc(void *p, uint n)
  61. {
  62. p = realloc(p, n);
  63. if(p == nil)
  64. error("realloc failed");
  65. return p;
  66. }
  67. void*
  68. emalloc(uint n)
  69. {
  70. void *p;
  71. p = malloc(n);
  72. if(p == nil)
  73. error("malloc failed");
  74. memset(p, 0, n);
  75. return p;
  76. }
  77. char*
  78. estrdup(char *s)
  79. {
  80. char *p;
  81. p = malloc(strlen(s)+1);
  82. if(p == nil)
  83. error("strdup failed");
  84. strcpy(p, s);
  85. return p;
  86. }
  87. int
  88. isalnum(Rune c)
  89. {
  90. /*
  91. * Hard to get absolutely right. Use what we know about ASCII
  92. * and assume anything above the Latin control characters is
  93. * potentially an alphanumeric.
  94. */
  95. if(c <= ' ')
  96. return FALSE;
  97. if(0x7F<=c && c<=0xA0)
  98. return FALSE;
  99. if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
  100. return FALSE;
  101. return TRUE;
  102. }
  103. Rune*
  104. strrune(Rune *s, Rune c)
  105. {
  106. Rune c1;
  107. if(c == 0) {
  108. while(*s++)
  109. ;
  110. return s-1;
  111. }
  112. while(c1 = *s++)
  113. if(c1 == c)
  114. return s-1;
  115. return nil;
  116. }
  117. int
  118. min(int a, int b)
  119. {
  120. if(a < b)
  121. return a;
  122. return b;
  123. }
  124. int
  125. max(int a, int b)
  126. {
  127. if(a > b)
  128. return a;
  129. return b;
  130. }
  131. char*
  132. runetobyte(Rune *r, int n, int *ip)
  133. {
  134. char *s;
  135. int m;
  136. s = emalloc(n*UTFmax+1);
  137. m = snprint(s, n*UTFmax+1, "%.*S", n, r);
  138. *ip = m;
  139. return s;
  140. }