/* 
   Copyright (c) 1991-1999 Thomas T. Wetmore IV

   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.
*/
/* modified 05 Jan 2000 by Paul B. McBride (pmcbride@tiac.net) */
/*=============================================================
 * gstrings.c -- Routines to creates child strings
 * Copyright(c) 1992-94 by T.T. Wetmore IV; all rights reserved
 *   2.3.4 - 24 Jun 93    2.3.5 - 25 Aug 93
 *   3.0.0 - 02 May 94    3.0.2 - 24 Nov 94
 *   3.0.3 - 15 Aug 95
 *===========================================================*/

#include "llstdlib.h"
#include "table.h"
#include "translat.h"
#include "gedcom.h"

/*********************************************
 * external/imported variables
 *********************************************/

extern STRING dspa_mar,dspa_bir,dspa_chr,dspa_dea,dspa_bur;
extern STRING unksps;

/*********************************************
 * local variables
 *********************************************/

static INT nchil = 0, maxchil = 0;
static STRING *chstrings = NULL, *chkeys = NULL;

static BOOLEAN displaykeys=TRUE;

/*********************************************
 * local function definitions
 * body of module
 *********************************************/

/*===================================================================
 * get_child_strings -- Return children strings; each string has name
 *   and event info, if avail  
 *  fam:   [in] family of interest
 *  rfmt:  [in] reformatting functions
 *  pnum:  [out] number of output strings
 *  pkeys: [out] array of output strings (children descriptions)
 *=================================================================*/
STRING *
get_child_strings (NODE fam, RFMT rfmt, INT *pnum, STRING **pkeys)
{
	NODE chil;
	INT i;

	for (i = 0; i < nchil; i++) {
		stdfree(chstrings[i]);
		stdfree(chkeys[i]);
	}
	nchil = *pnum = 0;
	if (!fam || !(chil = CHIL(fam))) return NULL;
	nchil = length_nodes(chil);
	if (nchil == 0) return NULL;
	if (nchil > (maxchil - 1)) {
		if (maxchil) {
			stdfree(chstrings); 
			stdfree(chkeys); 
		}
		chstrings = (STRING *) stdalloc((nchil+5)*sizeof(STRING));
		chkeys = (STRING *) stdalloc((nchil+5)*sizeof(STRING));
		maxchil = nchil + 5;
	}
	FORCHILDREN(fam,child,i)
		chstrings[i-1] = indi_to_list_string(child, NULL, 66, rfmt);
		chkeys[i-1] = strsave(rmvat(nxref(child)));
	ENDCHILDREN
	*pnum = nchil;
	*pkeys = chkeys;
	return chstrings;
}
/*================================================
 * indi_to_list_string -- Return menu list string.
 *  returns heap-alloc'd string
 *  indi:  [in] source person
 *  fam:   [in] relevant family (used in spouse lists)
 *  len:   [in] max length desired
 *  rfmt:  [in] reformating functions (may be NULL)
 *==============================================*/
STRING
indi_to_list_string (NODE indi, NODE fam, INT len, RFMT rfmt)
{
	char scratch[MAXLINELEN];
	STRING name, evt = NULL, p = scratch;
	TRANTABLE ttd = tran_tables[MINDS];
	int hasparents;
	int hasfamily;
	if (len>(INT)sizeof(scratch))
		len = sizeof(scratch);
	if (indi) {
		ASSERT(name = indi_to_name(indi, ttd, len));
	} else
		name = unksps;
	sprintf(p, "%s", name);
	p += strlen(p);
	if (fam)  evt = fam_to_event(fam, ttd, "MARR", dspa_mar, len, rfmt);
	if (!evt) evt = indi_to_event(indi, ttd, "BIRT", dspa_bir, len, rfmt);
	if (!evt) evt = indi_to_event(indi, ttd, "CHR", dspa_chr, len, rfmt);
	if (!evt) evt = indi_to_event(indi, ttd, "DEAT", dspa_dea, len, rfmt);
	if (!evt) evt = indi_to_event(indi, ttd, "BURI", dspa_bur, len, rfmt);
	if (evt) {
		sprintf(p, ", %s", evt);
		p += strlen(p);
	}
	if (indi && displaykeys) {
		sprintf(p, " (%s)", key_of_record(indi, ttd));
		p += strlen(p);
	}
	if (fam && displaykeys) {
		sprintf(p, " (%s)", key_of_record(fam, ttd));
		p += strlen(p);
	}
	if(indi) {
	    if(FAMC(indi)) hasparents = 1;
	    else hasparents = 0;
	    if(FAMS(indi)) hasfamily = 1;
	    else hasfamily = 0;
	    if(hasfamily || hasparents) {
		*p++ = ' ';
		*p++ = '[';
		if(hasparents) *p++ = 'P';
		if(hasfamily) *p++ = 'S';
		*p++ = ']';
		*p = '\0';
	    }
	}
	if ((INT)strlen(scratch) > len)
		scratch[len] = 0;
	return strsave(scratch);
}
/*================================================
 * sour_to_list_string -- Return menu list string.
 * Created: 2000/11/29, Perry Rapp
 *==============================================*/
