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 2007-12-28 15:40

OP here
>>10
Only these two? I forgot more, intentionally.
(eg # or rem or br)
That's why I put that comment there.
Left it as an exercise to the reader :P

>>11
Hard-on for purely stack-allocated memory? I hope your fucking overly revered stack fucking overflows when you shit some nice large arrays on it because you were too fucking pussy to dynamically allocate them on the heap.
Too much of a fucking pussy?
Are you right on your mind you fucking moron?
Not only it would make the size of my code 3x bigger, do you want such snippet to eat 10MB from your memory because the programmer wasn't a pussy?
You're a moron, what I could have done is to read input in chunks, less than what it takes for my static stack to fill, parse, print on stdout and repeat.
No fucking need for (re)allocations.

>>12
Release code?
Do you see a licence there? Or my name? Or some makefile?
(why the fuck would anyone put a licence on that anyway)
What I wanted to do is write something that works in C in < 10 minutes, post it on /prog/ and see what code Anon would post.
(like your lisp snippet)
Your lisp snippet however takes the expression as a list, my snip takes it from stdin, there are advantages to my snip. (not that it would take a lot of code to change yours to do that)

>>15
Actually I see more reasons to use a local object than threads :P

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