util.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. int is_symboli(char_t c, char_t ignore)
  57. {
  58. register char_t *p = symbols;
  59. for (p = symbols; *p != '\0'; p++)
  60. if (*p == c && *p != ignore) return 1;
  61. return 0;
  62. }
  63. void replace_all(char * str, char oldChar, char newChar)
  64. {
  65. int i = 0;
  66. /* Run till end of string */
  67. while(str[i] != '\0')
  68. {
  69. /* If occurrence of character is found */
  70. if(str[i] == oldChar) {
  71. str[i] = newChar;
  72. }
  73. i++;
  74. }
  75. }
  76. void cleanup_path(char *path, char *output)
  77. {
  78. char *dir, final_path[PATH_MAX+1] = "\0";
  79. const char *list_dirs[20];
  80. int i = 0;
  81. final_path[0] = '/';
  82. dir = strtok(path, "/");
  83. while( dir != NULL ) {
  84. if(dir[0] == '.' && dir[1] == '.') {
  85. i--;
  86. } else {
  87. list_dirs[i] = dir;
  88. i++;
  89. }
  90. dir = strtok(NULL, "/");
  91. }
  92. for(int z = 0 ; i > z; z++) {
  93. strcat(final_path, list_dirs[z]);
  94. if(z != i-1)
  95. strcat(final_path, "/");
  96. }
  97. final_path[PATH_MAX] = '\0';
  98. strcpy(output, final_path);
  99. return;
  100. }
  101. int is_quote(buffer_t *bp, char_t c)
  102. {
  103. int as = TRUE, ab = TRUE;
  104. if(bp != NULL && bp->b_keywords != NULL) {
  105. if(!bp->b_keywords->sqas) {
  106. as = FALSE;
  107. }
  108. if(!bp->b_keywords->bqas) {
  109. ab = FALSE;
  110. }
  111. }
  112. return (c == '\'' && as) || c == '"' || (c == '`' && ab);
  113. }
  114. point_t find_matching_bracket(buffer_t *bp, window_t *wp, int dir, int isrender)
  115. {
  116. char_t *p, z, op;
  117. point_t cp = bp->b_point;
  118. int depth = 0, newlines = 0, instring = FALSE,
  119. /* only search for matches that are on the current page
  120. wp doesn't update until after render, so we can't use w_row and
  121. should use bp's b_row instead
  122. */
  123. lun = dir == -1 ? wp->w_rows - (bp->b_row - wp->w_rows) : wp->w_rows - bp->b_row;
  124. int isquote = FALSE, skip = FALSE;
  125. p = ptr(bp, cp);
  126. op = *p;
  127. isquote = is_quote(bp, *p);
  128. if((z = is_bracket(*p, TRUE, NULL)) == 0) {
  129. // TODO: jump over whitespace to get to bracket
  130. return -1;
  131. }
  132. if(dir == -1) {
  133. cp--;
  134. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip || instring) && cp >= 0) {
  135. if(*p == '\n')
  136. newlines++;
  137. /* Skip over stuff like '}' or '('*/
  138. if(!skip && is_quote(bp, *p) &&
  139. (!isalpha(*ptr(bp, cp-1)) ||
  140. (!isalpha(*ptr(bp, cp-1)) && !isspace(*ptr(bp, cp-1))) ||
  141. (isalpha(*ptr(bp, cp-1)) && !isalpha(*ptr(bp, cp+1))))) {
  142. instring = !instring;
  143. }
  144. if(*p == op && !skip && !instring) {
  145. depth++;
  146. } else if(*p == z && !instring) {
  147. depth--;
  148. }
  149. skip = FALSE;
  150. /* Imagine this case: "this is \"a\" test."
  151. We want to skip the inner quotes that are escaped with the \.
  152. To detect this, we have to look 2 chars behind to find the \.
  153. */
  154. if(isquote) {
  155. char_t *s;
  156. if(*(s = ptr(bp, cp-2)) == '\\')
  157. skip = TRUE;
  158. }
  159. cp--;
  160. if(newlines > lun && isrender)
  161. break;
  162. }
  163. if(cp >= 0) {
  164. if(*ptr(bp, cp) == z) {
  165. // if(cp < bp->b_page && !isrender)
  166. // bp->b_reframe = 1;
  167. return cp;
  168. }
  169. }
  170. } else {
  171. cp++;
  172. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip || instring) && p <= bp->b_ebuf) {
  173. if(*p == '\n')
  174. newlines++;
  175. /* Skip over stuff like '}' or '('*/
  176. if(!skip && is_quote(bp, *p) &&
  177. (!isalpha(*ptr(bp, cp-1)) ||
  178. (!isalpha(*ptr(bp, cp-1)) && !isspace(*ptr(bp, cp-1))) ||
  179. (isalpha(*ptr(bp, cp-1)) && !isalpha(*ptr(bp, cp+1))))) {
  180. instring = !instring;
  181. }
  182. if(*p == op && !skip && !instring) {
  183. depth++;
  184. } else if(*p == z && !instring) {
  185. depth--;
  186. }
  187. skip = FALSE;
  188. if(*p == '\\' && isquote)
  189. skip = TRUE;
  190. cp++;
  191. if(newlines > lun && isrender)
  192. break;
  193. }
  194. if(p < bp->b_ebuf) {
  195. if(*ptr(bp, cp) == z) {
  196. if(cp > bp->b_epage && !isrender)
  197. bp->b_reframe = 1;
  198. return cp;
  199. }
  200. }
  201. }
  202. return -1;
  203. }
  204. const char unctrl(char_t p)
  205. {
  206. return p + (char)64;
  207. }
  208. point_t shift_pmark(int dir, point_t opoint)
  209. {
  210. point_t mark = curbp->b_pmark[0];
  211. if(dir) {
  212. if(mark == opoint) {
  213. return NOMARK;
  214. }
  215. for(int i = PMARK_SIZE-1; i > 0; i--) {
  216. curbp->b_pmark[i] = curbp->b_pmark[i-1];
  217. }
  218. curbp->b_pmark[0] = opoint != NOMARK ? opoint : curbp->b_point;
  219. return NOMARK;
  220. }
  221. for(int i = 0; i < PMARK_SIZE ; i++) {
  222. if(i+1 > PMARK_SIZE-1)
  223. curbp->b_pmark[i] = NOMARK;
  224. else
  225. curbp->b_pmark[i] = curbp->b_pmark[i+1];
  226. }
  227. return mark;
  228. }
  229. int is_combining_unicode(uint32_t result)
  230. {
  231. struct combine_t range;
  232. int match = FALSE;
  233. for(int b = 0; b < RANGES_MAX; b++) {
  234. range = unicode_combine_ranges[b];
  235. if(range.end == 0) {
  236. if(range.start == result) {
  237. match = TRUE;
  238. break;
  239. }
  240. } else if(range.start <= result && range.end >= result) {
  241. match = TRUE;
  242. break;
  243. }
  244. }
  245. return match;
  246. }
  247. static const unsigned char utf8_mask[6] = {0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
  248. uint32_t char_to_unicode(char_t *p, int nch)
  249. {
  250. /* The following 12 lines come from mle(1) */
  251. char_t mask = utf8_mask[nch - 1];
  252. uint32_t result = p[0] & mask;
  253. int i;
  254. for (i = 1; i < nch /*&& (p + i) < stop*/; ++i) {
  255. result <<= 6;
  256. result |= p[i] & 0x3f;
  257. }
  258. // replace incomplete code point with replacement char
  259. if (i != nch) {
  260. result = 0xfffd;
  261. nch = i;
  262. }
  263. return result;
  264. }
  265. void adjust_bline()
  266. {
  267. if(curbp->b_opoint > curbp->b_point) {
  268. curbp->b_opoint--;
  269. while(curbp->b_opoint >= curbp->b_point) {
  270. if(*ptr(curbp, curbp->b_opoint) == '\n')
  271. curbp->b_line--;
  272. curbp->b_opoint--;
  273. }
  274. } else if(curbp->b_opoint < curbp->b_point) {
  275. while(curbp->b_opoint < curbp->b_point) {
  276. if(*ptr(curbp, curbp->b_opoint) == '\n')
  277. curbp->b_line++;
  278. curbp->b_opoint++;
  279. }
  280. }
  281. }