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
     1.1 --- a/src/c2html.c	Wed Aug 31 14:47:01 2016 +0200
     1.2 +++ b/src/c2html.c	Wed Aug 31 16:20:58 2016 +0200
     1.3 @@ -102,17 +102,8 @@
     1.4      ucx_stream_copy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write,
     1.5              ibuf, ibuflen, (size_t)-1);
     1.6  
     1.7 -    UcxList *lines = ucx_list_append(NULL, content->space);
     1.8 -    for (size_t i = 1 ; i < content->size ; i++) {
     1.9 -        if (content->space[i] == '\r') {
    1.10 -            content->space[i] = '\n'; i++;
    1.11 -        }
    1.12 -        if (content->space[i] == '\n' && i+1 < content->size) {
    1.13 -            ucx_list_append(lines, content->space+i+1);
    1.14 -        }
    1.15 -    }
    1.16 -    
    1.17 -    size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
    1.18 +    size_t n = c2html_bformatn(content->space, content->size,
    1.19 +            outputbuffer, wfnc, maxlen, hltr, showln);
    1.20      
    1.21      ucx_buffer_free(content);
    1.22      return n;
    1.23 @@ -125,15 +116,44 @@
    1.24              outputbuffer, wfnc, (size_t)-1, hltr, showln);
    1.25  }
    1.26  
    1.27 -size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen,
    1.28 +size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen,
    1.29          void* outputbuffer, write_func wfnc,
    1.30          c2html_highlighter_func hltr, int showln) {
    1.31      return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
    1.32              outputbuffer, wfnc, hltr, showln);
    1.33  }
    1.34  
    1.35 -void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen,
    1.36 +void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen,
    1.37          FILE* outputfile, c2html_highlighter_func hltr, int showln) {
    1.38      c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
    1.39              outputfile, (write_func) fwrite, hltr, showln);
    1.40  }
    1.41 +
    1.42 +size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen,
    1.43 +        void* outputbuffer, write_func wfnc,
    1.44 +        size_t maxlen, c2html_highlighter_func hltr, int showln) {
    1.45 +    UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer);
    1.46 +    for (size_t i = 1 ; i < inputbuflen ; i++) {
    1.47 +        if (inputbuffer[i] == '\n' && i+1 < inputbuflen) {
    1.48 +            ucx_list_append(lines, (char*)inputbuffer+i+1);
    1.49 +        }
    1.50 +    }
    1.51 +    
    1.52 +    size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
    1.53 +    
    1.54 +    ucx_list_free(lines);
    1.55 +    return n;
    1.56 +}
    1.57 +
    1.58 +size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen,
    1.59 +        void* outputbuffer, write_func wfnc,
    1.60 +        c2html_highlighter_func hltr, int showln) {
    1.61 +    return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc,
    1.62 +            (size_t)-1, hltr, showln);
    1.63 +}
    1.64 +
    1.65 +void c2html_bformatf(const char* inputbuffer, size_t inputbuflen,
    1.66 +        FILE* outputfile, c2html_highlighter_func hltr, int showln) {
    1.67 +    c2html_bformatn(inputbuffer, inputbuflen, outputfile,
    1.68 +            (write_func) fwrite, (size_t)-1, hltr, showln);
    1.69 +}
     2.1 --- a/src/c2html.h	Wed Aug 31 14:47:01 2016 +0200
     2.2 +++ b/src/c2html.h	Wed Aug 31 16:20:58 2016 +0200
     2.3 @@ -39,19 +39,22 @@
     2.4      
     2.5  #define VERSION_MAJOR   2
     2.6  #define VERSION_MINOR   0
     2.7 -#define VERSION_DEVELOP 1 /* set this to zero for release version */
     2.8 +#define VERSION_DEVELOP 0 /* set this to zero for release version */
     2.9  
    2.10  /**
    2.11   * Reads a source file from the input buffer and writes at most
    2.12   * <code>maxlen</code> bytes of the formatted output to the output buffer.
    2.13   * 
    2.14   * The output buffer must either be large enough to hold <code>maxlen</code>
    2.15 - * bytes or the write function must trigger an automatic extension of the buffer.
    2.16 + * bytes or the write function must trigger an automatic extension of the
    2.17 + * buffer.
    2.18 + * 
    2.19 + * The input is copied via an intermediate buffer to an internal buffer.
    2.20   * 
    2.21   * @param inputbuffer the input buffer
    2.22   * @param rfnc a read function operating for the input buffer
    2.23   * @param ibuf intermediate processing buffer
    2.24 - * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
    2.25 + * @param ibuflen length of intermediate processing buffer
    2.26   * @param outputbuffer the output buffer
    2.27   * @param wfnc a write function for the output buffer
    2.28   * @param maxlen the maximum amount bytes which will be written to the
    2.29 @@ -76,10 +79,12 @@
    2.30   * The output buffer must either be large enough to hold the formatted data
    2.31   * or the write function must trigger an automatic extension of the buffer.
    2.32   * 
    2.33 + * The input is copied via an intermediate buffer to an internal buffer.
    2.34 + * 
    2.35   * @param inputbuffer the input buffer
    2.36   * @param rfnc a read function operating for the input buffer
    2.37   * @param ibuf intermediate processing buffer
    2.38 - * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
    2.39 + * @param ibuflen length of intermediate processing buffer
    2.40   * @param outputbuffer the output buffer
    2.41   * @param wfnc a write function for the output buffer
    2.42   * @param hltr the highlighter function
    2.43 @@ -102,9 +107,13 @@
    2.44   * The output buffer must either be large enough to hold the formatted data
    2.45   * or the write function must trigger an automatic extension of the buffer.
    2.46   * 
    2.47 + * The input is copied via an intermediate buffer to an internal buffer.
    2.48 + * For files, the recommended intermediate buffer length is the file system
    2.49 + * block size.
    2.50 + * 
    2.51   * @param inputfile the input file stream
    2.52   * @param ibuf intermediate processing buffer
    2.53 - * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
    2.54 + * @param ibuflen length of intermediate processing buffer
    2.55   * @param outputbuffer the output buffer
    2.56   * @param wfnc a write function for the output buffer
    2.57   * @param hltr the highlighter function
    2.58 @@ -116,7 +125,7 @@
    2.59   * @see c2html_c_highlighter()
    2.60   * @see c2html_java_highlighter()
    2.61   */
    2.62 -size_t c2html_format_file(FILE* inputfile, char *ibuf, size_t ibuflen,
    2.63 +size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen,
    2.64          void* outputbuffer, write_func wfnc,
    2.65          c2html_highlighter_func hltr, int showln);
    2.66  
    2.67 @@ -124,6 +133,9 @@
    2.68   * Reads a source file from the specified input file stream and directly writes
    2.69   * the formatted output to the output file stream.
    2.70   * 
    2.71 + * For files, the recommended intermediate buffer length is the file system
    2.72 + * block size.
    2.73 + * 
    2.74   * @param inputfile the input file stream
    2.75   * @param ibuf intermediate processing buffer
    2.76   * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
    2.77 @@ -135,7 +147,69 @@
    2.78   * @see c2html_c_highlighter()
    2.79   * @see c2html_java_highlighter()
    2.80   */
    2.81 -void c2html_fformat_file(FILE *inputfile, char *ibuf, size_t ibuflen,
    2.82 +void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen,
    2.83 +        FILE* outputfile, c2html_highlighter_func hltr, int showln);
    2.84 +
    2.85 +
    2.86 +/**
    2.87 + * Writes at most <code>maxlen</code> bytes of formatted source data to the
    2.88 + * output buffer.
    2.89 + * 
    2.90 + * @param inputbuffer the source file data as string
    2.91 + * @param inputbuflen the length of the source file
    2.92 + * @param outputbuffer the output buffer
    2.93 + * @param wfnc a write function for the output buffer
    2.94 + * @param maxlen the maximum amount bytes which will be written to the
    2.95 + * output buffer
    2.96 + * @param hltr the highlighter function
    2.97 + * @param showln zero, if line numbers shall be omitted, nonzero otherwise
    2.98 + * 
    2.99 + * @return total amount of bytes written to the output buffer
   2.100 + * 
   2.101 + * @see c2html_plain_highlighter()
   2.102 + * @see c2html_c_highlighter()
   2.103 + * @see c2html_java_highlighter()
   2.104 + */
   2.105 +size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen,
   2.106 +        void* outputbuffer, write_func wfnc,
   2.107 +        size_t maxlen, c2html_highlighter_func hltr, int showln);
   2.108 +    
   2.109 +
   2.110 +/**
   2.111 + * Writes the formatted source data to the output buffer.
   2.112 + * 
   2.113 + * @param inputbuffer the source file data as string
   2.114 + * @param inputbuflen the length of the source file
   2.115 + * @param outputbuffer the output buffer
   2.116 + * @param wfnc a write function for the output buffer
   2.117 + * @param hltr the highlighter function
   2.118 + * @param showln zero, if line numbers shall be omitted, nonzero otherwise
   2.119 + * 
   2.120 + * @return total amount of bytes written to the output buffer
   2.121 + * 
   2.122 + * @see c2html_plain_highlighter()
   2.123 + * @see c2html_c_highlighter()
   2.124 + * @see c2html_java_highlighter()
   2.125 + */
   2.126 +size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen,
   2.127 +        void* outputbuffer, write_func wfnc,
   2.128 +        c2html_highlighter_func hltr, int showln);
   2.129 +
   2.130 +
   2.131 +/**
   2.132 + * Writes the formatted source data directly to the specified file stream.
   2.133 + * 
   2.134 + * @param inputbuffer the source file data as string
   2.135 + * @param inputbuflen the length of the source file
   2.136 + * @param outputfile the output file stream
   2.137 + * @param hltr the highlighter function
   2.138 + * @param showln zero, if line numbers shall be omitted, nonzero otherwise
   2.139 + * 
   2.140 + * @see c2html_plain_highlighter()
   2.141 + * @see c2html_c_highlighter()
   2.142 + * @see c2html_java_highlighter()
   2.143 + */
   2.144 +void c2html_bformatf(const char* inputbuffer, size_t inputbuflen,
   2.145          FILE* outputfile, c2html_highlighter_func hltr, int showln);
   2.146  
   2.147  #ifdef __cplusplus
     3.1 --- a/src/frontend.c	Wed Aug 31 14:47:01 2016 +0200
     3.2 +++ b/src/frontend.c	Wed Aug 31 16:20:58 2016 +0200
     3.3 @@ -158,7 +158,7 @@
     3.4          /* Process input file */
     3.5          FILE *inputfile = fopen(settings.infilename, "r");
     3.6          if (inputfile) {
     3.7 -            c2html_fformat_file(
     3.8 +            c2html_fformatf(
     3.9                      inputfile, filebuf, FILEBUF_SIZE,
    3.10                      fout, hltr, settings.showlinenumbers
    3.11              );
     4.1 --- a/src/highlighter.c	Wed Aug 31 14:47:01 2016 +0200
     4.2 +++ b/src/highlighter.c	Wed Aug 31 16:20:58 2016 +0200
     4.3 @@ -73,9 +73,12 @@
     4.4  
     4.5  /* Plaintext Highlighter */
     4.6  
     4.7 -void c2html_plain_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) {
     4.8 +void c2html_plain_highlighter(char *src, UcxBuffer *dest,
     4.9 +        c2html_highlighter_data *hd) {
    4.10      while (*src && *src != '\n') {
    4.11 -        put_htmlescaped(dest, *src);
    4.12 +        if (*src != '\r') {
    4.13 +            put_htmlescaped(dest, *src);
    4.14 +        }
    4.15          src++;
    4.16      }
    4.17      ucx_buffer_putc(dest, '\n');
    4.18 @@ -91,7 +94,8 @@
    4.19      "while", NULL
    4.20  };
    4.21  
    4.22 -void c2html_c_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) {
    4.23 +void c2html_c_highlighter(char *src, UcxBuffer *dest,
    4.24 +        c2html_highlighter_data *hd) {
    4.25      /* reset buffers without clearing them */
    4.26      hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    4.27      hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    4.28 @@ -115,6 +119,7 @@
    4.29      char c;
    4.30      do {
    4.31          c = src[++sp];
    4.32 +        if (c == '\r') continue;
    4.33          
    4.34          /* comments */
    4.35          if (!isstring && c == '/') {
    4.36 @@ -234,7 +239,8 @@
    4.37      "volatile", "const", "float", "native", "super", "while", NULL
    4.38  };
    4.39  
    4.40 -void c2html_java_highlighter(char *src, UcxBuffer *dest, c2html_highlighter_data *hd) {
    4.41 +void c2html_java_highlighter(char *src, UcxBuffer *dest,
    4.42 +        c2html_highlighter_data *hd) {
    4.43      /* reset buffers without clearing them */
    4.44      hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    4.45      hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    4.46 @@ -256,6 +262,7 @@
    4.47      char c;
    4.48      do {
    4.49          c = src[++sp];
    4.50 +        if (c == '\r') continue;
    4.51          
    4.52          /* comments */
    4.53          if (!isstring && c == '/') {

mercurial