我是 C++ 的初学者,对 JAVA 有粗略的了解。我正在尝试读取一个看起来像这样的 HTML 文件。
<red>Red <dim>dim and red</dim> back to red</red>
<blue>Blue <underline>underlined blue <dim>dim</dim> underlined blue</underline>
and <cyan>cyan</cyan> and blue again</blue>
这是我正在使用的代码的一个非常小的片段。我的问题的一个例子是,我无法弄清楚如何清除暗淡的格式并将文本保持为红色,直到到达结束标记(上面的第一行代码)。
void print_well_formed_file(ifstream& ifs) {
Lexer lexer; Token tok;
stack<string> tags;
string fstring;
term_colors_t scancolor;
term_attrib_t scanattrib;
while(getline(ifs,fstring)){
lexer.set_input(fstring);
while(lexer.has_more_token()){
tok = lexer.next_token();
switch(tok.type){
case TAG:
if(tok.value[0] != '/'){
tags.push(tok.value);
if(tok.value == "red")
scancolor = RED;
else if(tok.value == "green")
scancolor = GREEN;
else if(tok.value == "yellow")
scancolor = YELLOW;
else if(tok.value == "blue")
scancolor = BLUE;
else if(tok.value == "magenta")
scancolor = MAGENTA;
else if(tok.value == "cyan")
scancolor = CYAN;
else if(tok.value == "dim")
scanattrib = DIM;
else if(tok.value == "underline")
scanattrib = UNDERLINE;
else if(tok.value == "bright")
scanattrib = BRIGHT;
cout << term_cc(scancolor, DEFAULT_COLOR, scanattrib);
}else if(tags.top() == tok.value.substr(1)){
tags.pop();
//THIS IS WHERE THE END TAGS WOULD BE PROCESSED.
}
break;
case IDENT:
cout << tok.value << " ";
break;
case ERRTOK:
cout << "Syntax Error: " << tok.value;
noerror = false;
break;
}
}
}
以及下面正在实现的功能。
std::string term_cc(term_colors_t fg=DEFAULT_COLOR,
term_colors_t bg=DEFAULT_COLOR,
term_attrib_t attr=DEFAULT_ATTRIB);
std::string term_bg(term_colors_t bg=DEFAULT_COLOR);
std::string term_fg(term_colors_t fg=DEFAULT_COLOR);
std::string term_attrib(term_attrib_t attrib=DEFAULT_ATTRIB);
std::string term_clear();
我曾尝试在 else 循环中使用堆栈命令,但在运行时收到分段错误。
我知道我要问的内容相对模糊,但我使用终端转义命令和枚举类型来模拟终端读取 HTML 的方式。
enum term_attrib_t {
DEFAULT_ATTRIB = '0',
BRIGHT = '1',
DIM = '2',
UNDERLINE = '4',
BLINK = '5',
REVERSE = '7',
HIDDEN = '8'
};
// the colors, background or foreground
enum term_colors_t {
BLACK = '0',
RED = '1',
GREEN = '2',
YELLOW = '3',
BLUE = '4',
MAGENTA = '5',
CYAN = '6',
WHITE = '7',
DEFAULT_COLOR = '9'
};