<commit_before>/*
    The following function and structure definitions manage the
    output of CALACS trailer file comments, both to STDOUT and to
    the trailer file(s).  Code for using a temp file as an output
    trailer file have been commented out, but left in to insure that
    no unsuspecting problems arise from this change.  They should be
    deleted upon the next release assuming everything works as desired.

    The overwrite switch will be used to specify whether the entire
    input trailer file should be kept or whether old comments starting
    at TRL_PREFIX should be overwritten.  This would be set to YES for
    initial SINGLE exposure trailer files.

    It includes the following functions, provided with their calling
    sequences.  For the trailer files, we use the following terms:
        preface - comments common to more than 1 input file, copied into
            the beginning of the trailer file (after the GENERIC conversion
            comments).  This usually only applies to _RAW files.
        buffer  - trailer file comments for use in a single trailer file
        prefix  - string which marks the beginning of CALACS processing
            comments in the trailer files. (TRL_PREFIX)

    These control the buffer which contains the messages to be written out:
    int InitTrlBuf (void);
        - MUST BE CALLED PRIOR TO ANY OTHER TRAILER FILE FUNCTION!
        - initializes trailer buffer structure 'trlbuf'
    void SetTrlPrefaceMode (int use);
        - This function sets the switch to specify whether the
            preface should be output to the trailer file or
            not.  This is used when no CRREJ is done on a list
            of images, and you don't want the preface in the
            trailer files of the sub-products again.
    void SetTrlOverwriteMode (int owrite);
        - This function sets the overwrite switch.  Once set, all
            comments after the first CALACSBEG string will be
            overwritten.
    void SetTrlQuietMode (int quiet);
        - This function records the value of the command-line
            parameter QUIET into the structure.
    void InitTrlPreface (void);
        - This function will copy contents of the buffer into the preface.
    void ResetTrlPreface (void);
        - This function will reset the preface to a null string, so
            nothing more gets written out to any other trailer files.
    void CloseTrlBuf (void);
        - moves buffer from temp file to final output trailer file,
            then closes temp and output trailer files.

    These control the actual trailer files themselves:
    int InitTrlFile (char *inlist, char *output);
        - opens temporary trailer file based on the
            input trailer file(s) given in the file list 'inlist'
    int WriteTrlFile (void);
        - writes out temp trailer file to final trailer file, closes both
            and deletes the temp file (if all went OK).

    These create the messages to be written out:
    void trlmessage (char *message);
        - calls WrtTrlBuf and asnmessage
    void trlwarn (char *message);
        - calls trlmessage after building appropriate message
    void trlerror (char *message);
        - calls trlmessage after building appropriate message
    void trlopenerr (char *filename);
        - calls trlerror after starting to build appropriate message
    void trlreaderr (char *filename);
        - calls trlerror after building appropriate message
    void trlkwerr (char *keyword, char *filename);
        - calls trlerror after building appropriate message
    void trlfilerr (char *filename);
        - calls trlerror after building appropriate message

    The remainder of the functions are NOT USED outside this file:
    static void CatTrlFile(FILE *ip, FILE *op);
        - appends ENTIRE input trailer file (ip) to output trailer file (op)
    static int AppendTrlFile();
        - sets up trailer file so that all new comments start after preface
    static void AddTrlBuf (char *message);
        - adds a new message (line) to trailer comments buffer
    static void ResetTrlBuf (void);
        - clears out comments (sets to blank) already written to file
    static void WriteTrlBuf (char *message);
        - writes out comments in buffer to open trailer file
          then calls ResetTrlBuf to clear buffer
          -OR-
          calls AddTrlBuf to append comment to buffer.


    Initial Version: 16 Nov 1998, WJH
    Revised:		 11 Dec 1998, WJH
    Revised:         13 Sep 1999, WJH
    Revised:         14 Apr 2000, WJH (removed use of temp file)
    Revised:         26 Aug 2002, WJH (increased buffer size for trldata)
    Revised:         18 Apr 2012, PLL (fixed InitTrlFile inf loop bug)
    Revised:          4 May 2015, PLL (replaced tmpnam with mkstemp)
*/

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "ximio.h"

# include "acs.h"
# include "acserr.h"

# define INIT_LEN	2

