improves API and adds functions for strings Version 2.0

Wed, 31 Aug 2016 16:20:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 31 Aug 2016 16:20:58 +0200
changeset 57
eba880c1705c
parent 56
81d99e9ceb20
child 58
f37984d9ab11

improves API and adds functions for strings

src/c2html.c file | annotate | diff | comparison | revisions
src/c2html.h file | annotate | diff | comparison | revisions
src/frontend.c file | annotate | diff | comparison | revisions
src/highlighter.c file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Wed Aug 31 14:47:01 2016 +0200
+++ b/src/c2html.c	Wed Aug 31 16:20:58 2016 +0200
@@ -102,17 +102,8 @@
     ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write,
             ibuf, ibuflen, (size_t)-1);
 
-    UcxList *lines = ucx_list_append(NULL, content->space);
-    for (size_t i = 1 ; i < content->size ; i++) {
-        if (content->space[i] == '\r') {
-            content->space[i] = '\n'; i++;
-        }
-        if (content->space[i] == '\n' && i+1 < content->size) {
-            ucx_list_append(lines, content->space+i+1);
-        }
-    }
-    
-    size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
+    size_t n = c2html_bformatn(content->space, content->size,
+            outputbuffer, wfnc, maxlen, hltr, showln);
     
     ucx_buffer_free(content);
     return n;
@@ -125,15 +116,44 @@
             outputbuffer, wfnc, (size_t)-1, hltr, showln);
 }
 
-size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen,
+size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen,
         void* outputbuffer, write_func wfnc,
         c2html_highlighter_func hltr, int showln) {
     return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
             outputbuffer, wfnc, hltr, showln);
 }
 
-void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen,
+void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen,
         FILE* outputfile, c2html_highlighter_func hltr, int showln) {
     c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
             outputfile, (write_func) fwrite, hltr, showln);
 }
+
+size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen,
+        void* outputbuffer, write_func wfnc,
+        size_t maxlen, c2html_highlighter_func hltr, int showln) {
+    UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer);
+    for (size_t i = 1 ; i < inputbuflen ; i++) {
+        if (inputbuffer[i] == '\n' && i+1 < inputbuflen) {
+            ucx_list_append(lines, (char*)inputbuffer+i+1);
+        }
+    }
+    
+    size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
+    
+    ucx_list_free(lines);
+    return n;
+}
+
+size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen,
+        void* outputbuffer, write_func wfnc,
+        c2html_highlighter_func hltr, int showln) {
+    return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc,
+            (size_t)-1, hltr, showln);
+}
+
+void c2html_bformatf(const char* inputbuffer, size_t inputbuflen,
+        FILE* outputfile, c2html_highlighter_func hltr, int showln) {
+    c2html_bformatn(inputbuffer, inputbuflen, outputfile,
+            (write_func) fwrite, (size_t)-1, hltr, showln);
+}
--- a/src/c2html.h	Wed Aug 31 14:47:01 2016 +0200
+++ b/src/c2html.h	Wed Aug 31 16:20:58 2016 +0200
@@ -39,19 +39,22 @@
     
 #define VERSION_MAJOR   2
 #define VERSION_MINOR   0
-#define VERSION_DEVELOP 1 /* set this to zero for release version */
+#define VERSION_DEVELOP 0 /* set this to zero for release version */
 
 /**
  * Reads a source file from the input buffer and writes at most
  * <code>maxlen</code> bytes of the formatted output to the output buffer.
  * 
  * The output buffer must either be large enough to hold <code>maxlen</code>
- * bytes or the write function must trigger an automatic extension of the buffer.
+ * bytes or the write function must trigger an automatic extension of the
+ * buffer.
+ * 
+ * The input is copied via an intermediate buffer to an internal buffer.
  * 
  * @param inputbuffer the input buffer
  * @param rfnc a read function operating for the input buffer
  * @param ibuf intermediate processing buffer
- * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
+ * @param ibuflen length of intermediate processing buffer
  * @param outputbuffer the output buffer
  * @param wfnc a write function for the output buffer
  * @param maxlen the maximum amount bytes which will be written to the
@@ -76,10 +79,12 @@
  * The output buffer must either be large enough to hold the formatted data
  * or the write function must trigger an automatic extension of the buffer.
  * 
+ * The input is copied via an intermediate buffer to an internal buffer.
+ * 
  * @param inputbuffer the input buffer
  * @param rfnc a read function operating for the input buffer
  * @param ibuf intermediate processing buffer
- * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
+ * @param ibuflen length of intermediate processing buffer
  * @param outputbuffer the output buffer
  * @param wfnc a write function for the output buffer
  * @param hltr the highlighter function
@@ -102,9 +107,13 @@
  * The output buffer must either be large enough to hold the formatted data
  * or the write function must trigger an automatic extension of the buffer.
  * 
+ * The input is copied via an intermediate buffer to an internal buffer.
+ * For files, the recommended intermediate buffer length is the file system
+ * block size.
+ * 
  * @param inputfile the input file stream
  * @param ibuf intermediate processing buffer
- * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
+ * @param ibuflen length of intermediate processing buffer
  * @param outputbuffer the output buffer
  * @param wfnc a write function for the output buffer
  * @param hltr the highlighter function
@@ -116,7 +125,7 @@
  * @see c2html_c_highlighter()
  * @see c2html_java_highlighter()
  */
