Leptonica  1.82.0
Image processing and image analysis suite
stringcode.c File Reference
#include <string.h>
#include "allheaders.h"
#include "stringcode.h"

Go to the source code of this file.

Data Structures

struct  L_GenAssoc
 

Macros

#define TEMPLATE1   "stringtemplate1.txt" /* for assembling autogen.*.c */
 
#define TEMPLATE2   "stringtemplate2.txt" /* for assembling autogen.*.h */
 

Functions

static l_int32 l_getIndexFromType (const char *type, l_int32 *pindex)
 
static l_int32 l_getIndexFromStructname (const char *sn, l_int32 *pindex)
 
static l_int32 l_getIndexFromFile (const char *filename, l_int32 *pindex)
 
static char * l_genDataString (const char *filein, l_int32 ifunc)
 
static char * l_genCaseString (l_int32 ifunc, l_int32 itype)
 
static char * l_genDescrString (const char *filein, l_int32 ifunc, l_int32 itype)
 
L_STRCODEstrcodeCreate (l_int32 fileno)
 
static void strcodeDestroy (L_STRCODE **pstrcode)
 
l_ok strcodeCreateFromFile (const char *filein, l_int32 fileno, const char *outdir)
 
l_ok strcodeGenerate (L_STRCODE *strcode, const char *filein, const char *type)
 
l_int32 strcodeFinalize (L_STRCODE **pstrcode, const char *outdir)
 
l_int32 l_getStructStrFromFile (const char *filename, l_int32 field, char **pstr)
 

Variables

static const l_int32 l_ntypes = 19
 
static const struct L_GenAssoc l_assoc []
 

Detailed Description


  Generation of code for storing and extracting serializable
  leptonica objects (such as pixa, recog, ...).

  The input is a set of files with serialized data.
  The output is two files, that must be compiled and linked:
    ~ autogen.*.c: code for base64 unencoding the strings and
                   deserializing the result.
    ~ autogen.*.h: function prototypes and base64 encoded strings
                   of the input data

  This should work for any data structures in leptonica that have
  *Write() and *Read() serialization functions.  An array of 20
  of these, including the Pix, is given below.  (The Pix is a special
  case, because it is serialized by standardized compression
  techniques, instead of a file format determined by leptonica.)

  Each time the generator function is invoked, three sets of strings are
  produced, which are written into their respective string arrays:
    ~ string of serialized, gzipped and base 64 encoded data
    ~ case string for base64 decoding, gunzipping and deserialization,
      to return the data struct in memory
    ~ description string for selecting which struct to return
  To create the two output files, a finalize function is invoked.

  There are two ways to do this, both shown in prog/autogentest1.c.
    ~ Explicitly call strcodeGenerate() for each file with the
      serialized data structure, followed by strcodeFinalize().
    ~ Put the filenames of the serialized data structures in a file,
      and call strcodeCreateFromFile().

  The generated code in autogen.X.c and autogen.X.h (where X is an
  integer supplied to strcodeCreate()) is then compiled, and the
  original data can be regenerated using the function l_autodecode_X().
  A test example is found in the two prog files:
      prog/autogentest1.c  -- generates autogen.137.c, autogen.137.h
      prog/autogentest2.c  -- uses autogen.137.c, autogen.137.h
  In general, the generator (e.g., autogentest1) would be compiled and
  run before compiling and running the application (e.g., autogentest2).

      L_STRCODE       *strcodeCreate()
      static void      strcodeDestroy()    (called as part of finalize)
      void             strcodeCreateFromFile()
      l_int32          strcodeGenerate()
      l_int32          strcodeFinalize()
      l_int32          l_getStructStrFromFile()   (useful externally)

  Static helpers
      static l_int32   l_getIndexFromType()
      static l_int32   l_getIndexFromStructname()
      static l_int32   l_getIndexFromFile()
      static char     *l_genDataString()
      static char     *l_genCaseString()
      static char     *l_genDescrString()

Definition in file stringcode.c.

Function Documentation

◆ l_genCaseString()

static char * l_genCaseString ( l_int32  ifunc,
l_int32  itype 
)
static

l_genCaseString()

Parameters
[in]ifuncindex into set of functions in generated file
[in]itypeindex into type of function to be used
Returns
case string for this decoding function
Notes:
     (1) ifunc and itype have been validated, so no error can occur

Definition at line 754 of file stringcode.c.

References l_assoc, and stringJoinIP().

Referenced by strcodeGenerate().

◆ l_genDataString()

static char * l_genDataString ( const char *  filein,
l_int32  ifunc 
)
static