STRING
sour_to_list_string (NODE sour, INT len, STRING delim)
{
	char scratch[1024];
	STRING name, p=scratch;
	INT mylen=len;
	TRANTABLE ttd = tran_tables[MINDS];
	if (mylen>(INT)sizeof(scratch))
		mylen=sizeof(scratch);
	p[0]=0;
	llstrcatn(&p, "(S", &mylen);
	llstrcatn(&p, rmvat(nxref(sour))+1, &mylen);
	llstrcatn(&p, ") ", &mylen);
	name = node_to_tag(sour, "REFN", ttd, len);
	if (name)
		llstrcatn(&p, name, &mylen);
	name = node_to_tag(sour, "TITL", ttd, len);
	if (name && mylen > 20)
	{
		llstrcatn(&p, delim, &mylen);
		llstrcatn(&p, name, &mylen);
	}
	name = node_to_tag(sour, "AUTH", ttd, len);
	if (name && mylen > 20)
	{
		llstrcatn(&p, delim, &mylen);
		llstrcatn(&p, name, &mylen);
	}
	return strsave(scratch);
}
/*================================================
 * even_to_list_string -- Return menu list string.
 * Created: 2001/12/16, Perry Rapp
 *==============================================*/
STRING
even_to_list_string (NODE even, INT len, STRING delim)
{
	char scratch[1024];
	STRING name, p=scratch;
	INT mylen=len;
	TRANTABLE ttd = tran_tables[MINDS];
	delim=delim; /* unused */
	if (mylen>(INT)sizeof(scratch))
		mylen=sizeof(scratch);
	p[0]=0;
	llstrcatn(&p, "(E", &mylen);
	llstrcatn(&p, rmvat(nxref(even))+1, &mylen);
	llstrcatn(&p, ") ", &mylen);
	name = node_to_tag(even, "NAME", ttd, len);
	if (name)
		llstrcatn(&p, name, &mylen);
        name = node_to_tag(even, "REFN", ttd, len);
        if (name) {
		llstrcatn(&p, " (", &mylen);
                llstrcatn(&p, name, &mylen);
		llstrcatn(&p, ")", &mylen);
	}
	return strsave(scratch);
}
/*================================================
 * fam_to_list_string -- Return menu list string.
 * Created: 2001/02/17, Perry Rapp
 *==============================================*/
STRING
fam_to_list_string (NODE fam, INT len, STRING delim)
{
	char scratch[1024];
	STRING name, p=scratch;
	INT mylen=len;
	char counts[32];
	INT husbands=0, wives=0, children=0;
	INT templen=0;
	TRANTABLE ttd = tran_tables[MINDS];
	NODE refn, husb, wife, chil, rest, node;
	if (mylen>(INT)sizeof(scratch))
		mylen=sizeof(scratch);
	p[0]=0;
	llstrcatn(&p, "(F", &mylen);
	llstrcatn(&p, rmvat(nxref(fam))+1, &mylen);
	llstrcatn(&p, ")", &mylen);
	name = node_to_tag(fam, "REFN", ttd, len);
	if (name) {
		llstrcatn(&p, " ", &mylen);
		llstrcatn(&p, name, &mylen);
	}
	split_fam(fam, &refn, &husb, &wife, &chil, &rest);
	for (node=husb; node; node=nsibling(node))
		husbands++;
	for (node=wife; node; node=nsibling(node))
		wives++;
	for (node=chil; node; node=nsibling(node))
		children++;
	sprintf(counts, "%dh,%dw,%dch", husbands, wives, children);
	llstrcatn(&p, " ", &mylen);
	llstrcatn(&p, counts, &mylen);
	if (husbands) {
		llstrcatn(&p, delim, &mylen);
		if (wives)
			templen = (mylen-4)/2;
		else
			templen = mylen;
		node = key_to_indi(rmvat(nval(husb)));
		llstrcatn(&p, indi_to_name(node, ttd, templen), &mylen);
		if (wives)
			llstrcatn(&p, " m. ", &mylen);
	}
	if (wives) {
		if (!husbands)
			templen = mylen;
		/* othewise we set templen above */
		node = key_to_indi(rmvat(nval(wife)));
		llstrcatn(&p, indi_to_name(node, ttd, templen), &mylen);
	}
	join_fam(fam, refn, husb, wife, chil, rest);
	/* TO DO - print a husband and a wife out */
	return strsave(scratch);
}
/*================================================
 * other_to_list_string -- Return menu list string.
 * Created: 2000/11/29, Perry Rapp
 *==============================================*/