-size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen,
+size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen,
         void* outputbuffer, write_func wfnc,
         c2html_highlighter_func hltr, int showln);
 
@@ -124,6 +133,9 @@
  * Reads a source file from the specified input file stream and directly writes
  * the formatted output to the output file stream.
  * 
+ * For files, the recommended intermediate buffer length is the file system
+ * block size.
+ * 
  * @param inputfile the input file stream
  * @param ibuf intermediate processing buffer
  * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
@@ -135,7 +147,69 @@
  * @see c2html_c_highlighter()
  * @see c2html_java_highlighter()
  */
-void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen,
+void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen,
+        FILE* outputfile, c2html_highlighter_func hltr, int showln);
+
+
+/**
+ * Writes at most <code>maxlen</code> bytes of formatted source data to the
+ * output buffer.
+ * 
+ * @param inputbuffer the source file data as string
+ * @param inputbuflen the length of the source file
+ * @param outputbuffer the output buffer
+ * @param wfnc a write function for the output buffer
+ * @param maxlen the maximum amount bytes which will be written to the
+ * output buffer
+ * @param hltr the highlighter function
+ * @param showln zero, if line numbers shall be omitted, nonzero otherwise
+ * 
+ * @return total amount of bytes written to the output buffer
+ * 
+ * @see c2html_plain_highlighter()
+ * @see c2html_c_highlighter()
+ * @see c2html_java_highlighter()
+ */
+size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen,
+        void* outputbuffer, write_func wfnc,
+        size_t maxlen, c2html_highlighter_func hltr, int showln);
+    
+
+/**
+ * Writes the formatted source data to the output buffer.
+ * 
+ * @param inputbuffer the source file data as string
+ * @param inputbuflen the length of the source file
+ * @param outputbuffer the output buffer
+ * @param wfnc a write function for the output buffer
+ * @param hltr the highlighter function
+ * @param showln zero, if line numbers shall be omitted, nonzero otherwise
+ * 
+ * @return total amount of bytes written to the output buffer
+ * 
+ * @see c2html_plain_highlighter()
+ * @see c2html_c_highlighter()
+ * @see c2html_java_highlighter()
+ */
+size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen,
+        void* outputbuffer, write_func wfnc,
+        c2html_highlighter_func hltr, int showln);
+
+
+/**
+ * Writes the formatted source data directly to the specified file stream.
+ * 
+ * @param inputbuffer the source file data as string
+ * @param inputbuflen the length of the source file
+ * @param outputfile the output file stream
+ * @param hltr the highlighter function
+ * @param showln zero, if line numbers shall be omitted, nonzero otherwise
+ * 
+ * @see c2html_plain_highlighter()
+ * @see c2html_c_highlighter()
+ * @see c2html_java_highlighter()
+ */
+void c2html_bformatf(const char* inputbuffer, size_t inputbuflen,
         FILE* outputfile, c2html_highlighter_func hltr, int showln);
 
 #ifdef __cplusplus
--- a/src/frontend.c	Wed Aug 31 14:47:01 2016 +0200
+++ b/src/frontend.c	Wed Aug 31 16:20:58 2016 +0200
@@ -158,7 +158,7 @@
         /* Process input file */
         FILE *inputfile = fopen(settings.infilename, "r");
         if (inputfile) {
-            c2html_fformat_file(
+            c2html_fformatf(
                     inputfile, filebuf, FILEBUF_SIZE,
                     fout, hltr, settings.showlinenumbers
             );
--- a/src/highlighter.c	Wed Aug 31 14:47:01 2016 +0200
+++ b/src/highlighter.c	Wed Aug 31 16:20:58 2016 +0200
@@ -73,9 +73,12 @@
 
 /* Plaintext Highlighter */
 
-void c2html_plain_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) {
+void c2html_plain_highlighter(char *src, UcxBuffer *dest,
+        c2html_highlighter_data *hd) {
     while (*src && *src != '\n') {
-        put_htmlescaped(dest, *src);
+        if (*src != '\r') {
+            put_htmlescaped(dest, *src);
+        }
         src++;
     }
     ucx_buffer_putc(dest, '\n');
@@ -91,7 +94,8 @@
     "while", NULL
 };
 
-void c2html_c_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) {
+void c2html_c_highlighter(char *src, UcxBuffer *dest,
+        c2html_highlighter_data *hd) {
     /* reset buffers without clearing them */
     hd->primary_buffer->size = hd->primary_buffer->pos = 0;
     hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
@@ -115,6 +119,7 @@
     char c;
     do {
         c = src[++sp];
+        if (c == '\r') continue;
         
         /* comments */
         if (!isstring && c == '/') {
@@ -234,7 +239,8 @@
     "volatile", "const", "float", "native", "super", "while", NULL
 };
 
-void c2html_java_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) {
+void c2html_java_highlighter(char *src, UcxBuffer *dest,
+        c2html_highlighter_data *hd) {
     /* reset buffers without clearing them */
     hd->primary_buffer->size = hd->primary_buffer->pos = 0;
     hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
@@ -256,6 +262,7 @@
     char c;
     do {
         c = src[++sp];
+        if (c == '\r') continue;
         
         /* comments */
         if (!isstring && c == '/') {

mercurial