1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
//
// C++ Implementation: SourceFileHighlighter
//
// Description:
//
//
// Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include <fstream>
#include "sourcefilehighlighter.h"
#include "sourcehighlighter.h"
#include "formatterparams.h"
#include "bufferedoutput.h"
#include "preformatter.h"
#include "formatter.h"
#include "linenumgenerator.h"
#include <sstream>
#include "ioexception.h"
#include "lineranges.h"
#include "regexranges.h"
using namespace std;
namespace srchilite {
typedef enum {
FOUND_EOF = 0, FOUND_NL, FOUND_END
} load_line_ret;
static load_line_ret load_line(std::string& s, std::istream& is);
SourceFileHighlighter::SourceFileHighlighter(const std::string &file,
SourceHighlighter *_sourceHighlighter, BufferedOutput *_output) :
fileName(file), sourceHighlighter(_sourceHighlighter), output(_output),
debugPolicy(NO_DEBUG), preformatter(0), lineNumGenerator(0),
lineRanges(0), regexRanges(0), contextFormatter(0) {
}
load_line_ret load_line(std::string& s, std::istream& is) {
s.erase();
if (is.bad() || is.eof())
return FOUND_EOF;
char c;
while (is.get(c)) {
if (c == '\n')
return FOUND_NL;
if (c != '\r')
s.append(1, c);
}
return FOUND_END;
}
void SourceFileHighlighter::setRangeSeparator(const std::string &rangeSep) {
if (preformatter) {
rangeSeparator = preformatter->preformat(rangeSep + "\n");
} else {
rangeSeparator = rangeSep + "\n";
}
}
void SourceFileHighlighter::highlight() {
istream *is = 0;
if (fileName != "") {
is = new ifstream(fileName.c_str());
if (!is || !(*is)) {
throw IOException("cannot open for input", fileName);
}
} else
is = &cin;
highlight(*is);
if (fileName != "")
delete is;
}
void SourceFileHighlighter::highlight(istream &is) {
std::string s;
FormatterParams params(fileName);
params.line = 1;
bool rangeSeparatorPrinted = false;
sourceHighlighter->setFormatterParams(¶ms);
sourceHighlighter->setSuspended(false);
// if we have a LineRanges, make sure we reset it
if (lineRanges) {
lineRanges->reset();
}
if (regexRanges) {
regexRanges->reset();
}
load_line_ret ret;
RangeResult rangeResult = IN_RANGE;
while ((ret = load_line(s, is)) != FOUND_EOF) {
// first check whether we must highlight only a specific range of lines
if (lineRanges) {
rangeResult = lineRanges->isInRange(params.line);
if (rangeResult == IN_RANGE) {
sourceHighlighter->setSuspended(false);
rangeSeparatorPrinted = false;
} else {
sourceHighlighter->setSuspended(true);
}
} else if (regexRanges) {
// we assume that regex range is set only if line range is not set
if (regexRanges->isInRange(s)) {
rangeResult = IN_RANGE;
sourceHighlighter->setSuspended(false);
} else {
rangeResult = NOT_IN_RANGE;
sourceHighlighter->setSuspended(true);
}
}
if (rangeResult != NOT_IN_RANGE) {
output->output(linePrefix);
if (lineNumGenerator) {
output->output(lineNumGenerator->generateLine(params.line));
}
} else {
// check whether we must print the range separator
if (!rangeSeparatorPrinted && params.line != 1) {
if (rangeSeparator.size()) {
output->output(linePrefix);
output->output(rangeSeparator);
rangeSeparatorPrinted = true;
}
}
}
if (rangeResult == CONTEXT_RANGE) {
// the whole line must be formatted using the "context" style
contextFormatter->format(s, ¶ms);
}
// highlighting must be performed anyway, since this will keep track
// of the current state; if we've set suspended, simply no output will be produced
sourceHighlighter->highlightParagraph(s);
if (rangeResult != NOT_IN_RANGE) {
// format the newline
if (ret == FOUND_NL) {
// the newline char is not highlighted, but only generated
// possibly after a transformation
output->output((preformatter ? preformatter->preformat("\n")
: "\n"));
}
output->writePostLine(linePrefix);
}
(params.line)++;
}
output->writePostDoc(linePrefix);
}
void SourceFileHighlighter::highlight(const string &s) {
istringstream is(s);
highlight(is);
}
}
|