home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Aktief 1995 #3 / CDA3.iso / genealog / pafctool.zip / NUPDATE.C < prev    next >
C/C++ Source or Header  |  1992-04-11  |  6KB  |  213 lines

  1. /*
  2.  * Copyright (C) 1992, Stephen A. Wood.  All rights reserved.
  3.  * 
  4.  * This file is part of PAF C-Tools.
  5.  * 
  6.  * These programs are free software; you can redistribute them and/or modify
  7.  * them under the terms of the GNU General Public License as published by the
  8.  * Free Software Foundation; either version 2 of the License, or (at your
  9.  * option) any later version.
  10.  * 
  11.  * These programs are distributed in the hope that they will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13.  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License along with
  17.  * this program; if not, write to the Free Software Foundation, Inc., 675
  18.  * Mass Ave, Cambridge, MA 02139, USA. The GNU General Public License can be
  19.  * found in the file LICENSE.
  20.  * 
  21.  * The author of PAF C-Tools is Stephen A. Wood.  He may be reached on internet
  22.  * at the address saw@cegaf.gov, or via mail at 328 Dominion Drive, Newport
  23.  * News, VA 23602.
  24.  * 
  25.  * ------------
  26.  * 
  27.  * Update notes file with standard input.  The indiv2.dat and notes2.dat files
  28.  * are modified by this program.  These files are backup to indiv2.bNN and
  29.  * notes2.bNN.
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "pafsubs.h"
  35.  
  36. void            backup_file(enum paffiles filetype);
  37. void            printnotes(RECORD_PTR rin);
  38.  
  39. main(argc, argv)
  40.     int             argc;
  41.     char           *argv[];
  42. {
  43.     PAF_FILE       *indiv_file, *notes_file;
  44.     int             anum;
  45.     short int       freelist[128];
  46.     int             i;
  47.  
  48.     backup_file(NOTE);
  49.     backup_file(INDIV);
  50.  
  51.     paf_open_notes('w');
  52.     notes_file = get_paf_filed(NOTE);
  53.     paf_open_indiv('w');
  54.     indiv_file = get_paf_filed(NOTE);
  55.  
  56.     printf("\n");
  57.  
  58.     {
  59.     char            s[200];
  60.     char           *eof_flag;
  61.  
  62.     eof_flag = gets(&s[1]);
  63.     /* Skip over preamble */
  64.     while (eof_flag && strncmp(&s[1], "%%", 2)) {
  65.         eof_flag = gets(&s[1]);
  66.     }
  67.     while (eof_flag && !strncmp(&s[1], "%%", 2)) {
  68.         int             newnote;
  69.         int             len;
  70.         int             c;
  71.         int             stop, xstop;
  72.         INDIV_REC       indiv;
  73.         NOTE_REC        notes;
  74.         int             padctr;
  75.         RECORD_PTR      rin;
  76.         RECORD_PTR      thispad, nextpad;
  77.  
  78.         {
  79.         char           *p;
  80.         p = strchr(&s[1], '[');
  81.         rin = atoi(++p);
  82.         }
  83.         /* printf("Found RIN=%d\n",rin); */
  84.         eof_flag = gets(&s[1]);
  85.         newnote = 1;
  86.         stop = strncmp(&s[1], "%%--", 4);
  87.         xstop = 1;
  88.         len = strlen(&s[1]);
  89.         newnote = 1;
  90.         get_indiv_rec(rin, &indiv);
  91.         if (stop) {
  92.         thispad = indiv.notepad;
  93.         nextpad = get_next(thispad, notes_file);
  94.         /* printf("Pad=%d, Next=%d\n",thispad,nextpad); */
  95.         if (thispad == 0) {
  96.             printf("RIN %5d: Adding new notes\n", rin);
  97.             thispad = get_free_rec(notes_file);
  98.             nextpad = 0;
  99.             indiv.notepad = thispad;
  100.             write_indiv_rec(rin, &indiv);
  101.         }
  102.         padctr = 0;
  103.         while (eof_flag && xstop) {
  104.             if (len == 0 && stop) {
  105.             newnote = 1;
  106.             } else {
  107.             s[0] = newnote;
  108.             newnote = 0;
  109.             i = 0;
  110.             len += 2;
  111.             while (i < len) {
  112.                 if (padctr < 254) {
  113.                 notes.notelines[padctr++] = s[i];
  114.                 } else {
  115.                 padctr = 0;
  116.                 if (nextpad == 0)
  117.                     nextpad = get_free_rec(notes_file);
  118.                 notes.next_pad = nextpad;
  119.                 /*
  120.                  * printf("Writing %d with
  121.                  * nextpad=%d\n",thispad,nextpad);
  122.                  */
  123.                 write_note_rec(thispad, ¬es);
  124.                 thispad = nextpad;
  125.                 nextpad = get_next(thispad, notes_file);
  126.                 notes.notelines[padctr++] = s[i];
  127.                 }
  128.                 /*
  129.                  * if(s[i]>1) putchar(s[i]); else
  130.                  * printf("%d",s[i]);
  131.                  */
  132.                 i++;
  133.             }
  134.             /* printf("\n"); */
  135.             }
  136.             xstop = stop;
  137.             eof_flag = gets(&s[1]);
  138.             stop = strncmp(&s[1], "%%--", 4);
  139.             if (!stop) {
  140.             newnote = 0;
  141.             s[1] = 0;
  142.             len = 0;
  143.             } else {
  144.             len = strlen(&s[1]);
  145.             }
  146.         }
  147.         if (padctr > 0) {
  148.             if (nextpad != 0) {
  149.             printf("RIN %5d: Removing some note pads\n", rin);
  150.             add_to_freelist(nextpad, notes_file);
  151.             }
  152.             notes.next_pad = 0;
  153.             /* printf("Writing pad %d\n",thispad); */
  154.             write_note_rec(thispad, ¬es);
  155.         }
  156.         } else {
  157.         if (indiv.notepad == 0) {
  158.             printf("RIN %5d: Notes remain empty\n", rin);
  159.         } else {
  160.             add_to_freelist(indiv.notepad, notes_file);
  161.             printf("RIN %5d: Removing notes\n", rin);
  162.             indiv.notepad = 0;
  163.             write_indiv_rec(rin, &indiv);
  164.         }
  165.         eof_flag = gets(&s[1]);
  166.         }
  167.     }
  168.     }
  169.  
  170.     fclose(notes_file->ptr);
  171.     fclose(indiv_file->ptr);
  172.     exit(0);
  173. }
  174.  
  175. void 
  176. backup_file(enum paffiles filetype)
  177. {
  178.     char           *pos;
  179.     FILE           *backup;
  180.     char            command[100];
  181.     char            backup_fname[30];
  182.     PAF_FILE       *orig;
  183.     int             i;
  184.  
  185.     orig = get_paf_filed(filetype);
  186.     strcpy(backup_fname, orig->fname);
  187.     pos = (char *) strrchr(backup_fname, '.');
  188.     if (pos == NULL) {
  189.     printf("nupdate: No . found in %s\n", backup_fname);
  190.     exit(1);
  191.     } else {
  192.     pos++;
  193.     strcpy(pos, "b00");
  194.     pos++;
  195.     i = 0;
  196.     while ((backup = fopen(backup_fname, "rb")) != NULL) {
  197.         fclose(backup);
  198.         i++;
  199.         sprintf(pos, "%2.2i", i);
  200.     }
  201. #ifdef unix
  202.     strcpy(command, "cp ");
  203. #else
  204.     strcpy(command, "copy ");
  205. #endif
  206.     strcat(command, orig->fname);
  207.     strcat(command, " ");
  208.     strcat(command, backup_fname);
  209.     printf("Backing up %s to %s\n", orig->fname, backup_fname);
  210.     system(command);
  211.     }
  212. }
  213.