l_genDataString()

Parameters
[in]fileininput file of serialized data
[in]ifuncindex into set of functions in output file
Returns
encoded ascii data string, or NULL on error reading from file

Definition at line 703 of file stringcode.c.

References l_binaryRead(), L_COPY, L_INSERT, sarrayAddString(), sarrayCreate(), sarrayDestroy(), sarrayToString(), and zlibCompress().

Referenced by strcodeGenerate().

◆ l_genDescrString()

static char * l_genDescrString ( const char *  filein,
l_int32  ifunc,
l_int32  itype 
)
static

l_genDescrString()

Parameters
[in]fileininput file of serialized data
[in]ifuncindex into set of functions in generated file
[in]itypeindex into type of function to be used
Returns
description string for this decoding function

Definition at line 788 of file stringcode.c.

References l_assoc, splitPathAtDirectory(), and stringNew().

Referenced by strcodeGenerate().

◆ l_getIndexFromFile()

static l_int32 l_getIndexFromFile ( const char *  filename,
l_int32 *  pindex 
)
static

l_getIndexFromFile()

Parameters
[in]filename
[out]pindexfound index
Returns
0 if found, 1 on error.

Definition at line 648 of file stringcode.c.

References findFileFormat(), fopenReadStream(), l_getIndexFromStructname(), L_NOCOPY, sarrayCreateWordsFromString(), sarrayDestroy(), and sarrayGetString().

Referenced by l_getStructStrFromFile(), and strcodeCreateFromFile().

◆ l_getIndexFromStructname()

static l_int32 l_getIndexFromStructname ( const char *  sn,
l_int32 *  pindex 
)
static

l_getIndexFromStructname()

Parameters
[in]snstructname e.g., "Pixa"
[out]pindexfound index
Returns
0 if found, 1 if not.
Notes:
     (1) This is used to identify the type of serialized file;
         the first word in the file is the structname.
     (2) For valid structname, found == true and index > 0.

Definition at line 615 of file stringcode.c.

References l_assoc, and l_ntypes.

Referenced by l_getIndexFromFile().

◆ l_getIndexFromType()

static l_int32 l_getIndexFromType ( const char *  type,
l_int32 *  pindex 
)
static

l_getIndexFromType()

Parameters
[in]typee.g., "PIXA"
[out]pindexfound index
Returns
0 if found, 1 if not.
Notes:
     (1) For valid type, found == true and index > 0.

Definition at line 575 of file stringcode.c.

References l_assoc, and l_ntypes.

Referenced by strcodeGenerate().

◆ l_getStructStrFromFile()

l_int32 l_getStructStrFromFile ( const char *  filename,
l_int32  field,
char **  pstr 
)

l_getStructStrFromFile()

Parameters
[in]filename
[in]field(L_STR_TYPE, L_STR_NAME, L_STR_READER, L_STR_MEMREADER)
[out]pstrstruct string for this file
Returns
0 if found, 1 on error.
Notes:
     (1) For example, if field == L_STR_NAME, and the file is a serialized
         pixa, this will return "Pixa", the name of the struct.
     (2) Caller must free the returned string.

Definition at line 528 of file stringcode.c.

References l_assoc, l_getIndexFromFile(), L_STR_MEMREADER, L_STR_NAME, L_STR_READER, L_STR_TYPE, and stringNew().

Referenced by pixaReadBoth().

◆ strcodeCreate()

L_STRCODE* strcodeCreate ( l_int32  fileno)

strcodeCreate()

Parameters
[in]filenointeger that labels the two output files
Returns
initialized L_StrCode, or NULL on error
Notes:
     (1) This struct exists to build two files containing code for
         any number of data objects.  The two files are named
            autogen.[fileno].c
            autogen.[fileno].h

Definition at line 159 of file stringcode.c.

References L_StrCode::data, L_StrCode::descr, L_StrCode::fileno, L_StrCode::function, lept_mkdir(), and sarrayCreate().

Referenced by strcodeCreateFromFile().

◆ strcodeCreateFromFile()

l_ok strcodeCreateFromFile ( const char *  filein,
l_int32  fileno,
const char *  outdir 
)

strcodeCreateFromFile()

Parameters
[in]fileincontaining filenames of serialized data
[in]filenointeger that labels the two output files
[in]outdir[optional] if null, files are made in /tmp/lept/auto
Returns
0 if OK, 1 on error
Notes:
     (1) The filein has one filename on each line.
         Comment lines begin with "#".
     (2) The output is 2 files:
            autogen.[fileno].c
            autogen.[fileno].h

