util.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. lun = dir == -1 ? wp->w_rows - (wp->w_row - wp->w_rows) : wp->w_rows - wp->w_row;
  101. int isquote = FALSE, skip = FALSE;
  102. p = ptr(bp, cp);
  103. op = *p;
  104. isquote = *p == '\'' || *p == '"' || *p == '`';
  105. if((z = is_bracket(*p, TRUE, NULL)) == 0) {
  106. // TODO: jump over whitespace to get to bracket
  107. return -1;
  108. }
  109. if(dir == -1) {
  110. cp--;
  111. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip) && cp >= 0) {
  112. if(*p == '\n')
  113. newlines++;
  114. if(*p == op && !skip) {
  115. depth++;
  116. } else if(*p == z) {
  117. depth--;
  118. }
  119. skip = FALSE;
  120. /* Imagine this case: "this is \"a\" test."
  121. We want to skip the inner quotes that are escaped with the \.
  122. To detect this, we have to look 2 chars behind to find the \.
  123. */
  124. if(isquote) {
  125. char_t *s;
  126. if(*(s = ptr(bp, cp-2)) == '\\')
  127. skip = TRUE;
  128. }
  129. cp--;
  130. if(newlines > lun && isrender)
  131. break;
  132. }
  133. if(cp >= 0) {
  134. if(*ptr(bp, cp) == z) {
  135. // if(cp < bp->b_page && !isrender)
  136. // bp->b_reframe = 1;
  137. return cp;
  138. }
  139. }
  140. } else {
  141. cp++;
  142. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip) && p <= bp->b_ebuf) {
  143. if(*p == '\n')
  144. newlines++;
  145. if(*p == op && !skip) {
  146. depth++;
  147. } else if(*p == z) {
  148. depth--;
  149. }
  150. skip = FALSE;
  151. if(*p == '\\' && isquote)
  152. skip = TRUE;
  153. cp++;
  154. if(newlines > lun && isrender)
  155. break;
  156. }
  157. if(p < bp->b_ebuf) {
  158. if(*ptr(bp, cp) == z) {
  159. if(cp > bp->b_epage && !isrender)
  160. bp->b_reframe = 1;
  161. return cp;
  162. }
  163. }
  164. }
  165. return -1;
  166. }
  167. const char unctrl(char_t p)
  168. {
  169. return p + (char)64;
  170. }
  171. point_t shift_pmark(int dir, point_t opoint)
  172. {
  173. point_t mark = curbp->b_pmark[0];
  174. if(dir) {
  175. for(int i = PMARK_SIZE-1; i > 0; i--) {
  176. curbp->b_pmark[i] = curbp->b_pmark[i-1];
  177. }
  178. curbp->b_pmark[0] = opoint != NOMARK ? opoint : curbp->b_point;
  179. return NOMARK;
  180. }
  181. for(int i = 0; i < PMARK_SIZE ; i++) {
  182. if(i+1 > PMARK_SIZE-1)
  183. curbp->b_pmark[i] = NOMARK;
  184. else
  185. curbp->b_pmark[i] = curbp->b_pmark[i+1];
  186. }
  187. return mark;
  188. }
  189. int is_combining_unicode(uint32_t result)
  190. {
  191. struct combine_t range;
  192. int match = FALSE;
  193. for(int b = 0; b < RANGES_MAX; b++) {
  194. range = unicode_combine_ranges[b];
  195. if(range.end == 0) {
  196. if(range.start == result) {
  197. match = TRUE;
  198. break;
  199. }
  200. } else if(range.start <= result && range.end >= result) {
  201. match = TRUE;
  202. break;
  203. }
  204. }
  205. return match;
  206. }
  207. static const unsigned char utf8_mask[6] = {0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
  208. uint32_t char_to_unicode(char_t *p, int nch)
  209. {
  210. /* The following 12 lines come from mle(1) */
  211. char_t mask = utf8_mask[nch - 1];
  212. uint32_t result = p[0] & mask;
  213. int i;
  214. for (i = 1; i < nch /*&& (p + i) < stop*/; ++i) {
  215. result <<= 6;
  216. result |= p[i] & 0x3f;
  217. }
  218. // replace incomplete code point with replacement char
  219. if (i != nch) {
  220. result = 0xfffd;
  221. nch = i;
  222. }
  223. return result;
  224. }
  225. void adjust_bline()
  226. {
  227. if(curbp->b_opoint > curbp->b_point) {
  228. curbp->b_opoint--;
  229. while(curbp->b_opoint >= curbp->b_point) {
  230. if(*ptr(curbp, curbp->b_opoint) == '\n')
  231. curbp->b_line--;
  232. curbp->b_opoint--;
  233. }
  234. } else if(curbp->b_opoint < curbp->b_point) {
  235. while(curbp->b_opoint < curbp->b_point) {
  236. if(*ptr(curbp, curbp->b_opoint) == '\n')
  237. curbp->b_line++;
  238. curbp->b_opoint++;
  239. }
  240. }
  241. }