util.c 6.7 KB

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