util.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "header.h"
  2. #include "util.h"
  3. int power(int base, int exp)
  4. {
  5. int product = 1;
  6. for(int i = exp; i > 0; i--)
  7. product *= base;
  8. return product;
  9. }
  10. static char_t symbols[] = "{}[]()!'£$%^&*-+=:;@~#<>,.?/\\|_\"`";
  11. char_t is_bracket(char_t ch, int quote) {
  12. switch (ch) {
  13. case '[': return ']';
  14. case '(': return ')';
  15. case '{': return '}';
  16. case '<': return '>';
  17. case ']': return '[';
  18. case ')': return '(';
  19. case '}': return '{';
  20. case '>': return '<';
  21. case '"': return quote ? '"' : 0;
  22. case '\'': return quote ? '\'' : 0;
  23. case '`': return quote ? '`' : 0;
  24. default: return 0;
  25. }
  26. return 0;
  27. }
  28. int is_symbol(char_t c)
  29. {
  30. register char_t *p = symbols;
  31. for (p = symbols; *p != '\0'; p++)
  32. if (*p == c) return 1;
  33. return 0;
  34. }
  35. void replace_all(char * str, char oldChar, char newChar)
  36. {
  37. int i = 0;
  38. /* Run till end of string */
  39. while(str[i] != '\0')
  40. {
  41. /* If occurrence of character is found */
  42. if(str[i] == oldChar) {
  43. str[i] = newChar;
  44. }
  45. i++;
  46. }
  47. }
  48. void cleanup_path(char *path, char *output)
  49. {
  50. char *dir, final_path[PATH_MAX+1] = "\0";
  51. const char *list_dirs[20];
  52. int i = 0;
  53. final_path[0] = '/';
  54. dir = strtok(path, "/");
  55. while( dir != NULL ) {
  56. if(dir[0] == '.' && dir[1] == '.') {
  57. i--;
  58. } else {
  59. list_dirs[i] = dir;
  60. i++;
  61. }
  62. dir = strtok(NULL, "/");
  63. }
  64. for(int z = 0 ; i > z; z++) {
  65. strcat(final_path, list_dirs[z]);
  66. if(z != i-1)
  67. strcat(final_path, "/");
  68. }
  69. final_path[PATH_MAX] = '\0';
  70. strcpy(output, final_path);
  71. return;
  72. }
  73. point_t find_matching_bracket(buffer_t *bp, window_t *wp, int dir, int isrender)
  74. {
  75. char_t *p, z, op;
  76. point_t cp = bp->b_point;
  77. int depth = 0, newlines = 0,
  78. /* only search for matches that are on the current page */
  79. lun = dir == -1 ? wp->w_rows - (wp->w_row - wp->w_rows) : wp->w_rows - wp->w_row;
  80. int isquote = FALSE, skip = FALSE;
  81. p = ptr(bp, cp);
  82. op = *p;
  83. isquote = *p == '\'' || *p == '"' || *p == '`';
  84. if((z = is_bracket(*p, TRUE)) == 0) {
  85. // TODO: jump over whitespace to get to bracket
  86. return -1;
  87. }
  88. if(dir == -1) {
  89. cp--;
  90. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip) && cp >= 0) {
  91. if(*p == '\n')
  92. newlines++;
  93. if(*p == op && !skip) {
  94. depth++;
  95. } else if(*p == z) {
  96. depth--;
  97. }
  98. skip = FALSE;
  99. /* Imagine this case: "this is \"a\" test."
  100. We want to skip the inner quotes that are escaped with the \.
  101. To detect this, we have to look 2 chars behind to find the \.
  102. */
  103. if(isquote) {
  104. char_t *s;
  105. if(*(s = ptr(bp, cp-2)) == '\\')
  106. skip = TRUE;
  107. }
  108. cp--;
  109. if(newlines > lun && isrender)
  110. break;
  111. }
  112. if(cp >= 0) {
  113. if(*ptr(bp, cp) == z) {
  114. // if(cp < bp->b_page && !isrender)
  115. // bp->b_reframe = 1;
  116. return cp;
  117. }
  118. }
  119. } else {
  120. cp++;
  121. while ((*(p = ptr(bp, cp)) != z || depth > 0 || skip) && p <= bp->b_ebuf) {
  122. if(*p == '\n')
  123. newlines++;
  124. if(*p == op && !skip) {
  125. depth++;
  126. } else if(*p == z) {
  127. depth--;
  128. }
  129. skip = FALSE;
  130. if(*p == '\\' && isquote)
  131. skip = TRUE;
  132. cp++;
  133. if(newlines > lun && isrender)
  134. break;
  135. }
  136. if(p < bp->b_ebuf) {
  137. if(*ptr(bp, cp) == z) {
  138. if(cp > bp->b_epage && !isrender)
  139. bp->b_reframe = 1;
  140. return cp;
  141. }
  142. }
  143. }
  144. return -1;
  145. }
  146. const char unctrl(char_t p)
  147. {
  148. return p + (char)64;
  149. }