home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / geneal / part02 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-08  |  5.8 KB  |  375 lines

  1. /* misc.c - miscellaneous routines for geneal
  2.  *
  3.  * 17.Aug.87  jimmc  Initial definition
  4.  *  4.Jan.88  jimmc  Add gdblist, annlist, LAll, LFieldMatch, LRefs
  5.  *  8.Jan.88  jimmc  Add GSetOutput, GGetOutput, GFlushOutput, GSetSep, GGetSep
  6.  *            gfamily, gfamilyh, gindivs; lint cleanup
  7.  *  1.Mar.88  jimmc  Make GVersion return a string intead of printing it
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <strings.h>
  12. #include "spin.h"
  13. #include "geneal.h"
  14.  
  15. extern SPtoken *SPiarrtolist();
  16.  
  17. extern char *genealVersion;
  18. extern int dataDebug;
  19. extern int indexDebug;
  20.  
  21. extern char *strsave();
  22. extern int olisttoiarr();
  23.  
  24. char outfpname[1024+1];
  25. char *sepstr;
  26.  
  27. char Gflag[256];
  28. int Gflagcount = sizeof(Gflag)/sizeof(Gflag[0]);
  29.  
  30. char Gflagr[256+1];
  31. char *Label;
  32.  
  33. char *
  34. GFlags(flags)
  35. char *flags;
  36. {
  37. int i;
  38. char *cp, *np;
  39.  
  40.     if (!flags) flags="";
  41.     switch (flags[0]) {
  42.     case '=':
  43.         for (i=0; i<Gflagcount; i++) Gflag[i]=0;
  44.         /* FALL THROUGH */
  45.     case '+':
  46.         for (cp=flags+1; *cp; cp++) {
  47.             Gflag[*cp] = 1;
  48.             switch (*cp) {    /* some special flags */
  49.             case 'D': dataDebug=1; break;
  50.             case 'I': indexDebug=1; break;
  51.             default:  break;    /* do nothing */
  52.             }
  53.         }
  54.         break;
  55.     case '-':
  56.         for (cp=flags+1; *cp; cp++) {
  57.             Gflag[*cp] = 0;
  58.             switch (*cp) {    /* some special flags */
  59.             case 'D': dataDebug=0; break;
  60.             case 'I': indexDebug=0; break;
  61.             default:  break;    /* do nothing */
  62.             }
  63.         }
  64.         break;
  65.     case '?':
  66.     case '\0':
  67.         break;        /* just return the value */
  68.     case 'h':
  69.         GFlagsHelp();
  70.         break;
  71.     default:
  72.         break;    /*** should output error message */
  73.     }
  74.     for (np=Gflagr, i=0; i<Gflagcount; i++)
  75.         if (Gflag[i]) *np++ = i;
  76.     *np = '\0';
  77.     return Gflagr;
  78. }
  79.  
  80. GFlagsHelp()
  81. {
  82.     printf("GFlags [+-=?][flagchars]\n");
  83.     printf("a  enables output of addresses in family pages\n");
  84.     printf("b  enables additional birth/death info in famtree\n");
  85.     printf("m  includes spouses in famtrees\n");
  86. printf("N  enables additional printing of index numbers (only if +n)\n");
  87.     printf("n  enables printing of index numbers\n");
  88.     printf("s  enables printing of sibling info in atrees\n");
  89.     printf("t  include TNOTEs in famtree\n");
  90.     printf("y  makes date sorts include year (by default they do not)\n");
  91.     printf("D  debug data routines\n");
  92.     printf("I  debug index routines\n");
  93. }
  94.  
  95. char *
  96. GVersion()
  97. {
  98.     return genealVersion;
  99. }
  100.  
  101. int
  102. GSetOutput(name)
  103. char *name;
  104. {
  105. FILE *f;
  106. int t;
  107. char *mode;
  108. char m[4];
  109.  
  110.     if (outfp!=stdout && outfp!=stderr) {
  111.         t = ferror(outfp) || fclose(outfp);
  112.         if (t) {
  113.             warning("error closing previous output file %s",
  114.                     outfpname);
  115.         }
  116.     }
  117.     t = 0;        /* assume no errors */
  118.     if (strcmp(name,"stdout")==0)
  119.         f = stdout;
  120.     else if (strcmp(name,"stderr")==0)
  121.         f = stderr;
  122.     else {
  123.         if (name[0]=='+') {
  124.             name++;
  125.             mode = "append";
  126.         }
  127.         else {
  128.             mode = "write";
  129.         }
  130.         m[0] = mode[0];
  131.         m[1] = 0;
  132.         f = fopen(name,m);
  133.         if (!f) {
  134.             warning("can't open file %s for %s",name,mode);
  135.             f = stdout;
  136.             name = "stdout";
  137.             t = 1;        /* error */
  138.         }
  139.     }
  140.     strcpy(outfpname,name);        /* save for later messages */
  141.     outfp = f;
  142.     return t;        /* non-zero is error */
  143. }
  144.  
  145. char *
  146. GGetOutput()
  147. {
  148.     return outfpname;
  149. }
  150.  
  151. GFlushOutput()
  152. {
  153.     fflush(outfp);
  154. }
  155.  
  156. GSetSep(s)
  157. char *s;        /* page separator string */
  158. {
  159.     if (sepstr) freestr(sepstr);
  160.     sepstr = strsav(s);
  161. }
  162.  
  163. char *
  164. GGetSep()
  165. {
  166.     return sepstr;
  167. }
  168.  
  169. GSetLabel(s)
  170. char *s;
  171. {
  172.     if (Label) free(Label);
  173.     Label = strsave(s);
  174. }
  175.  
  176. char *
  177. GGetLabel()
  178. {
  179.     return Label;
  180. }
  181.  
  182. int
  183. gfamily(list)
  184. SPtoken *list;
  185. {
  186. int ac;
  187. int *av;
  188. int t;
  189.  
  190.     ac = olisttoiarr(list,&av);
  191.     t =  family(ac,av);
  192.     free((char *)av);
  193.     return t;
  194. }
  195.  
  196. int
  197. gfamilyh(list)
  198. SPtoken *list;
  199. {
  200. int ac;
  201. int *av;
  202. int t;
  203.  
  204.     ac = olisttoiarr(list,&av);
  205.     t =  familyh(ac,av);
  206.     free((char *)av);
  207.     return t;
  208. }
  209.  
  210. int
  211. gindivs(list)
  212. SPtoken *list;
  213. {
  214. int ac;
  215. int *av;
  216. int t;
  217.  
  218.     ac = olisttoiarr(list,&av);
  219.     t =  indivs(ac,av);
  220.     free((char *)av);
  221.     return t;
  222. }
  223.  
  224. int
  225. gfamntree(list)
  226. SPtoken *list;
  227. {
  228. int ac;
  229. int *av;
  230. int t;
  231.  
  232.     ac = olisttoiarr(list,&av);
  233.     t = famntree(ac,av);
  234.     free((char *)av);
  235.     return t;
  236. }
  237.  
  238. int
  239. gannlist(list)
  240. SPtoken *list;
  241. {
  242. int ac;
  243. int *av;
  244. int t;
  245.  
  246.     ac = olisttoiarr(list,&av);
  247.     t = annlist(ac,av);
  248.     free((char *)av);
  249.     return t;
  250. }
  251.  
  252. int
  253. gbdlist(list)
  254. SPtoken *list;
  255. {
  256. int ac;
  257. int *av;
  258. int t;
  259.  
  260.     ac = olisttoiarr(list,&av);
  261.     t = bdlist(ac,av);
  262.     free((char *)av);
  263.     return t;
  264. }
  265.  
  266. SPtoken *
  267. LAnc(list,n)        /* ancestor list */
  268. SPtoken *list;
  269. int n;
  270. {
  271. int iac, *iav;
  272. int ac, *av;
  273. SPtoken *l;
  274.  
  275.     iac = olisttoiarr(list,&iav);
  276.     ac = famalist(iac,iav,&av,n);
  277.     l = SPiarrtolist(ac,av);
  278.     free((char *)av);
  279.     free((char *)iav);
  280.     return l;
  281. }
  282.  
  283. SPtoken *
  284. LDesc(list,n)        /* descendant list */
  285. SPtoken *list;
  286. int n;
  287. {
  288. int iac, *iav;
  289. int ac, *av;
  290. SPtoken *l;
  291.  
  292.     iac = olisttoiarr(list,&iav);
  293.     ac = famdlist(iac,iav,&av,n);
  294.     l = SPiarrtolist(ac,av);
  295.     free((char *)av);
  296.     free((char *)iav);
  297.     return l;
  298. }
  299.  
  300. SPtoken *
  301. LAll()            /* all entries in the database */
  302. {
  303. int ac, *av;
  304. SPtoken *l;
  305.  
  306.     ac = alllist(&av);
  307.     l = SPiarrtolist(ac,av);
  308.     free((char *)av);
  309.     return l;
  310. }
  311.  
  312. SPtoken *
  313. LFieldMatch(il,fname,fvalue)    /* match value of a field */
  314. SPtoken *il;        /* the list of entry id's to check */
  315. char *fname;        /* field name */
  316. char *fvalue;        /* field value to look for */
  317. {
  318. int iac, *iav;
  319. int ac, *av;
  320. SPtoken *l;
  321.  
  322.     iac = olisttoiarr(il,&iav);
  323.     ac = lfieldmatch(iac,iav,fname,fvalue,&av);
  324.     l = SPiarrtolist(ac,av);
  325.     free((char *)av);
  326.     return l;
  327. }
  328.  
  329. SPtoken *
  330. LRefs(il,refname)    /* follow field references */
  331. SPtoken *il;        /* the list of entry id's to check */
  332. char *refname;        /* field name */
  333. {
  334. int iac, *iav;
  335. int ac, *av;
  336. SPtoken *l;
  337.  
  338.     iac = olisttoiarr(il,&iav);
  339.     ac = lrefs(iac,iav,refname,&av);
  340.     l = SPiarrtolist(ac,av);
  341.     free((char *)av);
  342.     return l;
  343. }
  344.  
  345. int
  346. GPfi(il)    /*print family or individual pages */
  347. SPtoken *il;
  348. {
  349. int ac;
  350. int *av;
  351. int n;
  352. int i,t;
  353.  
  354.     t = 0;
  355.     ac = olisttoiarr(il,&av);
  356.     for (i=0; i<ac; i++) {
  357.         if (i>0) fprintf(outfp,sepstr);
  358.         n = av[i];
  359.         switch (fgtype(n)) {
  360.         case 'I':
  361.             t += indivs1(n);
  362.             break;
  363.         case 'F':
  364.             t += family1(n);
  365.             break;
  366.         default:
  367.             warning("bad record %d\n", n);
  368.             break;
  369.         }
  370.     }
  371.     return t;
  372. }
  373.  
  374. /* end */
  375.