util.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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[NAME_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[NAME_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. p = ptr(bp, cp);
  81. op = *p;
  82. if((z = is_bracket(*p, TRUE)) == 0) {
  83. // TODO: jump over whitespace to get to bracket
  84. return -1;
  85. }
  86. if(dir == -1) {
  87. cp--;
  88. while ((*(p = ptr(bp, cp)) != z || depth > 0) && cp >= 0) {
  89. if(*p == '\n')
  90. newlines++;
  91. if(*p == op) {
  92. depth++;
  93. } else if(*p == z) {
  94. depth--;
  95. }
  96. cp--;
  97. if(newlines > lun && isrender)
  98. break;
  99. }
  100. if(cp >= 0) {
  101. if(*ptr(bp, cp) == z) {
  102. // if(cp < bp->b_page && !isrender)
  103. // bp->b_reframe = 1;
  104. return cp;
  105. }
  106. }
  107. } else {
  108. cp++;
  109. while ((*(p = ptr(bp, cp)) != z || depth > 0) && p <= bp->b_ebuf) {
  110. if(*p == '\n')
  111. newlines++;
  112. if(*p == op) {
  113. depth++;
  114. } else if(*p == z) {
  115. depth--;
  116. }
  117. cp++;
  118. if(newlines > lun && isrender)
  119. break;
  120. }
  121. if(p < bp->b_ebuf) {
  122. if(*ptr(bp, cp) == z) {
  123. if(cp > bp->b_epage && !isrender)
  124. bp->b_reframe = 1;
  125. return cp;
  126. }
  127. }
  128. }
  129. return -1;
  130. }
  131. const char unctrl(char_t p)
  132. {
  133. return p + (char)64;
  134. }