94 lines
2.6 KiB
C++
94 lines
2.6 KiB
C++
#include <iostream>
|
|
#include <deque>
|
|
#include <string>
|
|
#include <algorithm>
|
|
#include "bbcodes.h"
|
|
|
|
using namespace std;
|
|
using namespace bbcode;
|
|
|
|
struct BBStack {
|
|
shared_ptr<BBEntry> head = make_shared<BBText>();
|
|
shared_ptr<BBEntry> tail = head;
|
|
|
|
string key;
|
|
string content;
|
|
};
|
|
|
|
shared_ptr<BBEntry> bbcode::parse(string message) {
|
|
string current_text;
|
|
string current_key;
|
|
|
|
deque<shared_ptr<BBStack>> stack;
|
|
stack.push_back(make_shared<BBStack>());
|
|
|
|
bool escaped = false;
|
|
bool key_begin = false;
|
|
bool key_end = false;
|
|
for(size_t i = 0; i < message.length(); i++) {
|
|
if(message[i] == '[' && (i == 0 || message[i - 1] != '\\')) {
|
|
if(!current_text.empty()) {
|
|
stack.back()->tail->next(make_shared<BBText>(current_text));
|
|
stack.back()->tail = stack.back()->tail->next();
|
|
}
|
|
if(i + 2 < message.length() && message[i + 1] == '/') { //Should be a key end
|
|
key_end = true;
|
|
i += 1;
|
|
continue;
|
|
} else {
|
|
key_begin = true;
|
|
cout << "Message: " << current_text << endl;
|
|
stack.push_back(make_shared<BBStack>());
|
|
continue;
|
|
}
|
|
} else if(message[i] == ']') {
|
|
if(key_begin) {
|
|
cout << "Got key begin '" << current_key << "'" << endl;
|
|
key_begin = false;
|
|
stack.back()->key = current_key;
|
|
current_key = "";
|
|
continue;
|
|
} else if(key_end) {
|
|
cout << "Got key end '" << current_key << "'" << endl;
|
|
key_end = false;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(key_begin || key_end) current_key += message[i];
|
|
else current_text += message[i];
|
|
}
|
|
|
|
return stack.front()->head;
|
|
}
|
|
|
|
bool bbcode::sloppy::has_tag(std::string message, std::deque<std::string> tag) {
|
|
std::transform(message.begin(), message.end(), message.begin(), ::tolower);
|
|
for(auto& entry : tag)
|
|
std::transform(entry.begin(), entry.end(), entry.begin(), ::tolower);
|
|
|
|
std::deque<std::string> begins;
|
|
size_t index = 0, found, length = 0;
|
|
do {
|
|
found = string::npos;
|
|
for(auto it = tag.begin(); it != tag.end() && found == string::npos; it++) {
|
|
found = message.find(*it, index);
|
|
length = it->length();
|
|
};
|
|
if(found > 0 && found + length < message.length()) {
|
|
if(message[found + length] == ']' || (message[found + length] == '=' && message.find(']', found + length) != string::npos)) {
|
|
if(message[found - 1] == '/') {
|
|
auto found_tag = message.substr(found, length);
|
|
for(const auto& entry : begins)
|
|
if(entry == found_tag) return true;
|
|
} else if(message[found - 1] == '[' && (found < 2 || message[found - 2] != '\\'))
|
|
begins.push_back(message.substr(found, length));
|
|
if(message[found + length] != ']')
|
|
found = message.find(']', found + length);
|
|
}
|
|
}
|
|
index = found + 1;
|
|
} while(index != 0);
|
|
|
|
return false;
|
|
} |