Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C] lisp to bbcode [BBCODE] [FAGGOTRY]

Name: Anonymous 2007-12-27 22:52


#include <stdio.h>
#include <string.h>

#define MAX_NESTING 128
#define MAX_POST_SIZE 4096

typedef struct {

    int id;
    const char * name;

} Tags;

typedef struct {

    int memory[MAX_NESTING];
    int * ptr;
    size_t elements;

} Stack;

void stackinit(Stack *);
int pushid(Stack *, int);
int popid(Stack *);

#define pushid(x,y) ((x)->elements++ == MAX_NESTING ? -1 : (*(x)->ptr++ = y))
#define popid(x) ((x)->elements ? (x)->elements--,*--(x)->ptr : 0)
#define stackinit(x) (void)((x)->elements = 0, (x)->ptr = (x)->memory)

int main(void) {

    char buf[MAX_POST_SIZE] = {0};
    char * p = buf;
    int c;
    size_t n;
    Stack stack;

    Tags tags[] = {
        { 0, ")"},
        { 1, "br"},
        { 2, "b"},
        { 3, "i"},
        { 4, "u"},
        { 5, "o"},
        { 6, "code"},
        { 7, "spoiler"} /*add more tags */
    };

    stackinit(&stack);


    fread(buf, 1, sizeof buf - 1, stdin);
    while(*p != 0) {
        if(*p == '\\' && p[1]) {
            putchar(p[1]);
            p += 2;
            continue;
        } else if(*p == '(') {
            if(p[1] == ' ') {
                p++;
                continue;
            }

            if(strtok(++p, " \n") == NULL) {
                fputs(p, stdout);
                break;
            } else {
                printf("[%s]", p);
               
                for(n = 0; n < sizeof tags / sizeof tags[0]; n++)
                    if(strcmp(p, tags[n].name) == 0)
                        if(pushid(&stack, tags[n].id) == -1) {
                            fprintf(stderr, "max nesting level reached\n");
                            return -1;
                        }
                p += strlen(p);
            }
        }
        else if(*p == ')') {
            c = popid(&stack);
            for(n = 0; n < sizeof tags / sizeof tags[0]; n++)
                if(c == tags[n].id) {
                    c ? printf("[/%s]", tags[n].name) : putchar(*tags[n].name);
                    break;
                }
        }

        else putchar(*p);

        p++;
    }

    putchar('\n');


    return 0;

}


Example:

$ cc foo.c -o a
$ ./a
(i (b this is bold and italic) and this just italic)
^D
[i][b]this is bold and italic[/b] and this just italic[/i]

Name: Anonymous 2008-01-01 7:08

>>74
What? I thought cat file.txt|less was the way to go. Sometimes I get so excited, I run less file.txt|less.

Name: Anonymous 2008-01-01 17:04

>>79
Oh Sussman, I lolled.

Name: Anonymous 2008-01-01 17:06

>>80
Uhm,
WELL UHM UHM UHM, UHM UHM, UHM UHM UHM.

Name: Anonymous 2008-01-01 17:24

Player appears to have no name on the leaderboard or in kill messages.
Name in status command appears as "& &"

Name: Anonymous 2008-01-01 17:32

>>1
Compiles with no problems with Microsoft Visual Studio 8.

lol.

Name: Anonymous 2008-01-01 17:48

They're up to VS9 now.

Name: Anonymous 2008-01-02 0:20

>>85
well that makes sense.. it's valid C89

Name: Anonymous 2008-01-02 8:38

We had 'solutions' in C,lisp,pcre but not haskell.
haskellfags, where's your monad now?!

Name: Anonymous 2008-01-02 17:24

>>87
i lol'd

Name: Anonymous 2008-01-02 19:45

Name: Anonymous 2009-02-25 8:08

Favorite musical genre chiptunes   if that counts   socket connects and   disconnects Keyword is.

Name: Anonymous 2010-11-14 19:24


[/br]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List