STRING
other_to_list_string(NODE node, INT len, STRING delim)
{
	char scratch[1024];
	STRING name, p=scratch;
	INT mylen=len;
	TRANTABLE ttd = tran_tables[MINDS];
	NODE child;
	delim=delim; /* unused */
	if (mylen>(INT)sizeof(scratch))
		mylen=sizeof(scratch);
	p[0]=0;
	llstrcatn(&p, "(X", &mylen);
	llstrcatn(&p, rmvat(nxref(node))+1, &mylen);
	llstrcatn(&p, ") (", &mylen);
	translate_catn(ttd, &p, ntag(node), &mylen);
	llstrcatn(&p, ") ", &mylen);
	name = node_to_tag(node, "REFN", ttd, mylen);
	if (name)
		llstrcatn(&p, name, &mylen);
	if (nval(node)) {
		char scratch[MAXGEDNAMELEN+1];
		translate_string(ttd, nval(node), scratch, sizeof(scratch)-1);
		llstrcatn(&p, scratch, &mylen);
	}
	/* append any CONC/CONT nodes that fit */
	child = nchild(node);
	while (mylen>5 && child) {
		if (!strcmp(ntag(child), "CONC")
			|| !strcmp(ntag(child), "CONT")) {
			/* skip empty CONC/CONT nodes */
			if (nval(child)) {
				llstrcatn(&p, " ", &mylen);
				llstrcatn(&p, nval(child), &mylen);
			}
		} else {
			break;
		}
		if (nchild(child))
			break;
		else if (nsibling(child))
			child = nsibling(child);
		else
			break;
	}
	return strsave(scratch);
}
/*===========================================
 * generic_to_list_string -- Format a print line from
 *  a top-level node of any type
 * Caller may specify either node or key (& leave other NULL)
 *  returns heap-alloc'd string
 * Caller must specify either node or key (or both)
 * Used in lists and in extended gedcom view
 * Created: 2001/02/12, Perry Rapp
 *  node:  [in] node tree of indi or fam ... to be described
 *  key:   [in] key of record specified by node
 *  len:   [in] max description desired
 *  delim: [in] separator to use between events
 *  rfmt:  [in] reformatting information
 *=========================================*/
STRING
generic_to_list_string (NODE node, STRING key, INT len, STRING delim, RFMT rfmt)
{
	STRING str;
	str=NULL; /* set to appropriate format */
	if (!node && key)
		node = qkey_to_type(key);
	if (!key && node)
		key = rmvat(nxref(node));
	if (node) {
		switch (key[0])
		{
		case 'I':
			str = indi_to_list_string(node, NULL, len, rfmt);
			break;
		case 'S':
			str = sour_to_list_string(node, len, delim);
			break;
		case 'F':
			str = fam_to_list_string(node, len, delim);
			break;
		case 'E':
			str = even_to_list_string(node, len, delim);
			break;
		case 'X':
			str = other_to_list_string(node, len, delim);
			break;
		}
	}
	if (!str) {
		if (key)
			str = strsave(key);
		else
			str = strsave("??");
	}
	return str;
}
/*=======================================================
 * set_displaykeys -- Enable/disable keys in list strings
 * Created: 2001/01/01, Perry Rapp
 *=====================================================*/
void
set_displaykeys (BOOLEAN keyflag)
{
	displaykeys = keyflag;
}