Definition at line 225 of file stringcode.c.

References l_assoc, l_binaryRead(), l_getIndexFromFile(), L_NOCOPY, sarrayCreateLinesFromString(), sarrayDestroy(), sarrayGetCount(), sarrayGetString(), strcodeCreate(), strcodeFinalize(), and strcodeGenerate().

◆ strcodeDestroy()

static void strcodeDestroy ( L_STRCODE **  pstrcode)
static

strcodeDestroy()

Parameters
[out]pstrcodewill be set to null after destroying the sarrays
Returns
void

Definition at line 185 of file stringcode.c.

References L_StrCode::data, L_StrCode::descr, L_StrCode::function, and sarrayDestroy().

◆ strcodeFinalize()

l_int32 strcodeFinalize ( L_STRCODE **  pstrcode,
const char *  outdir 
)

strcodeFinalize()

Parameters
[in,out]pstrcodedestroys and sets to null after .c and .h files have been generated
[in]outdir[optional] if NULL, make files in /tmp/lept/auto
Returns
0 if OK; 1 on error

Definition at line 339 of file stringcode.c.

References l_binaryRead(), lept_mkdir(), and stringNew().

Referenced by strcodeCreateFromFile().

◆ strcodeGenerate()

l_ok strcodeGenerate ( L_STRCODE strcode,
const char *  filein,
const char *  type 
)

strcodeGenerate()

Parameters
[in]strcodefor accumulating data
[in]fileininput file with serialized data
[in]typeof data; use the typedef string
Returns
0 if OK, 1 on error.
Notes:
     (1) The generated function name is
           l_autodecode_[fileno]()
         where [fileno] is the index label for the pair of output files.
     (2) To deserialize this data, the function is called with the
         argument 'ifunc', which increments each time strcodeGenerate()
         is called.

Definition at line 291 of file stringcode.c.

References L_StrCode::data, L_StrCode::descr, L_StrCode::function, L_StrCode::ifunc, l_genCaseString(), l_genDataString(), l_genDescrString(), l_getIndexFromType(), L_INSERT, L_StrCode::n, and sarrayAddString().

Referenced by strcodeCreateFromFile().

Variable Documentation

◆ l_assoc

const struct L_GenAssoc l_assoc[]
static
Initial value:
= {
{0, "INVALID", "invalid", "invalid", "invalid" },
{1, "BOXA", "Boxa", "boxaRead", "boxaReadMem" },
{2, "BOXAA", "Boxaa", "boxaaRead", "boxaaReadMem" },
{3, "L_DEWARP", "Dewarp", "dewarpRead", "dewarpReadMem" },
{4, "L_DEWARPA", "Dewarpa", "dewarpaRead", "dewarpaReadMem" },
{5, "L_DNA", "L_Dna", "l_dnaRead", "l_dnaReadMem" },
{6, "L_DNAA", "L_Dnaa", "l_dnaaRead", "l_dnaaReadMem" },
{7, "DPIX", "DPix", "dpixRead", "dpixReadMem" },
{8, "FPIX", "FPix", "fpixRead", "fpixReadMem" },
{9, "NUMA", "Numa", "numaRead", "numaReadMem" },
{10, "NUMAA", "Numaa", "numaaRead", "numaaReadMem" },
{11, "PIX", "Pix", "pixRead", "pixReadMem" },
{12, "PIXA", "Pixa", "pixaRead", "pixaReadMem" },
{13, "PIXAA", "Pixaa", "pixaaRead", "pixaaReadMem" },
{14, "PIXACOMP", "Pixacomp", "pixacompRead", "pixacompReadMem" },
{15, "PIXCMAP", "Pixcmap", "pixcmapRead", "pixcmapReadMem" },
{16, "PTA", "Pta", "ptaRead", "ptaReadMem" },
{17, "PTAA", "Ptaa", "ptaaRead", "ptaaReadMem" },
{18, "RECOG", "Recog", "recogRead", "recogReadMem" },
{19, "SARRAY", "Sarray", "sarrayRead", "sarrayReadMem" }
}

Serializable data types

Definition at line 109 of file stringcode.c.

Referenced by l_genCaseString(), l_genDescrString(), l_getIndexFromStructname(), l_getIndexFromType(), l_getStructStrFromFile(), and strcodeCreateFromFile().

◆ l_ntypes

const l_int32 l_ntypes = 19
static

Number of serializable data types

Definition at line 109 of file stringcode.c.

Referenced by l_getIndexFromStructname(), and l_getIndexFromType().