私は Java を大まかに理解している C++ の初心者です。このような 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>
これは、私が使用しているコードの非常に小さなスニペットです。私の問題の例は、dim の書式設定をクリアし、終了タグに到達するまでテキストを赤のままにする方法がわからないことです (上記のコードの 1 行目)。
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'
};