/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4paper.ctr */ /* Copyright (C) 2016-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file dk4paper.c The dk4paper module. */ #line 288 "dk4paper.ctr" #include "dk4mem.h" #include "dk4strd.h" #include "dk4paper.h" #include "dk4maiddbl.h" #include "dk4fs.h" #include "dk4mpl.h" #include "dk4tspdk.h" #include "dk4strm.h" #include "dk4strmr.h" #include "dk4enc.h" #include "dk4loc.h" #include "dk4user.h" #include "dk4inst.h" #line 306 "dk4paper.ctr" /** Object passed as pointer to the line handler. */ typedef struct { dk4_paper_size_collection_t *coll; /**< Collection to modify. */ } reader_obj_t; /** Configuration files to search for. */ static const dkChar * const conf_file_names[] = { dkT("dk4paper.conf"), dkT("dk3paper.conf") }; /** Default paper size names always defined. */ static const dkChar *default_paper_sizes[] = { /* 0 */ dkT("A4"), /* 1 */ dkT("a4"), /* 2 */ dkT("Letter"), /* 3 */ dkT("letter"), NULL #line 335 "dk4paper.ctr" }; void dk4papersize_delete(dk4_paper_size_t *pps) { #line 343 "dk4paper.ctr" if (NULL != pps) { #line 344 "dk4paper.ctr" if (NULL != pps->name) { #line 345 "dk4paper.ctr" #line 346 "dk4paper.ctr" dk4mem_free(pps->name); pps->name = NULL; } dk4mem_free(pps); } #if TRACE_DEBUG else { #line 353 "dk4paper.ctr" } #endif #line 356 "dk4paper.ctr" } dk4_paper_size_t * dk4papersize_new(const dkChar *name, dk4_er_t *erp) { dk4_paper_size_t *back = NULL; #line 365 "dk4paper.ctr" if (NULL != name) { back = dk4mem_new(dk4_paper_size_t,1,erp); if (NULL != back) { back->w = 0.0; back->h = 0.0; back->i = 0.0; back->o = 0.0; back->t = 0.0; back->b = 0.0; back->name = dk4str_dup(name, erp); if (NULL == back->name) { dk4mem_free(back); back = NULL; } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 385 "dk4paper.ctr" return back; } void dk4papersize_coll_close(dk4_paper_size_collection_t *ptr) { dk4_paper_size_t *pps; #line 396 "dk4paper.ctr" if (NULL != ptr) { #line 397 "dk4paper.ctr" if (NULL != ptr->s_ps) { #line 398 "dk4paper.ctr" if (NULL != ptr->i_ps) { #line 399 "dk4paper.ctr" dk4sto_it_reset(ptr->i_ps); do { pps = (dk4_paper_size_t *)dk4sto_it_next(ptr->i_ps); if (NULL != pps) { #line 403 "dk4paper.ctr" dk4papersize_delete(pps); } } while (NULL != pps); dk4sto_it_close(ptr->i_ps); ptr->i_ps = NULL; } #if TRACE_DEBUG else { #line 411 "dk4paper.ctr" } #endif dk4sto_close(ptr->s_ps); ptr->s_ps = NULL; } #if TRACE_DEBUG else { #line 418 "dk4paper.ctr" } #endif #line 421 "dk4paper.ctr" dk4mem_free(ptr); } #if TRACE_DEBUG else { #line 425 "dk4paper.ctr" } #endif #line 428 "dk4paper.ctr" } int dk4papersize_cmp(const void *l, const void *r, int cr) { const dk4_paper_size_t *pl = NULL; const dk4_paper_size_t *pr = NULL; const dkChar *nm = NULL; int back = 0; if (NULL != l) { if (NULL != r) { pl = (const dk4_paper_size_t *)l; switch (cr) { case 1 : { nm = (const dkChar *)r; back = dk4str_cmp(pl->name, nm); } break; default : { pr = (const dk4_paper_size_t *)r; back = dk4str_cmp(pl->name, pr->name); } break; } } else { back = 1; } } else { if (NULL != r) { back = -1; } } return back; } static int dk4papersize_i_add_one_default_size( dk4_paper_size_collection_t *pscptr, const dkChar *name, double w, double h, double i, double o, double t, double b, dk4_er_t *erp ) { dk4_paper_size_t *ptr; int back = 0; #line 486 "dk4paper.ctr" ptr = dk4papersize_new(name, erp); if (NULL != ptr) { #line 488 "dk4paper.ctr" ptr->w = w; ptr->h = h; ptr->i = i; ptr->o = o; ptr->t = t; ptr->b = b; #line 490 "dk4paper.ctr" #line 491 "dk4paper.ctr" #line 492 "dk4paper.ctr" #line 493 "dk4paper.ctr" #line 494 "dk4paper.ctr" #line 495 "dk4paper.ctr" if (0 != dk4sto_add(pscptr->s_ps, ptr, erp)) { back = 1; #line 497 "dk4paper.ctr" } else { #line 499 "dk4paper.ctr" dk4papersize_delete(ptr); ptr = NULL; } } #if TRACE_DEBUG else { #line 505 "dk4paper.ctr" } #endif #line 508 "dk4paper.ctr" return back; } static int dk4papersize_i_add_default_sizes( dk4_paper_size_collection_t *pscptr, dk4_er_t *erp ) { int back = 0; #line 522 "dk4paper.ctr" back = dk4papersize_i_add_one_default_size( pscptr, default_paper_sizes[0], 595.0, 842.0, 0.0, 0.0, 0.0, 0.0, erp ); if (0 == back) { goto finished; } back = dk4papersize_i_add_one_default_size( pscptr, default_paper_sizes[1], 595.0, 842.0, 56.0, 28.0, 14.0, 14.0, erp ); if (0 == back) { goto finished; } back = dk4papersize_i_add_one_default_size( pscptr, default_paper_sizes[2], 612.0, 792.0, 0.0, 0.0, 0.0, 0.0, erp ); if (0 == back) { goto finished; } back = dk4papersize_i_add_one_default_size( pscptr, default_paper_sizes[3], 612.0, 792.0, 56.0, 28.0, 14.0, 14.0, erp ); if (0 == back) { goto finished; } back = 1; finished: #line 546 "dk4paper.ctr" return back; } dk4_paper_size_collection_t * dk4papersize_coll_open(dk4_er_t *erp) { dk4_paper_size_collection_t *back = NULL; int ok = 0; #line 558 "dk4paper.ctr" back = dk4mem_new(dk4_paper_size_collection_t,1,erp); if (NULL != back) { back->i_ps = NULL; back->s_ps = dk4sto_open(erp); if (NULL != back->s_ps) { (void)dk4sto_set_comp(back->s_ps, dk4papersize_cmp, 0); back->i_ps = dk4sto_it_open(back->s_ps, erp); if (NULL != back->i_ps) { ok = dk4papersize_i_add_default_sizes(back, erp); } } if (0 == ok) { dk4papersize_coll_close(back); back = NULL; } } #line 574 "dk4paper.ctr" return back; } const dk4_paper_size_t * dk4papersize_coll_find( dk4_paper_size_collection_t *ptr, const dkChar *name, dk4_er_t *erp ) { dk4_paper_size_t *back = NULL; #line 589 "dk4paper.ctr" if ((NULL != ptr) && (NULL != name)) { back = (dk4_paper_size_t *)dk4sto_it_find_like(ptr->i_ps, name, 1); if (NULL == back) { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 598 "dk4paper.ctr" return ((const dk4_paper_size_t *)back); } double dk4papersize_get_width(const dk4_paper_size_t *psptr) { double back = -1.0; if (NULL != psptr) { back = psptr->w; } return back; } double dk4papersize_get_height(const dk4_paper_size_t *psptr) { double back = -1.0; if (NULL != psptr) { back = psptr->h; } return back; } double dk4papersize_get_left_border(const dk4_paper_size_t *psptr) { double back = -1.0; if (NULL != psptr) { back = psptr->i; } return back; } double dk4papersize_get_right_border(const dk4_paper_size_t *psptr) { double back = -1.0; if (NULL != psptr) { back = psptr->o; } return back; } double dk4papersize_get_top_border(const dk4_paper_size_t *psptr) { double back = -1.0; if (NULL != psptr) { back = psptr->t; } return back; } double dk4papersize_get_bottom_border(const dk4_paper_size_t *psptr) { double back = -1.0; if (NULL != psptr) { back = psptr->b; } return back; } const dkChar * dk4papersize_get_name(const dk4_paper_size_t *psptr) { const dkChar *back = NULL; if (NULL != psptr) { back = psptr->name; } return back; } size_t dk4papersize_coll_size(dk4_paper_size_collection_t *ptr) { size_t back = 0; if (NULL != ptr) { dk4sto_it_reset(ptr->i_ps); while (NULL != dk4sto_it_next(ptr->i_ps)) { back++; } } return back; } void dk4papersize_coll_reset(dk4_paper_size_collection_t *ptr) { if (NULL != ptr) { dk4sto_it_reset(ptr->i_ps); } } const dk4_paper_size_t * dk4papersize_coll_next(dk4_paper_size_collection_t *ptr) { dk4_paper_size_t *back = NULL; if (NULL != ptr) { back = (dk4_paper_size_t *)dk4sto_it_next(ptr->i_ps); } return ((const dk4_paper_size_t *)back); } const dk4_paper_size_t * dk4papersize_coll_get(dk4_paper_size_collection_t *ptr, size_t n) { dk4_paper_size_t *back = NULL; size_t i = 0; if (NULL != ptr) { dk4sto_it_reset(ptr->i_ps); do { back = (dk4_paper_size_t *)dk4sto_it_next(ptr->i_ps); } while ((NULL != back) && (++i <= n)); } return ((const dk4_paper_size_t *)back); } /** Process one input line. @param obj Reader object. @param line Current line to process. @param lineno Line number. @param erp Error report, may be NULL. @return DK4_TSP_RES_OK if processing succeeded, DK4_TSP_RES_ERROR on errors (can continue), DK4_TSP_RES_FATAL on fatal errors (can not continue). */ static int dk4papersize_line_handler( void *obj, dkChar *line, dk4_um_t lineno, dk4_er_t *erp ) { double val[6]; /* w, h, i, o, t, b */ dkChar *tok[16]; /* tokens from the value side */ dk4_paper_size_collection_t *coll; /* collection to complete */ dk4_paper_size_t *ps; /* paper size to modify */ reader_obj_t *rdo; /* reader object */ const dkChar *ep; /* end pointer after double value */ dkChar *pls; /* Line start */ dkChar *pval; /* Start of value side */ size_t ntok; /* number of tokens found */ size_t i; /* traverse tokens */ int back = DK4_TSP_RES_OK; #line 774 "dk4paper.ctr" /* Retrieve pointers */ rdo = (reader_obj_t *)obj; coll = rdo->coll; /* Find start of line, ignore empty lines */ pls = dk4str_start(line, NULL); if (NULL == pls) { #line 783 "dk4paper.ctr" goto finished; } /* Ignore comment lines */ if (dkT('#') == *pls) { #line 789 "dk4paper.ctr" goto finished; } /* Find value side */ pval = dk4str_chr(pls, dkT('=')); if (NULL == pval) { /* ERROR: Syntax */ dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); back = DK4_TSP_RES_ERROR; goto finished; } /* Check whether value side contains text */ *(pval++) = dkT('\0'); pval = dk4str_start(pval, NULL); if (NULL == pval) { /* ERROR: Syntax */ dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); back = DK4_TSP_RES_ERROR; goto finished; } /* Check name length */ dk4str_rtwh(pls, NULL); if (0 == dk4str_len(pls)) { /* ERROR: Syntax */ dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); back = DK4_TSP_RES_ERROR; goto finished; } /* Tokenize string */ ntok = dk4str_tokenize(tok, 16, pval, NULL, NULL); if ((2 > ntok) || (6 < ntok)) { /* ERROR: Syntax */ dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); back = DK4_TSP_RES_ERROR; goto finished; } /* Initialize values */ for (i = 0; i < 6; i++) { val[i] = 0.0; } /* Process tokens to values */ for (i = 0; i < ntok; i++) { ep = NULL; if (0 == dk4ma_input_dk_double(&(val[i]), tok[i], &ep, 1, NULL)) { /* ERROR: Syntax */ dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); back = DK4_TSP_RES_ERROR; goto finished; } } /* Modify existing paper size or create a new one */ ps = (dk4_paper_size_t *)dk4sto_it_find_like(coll->i_ps, pls, 1); if (NULL == ps) { ps = dk4papersize_new(pls, NULL); if (NULL != ps) { if (0 == dk4sto_add(coll->s_ps, ps, NULL)) { /* ERROR: Memory */ dk4error_set_simple_error_code( erp, DK4_E_MEMORY_ALLOCATION_FAILED ); dk4papersize_delete(ps); ps = NULL; back = DK4_TSP_RES_FATAL; } } else { /* ERROR: Memory */ dk4error_set_simple_error_code(erp, DK4_E_MEMORY_ALLOCATION_FAILED); back = DK4_TSP_RES_FATAL; } } if (NULL == ps) { goto finished; } ps->w = val[0]; ps->h = val[1]; ps->i = val[2]; ps->o = val[3]; ps->t = val[4]; ps->b = val[5]; finished: #line 871 "dk4paper.ctr" return back; } /** Apply a file to a paper size collection. @param coll Collection to modify. @param fn Configuration file name. @param enc Encoding used in memory. @param erp Error report, may be NULL. @return 1 on success, 0 on errors. */ static int dk4paper_apply_file( dk4_paper_size_collection_t *coll, const dkChar *fn, int enc, dk4_er_t *erp ) { char ib[4096]; dkChar il[1024]; dk4_tspdk_t tsp; reader_obj_t rdo; dk4_stream_t *istrm = NULL; size_t rb = 0; int cc = 1; int res = 0; int back = 1; #line 903 "dk4paper.ctr" /* Open input stream */ istrm = dk4stream_open_file_reader(fn, erp); if (NULL == istrm) { goto finished; } /* Set up reader structure */ rdo.coll = coll; /* Set up text processing */ res = dk4tspdk_setup_line( &tsp, &rdo, dk4papersize_line_handler, il, DK4_SIZEOF(il,dkChar), enc, ( (DK4_ENCODING_UTF8 == enc) ? (DK4_FILE_ENCODING_UTF8) : (DK4_FILE_ENCODING_ASCII) ), erp ); if (0 == res) { goto finished; } /* Process input file */ res = DK4_TSP_RES_OK; do { #line 930 "dk4paper.ctr" cc = 0; rb = sizeof(ib); if (0 != dk4stream_read(ib, &rb, istrm, erp)) { #line 933 "dk4paper.ctr" if (0 < rb) { #line 934 "dk4paper.ctr" res = dk4tspdk_add_bytes(&tsp, (const unsigned char *)ib, rb); if (DK4_TSP_RES_FATAL == res) { #line 936 "dk4paper.ctr" back = 0; } else { #line 939 "dk4paper.ctr" cc = 1; } } #if TRACE_DEBUG else { #line 944 "dk4paper.ctr" } #endif } #if TRACE_DEBUG else { #line 949 "dk4paper.ctr" } #endif #line 952 "dk4paper.ctr" } while (1 == cc); /* Finalize text stream processor */ if (DK4_TSP_RES_FATAL != res) { if (DK4_TSP_RES_FATAL == dk4tspdk_finish(&tsp)) { #line 958 "dk4paper.ctr" back = 0; } } finished: if (NULL != istrm) { dk4stream_close(istrm, NULL); } #line 965 "dk4paper.ctr" return back; } dk4_paper_size_collection_t * dk4papersize_coll_for_application( const dkChar *progname, const dkChar *grpname, dk4_er_t *erp ) { dkChar fnb[DK4_MAX_PATH]; /* Construct filename */ dkChar home[DK4_MAX_PATH]; /* User home dir */ dkChar langbuf[16]; /* Language */ dkChar regbuf[16]; /* Region */ dk4_paper_size_collection_t *back = NULL; /* Result */ size_t i; /* File search index */ int enc; /* Encoding */ int res; /* Operation result */ int ok; /* Flag: No error */ #line 987 "dk4paper.ctr" /* Create default collection */ back = dk4papersize_coll_open(erp); if (NULL == back) { #line 991 "dk4paper.ctr" goto finished; } #line 993 "dk4paper.ctr" /* Check program and program group name */ if ((NULL == progname) || (NULL == grpname)) { #line 996 "dk4paper.ctr" dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); goto finished; } #line 999 "dk4paper.ctr" /* Find users home directory. */ if (0 == dk4user_get_homedir(home, DK4_SIZEOF(home,dkChar), 0, NULL)) { goto finished; } #line 1004 "dk4paper.ctr" /* Find encoding */ res = dk4loc_get_settings( langbuf, DK4_SIZEOF(langbuf,dkChar), regbuf, DK4_SIZEOF(regbuf,dkChar), &enc, NULL ); #line 1010 "dk4paper.ctr" if (0 == res) { enc = DK4_ENCODING_ASCII; } #line 1013 "dk4paper.ctr" /* Apply configuration files from build-in directory names */ ok = 1; for (i = 0; i <= DK4_FS_CONF_MAX; i++) { #if DK4_HAVE_COMPATDKTOOLS3 res = dk4fs_config_compat_one( fnb, DK4_SIZEOF(fnb,dkChar), conf_file_names[1], dk4inst_get_directory(2), dk4inst_get_directory(1), home, progname, grpname, i, 1, 1, NULL ); if (0 != res) { #line 1024 "dk4paper.ctr" if (0 == dk4paper_apply_file(back, fnb, enc, erp)) { ok = 0; #line 1026 "dk4paper.ctr" } } #endif res = dk4fs_config_one( fnb, DK4_SIZEOF(fnb,dkChar), conf_file_names[0], dk4inst_get_directory(2), dk4inst_get_directory(1), home, progname, grpname, i, 1, NULL ); if (0 != res) { #line 1035 "dk4paper.ctr" if (0 == dk4paper_apply_file(back, fnb, enc, erp)) { ok = 0; #line 1037 "dk4paper.ctr" } } } if (0 == ok) { #line 1041 "dk4paper.ctr" dk4papersize_coll_close(back); back = NULL; } finished: #line 1047 "dk4paper.ctr" return back; } /* vim: set ai sw=4 ts=4 : */