Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const char __attribute__ ((hot)) *ConvertBBCode(char *Output, const char *Input, int BufSize) {
- if(!strchr(Input, '[')) return Input; // no conversion necessary
- const char *TagName[] = {"b]","i]","u]","s]","sub]","sup]","url=","url]","icon]","user]","color=","color]","noparse]",NULL};
- const int TagLen[] = {2, 2, 2, 2, 4, 4, 4, 4, 5, 5, 6, 6, 8}; // 5+ treated specially
- const char *TagEndName[] = {"[/b]","[/i]","[/u]","[/s]","[/sub]","[/sup]","[/url]","[/url]","[/icon]","[/user]","[/color]",NULL,"[/noparse]"};
- const int TagCodes[] = {0x02, 0x1d, 0x1f, 128+FORMAT_STRIKEOUT, 128+FORMAT_SUBSCRIPT, 128+FORMAT_SUPERSCRIPT,
- 128+FORMAT_URL_LINK, 128+FORMAT_URL_LINK, 128+FORMAT_ICON_CMDLINK, 128+FORMAT_COMMAND_LINK,
- 128+FORMAT_CANCEL_COLORS, 128+FORMAT_CANCEL_COLORS};
- int TagEnabled[] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
- char *Out = Output;
- int i; // tag index to try
- while(*Input) {
- char c = *(Input++);
- if(c != '[') // if not [ just copy it
- *(Out++) = c;
- else
- if(*Input == '/') { // cancel formatting
- for(i=0;TagName[i];i++)
- if(!memcmp(Input+1, TagName[i], TagLen[i])) {
- if(TagEnabled[i]) {
- Input+=TagLen[i]+1;
- if(TagCodes[i] & 128) // need escape code?
- *(Out++) = 0xff;
- *(Out++) = TagCodes[i]&127;
- TagEnabled[i] = 0;
- } else {
- *(Out++) = '['; // tag isn't enabled, print it
- }
- break;
- }
- if(!TagName[i]) // put the [ if not a valid tag
- *(Out++) = '[';
- } else { // start formatting
- for(i=0;TagName[i];i++)
- if(!memcmp(Input, TagName[i], TagLen[i])) {
- if(!TagEndName[i] || !strstr(Input, TagEndName[i])) { // if tag isn't closed, tag should be visible
- *(Out++) = '['; // add the [ back
- break;
- }
- Input+=TagLen[i];
- if(TagLen[i] < 6) { // 6+ handle outputting on their own
- if(TagCodes[i] & 128) // need escape code?
- *(Out++) = 0xff;
- *(Out++) = TagCodes[i]&127;
- if(i!=6)
- TagEnabled[i] = 1;
- else // url= has to set url]
- TagEnabled[i+1] = 1;
- }
- // special handling for lengths of at least 5
- if(TagLen[i]>=5) {
- if(TagLen[i]==5) { // [icon] or [user], stick in a command name
- strcpy(Out, "char ");
- Out+=5;
- } else if(TagLen[i]==6) { // [color]
- const char *End = strchr(Input, ']');
- char ColorName[End-Input+1];
- strlcpy(ColorName, Input, sizeof(ColorName));
- Input = End+1;
- if(*ColorName == '#') { // hex color
- *(Out++) = 0x04;
- if(strlen(ColorName) == 4) { // convert 3 digit to 6 digit
- for(int j=1;j<4;j++) { // double every digit
- *(Out++)=ColorName[j];
- *(Out++)=ColorName[j];
- }
- } else { // assume 6 digits
- strcpy(Out, ColorName+1);
- Out+=6;
- }
- } else {
- for(int j=0;FChatColorNames[j];j++)
- if(!strcasecmp(ColorName, FChatColorNames[j])) {
- sprintf(ColorName, "\3%.2d", j+16); // F-Chat colors start at 16
- strcpy(Out, ColorName);
- Out+=3;
- break;
- }
- }
- TagEnabled[i+1] = 1;
- } else { // [noparse]
- const char *End = strstr(Input, "[/noparse]");
- memcpy(Out, Input, End-Input);
- Out+=(End-Input)+10;
- }
- }
- break;
- }
- if(!TagName[i]) // put the [ if not a valid tag
- *(Out++) = '[';
- }
- }
- *Out = 0;
- return Output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement