PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/output.cc

https://gitlab.com/witwall/pdfgrep
C++ | 184 lines | 133 code | 32 blank | 19 comment | 48 complexity | 054cfeb81d6c158f02feb6f83dc32d71 MD5 | raw file
Possible License(s): GPL-2.0
  1. /***************************************************************************
  2. * Copyright (C) 2015 by Hans-Peter Deifel *
  3. * hpdeifel@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
  18. * Boston, MA 02110-1301 USA. *
  19. ***************************************************************************/
  20. #include "output.h"
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. bool is_valid_color(const char* colorcode) {
  25. return colorcode && strcmp(colorcode, "");
  26. }
  27. void start_color(bool use_colors, const char *colorcode)
  28. {
  29. if (use_colors && is_valid_color(colorcode))
  30. printf("\33[%sm\33[K", colorcode);
  31. }
  32. void end_color(bool use_colors, const char *colorcode)
  33. {
  34. if (use_colors && is_valid_color(colorcode))
  35. printf("\33[m\33[K");
  36. }
  37. #define with_color(use_colors, color, code) \
  38. do { \
  39. start_color(use_colors, color); \
  40. { \
  41. code; \
  42. } \
  43. end_color(use_colors, color); \
  44. } while (0);
  45. void print_line_prefix(const struct outconf *conf, const char *filename, const int pagenum)
  46. {
  47. if (filename && conf->filename) {
  48. with_color(conf->color, conf->colors.filename,
  49. printf("%s", filename););
  50. with_color(conf->color, conf->colors.separator, printf(":"););
  51. }
  52. if (pagenum >= 0 && conf->pagenum) {
  53. with_color(conf->color, conf->colors.pagenum,
  54. printf("%d", pagenum););
  55. with_color(conf->color, conf->colors.separator, printf(":"););
  56. }
  57. }
  58. void putsn(char *string, int from, int to)
  59. {
  60. for (; from < to; from++) {
  61. char c = string[from];
  62. if (isprint(c))
  63. putchar(c);
  64. }
  65. }
  66. int next_word_left(char *string, int index)
  67. {
  68. int i = 0;
  69. int in_whitespace;
  70. if (index < 0 || string[index] == '\n')
  71. return -1;
  72. in_whitespace = isspace(string[index]);
  73. while (index >= 0 && string[index] != '\n') {
  74. if (in_whitespace) {
  75. if (!isspace(string[index]))
  76. in_whitespace = 0;
  77. } else {
  78. if (isspace(string[index]))
  79. break;
  80. }
  81. i++;
  82. index--;
  83. }
  84. return i;
  85. }
  86. int next_word_right(char *string, int index, int buflen)
  87. {
  88. int i = 0;
  89. int in_whitespace;
  90. if (index >= buflen || string[index] == '\n')
  91. return -1;
  92. in_whitespace = isspace(string[index]);
  93. while (index < buflen && string[index] != '\n') {
  94. if (in_whitespace) {
  95. if (!isspace(string[index]))
  96. in_whitespace = 0;
  97. } else {
  98. if (isspace(string[index]))
  99. break;
  100. }
  101. i++;
  102. index++;
  103. }
  104. return i;
  105. }
  106. void print_context_chars(const struct context *context, const struct match *match)
  107. {
  108. int a = match->start;
  109. int b = match->end;
  110. int chars_left = context->before;
  111. int left = next_word_left(match->string, a-1);
  112. int right = next_word_right(match->string, b, match->strlen);
  113. while (true) {
  114. if ((left < 0 || left > chars_left)
  115. && (right < 0 || right > chars_left))
  116. break;
  117. if (right < 0 || right > chars_left ||
  118. (left > 0 && left <= chars_left
  119. && (match->start - a) + left
  120. < (b - match->end) + right)) {
  121. a -= left;
  122. chars_left -= left;
  123. left = next_word_left(match->string,a-1);
  124. } else {
  125. b += right;
  126. chars_left -= right;
  127. right = next_word_right(match->string, b, match->strlen);
  128. }
  129. }
  130. print_line_prefix(context->out, context->filename, context->pagenum);
  131. putsn(match->string, a, match->start);
  132. with_color(context->out->color, context->out->colors.highlight,
  133. putsn(match->string, match->start, match->end);
  134. );
  135. putsn(match->string, match->end, b);
  136. }
  137. void print_context_line(const struct context *context, const struct match *match)
  138. {
  139. int a = match->start;
  140. int b = match->end;
  141. while (a >= 0 && match->string[a] != '\n')
  142. a--;
  143. a++;
  144. while (b < match->strlen && match->string[b] != '\n')
  145. b++;
  146. print_line_prefix(context->out, context->filename, context->pagenum);
  147. putsn(match->string, a, match->start);
  148. with_color(context->out->color, context->out->colors.highlight,
  149. putsn(match->string, match->start, match->end);
  150. );
  151. putsn(match->string, match->end, b);
  152. }