replaces stack buffers with UCX buffers

Thu, 25 Aug 2016 12:16:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 25 Aug 2016 12:16:57 +0200
changeset 51
f25ba6fd7a08
parent 50
17408c3607ce
child 52
33ded421c512

replaces stack buffers with UCX buffers

src/c2html.c file | annotate | diff | comparison | revisions
src/ccodegen.c file | annotate | diff | comparison | revisions
src/ccodegen.h file | annotate | diff | comparison | revisions
src/codegens.c file | annotate | diff | comparison | revisions
src/codegens.h file | annotate | diff | comparison | revisions
src/javacodegen.c file | annotate | diff | comparison | revisions
src/javacodegen.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/c2html.c	Thu Aug 25 11:30:30 2016 +0200
     1.2 +++ b/src/c2html.c	Thu Aug 25 12:16:57 2016 +0200
     1.3 @@ -45,7 +45,7 @@
     1.4          "\n");
     1.5  }
     1.6  
     1.7 -static void plain_highlighter(char *src, UcxBuffer *dest, int* x) {
     1.8 +static void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
     1.9      while (*src && *src != '\n') {
    1.10          put_htmlescaped(dest, *src);
    1.11          src++;
    1.12 @@ -65,19 +65,17 @@
    1.13          while ((p*=10) < lines) lnw++;
    1.14      }
    1.15      
    1.16 -    /* allocate line buffer */
    1.17 -    UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    1.18 -    if(!line) {
    1.19 -        perror("Error allocating line buffer for output");
    1.20 -        return;
    1.21 -    }
    1.22 -    
    1.23      /* start monospace formatting */
    1.24      out("<pre>\n", 1, 6, stream);
    1.25  
    1.26      /* process lines */
    1.27      size_t lineno = 0;
    1.28 -    int multiline_comment = 0;
    1.29 +    HighlighterData *hd = new_highlighter_data();
    1.30 +    UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    1.31 +    if(!line || !hd) {
    1.32 +        perror("Error allocating buffer for output");
    1.33 +        return;
    1.34 +    }
    1.35      
    1.36      UCX_FOREACH(sourceline, in) {
    1.37          /* increase line number and clean line buffer */
    1.38 @@ -92,7 +90,7 @@
    1.39          }
    1.40          
    1.41          /* process code line */
    1.42 -        highlighter(sourceline->data, line, &multiline_comment);
    1.43 +        highlighter(sourceline->data, line, hd);
    1.44          
    1.45          /* write code line */
    1.46          out(line->space, 1, line->size, stream);
    1.47 @@ -102,6 +100,7 @@
    1.48      out("</pre>\n", 1, 7, stream);
    1.49      
    1.50      /* cleanup and return */
    1.51 +    free_highlighter_data(hd);
    1.52      ucx_buffer_free(line);
    1.53  }
    1.54  
     2.1 --- a/src/ccodegen.c	Thu Aug 25 11:30:30 2016 +0200
     2.2 +++ b/src/ccodegen.c	Thu Aug 25 12:16:57 2016 +0200
     2.3 @@ -37,24 +37,23 @@
     2.4      "while", NULL
     2.5  };
     2.6  
     2.7 -void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
     2.8 -    /* TODO: try to replace these buffers */
     2.9 -    char wordbuf[WORDBUF_SIZE];
    2.10 -    sstr_t word;
    2.11 -    word.ptr = wordbuf; word.length = 0;
    2.12 +void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
    2.13 +    /* reset buffers without clearing them */
    2.14 +    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    2.15 +    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    2.16      
    2.17 -    char includefilebuf[512];
    2.18 -    sstr_t includefile;
    2.19 -    includefile.ptr = includefilebuf;
    2.20 -    includefile.length = 0;
    2.21 +    /* alias the buffers for better handling */
    2.22 +    UcxBuffer *wbuf = hd->primary_buffer;
    2.23 +    UcxBuffer *ifilebuf = hd->secondary_buffer;
    2.24      
    2.25 +    /* local information */
    2.26      size_t sp = (size_t)-1;
    2.27      int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    2.28      char quote = '\0';
    2.29      int isescaping = 0;
    2.30      
    2.31      /* continue a multi line comment highlighting */
    2.32 -    if (*multiline_comment) {
    2.33 +    if (hd->multiline_comment) {
    2.34          iscomment = 1;
    2.35          ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    2.36      }
    2.37 @@ -66,14 +65,14 @@
    2.38          
    2.39          /* comments */
    2.40          if (!isstring && c == '/') {
    2.41 -            if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    2.42 +            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
    2.43                  iscomment = 0;
    2.44 -                *multiline_comment = 0;
    2.45 +                hd->multiline_comment = 0;
    2.46                  ucx_buffer_puts(dest, "/</span>");
    2.47                  continue;
    2.48              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    2.49                  iscomment = 1;
    2.50 -                *multiline_comment = (src[sp+1] == '*');
    2.51 +                hd->multiline_comment = (src[sp+1] == '*');
    2.52                  ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    2.53              }
    2.54          }
    2.55 @@ -90,21 +89,21 @@
    2.56                          "<span class=\"c2html-stdinclude\">&lt;");
    2.57              } else if (c == '\"') {
    2.58                  if (parseinclude) {
    2.59 -                    ucx_bprintf(dest, "\">%.*s\"</a>",
    2.60 -                            includefile.length, includefile.ptr);
    2.61 +                    ucx_buffer_puts(dest, "\">");
    2.62 +                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
    2.63 +                    ucx_buffer_puts(dest, "\"</a>");
    2.64                      parseinclude = 0;
    2.65                  } else {
    2.66                      ucx_buffer_puts(dest,
    2.67                              "<a class=\"c2html-userinclude\" href=\"");
    2.68 -                    includefile.length = 0;
    2.69 -                    includefile.ptr[includefile.length++] = '\"';
    2.70 +                    ucx_buffer_putc(ifilebuf, '\"');
    2.71                      parseinclude = 1;
    2.72                  }
    2.73              } else if (c == '>') {
    2.74                  ucx_buffer_puts(dest,  "&gt;</span>");
    2.75              } else {
    2.76                  if (parseinclude) {
    2.77 -                    includefile.ptr[includefile.length++] = c;
    2.78 +                    ucx_buffer_putc(ifilebuf, c);
    2.79                  }
    2.80                  put_htmlescaped(dest, c);
    2.81              }
    2.82 @@ -128,8 +127,10 @@
    2.83              } else {
    2.84                  if (isstring) {
    2.85                      put_htmlescaped(dest, c);
    2.86 -                } else if (!isalnum(c) && c!='_' && c!='#' && c!='@') {
    2.87 -                    if (word.length > 0 && word.length < WORDBUF_SIZE) {
    2.88 +                } else if (!isalnum(c) && c!='_' && c!='#') {
    2.89 +                    /* write buffered word, if any */
    2.90 +                    if (wbuf->size > 0) {
    2.91 +                        sstr_t word = sstrn(wbuf->space, wbuf->size);
    2.92                          int closespan = 1;
    2.93                          sstr_t typesuffix = ST("_t");
    2.94                          if (check_keyword(word, ckeywords)) {
    2.95 @@ -153,20 +154,13 @@
    2.96                              ucx_buffer_puts(dest, "</span>");
    2.97                          }
    2.98                      }
    2.99 -                    word.length = 0;
   2.100 +                    wbuf->pos = wbuf->size = 0; /* reset word buffer */
   2.101 +                    
   2.102 +                    /* write current character */
   2.103                      put_htmlescaped(dest, c);
   2.104                  } else {
   2.105 -                    /* read word */
   2.106 -                    if (word.length < WORDBUF_SIZE) {
   2.107 -                        word.ptr[word.length++] = c;
   2.108 -                    } else if (word.length == WORDBUF_SIZE) {
   2.109 -                        /* TODO: this will be removed */
   2.110 -                        ucx_buffer_puts(dest,
   2.111 -                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
   2.112 -                        word.length = 0;
   2.113 -                    } else {
   2.114 -                        put_htmlescaped(dest, c);
   2.115 -                    }
   2.116 +                    /* buffer the current word */
   2.117 +                    ucx_buffer_putc(wbuf, c);
   2.118                  }
   2.119              }
   2.120  
     3.1 --- a/src/ccodegen.h	Thu Aug 25 11:30:30 2016 +0200
     3.2 +++ b/src/ccodegen.h	Thu Aug 25 12:16:57 2016 +0200
     3.3 @@ -36,7 +36,7 @@
     3.4  extern "C" {
     3.5  #endif
     3.6  
     3.7 -void c_highlighter(char *src, UcxBuffer *dest, int *mlc);
     3.8 +void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
     3.9  
    3.10  #ifdef	__cplusplus
    3.11  }
     4.1 --- a/src/codegens.c	Thu Aug 25 11:30:30 2016 +0200
     4.2 +++ b/src/codegens.c	Thu Aug 25 12:16:57 2016 +0200
     4.3 @@ -29,6 +29,24 @@
     4.4  
     4.5  #include "codegens.h"
     4.6  
     4.7 +HighlighterData* new_highlighter_data() {
     4.8 +    HighlighterData* hd = malloc(sizeof(HighlighterData));
     4.9 +    if (hd == NULL) {
    4.10 +        return NULL;
    4.11 +    } else {
    4.12 +        hd->multiline_comment = 0;
    4.13 +        hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
    4.14 +        hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
    4.15 +        return hd;
    4.16 +    }
    4.17 +}
    4.18 +
    4.19 +void free_highlighter_data(HighlighterData *hd) {
    4.20 +    ucx_buffer_free(hd->primary_buffer);
    4.21 +    ucx_buffer_free(hd->secondary_buffer);
    4.22 +    free(hd);
    4.23 +}
    4.24 +
    4.25  void put_htmlescaped(UcxBuffer *dest, char c) {
    4.26      if (c == '>') {
    4.27          ucx_buffer_puts(dest, "&gt;");
     5.1 --- a/src/codegens.h	Thu Aug 25 11:30:30 2016 +0200
     5.2 +++ b/src/codegens.h	Thu Aug 25 12:16:57 2016 +0200
     5.3 @@ -41,10 +41,17 @@
     5.4  #ifdef	__cplusplus
     5.5  extern "C" {
     5.6  #endif
     5.7 +    
     5.8 +typedef struct {
     5.9 +    int multiline_comment;
    5.10 +    UcxBuffer* primary_buffer;
    5.11 +    UcxBuffer* secondary_buffer;
    5.12 +} HighlighterData;
    5.13  
    5.14 -#define WORDBUF_SIZE 256
    5.15 +HighlighterData* new_highlighter_data();
    5.16 +void free_highlighter_data(HighlighterData*);
    5.17  
    5.18 -typedef void(*highlighter_func)(char*,UcxBuffer*,int*);
    5.19 +typedef void(*highlighter_func)(char*,UcxBuffer*,HighlighterData*);
    5.20  
    5.21  void put_htmlescaped(UcxBuffer *dest, char c);
    5.22  void put_htmlescapedstr(UcxBuffer *dest, sstr_t s);
     6.1 --- a/src/javacodegen.c	Thu Aug 25 11:30:30 2016 +0200
     6.2 +++ b/src/javacodegen.c	Thu Aug 25 12:16:57 2016 +0200
     6.3 @@ -39,19 +39,21 @@
     6.4      "volatile", "const", "float", "native", "super", "while", NULL
     6.5  };
     6.6  
     6.7 -void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
     6.8 +void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
     6.9 +    /* reset buffers without clearing them */
    6.10 +    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    6.11 +    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    6.12  
    6.13 -    /* TODO: try to replace this buffer */
    6.14 -    char wordbuf[WORDBUF_SIZE];
    6.15 -    sstr_t word;
    6.16 -    word.ptr = wordbuf; word.length = 0;
    6.17 +    /* alias the buffers for better handling */
    6.18 +    UcxBuffer *wbuf = hd->primary_buffer;
    6.19      
    6.20 +    /* local information */
    6.21      size_t sp = (size_t)-1;
    6.22      int isstring = 0, iscomment = 0, isimport = 0;
    6.23      char quote = '\0';
    6.24      int isescaping = 0;
    6.25  
    6.26 -    if (*multiline_comment) {
    6.27 +    if (hd->multiline_comment) {
    6.28          iscomment = 1;
    6.29          ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    6.30      }
    6.31 @@ -63,14 +65,14 @@
    6.32          
    6.33          /* comments */
    6.34          if (!isstring && c == '/') {
    6.35 -            if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    6.36 +            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
    6.37                  iscomment = 0;
    6.38 -                *multiline_comment = 0;
    6.39 +                hd->multiline_comment = 0;
    6.40                  ucx_buffer_puts(dest, "/</span>");
    6.41                  continue;
    6.42              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    6.43                  iscomment = 1;
    6.44 -                *multiline_comment = (src[sp+1] == '*');
    6.45 +                hd->multiline_comment = (src[sp+1] == '*');
    6.46                  ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    6.47              }
    6.48          }
    6.49 @@ -104,8 +106,10 @@
    6.50              } else {
    6.51                  if (isstring) {
    6.52                      put_htmlescaped(dest, c);
    6.53 -                } else if (!isalnum(c) && c!='_' && c!='#' && c!='@') {
    6.54 -                    if (word.length > 0 && word.length < WORDBUF_SIZE) {
    6.55 +                } else if (!isalnum(c) && c!='_' && c!='@') {
    6.56 +                    /* write buffered word, if any */
    6.57 +                    if (wbuf->size > 0) {
    6.58 +                        sstr_t word = sstrn(wbuf->space, wbuf->size);
    6.59                          int closespan = 1;
    6.60                          if (check_keyword(word, jkeywords)) {
    6.61                              ucx_buffer_puts(dest,
    6.62 @@ -128,20 +132,13 @@
    6.63                              ucx_buffer_puts(dest, "</span>");
    6.64                          }
    6.65                      }
    6.66 -                    word.length = 0;
    6.67 +                    wbuf->pos = wbuf->size = 0; /* reset buffer */
    6.68 +                    
    6.69 +                    /* write current character */
    6.70                      put_htmlescaped(dest, c);
    6.71                  } else {
    6.72 -                    /* read word */
    6.73 -                    if (word.length < WORDBUF_SIZE) {
    6.74 -                        word.ptr[word.length++] = c;
    6.75 -                    } else if (word.length == WORDBUF_SIZE) {
    6.76 -                        /* TODO: this will be removed */
    6.77 -                        ucx_buffer_puts(dest,
    6.78 -                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
    6.79 -                        word.length = 0;
    6.80 -                    } else {
    6.81 -                        put_htmlescaped(dest, c);
    6.82 -                    }
    6.83 +                    /* buffer the current word */
    6.84 +                    ucx_buffer_putc(wbuf, c);
    6.85                  }
    6.86              }
    6.87  
     7.1 --- a/src/javacodegen.h	Thu Aug 25 11:30:30 2016 +0200
     7.2 +++ b/src/javacodegen.h	Thu Aug 25 12:16:57 2016 +0200
     7.3 @@ -36,7 +36,7 @@
     7.4  extern "C" {
     7.5  #endif
     7.6  
     7.7 -void java_highlighter(char *src, UcxBuffer *dest, int *mlc);
     7.8 +void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
     7.9  
    7.10  #ifdef	__cplusplus
    7.11  }

mercurial