"Fossies" - the Fresh Open Source Software Archive

Member "mlr-5.6.2/c/output/lrec_writer_xtab.c" (25 Aug 2019, 5439 Bytes) of package /linux/misc/mlr-5.6.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lrec_writer_xtab.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdlib.h>
    2 #include <string.h>
    3 #include "lib/mlrutil.h"
    4 #include "output/lrec_writers.h"
    5 
    6 // ----------------------------------------------------------------
    7 // Note: If OPS is single-character then we can do alignment of the form
    8 //
    9 //   ab  123
   10 //   def 4567
   11 //
   12 // On the other hand, if it's multi-character, we won't be able to align
   13 // neatly in all cases. Yet we do allow multi-character OPS, just without
   14 // repetition: if someone wants to use OPS ": " and format data as
   15 //
   16 //   ab: 123
   17 //   def: 4567
   18 //
   19 // then they can do that.
   20 // ----------------------------------------------------------------
   21 
   22 typedef struct _lrec_writer_xtab_state_t {
   23     char* ofs;
   24     char* ops;
   25     int   opslen;
   26     long long record_count;
   27     int   right_justify_value;
   28 } lrec_writer_xtab_state_t;
   29 
   30 static void lrec_writer_xtab_free(lrec_writer_t* pwriter, context_t* pctx);
   31 static void lrec_writer_xtab_process_aligned(void* pvstate, FILE* output_stream, lrec_t* prec, char* ofs);
   32 static void lrec_writer_xtab_process_aligned_auto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx);
   33 static void lrec_writer_xtab_process_aligned_nonauto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx);
   34 static void lrec_writer_xtab_process_unaligned(void* pvstate, FILE* output_stream, lrec_t* prec, char* ofs);
   35 static void lrec_writer_xtab_process_unaligned_auto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx);
   36 static void lrec_writer_xtab_process_unaligned_nonauto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx);
   37 
   38 // ----------------------------------------------------------------
   39 lrec_writer_t* lrec_writer_xtab_alloc(char* ofs, char* ops, int right_justify_value) {
   40     lrec_writer_t* plrec_writer = mlr_malloc_or_die(sizeof(lrec_writer_t));
   41 
   42     lrec_writer_xtab_state_t* pstate = mlr_malloc_or_die(sizeof(lrec_writer_xtab_state_t));
   43     pstate->ofs          = ofs;
   44     pstate->ops          = ops;
   45     pstate->opslen       = strlen(ops);
   46     pstate->record_count = 0LL;
   47     pstate->right_justify_value = right_justify_value;
   48 
   49     plrec_writer->pvstate = pstate;
   50     if (pstate->opslen == 1) {
   51         plrec_writer->pprocess_func = streq(ofs, "auto")
   52             ? lrec_writer_xtab_process_aligned_auto_ofs
   53             : lrec_writer_xtab_process_aligned_nonauto_ofs;
   54     } else {
   55         plrec_writer->pprocess_func = streq(ofs, "auto")
   56             ? lrec_writer_xtab_process_unaligned_auto_ofs
   57             : lrec_writer_xtab_process_unaligned_nonauto_ofs;
   58     }
   59     plrec_writer->pfree_func = lrec_writer_xtab_free;
   60 
   61     return plrec_writer;
   62 }
   63 
   64 static void lrec_writer_xtab_free(lrec_writer_t* pwriter, context_t* pctx) {
   65     free(pwriter->pvstate);
   66     free(pwriter);
   67 }
   68 
   69 // ----------------------------------------------------------------
   70 static void lrec_writer_xtab_process_aligned_auto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx) {
   71     lrec_writer_xtab_process_aligned(pvstate, output_stream, prec, pctx->auto_line_term);
   72 }
   73 
   74 static void lrec_writer_xtab_process_aligned_nonauto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx) {
   75     lrec_writer_xtab_state_t* pstate = pvstate;
   76     lrec_writer_xtab_process_aligned(pvstate, output_stream, prec, pstate->ofs);
   77 }
   78 
   79 static void lrec_writer_xtab_process_aligned(void* pvstate, FILE* output_stream, lrec_t* prec, char* ofs) {
   80     if (prec == NULL)
   81         return;
   82     lrec_writer_xtab_state_t* pstate = pvstate;
   83     if (pstate->record_count > 0LL)
   84         fputs(ofs, output_stream);
   85     pstate->record_count++;
   86 
   87     int max_key_width = 1;
   88     int max_value_width = 1;
   89     for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) {
   90         int key_width = strlen_for_utf8_display(pe->key);
   91         int value_width = strlen_for_utf8_display(pe->value);
   92         if (key_width > max_key_width)
   93             max_key_width = key_width;
   94         if (value_width > max_value_width)
   95             max_value_width = value_width;
   96     }
   97 
   98     for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) {
   99         // "%-*s" fprintf format isn't correct for non-ASCII UTF-8
  100         fprintf(output_stream, "%s", pe->key);
  101         int d = max_key_width - strlen_for_utf8_display(pe->key);
  102         for (int i = 0; i < d; i++)
  103             fputs(pstate->ops, output_stream);
  104 
  105         if (pstate->right_justify_value) {
  106             int d = max_value_width - strlen_for_utf8_display(pe->value);
  107             for (int i = 0; i < d; i++)
  108                 fputs(pstate->ops, output_stream);
  109         }
  110         fprintf(output_stream, "%s%s%s", pstate->ops, pe->value, ofs);
  111     }
  112     lrec_free(prec); // end of baton-pass
  113 }
  114 
  115 // ----------------------------------------------------------------
  116 static void lrec_writer_xtab_process_unaligned_auto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx) {
  117     lrec_writer_xtab_process_unaligned(pvstate, output_stream, prec, pctx->auto_line_term);
  118 }
  119 
  120 static void lrec_writer_xtab_process_unaligned_nonauto_ofs(void* pvstate, FILE* output_stream, lrec_t* prec, context_t* pctx) {
  121     lrec_writer_xtab_state_t* pstate = pvstate;
  122     lrec_writer_xtab_process_unaligned(pvstate, output_stream, prec, pstate->ofs);
  123 }
  124 
  125 static void lrec_writer_xtab_process_unaligned(void* pvstate, FILE* output_stream, lrec_t* prec, char* ofs) {
  126     if (prec == NULL)
  127         return;
  128     lrec_writer_xtab_state_t* pstate = pvstate;
  129     if (pstate->record_count > 0LL)
  130         fputs(ofs, output_stream);
  131     pstate->record_count++;
  132 
  133     for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) {
  134         // "%-*s" fprintf format isn't correct for non-ASCII UTF-8
  135         fprintf(output_stream, "%s%s%s%s", pe->key, pstate->ops, pe->value, ofs);
  136     }
  137     lrec_free(prec); // end of baton-pass
  138 }