changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet

Tue, 23 Aug 2016 15:28:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 15:28:56 +0200
changeset 45
1f3835182aeb
parent 44
2b4ac35d061d
child 46
534a4ef4143d

changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet

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.h file | annotate | diff | comparison | revisions
src/javacodegen.c file | annotate | diff | comparison | revisions
src/javacodegen.h file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Tue Aug 23 15:07:29 2016 +0200
+++ b/src/c2html.c	Tue Aug 23 15:28:56 2016 +0200
@@ -47,10 +47,23 @@
         "\n");
 }
 
+/* TODO: remove this workaround after refactoring highlighter structure */
+static void plainparseline(char *src, UcxBuffer *dest, highlighter_t* hltr) {
+    size_t dp = 0;
+    char *buf = dest->space + dest->pos;
+    while (*src && *src != '\n') {
+        dp = writeescapedchar(buf, dp, *src);
+        src++;
+    }
+    buf[dp++] = '\n';
+    buf[dp] = '\0';
+    dest->pos += dp;
+    dest->size += dp;
+}
+
 int formatfile(
         highlighter_t *highlighter,
-        UcxList *in,
-        write_func out, void *stream,
+        UcxList *in, write_func out, void *stream,
         int showlineno) {
     
     /* compute width of line numbering */
@@ -85,23 +98,8 @@
                     lineno, lineno, lnw, lineno);
         }
         
-        /* TODO: backwards compatibility: replace line->space in all occasions
-         * and use UcxBuffer functions
-         */
-        char *buf = line->space + line->pos;
-        if (highlighter) {
-            highlighter->parser(sourceline->data, buf, highlighter);
-        } else {
-            char *c = sourceline->data;
-            size_t dp = 0;
-            while (*c && *c != '\n') {
-                dp = writeescapedchar(buf, dp, *c);
-                c++;
-            }
-            buf[dp++] = '\n';
-            buf[dp] = '\0';
-        }
-        line->size = strlen(line->space);
+        /* process code line */
+        highlighter->parser(sourceline->data, line, highlighter);
         
         /* write code line */
         out(line->space, 1, line->size, stream);
@@ -115,22 +113,6 @@
     return 0;
 }
 
-void init_c_highlighter(highlighter_t *highlighter) {
-    memset(highlighter, 0, sizeof(highlighter_t));
-    highlighter->isdirective = check_cdirective;
-    highlighter->istype = check_ctype;
-    highlighter->keywords = ckeywords;
-    highlighter->parser = cparseline;
-}
-
-void init_java_highlighter(highlighter_t *highlighter) {
-    memset(highlighter, 0, sizeof(highlighter_t));
-    highlighter->isdirective = check_jdirective;
-    highlighter->istype = check_jtype;
-    highlighter->keywords = jkeywords;
-    highlighter->parser = jparseline;
-}
-
 #define FILEBUF_SIZE 4096
 
 enum source_type {
@@ -192,17 +174,22 @@
         return EXIT_FAILURE;
     } else {
         /* Configure highlighter */
-        highlighter_t highlighter;
-        highlighter_t *hptr = &highlighter;
+        highlighter_t *highlighter = calloc(1, sizeof(highlighter_t));
         switch (sourcetype) {
             case SOURCE_C:
-                init_c_highlighter(&highlighter);
+                highlighter->isdirective = check_cdirective;
+                highlighter->istype = check_ctype;
+                highlighter->keywords = ckeywords;
+                highlighter->parser = cparseline;
                 break;
             case SOURCE_JAVA:
-                init_java_highlighter(&highlighter);
+                highlighter->isdirective = check_jdirective;
+                highlighter->istype = check_jtype;
+                highlighter->keywords = jkeywords;
+                highlighter->parser = jparseline;
                 break;
             case SOURCE_PLAIN:
-                hptr = NULL;
+                highlighter->parser = plainparseline;
                 break;
             default: /* should be unreachable */
                 fprintf(stderr, "error in enum source_type\n");
@@ -268,11 +255,13 @@
             }
             
             formatfile(
-                    hptr,
+                    highlighter,
                     inputlines,
                     (write_func) fwrite,
                     fout,
                     settings.showlinenumbers);
+            
+            free(highlighter);
             ucx_buffer_free(content);
         } else {
             perror("Error opening input file");
--- a/src/ccodegen.c	Tue Aug 23 15:07:29 2016 +0200
+++ b/src/ccodegen.c	Tue Aug 23 15:28:56 2016 +0200
@@ -50,7 +50,9 @@
 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
                                     dp += sizeof(str)-1
 
-void cparseline(char *src, char *dest, highlighter_t *hltr) {
+void cparseline(char *src, UcxBuffer *destbuf, highlighter_t *hltr) {
+    /* TODO: workaround for using old code with UcxBuffer */
+    char *dest = destbuf->space + destbuf->pos;
 
     memset(hltr->word, 0, WORDBUF_SIZE);
     size_t wp = 0, ifp = 0, sp = (size_t)-1, dp = 0;
@@ -191,4 +193,8 @@
         }
     } while (c != '\n');
     dest[dp] = 0;
+    
+    /* TODO: workaround */
+    destbuf->pos += dp;
+    destbuf->size += dp;
 }
