src/ccodegen.c

changeset 48
b2724c711203
parent 47
c39ecbbca7c0
child 49
f86f0b054464
     1.1 --- a/src/ccodegen.c	Tue Aug 23 16:34:02 2016 +0200
     1.2 +++ b/src/ccodegen.c	Tue Aug 23 17:24:58 2016 +0200
     1.3 @@ -28,6 +28,7 @@
     1.4   */
     1.5  
     1.6  #include "ccodegen.h"
     1.7 +#include "ucx/utils.h"
     1.8  #include <string.h>
     1.9  #include <ctype.h>
    1.10  
    1.11 @@ -39,13 +40,7 @@
    1.12      "while", NULL
    1.13  };
    1.14  
    1.15 -#define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
    1.16 -                                    dp += sizeof(str)-1
    1.17 -
    1.18 -void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
    1.19 -    /* TODO: workaround for using old code with UcxBuffer */
    1.20 -    char *dest = destbuf->space + destbuf->pos;
    1.21 -
    1.22 +void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
    1.23      /* TODO: try to replace these buffers */
    1.24      char wordbuf[WORDBUF_SIZE];
    1.25      sstr_t word;
    1.26 @@ -56,7 +51,7 @@
    1.27      includefile.ptr = includefilebuf;
    1.28      includefile.length = 0;
    1.29      
    1.30 -    size_t sp = (size_t)-1, dp = 0;
    1.31 +    size_t sp = (size_t)-1;
    1.32      int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    1.33      char quote = '\0';
    1.34      int isescaping = 0;
    1.35 @@ -64,7 +59,7 @@
    1.36      /* continue a multi line comment highlighting */
    1.37      if (*multiline_comment) {
    1.38          iscomment = 1;
    1.39 -        memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    1.40 +        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    1.41      }
    1.42  
    1.43      char c;
    1.44 @@ -77,113 +72,103 @@
    1.45              if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    1.46                  iscomment = 0;
    1.47                  *multiline_comment = 0;
    1.48 -                memcpy_const(dest, dp, "/</span>");
    1.49 +                ucx_buffer_puts(dest, "/</span>");
    1.50                  continue;
    1.51              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    1.52                  iscomment = 1;
    1.53                  *multiline_comment = (src[sp+1] == '*');
    1.54 -                memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    1.55 +                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    1.56              }
    1.57          }
    1.58  
    1.59          if (iscomment) {
    1.60              if (c == '\n') {
    1.61 -                memcpy_const(dest, dp,  "</span>");
    1.62 +                ucx_buffer_puts(dest, "</span>\n");
    1.63 +            } else {
    1.64 +                put_htmlescaped(dest, c);
    1.65              }
    1.66 -            dp = writeescapedchar(dest, dp, c);
    1.67          } else if (isinclude) {
    1.68              if (c == '<') {
    1.69 -                memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">");
    1.70 -                dp = writeescapedchar(dest, dp, c);
    1.71 +                ucx_buffer_puts(dest,
    1.72 +                        "<span class=\"c2html-stdinclude\">&lt;");
    1.73              } else if (c == '\"') {
    1.74                  if (parseinclude) {
    1.75 -                    dest[dp++] = '\"';
    1.76 -                    dest[dp++] = '>';
    1.77 -                    memcpy(&(dest[dp]), includefile.ptr, includefile.length);
    1.78 -                    dp += includefile.length;
    1.79 -
    1.80 -                    dp = writeescapedchar(dest, dp, c);
    1.81 -                    memcpy_const(dest, dp, "</a>");
    1.82 +                    ucx_bprintf(dest, "\">%.*s\"</a>",
    1.83 +                            includefile.length, includefile.ptr);
    1.84                      parseinclude = 0;
    1.85                  } else {
    1.86 -                    memcpy_const(dest, dp,
    1.87 -                        "<a class=\"c2html-userinclude\" href=");
    1.88 -                    dp = writeescapedchar(dest, dp, c);
    1.89 +                    ucx_buffer_puts(dest,
    1.90 +                            "<a class=\"c2html-userinclude\" href=\"");
    1.91                      includefile.length = 0;
    1.92                      includefile.ptr[includefile.length++] = '\"';
    1.93                      parseinclude = 1;
    1.94                  }
    1.95              } else if (c == '>') {
    1.96 -                dp = writeescapedchar(dest, dp, c);
    1.97 -                memcpy_const(dest, dp, "</span>");
    1.98 +                ucx_buffer_puts(dest,  "&gt;</span>");
    1.99              } else {
   1.100                  if (parseinclude) {
   1.101                      includefile.ptr[includefile.length++] = c;
   1.102                  }
   1.103 -                dp = writeescapedchar(dest, dp, c);
   1.104 +                put_htmlescaped(dest, c);
   1.105              }
   1.106          } else {
   1.107              /* strings */
   1.108              if (!isescaping && (c == '\'' || c == '\"')) {
   1.109                  if (isstring) {
   1.110 -                    dp = writeescapedchar(dest, dp, c);
   1.111 +                    put_htmlescaped(dest, c);
   1.112                      if (c == quote) {
   1.113                          isstring = 0;
   1.114 -                        memcpy_const(dest, dp, "</span>");
   1.115 +                        ucx_buffer_puts(dest, "</span>");
   1.116                      } else {
   1.117 -                        dp = writeescapedchar(dest, dp, c);
   1.118 +                        put_htmlescaped(dest, c);
   1.119                      }
   1.120                  } else {
   1.121                      isstring = 1;
   1.122                      quote = c;
   1.123 -                    memcpy_const(dest, dp,
   1.124 -                        "<span class=\"c2html-string\">");
   1.125 -                    dp = writeescapedchar(dest, dp, c);
   1.126 +                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
   1.127 +                    put_htmlescaped(dest, c);
   1.128                  }
   1.129              } else {
   1.130                  if (isstring) {
   1.131 -                    dp = writeescapedchar(dest, dp, c);
   1.132 -                } else if (!iswordcharacter(c)) {
   1.133 +                    put_htmlescaped(dest, c);
   1.134 +                } else if (!check_alnumex(c)) {
   1.135                      if (word.length > 0 && word.length < WORDBUF_SIZE) {
   1.136                          int closespan = 1;
   1.137                          sstr_t typesuffix = ST("_t");
   1.138                          if (check_keyword(word, ckeywords)) {
   1.139 -                            memcpy_const(dest, dp, 
   1.140 -                                "<span class=\"c2html-keyword\">");
   1.141 +                            ucx_buffer_puts(dest,
   1.142 +                                    "<span class=\"c2html-keyword\">");
   1.143                          } else if (sstrsuffix(word, typesuffix)) {
   1.144 -                            memcpy_const(dest, dp, 
   1.145 +                            ucx_buffer_puts(dest,
   1.146                                  "<span class=\"c2html-type\">");
   1.147                          } else if (word.ptr[0] == '#') {
   1.148                              isinclude = !sstrcmp(word, S("#include"));
   1.149 -                            memcpy_const(dest, dp, 
   1.150 +                            ucx_buffer_puts(dest,
   1.151                                  "<span class=\"c2html-directive\">");
   1.152                          } else if (check_capsonly(word)) {
   1.153 -                            memcpy_const(dest, dp, 
   1.154 +                            ucx_buffer_puts(dest,
   1.155                                  "<span class=\"c2html-macroconst\">");
   1.156                          } else {
   1.157                              closespan = 0;
   1.158                          }
   1.159 -                        for (int i = 0 ; i < word.length ; i++) {
   1.160 -                            dp = writeescapedchar(dest, dp, word.ptr[i]);
   1.161 -                        }
   1.162 +                        put_htmlescapedstr(dest, word);
   1.163                          if (closespan) {
   1.164 -                            memcpy_const(dest, dp, "</span>");
   1.165 +                            ucx_buffer_puts(dest, "</span>");
   1.166                          }
   1.167                      }
   1.168                      word.length = 0;
   1.169 -                    dp = writeescapedchar(dest, dp, c);
   1.170 +                    put_htmlescaped(dest, c);
   1.171                  } else {
   1.172                      /* read word */
   1.173                      if (word.length < WORDBUF_SIZE) {
   1.174                          word.ptr[word.length++] = c;
   1.175                      } else if (word.length == WORDBUF_SIZE) {
   1.176 -                        for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   1.177 -                            dp = writeescapedchar(dest, dp, word.ptr[i]);
   1.178 -                        }
   1.179 -                        word.length++;
   1.180 -                        dp = writeescapedchar(dest, dp, c);
   1.181 +                        /* TODO: this will be removed */
   1.182 +                        ucx_buffer_puts(dest,
   1.183 +                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
   1.184 +                        word.length = 0;
   1.185                      } else {
   1.186 -                        dp = writeescapedchar(dest, dp, c);
   1.187 +                        put_htmlescaped(dest, c);
   1.188                      }
   1.189                  }
   1.190              }
   1.191 @@ -191,9 +176,4 @@
   1.192              isescaping = !isescaping & (c == '\\');
   1.193          }
   1.194      } while (c != '\n');
   1.195 -    dest[dp] = 0;
   1.196 -    
   1.197 -    /* TODO: workaround */
   1.198 -    destbuf->pos += dp;
   1.199 -    destbuf->size += dp;
   1.200  }

mercurial