src/ccodegen.c

Thu, 23 Jan 2014 14:17:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Jan 2014 14:17:06 +0100
changeset 22
f463693b5eeb
parent 21
537aec525835
child 24
e43dee5892f4
permissions
-rw-r--r--

added command line parameters for header and footer file

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "ccodegen.h"
#include <string.h>
#include <ctype.h>

const char* ckeywords[] = {
    "auto", "break", "case", "char", "const", "continue", "default", "do",
    "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
    "long", "register", "return", "short", "signed", "sizeof", "static",
    "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
    "while", NULL
};

int isctype(char *word, size_t len) {
    return (word[len-2] == '_' && word[len-1] == 't');
}

int iscdirective(char *word) {
    return (word[0] == '#');
}

void cparseline(char *src, char *dest, highlighter_t *hltr) {
    size_t sp = 0, dp = 0;
    /* indent */
    while (isspace(src[sp])) {
        dest[dp++] = src[sp++];
    }

    memset(hltr->word, 0, WORDBUF_SIZE);
    size_t wp = 0, ifp = 0;
    int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    int isescaping = 0;

    if (hltr->iscommentml) {
        iscomment = 1;
        memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
        dp += 29;
    }

    for (char c = src[sp] ; c ; c=src[++sp]) {
        /* comments */
        if (c == '/') {
            if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
                iscomment = 0;
                hltr->iscommentml = 0;
                memcpy(&(dest[dp]), "/</span>", 8);
                dp += 8;
                continue;
            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                iscomment = 1;
                hltr->iscommentml = (src[sp+1] == '*');
                memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
                dp += 29;
            }
        }

        if (iscomment) {
            if (c == '\n') {
                memcpy(&(dest[dp]), "</span>", 7);
                dp += 7;
            }
            dp = writeescapedchar(dest, dp, c);
        } else if (isinclude) {
            if (c == '<') {
                memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32);
                dp += 32;
                dp = writeescapedchar(dest, dp, c);
            } else if (c == '\"') {
                if (parseinclude) {
                    dest[dp++] = '\"';
                    dest[dp++] = '>';
                    memcpy(&(dest[dp]), hltr->includefile, ifp);
                    dp += ifp;

                    dp = writeescapedchar(dest, dp, c);
                    memcpy(&(dest[dp]), "</a>", 4);
                    dp += 4;
                    parseinclude = 0;
                } else {
                    memcpy(&(dest[dp]),
                        "<a class=\"c2html-userinclude\" href=", 35);
                    dp += 35;
                    dp = writeescapedchar(dest, dp, c);
                    ifp = 0;
                    hltr->includefile[ifp++] = '\"';
                    parseinclude = 1;
                }
            } else if (c == '>') {
                dp = writeescapedchar(dest, dp, c);
                memcpy(&(dest[dp]), "</span>", 7);
                dp += 7;
            } else {
                if (parseinclude) {
                    hltr->includefile[ifp++] = c;
                }
                dp = writeescapedchar(dest, dp, c);
            }
        } else {
            /* strings */
            if (!isescaping && (c == '\'' || c == '\"')) {
                isstring ^= 1;
                if (isstring) {
                    memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
                    dp += 28;
                    dp = writeescapedchar(dest, dp, c);
                } else {
                    dp = writeescapedchar(dest, dp, c);
                    memcpy(&(dest[dp]), "</span>", 7);
                    dp += 7;
                }
            } else {
                if (isstring) {
                    dp = writeescapedchar(dest, dp, c);
                } else if (!iswordcharacter(c)) {
                    /* interpret word int_t */
                    if (wp > 0 && wp < WORDBUF_SIZE) {
                        int closespan = 1;
                        if (iskeyword(hltr->word, hltr->keywords)) {
                            memcpy(&(dest[dp]),
                                "<span class=\"c2html-keyword\">", 29);
                            dp += 29;
                        } else if (hltr->istype(hltr->word, wp)) {
                            memcpy(&(dest[dp]),
                                "<span class=\"c2html-type\">", 26);
                            dp += 26;
                        } else if (hltr->isdirective(hltr->word)) {
                            isinclude = !strncmp(
                                "#include", hltr->word, WORDBUF_SIZE);
                            memcpy(&(dest[dp]),
                                "<span class=\"c2html-directive\">", 31);
                            dp += 31;
                        } else if (iscapsonly(hltr->word, wp)) {
                            memcpy(&(dest[dp]),
                                "<span class=\"c2html-macroconst\">", 32);
                            dp += 32;
                        } else {
                            closespan = 0;
                        }
                        for (int i = 0 ; i < wp ; i++) {
                            dp = writeescapedchar(dest, dp, hltr->word[i]);
                        }
                        if (closespan) {
                            memcpy(&(dest[dp]), "</span>", 7);
                            dp += 7;
                        }
                    }
                    memset(hltr->word, 0, WORDBUF_SIZE);
                    wp = 0;
                    dp = writeescapedchar(dest, dp, c);
                } else {
                    /* read word */
                    if (wp < WORDBUF_SIZE) {
                        hltr->word[wp++] = c;
                    } else if (wp == WORDBUF_SIZE) {
                        for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
                            dp = writeescapedchar(dest, dp, hltr->word[i]);
                        }
                        wp++;
                        dp = writeescapedchar(dest, dp, c);
                    } else {
                        dp = writeescapedchar(dest, dp, c);
                    }
                }
            }

            isescaping = !isescaping & (c == '\\');
        }
    }
    dest[dp] = 0;
}

mercurial