Tue, 23 Aug 2016 13:59:48 +0200
fixes plaintext output
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2016 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 "javacodegen.h" #include <string.h> #include <ctype.h> const char* jkeywords[] = { "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized", "boolean", "do", "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", NULL }; int check_jtype(char *word, size_t len) { return isupper(word[0]); } int check_jdirective(char *word) { return word[0] == '@'; } #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ dp += sizeof(str)-1 void jparseline(char *src, char *dest, highlighter_t *hltr) { memset(hltr->word, 0, WORDBUF_SIZE); size_t wp = 0, sp = (size_t)-1, dp = 0; int isstring = 0, iscomment = 0, isimport = 0; char quote = '\0'; int isescaping = 0; if (hltr->iscommentml) { iscomment = 1; memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); } char c; do { c = src[++sp]; if (!c) break; /* comments */ if (!isstring && c == '/') { if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { iscomment = 0; hltr->iscommentml = 0; memcpy_const(dest, dp, "/</span>"); continue; } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { iscomment = 1; hltr->iscommentml = (src[sp+1] == '*'); memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); } } if (iscomment) { if (c == '\n') { memcpy(&(dest[dp]), "</span>", 7); dp += 7; } dp = writeescapedchar(dest, dp, c); } else if (isimport) { // TODO: local imports } else { /* strings */ if (!isescaping && (c == '\'' || c == '\"')) { if (isstring) { dp = writeescapedchar(dest, dp, c); if (c == quote) { isstring = 0; memcpy_const(dest, dp, "</span>"); } else { dp = writeescapedchar(dest, dp, c); } } else { isstring = 1; quote = c; memcpy_const(dest, dp, "<span class=\"c2html-string\">"); dp = writeescapedchar(dest, dp, c); } } 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 (check_keyword(hltr->word, hltr->keywords)) { memcpy_const(dest, dp, "<span class=\"c2html-keyword\">"); } else if (hltr->istype(hltr->word, wp)) { memcpy_const(dest, dp, "<span class=\"c2html-type\">"); } else if (hltr->isdirective(hltr->word)) { memcpy_const(dest, dp, "<span class=\"c2html-directive\">"); } else if (check_capsonly(hltr->word, wp)) { memcpy_const(dest, dp, "<span class=\"c2html-macroconst\">"); } else { closespan = 0; } for (int i = 0 ; i < wp ; i++) { dp = writeescapedchar(dest, dp, hltr->word[i]); } if (closespan) { memcpy_const(dest, dp, "</span>"); } } 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 == '\\'); } } while (c != '\n'); dest[dp] = 0; }