libhpdftbl 1.5.0
Table construction library for Haru PDF library
Loading...
Searching...
No Matches
hpdftbl_dump.c File Reference

Functions for json serializing of table data structure. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <hpdf.h>
#include "hpdftbl.h"
#include <jansson.h>

Macros

#define jsonprint(...)   if( _string_ ) { sprintf (_sbuff_, __VA_ARGS__);size_t _len_=xstrlcat(_jsonbuff_,_sbuff_,_jsonbuff_size_); if(_len_>=_jsonbuff_size_) return -1;} else {fprintf(fh,__VA_ARGS__);}
 
#define OUTJSON_NEWBLK()   do { jsonprint("%*s{\n", tab,""); } while(0)
 
#define OUTJSON_ENDDOC()   do { jsonprint( "}\n"); } while(0)
 
#define OUTJSON_INDENT()   do { jsonprint( "%*s", tab,""); } while(0)
 
#define OUTJSON_NEWLINE()   do { jsonprint( "\n"); } while(0)
 
#define OUTJSON_STRINT(k, v, e)   do { jsonprint( "%*s\"%s\": %d%c\n",tab,"",k,(int)v,e); } while(0)
 
#define OUTJSON_STRREAL(k, v, e)   do { jsonprint( "%*s\"%s\": %.8f%c\n",tab,"",k,v,e); } while(0)
 
#define OUTJSON_STRSTR(k, v)   do { if( v==NULL ) {jsonprint( "%*s\"%s\": \"\",\n",tab,"",k) } else { jsonprint( "%*s\"%s\": \"%s\",\n",tab,"",k,v);} } while(0)
 
#define OUTJSON_STRBLK(k)   do { jsonprint( "%*s\"%s\": {\n",tab,"",k); } while(0)
 
#define OUTJSON_STRLIST(k)   do { jsonprint( "%*s\"%s\": [\n",tab,"",k); } while(0)
 
#define OUTJSON_ENDLIST(e)   do { jsonprint( "\n%*s]%c\n",tab,"",e); } while(0)
 
#define OUTJSON_STARTBLK()   do { jsonprint( "%*s{\n",tab,""); } while(0)
 
#define OUTJSON_ENDBLK(c)   do { jsonprint( "%*s}%c\n",tab,"",c); } while(0)
 
#define OUTJSON_BOOL(k, v)   do { if(v) { jsonprint( "%*s\"%s\": true,\n",tab,"",k);} else { jsonprint( "%*s\"%s\": false,\n",tab,"",k);} } while(0)
 
#define OUTJSON_RGB(k, v)   do { jsonprint( "%*s\"%s\": [%.5f, %.5f, %.5f],\n",tab,"",k,v.r,v.g,v.b); } while(0)
 
#define OUTJSON_GRID(k, v)
 
#define OUTJSON_TXTSTYLE(k, v, c)
 

Functions

int hpdftbl_theme_dump (hpdftbl_theme_t *theme, char *filename)
 Serialize the specified theme structure to a named file. More...
 
int hpdftbl_theme_dumps (hpdftbl_theme_t *theme, char *buff, const size_t buffsize)
 Serialize theme structure to a string buffer. More...
 
int hpdftbl_dump (hpdftbl_t tbl, char *filename)
 Serialize a table structure as a JSON file. More...
 
int hpdftbl_dumps (hpdftbl_t tbl, char *buff, size_t buffsize)
 Serialize a table structure to a string buffer. More...
 

Detailed Description

Functions for json serializing of table data structure.

Author
Johan Persson (johan.nosp@m.162@.nosp@m.gmail.nosp@m..com)

Copyright (C) 2022 Johan Persson

See also
LICENSE

Released under the MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Macro Definition Documentation

◆ OUTJSON_GRID

#define OUTJSON_GRID (   k,
 
)
Value:
do { \
OUTJSON_STRBLK(k); \
tab += 2; \
OUTJSON_STRREAL("width",v.width,','); \
OUTJSON_RGB("color",v.color); \
OUTJSON_STRINT("dashstyle",v.line_dashstyle,' '); \
tab -= 2; \
OUTJSON_ENDBLK(','); \
} while(0)

◆ OUTJSON_TXTSTYLE

#define OUTJSON_TXTSTYLE (   k,
  v,
 
)
Value:
do { \
jsonprint( "%*s\"%s\": {\n",tab,"",k); \
tab += 2;\
OUTJSON_STRSTR("font",v.font); \
OUTJSON_STRREAL("fsize",v.fsize,',');\
OUTJSON_RGB("color",v.color); \
OUTJSON_RGB("background",v.background); \
OUTJSON_STRINT("halign",v.halign,' '); \
tab -= 2; \
OUTJSON_ENDBLK(c); \
} while(0)

Function Documentation

◆ hpdftbl_dump()

int hpdftbl_dump ( hpdftbl_t  tbl,
char *  filename 
)

Serialize a table structure as a JSON file.

The table is serialized as JSON file and have whitespaces and newlines to make it more human readable. The serialization is a complete representation of a table.

Parameters
tblTable handle
filenameFilename to write to. Any path specified must exists
Returns
-1 on failure, 0 on success

◆ hpdftbl_dumps()

int hpdftbl_dumps ( hpdftbl_t  tbl,
char *  buff,
size_t  buffsize 
)

Serialize a table structure to a string buffer.

The table is serialized as JSON and have whitespaces and newlines to make it more human readable. Note is is the callers responsibility to make sure the buffer is large enough to hold the serialized table.

Parameters
tblTable handle of table to dump
buffBuffer to dump structure to
buffsizeSize of buffer
Returns
-1 on failure, 0 on success
See also
hpdftbl_load(),hpdftbl_dump(), hpdftbl_stroke_pos(),

Referenced by hpdftbl_dump().

◆ hpdftbl_theme_dump()

int hpdftbl_theme_dump ( hpdftbl_theme_t theme,
char *  filename 
)

Serialize the specified theme structure to a named file.

The theme is serialized as JSON string array and have whitespaces and newlines to make it more human readable.

Parameters
themePointer to theme structure to be serialized
filenameFilename to write to
Returns
0 on success, -1 on failure

◆ hpdftbl_theme_dumps()

int hpdftbl_theme_dumps ( hpdftbl_theme_t theme,
char *  buff,
const size_t  buffsize 
)

Serialize theme structure to a string buffer.

The theme is serialized as JSON string array and have whitespaces and newlines to make it more human readable.

Parameters
themeTheme to serialize
buffBuffer to write serialized theme to. It should be a minimum of 2k chars.
buffsizeBuffer size (including ending string NULL)
Returns
0 on success, < 0 on failure

Referenced by hpdftbl_theme_dump().