util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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) {
  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 '"';
  22. case '\'': return '\'';
  23. case '`': return '`';
  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, int dir)
  74. {
  75. char_t *p, z, op;
  76. point_t cp = bp->b_point;
  77. int depth = 0;
  78. p = ptr(bp, cp);
  79. op = *p;
  80. if((z = is_bracket(*p)) == 0) {
  81. // TODO: jump over whitespace to get to bracket
  82. return -1;
  83. }
  84. if(dir == -1) {
  85. cp--;
  86. while ((*(p = ptr(bp, cp)) != z || depth > 0) && cp >= 0) {
  87. if(*p == op) {
  88. depth++;
  89. } else if(*p == z) {
  90. depth--;
  91. }
  92. cp--;
  93. }
  94. if(cp > 0)
  95. return cp;
  96. } else {
  97. cp++;
  98. while ((*(p = ptr(bp, cp)) != z || depth > 0) && p <= bp->b_ebuf) {
  99. if(*p == op) {
  100. depth++;
  101. } else if(*p == z) {
  102. depth--;
  103. }
  104. cp++;
  105. }
  106. if(p < bp->b_ebuf)
  107. return cp;
  108. }
  109. return -1;
  110. }
  111. const char unctrl(char_t p)
  112. {
  113. return p + (char)64;
  114. }