util.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "header.h"
  2. #include "util.h"
  3. #include "unicode.h"
  4. int power(int base, int exp)
  5. {
  6. int product = 1;
  7. for(int i = exp; i > 0; i--)
  8. product *= base;
  9. return product;
  10. }
  11. static char_t symbols[] = "{}[]()!'£$%^&*-+=:;@~#<>,.?/\\|_\"`";
  12. char_t is_bracket(char_t ch, int quote, int *is_start) {
  13. switch (ch) {
  14. case '[': {
  15. if(is_start != NULL) {
  16. *is_start = TRUE;
  17. }
  18. return ']';
  19. }
  20. case '(': {
  21. if(is_start != NULL) {
  22. *is_start = TRUE;
  23. }
  24. return ')';
  25. }
  26. case '{': {
  27. if(is_start != NULL) {
  28. *is_start = TRUE;
  29. }
  30. return '}';
  31. }
  32. case '<': {
  33. if(is_start != NULL) {
  34. *is_start = TRUE;
  35. }
  36. return '>';
  37. }
  38. case ']': return '[';
  39. case ')': return '(';
  40. case '}': return '{';
  41. case '>': return '<';
  42. case '"': return quote ? '"' : 0;
  43. case '\'': return quote ? '\'' : 0;
  44. case '`': return quote ? '`' : 0;
  45. default: return 0;
  46. }
  47. return 0;
  48. }
  49. int is_symbol(char_t c)
  50. {
  51. register char_t *p = symbols;
  52. for (p = symbols; *p != '\0'; p++)
  53. if (*p == c) return 1;
  54. return 0;
  55. }
  56. void replace_all(char * str, char oldChar, char newChar)
  57. {
  58. int i = 0;
  59. /* Run till end of string */
  60. while(str[i] != '\0')
  61. {
  62. /* If occurrence of character is found */
  63. if(str[i] == oldChar) {
  64. str[i] = newChar;
  65. }
  66. i++;
  67. }
  68. }
  69. void cleanup_path(char *path, char *output)
  70. {
  71. char *dir, final_path[PATH_MAX+1] = "\0";
  72. const char *list_dirs[20];
  73. int i = 0;
  74. final_path[0] = '/';
  75. dir = strtok(path, "/");
  76. while( dir != NULL ) {
  77. if(dir[0] == '.' && dir[1] == '.') {
  78. i--;
  79. } else {
  80. list_dirs[i] = dir;
  81. i++;
  82. }
  83. dir = strtok(NULL, "/");
  84. }
  85. for(int z = 0 ; i > z; z++) {
  86. strcat(final_path, list_dirs[z]);
  87. if(z != i-1)
  88. strcat(final_path, "/");
  89. }
  90. final_path[PATH_MAX] = '\0';
  91. strcpy(output, final_path);
  92. return;
  93. }
  94. point_t find_matching_bracket(buffer_t *bp, window_t *wp, int dir, int isrender)
  95. {
  96. char_t *p, z, op;
  97. point_t cp = bp->b_point;
  98. int depth = 0, newlines = 0,
  99. /* only search for matches that are on the current page
  100. wp doesn't update until after render, so we can't use w_row and
  101. should use bp's b_row instead
  102. */
  103. lun = dir == -1 ? wp->w_rows - (bp->b_row - wp->w_rows) : wp->w_rows - bp->b_row;
  104. int isquote = FALSE, skip = FALSE;
  105. p = ptr(bp, cp);
  106. op = *p;
  107. isquote = *p == '\'' || *p == '"' || *p == '`';
  108. if((z = is_bracket(*p, TRUE, NULL)) == 0) {
  109. // TODO: jump over whitespace to get to bracket
  110. return -1;
  111. }
  112. if(dir == -1) {
  113. cp--;
  114. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip) && cp >= 0) {
  115. if(*p == '\n')
  116. newlines++;
  117. if(*p == op && !skip) {
  118. depth++;
  119. } else if(*p == z) {
  120. depth--;
  121. }
  122. skip = FALSE;
  123. /* Imagine this case: "this is \"a\" test."
  124. We want to skip the inner quotes that are escaped with the \.
  125. To detect this, we have to look 2 chars behind to find the \.
  126. */
  127. if(isquote) {
  128. char_t *s;
  129. if(*(s = ptr(bp, cp-2)) == '\\')
  130. skip = TRUE;
  131. }
  132. cp--;
  133. if(newlines > lun && isrender)
  134. break;
  135. }
  136. if(cp >= 0) {
  137. if(*ptr(bp, cp) == z) {
  138. // if(cp < bp->b_page && !isrender)
  139. // bp->b_reframe = 1;
  140. return cp;
  141. }
  142. }
  143. } else {
  144. cp++;
  145. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip) && p <= bp->b_ebuf) {
  146. if(*p == '\n')
  147. newlines++;
  148. if(*p == op && !skip) {
  149. depth++;
  150. } else if(*p == z) {
  151. depth--;
  152. }
  153. skip = FALSE;
  154. if(*p == '\\' && isquote)
  155. skip = TRUE;
  156. cp++;
  157. if(newlines > lun && isrender)
  158. break;
  159. }
  160. if(p < bp->b_ebuf) {
  161. if(*ptr(bp, cp) == z) {
  162. if(cp > bp->b_epage && !isrender)
  163. bp->b_reframe = 1;
  164. return cp;
  165. }
  166. }
  167. }
  168. return -1;
  169. }
  170. const char unctrl(char_t p)
  171. {
  172. return p + (char)64;
  173. }
  174. point_t shift_pmark(int dir, point_t opoint)
  175. {
  176. point_t mark = curbp->b_pmark[0];
  177. if(dir) {
  178. for(int i = PMARK_SIZE-1; i > 0; i--) {
  179. curbp->b_pmark[i] = curbp->b_pmark[i-1];
  180. }
  181. curbp->b_pmark[0] = opoint != NOMARK ? opoint : curbp->b_point;
  182. return NOMARK;
  183. }
  184. for(int i = 0; i < PMARK_SIZE ; i++) {
  185. if(i+1 > PMARK_SIZE-1)
  186. curbp->b_pmark[i] = NOMARK;
  187. else
  188. curbp->b_pmark[i] = curbp->b_pmark[i+1];
  189. }
  190. return mark;
  191. }
  192. int is_combining_unicode(uint32_t result)
  193. {
  194. struct combine_t range;
  195. int match = FALSE;
  196. for(int b = 0; b < RANGES_MAX; b++) {
  197. range = unicode_combine_ranges[b];
  198. if(range.end == 0) {
  199. if(range.start == result) {
  200. match = TRUE;
  201. break;
  202. }
  203. } else if(range.start <= result && range.end >= result) {
  204. match = TRUE;
  205. break;
  206. }
  207. }
  208. return match;
  209. }
  210. static const unsigned char utf8_mask[6] = {0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
  211. uint32_t char_to_unicode(char_t *p, int nch)
  212. {
  213. /* The following 12 lines come from mle(1) */
  214. char_t mask = utf8_mask[nch - 1];
  215. uint32_t result = p[0] & mask;
  216. int i;
  217. for (i = 1; i < nch /*&& (p + i) < stop*/; ++i) {
  218. result <<= 6;
  219. result |= p[i] & 0x3f;
  220. }
  221. // replace incomplete code point with replacement char
  222. if (i != nch) {
  223. result = 0xfffd;
  224. nch = i;
  225. }
  226. return result;
  227. }
  228. void adjust_bline()
  229. {
  230. if(curbp->b_opoint > curbp->b_point) {
  231. curbp->b_opoint--;
  232. while(curbp->b_opoint >= curbp->b_point) {
  233. if(*ptr(curbp, curbp->b_opoint) == '\n')
  234. curbp->b_line--;
  235. curbp->b_opoint--;
  236. }
  237. } else if(curbp->b_opoint < curbp->b_point) {
  238. while(curbp->b_opoint < curbp->b_point) {
  239. if(*ptr(curbp, curbp->b_opoint) == '\n')
  240. curbp->b_line++;
  241. curbp->b_opoint++;
  242. }
  243. }
  244. }