--- a/src/ccodegen.h	Tue Aug 23 15:07:29 2016 +0200
+++ b/src/ccodegen.h	Tue Aug 23 15:28:56 2016 +0200
@@ -41,7 +41,7 @@
 
 int check_ctype(char *word, size_t len);
 int check_cdirective(char *word);
-void cparseline(char *src, char *dest, highlighter_t *hltr);
+void cparseline(char *src, UcxBuffer *dest, highlighter_t *hltr);
 
 #ifdef	__cplusplus
 }
--- a/src/codegens.h	Tue Aug 23 15:07:29 2016 +0200
+++ b/src/codegens.h	Tue Aug 23 15:28:56 2016 +0200
@@ -31,6 +31,7 @@
 #define	CODEGENS_H
 
 #include <stdio.h>
+#include "ucx/buffer.h"
 
 #ifdef	__cplusplus
 extern "C" {
@@ -41,7 +42,7 @@
 #define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
     
 typedef struct _highlighter_t highlighter_t;
-typedef void(*parser_fnc)(char*,char*,highlighter_t*);
+typedef void(*parser_fnc)(char*,UcxBuffer*,highlighter_t*);
 
 struct _highlighter_t {
     const char** keywords;
--- a/src/javacodegen.c	Tue Aug 23 15:07:29 2016 +0200
+++ b/src/javacodegen.c	Tue Aug 23 15:28:56 2016 +0200
@@ -52,7 +52,10 @@
 #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) {
+void jparseline(char *src, UcxBuffer *destbuf, highlighter_t *hltr) {
+    /* TODO: workaround for using old code with UcxBuffer */
+    char *dest = destbuf->space + destbuf->pos;
+    
     memset(hltr->word, 0, WORDBUF_SIZE);
     size_t wp = 0, sp = (size_t)-1, dp = 0;
     int isstring = 0, iscomment = 0, isimport = 0;
@@ -161,4 +164,8 @@
         }
     } while (c != '\n');
     dest[dp] = 0;
+    
+    /* TODO: workaround */
+    destbuf->pos += dp;
+    destbuf->size += dp;
 }
--- a/src/javacodegen.h	Tue Aug 23 15:07:29 2016 +0200
+++ b/src/javacodegen.h	Tue Aug 23 15:28:56 2016 +0200
@@ -41,7 +41,7 @@
 
 int check_jtype(char *word, size_t len);
 int check_jdirective(char *word);
-void jparseline(char *src, char *dest, highlighter_t *hltr);
+void jparseline(char *src, UcxBuffer *dest, highlighter_t *hltr);
 
 
 #ifdef	__cplusplus

mercurial