/* Internal trailer file buffer routines */
static void ResetTrlBuf (void);
static void AddTrlBuf (char *message);
static void WriteTrlBuf (char *message);
static void CatTrlFile (FILE *ip, FILE *op);
static int AppendTrlFile();

static struct _TrlBuf {
    char trlfile[ACS_LINE]; /* name of output trailer file */
    int overwrite;          /* overwrite previous comments or append */
    int quiet;              /* Suppress STDOUT output? */
    FILE *fp;               /* pointer to open trailer file */
    char *buffer;
    char *preface;          /* CALACS comments common to all inputs */
    int usepref;            /* Switch to specify whether preface is used */
} trlbuf;

/*
    This initialization function sets up the trailer file to be used
    as output for all the messages from the task it is called from.
    It will concatenate multiple input trailer files
    or copy intial portion of input trailer file into one output
    file, creating the single trailer file for the messages to be
    written to...
*/
int InitTrlFile (char *inlist, char *output) {

/* Parameters:
char *inlist        i: list of input trailer filenames
char *output        i: full filename of output (final) trailer file
*/
    extern int status;

    IRAFPointer tpin;
    FILE *ip, *tp;
    int n, td;
    char trldata[ACS_LINE+1];
    static char uniq_outtemplate[] = "bozoXXXXXX";
    char uniq_outname[ACS_CBUF];

    void SetTrlOverwriteMode (int);

    trldata[0] = '\0';

    /* Copy name of output file to trlbuf */
    strcpy (trlbuf.trlfile, output);

    /*  Open the output file, for concatenating
        exposure trailers or sub-product trailers, or simply for
        appending new comments to end of existing trailer file.
    */
    if ( (trlbuf.fp = fopen(trlbuf.trlfile,"a+")) == NULL) {
        trlopenerr(trlbuf.trlfile);
        return(status=INVALID_TEMP_FILE);
    }


    /* open the input file template */
    tpin = c_imtopen (inlist);<commit_msg>Changed template name to Mark's suggestion.<commit_after>/*
    The following function and structure definitions manage the
    output of CALACS trailer file comments, both to STDOUT and to
    the trailer file(s).  Code for using a temp file as an output
    trailer file have been commented out, but left in to insure that
    no unsuspecting problems arise from this change.  They should be
    deleted upon the next release assuming everything works as desired.

    The overwrite switch will be used to specify whether the entire
    input trailer file should be kept or whether old comments starting
    at TRL_PREFIX should be overwritten.  This would be set to YES for
    initial SINGLE exposure trailer files.

    It includes the following functions, provided with their calling
    sequences.  For the trailer files, we use the following terms:
        preface - comments common to more than 1 input file, copied into
            the beginning of the trailer file (after the GENERIC conversion
            comments).  This usually only applies to _RAW files.
        buffer  - trailer file comments for use in a single trailer file
        prefix  - string which marks the beginning of CALACS processing
            comments in the trailer files. (TRL_PREFIX)

    These control the buffer which contains the messages to be written out:
    int InitTrlBuf (void);
        - MUST BE CALLED PRIOR TO ANY OTHER TRAILER FILE FUNCTION!
        - initializes trailer buffer structure 'trlbuf'
    void SetTrlPrefaceMode (int use);
        - This function sets the switch to specify whether the
            preface should be output to the trailer file or
            not.  This is used when no CRREJ is done on a list
            of images, and you don't want the preface in the
            trailer files of the sub-products again.
    void SetTrlOverwriteMode (int owrite);
        - This function sets the overwrite switch.  Once set, all
            comments after the first CALACSBEG string will be
            overwritten.
    void SetTrlQuietMode (int quiet);
        - This function records the value of the command-line
            parameter QUIET into the structure.
    void InitTrlPreface (void);
        - This function will copy contents of the buffer into the preface.
    void ResetTrlPreface (void);
        - This function will reset the preface to a null string, so
            nothing more gets written out to any other trailer files.
    void CloseTrlBuf (void);
        - moves buffer from temp file to final output trailer file,
            then closes temp and output trailer files.

    These control the actual trailer files themselves:
    int InitTrlFile (char *inlist, char *output);
        - opens temporary trailer file based on the
            input trailer file(s) given in the file list 'inlist'
    int WriteTrlFile (void);
        - writes out temp trailer file to final trailer file, closes both
            and deletes the temp file (if all went OK).

    These create the messages to be written out:
    void trlmessage (char *message);
        - calls WrtTrlBuf and asnmessage
    void trlwarn (char *message);
        - calls trlmessage after building appropriate message
    void trlerror (char *message);
        - calls trlmessage after building appropriate message
    void trlopenerr (char *filename);
        - calls trlerror after starting to build appropriate message
    void trlreaderr (char *filename);
        - calls trlerror after building appropriate message
    void trlkwerr (char *keyword, char *filename);
        - calls trlerror after building appropriate message
    void trlfilerr (char *filename);
        - calls trlerror after building appropriate message

    The remainder of the functions are NOT USED outside this file:
    static void CatTrlFile(FILE *ip, FILE *op);
        - appends ENTIRE input trailer file (ip) to output trailer file (op)
    static int AppendTrlFile();
        - sets up trailer file so that all new comments start after preface
    static void AddTrlBuf (char *message);
        - adds a new message (line) to trailer comments buffer
    static void ResetTrlBuf (void);
        - clears out comments (sets to blank) already written to file
    static void WriteTrlBuf (char *message);
        - writes out comments in buffer to open trailer file
          then calls ResetTrlBuf to clear buffer
          -OR-
          calls AddTrlBuf to append comment to buffer.


    Initial Version: 16 Nov 1998, WJH
    Revised:		 11 Dec 1998, WJH
    Revised:         13 Sep 1999, WJH
    Revised:         14 Apr 2000, WJH (removed use of temp file)
    Revised:         26 Aug 2002, WJH (increased buffer size for trldata)
    Revised:         18 Apr 2012, PLL (fixed InitTrlFile inf loop bug)
    Revised:          4 May 2015, PLL (replaced tmpnam with mkstemp)
*/

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "ximio.h"

# include "acs.h"
# include "acserr.h"

# define INIT_LEN	2

/* Internal trailer file buffer routines */
static void ResetTrlBuf (void);
static void AddTrlBuf (char *message);
static void WriteTrlBuf (char *message);
static void CatTrlFile (FILE *ip, FILE *op);
static int AppendTrlFile();

static struct _TrlBuf {
    char trlfile[ACS_LINE]; /* name of output trailer file */
    int overwrite;          /* overwrite previous comments or append */
    int quiet;              /* Suppress STDOUT output? */
    FILE *fp;               /* pointer to open trailer file */
    char *buffer;
    char *preface;          /* CALACS comments common to all inputs */
    int usepref;            /* Switch to specify whether preface is used */
} trlbuf;

/*
    This initialization function sets up the trailer file to be used
    as output for all the messages from the task it is called from.
    It will concatenate multiple input trailer files
    or copy intial portion of input trailer file into one output
    file, creating the single trailer file for the messages to be
    written to...
*/
int InitTrlFile (char *inlist, char *output) {

/* Parameters:
char *inlist        i: list of input trailer filenames
char *output        i: full filename of output (final) trailer file
*/
    extern int status;

    IRAFPointer tpin;
    FILE *ip, *tp;
    int n, td;
    char trldata[ACS_LINE+1];
    static char uniq_outtemplate[] = "tmp_calacs_XXXXXX";
    char uniq_outname[ACS_CBUF];

    void SetTrlOverwriteMode (int);

    trldata[0] = '\0';

    /* Copy name of output file to trlbuf */
    strcpy (trlbuf.trlfile, output);

    /*  Open the output file, for concatenating
        exposure trailers or sub-product trailers, or simply for
        appending new comments to end of existing trailer file.
    */
    if ( (trlbuf.fp = fopen(trlbuf.trlfile,"a+")) == NULL) {
        trlopenerr(trlbuf.trlfile);
        return(status=INVALID_TEMP_FILE);
    }


    /* open the input file template */
    tpin = c_imtopen (inlist);
{'fraction_non_alphanumeric': 0.05655083099503561, 'fraction_numerical': 0.006619181236060148, 'mean_word_length': 2.6264022958518134, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '745863', 'n_tokens_mistral': 3775, 'n_tokens_neox': 3607, 'n_words': 1772}