home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / lout / part12 / z33.c < prev   
C/C++ Source or Header  |  1993-08-11  |  26KB  |  545 lines

  1. /*@z33.c:Database Service:OldCrossDb(), NewCrossDb(), SymToNum()@*************/
  2. /*                                                                           */
  3. /*  LOUT: A HIGH-LEVEL LANGUAGE FOR DOCUMENT FORMATTING (VERSION 2.05)       */
  4. /*  COPYRIGHT (C) 1993 Jeffrey H. Kingston                                   */
  5. /*                                                                           */
  6. /*  Jeffrey H. Kingston (jeff@cs.su.oz.au)                                   */
  7. /*  Basser Department of Computer Science                                    */
  8. /*  The University of Sydney 2006                                            */
  9. /*  AUSTRALIA                                                                */
  10. /*                                                                           */
  11. /*  This program is free software; you can redistribute it and/or modify     */
  12. /*  it under the terms of the GNU General Public License as published by     */
  13. /*  the Free Software Foundation; either version 1, or (at your option)      */
  14. /*  any later version.                                                       */
  15. /*                                                                           */
  16. /*  This program is distributed in the hope that it will be useful,          */
  17. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
  18. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
  19. /*  GNU General Public License for more details.                             */
  20. /*                                                                           */
  21. /*  You should have received a copy of the GNU General Public License        */
  22. /*  along with this program; if not, write to the Free Software              */
  23. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  24. /*                                                                           */
  25. /*  FILE:         z33.c                                                      */
  26. /*  MODULE:       Database Service                                           */
  27. /*  EXTERNS:      OldCrossDb, NewCrossDb, DbCreate(), DbInsert(),            */
  28. /*                DbConvert(), DbClose(), DbLoad(), DbRetrieve(),            */
  29. /*                DbRetrieveNext()                                           */
  30. /*                                                                           */
  31. /*****************************************************************************/
  32. #include "externs"
  33.  
  34.  
  35. /*****************************************************************************/
  36. /*                                                                           */
  37. /*  OldCrossDb     Database containing cross references from previous run.   */
  38. /*  NewCrossDb     Writable database of cross references from this run.      */
  39. /*                                                                           */
  40. /*****************************************************************************/
  41.  
  42. OBJECT OldCrossDb, NewCrossDb;
  43.  
  44.  
  45. /*****************************************************************************/
  46. /*                                                                           */
  47. /*  #define SymToNum(db, sym, num, gall)                                     */
  48. /*                                                                           */
  49. /*  Set num to the number used to refer to sym in database db.  If sym is    */
  50. /*  not currently referred to in db, create a new number and record sym.     */
  51. /*  If gall is true, sym is the target of galleys stored in this database.   */
  52. /*  Store in boolean fields db_targ(link) and is_extern_target(sym).         */
  53. /*                                                                           */
  54. /*****************************************************************************/
  55.  
  56. #define SymToNum(db, sym, num, gall)                    \
  57. { OBJECT link, yy;  int count;                        \
  58.   count = 0;                                \
  59.   for( link = Down(db);  link != db;  link = NextDown(link) )        \
  60.   { Child(yy, link);                            \
  61.     assert(type(yy)==CROSS_SYM || type(yy)==ACAT, "SymToNum: yy!");    \
  62.     if( type(yy) != CROSS_SYM )  continue;                \
  63.     if( symb(yy) == sym )  break;                    \
  64.     if( number(link) > count )  count = number(link);            \
  65.   }                                    \
  66.   if( link == db )                            \
  67.   { if( cross_sym(sym) == nil )  CrossInit(sym);            \
  68.     link = Link(db, cross_sym(sym));                    \
  69.     number(link) = count + 1;                        \
  70.     db_targ(link) = FALSE;                        \
  71.   }                                    \
  72.   num = number(link);                            \
  73.   if( gall )  db_targ(link) = is_extern_target(sym) =            \
  74.                 uses_extern_target(sym) = TRUE;        \
  75. } /* end SymToNum */
  76.  
  77.  
  78. /*@::NumToSym(), DbCreate()@**************************************************/
  79. /*                                                                           */
  80. /*  #define NumToSym(db, num, sym)                                           */
  81. /*                                                                           */
  82. /*  Set sym to the symbol which is referred to in database db by num.        */
  83. /*                                                                           */
  84. /*****************************************************************************/
  85.  
  86. #define NumToSym(db, num, sym)                        \
  87. { OBJECT link, y;                            \
  88.   for( link = Down(db);  link != db;  link = NextDown(link) )        \
  89.   { Child(y, link);                            \
  90.     if( type(y) == CROSS_SYM && number(link) == num )  break;        \
  91.   }                                    \
  92.   if( link == db )  Error(INTERN, &fpos(db), "NumToSym: no sym!");    \
  93.   assert( type(y) == CROSS_SYM, "NumToSym: y!" );            \
  94.   sym = symb(y);                            \
  95. } /* end NumToSym */
  96.  
  97.  
  98. /*****************************************************************************/
  99. /*                                                                           */
  100. /*  OBJECT DbCreate(x)                                                       */
  101. /*                                                                           */
  102. /*  Create a new writable database with name (i.e. file stem) x and file     */
  103. /*  position fpos for error messages.                                        */
  104. /*                                                                           */
  105. /*****************************************************************************/
  106.  
  107. OBJECT DbCreate(x)
  108. OBJECT x;
  109. { OBJECT db = x;
  110.   debug1(DBS, D, "DbCreate(%s)", string(db));
  111.   assert( is_word(type(x)), "DbCreate: !is_word(type(x))" );
  112.   reading(db) = FALSE;  filep(db) = null;
  113.   debug1(DBS, D, "DbCreate returning %s", EchoObject(db));
  114.   return db;
  115. } /* end DbCreate */
  116.  
  117.  
  118. /*@::DbInsert()@**************************************************************/
  119. /*                                                                           */
  120. /*  DbInsert(db, gall, sym, tag, seq, dfnum, dfpos)                          */
  121. /*                                                                           */
  122. /*  Insert a new entry into writable database db.  The primary key of the    */
  123. /*  entry has these three parts:                                             */
  124. /*                                                                           */
  125. /*      gall        TRUE if inserting a galley                               */
  126. /*      sym         The symbol which is the target of this entry             */
  127. /*      tag         The tag of this target (must be a non-null string)       */
  128. /*                                                                           */
  129. /*  There is also an auxiliary key, seq, which enforces an ordering on       */
  130. /*  entries with equal primary keys but is not itself ever retrieved.  This  */
  131. /*  ordering is used for sorted galleys.  The value of the entry has the     */
  132. /*  following parts:                                                         */
  133. /*                                                                           */
  134. /*      dfnum       The file containing the object                           */
  135. /*      dfpos       The position of the object in that file                  */
  136. /*                                                                           */
  137. /*****************************************************************************/
  138.  
  139. DbInsert(db, gall, sym, tag, seq, dfnum, dfpos)
  140. OBJECT db;  BOOLEAN gall;  OBJECT sym;  FULL_CHAR *tag, *seq;
  141. FILE_NUM dfnum;  long dfpos;
  142. { int symnum;
  143.   FULL_CHAR buff[MAX_LINE];
  144.   assert( is_word(type(db)), "DbInsert: db!" );
  145.   assert( tag[0] != '\0', "DbInsert: null tag!" );
  146.   assert( seq[0] != '\0', "DbInsert: null seq!" );
  147.   ifdebug(DPP, D, ProfileOn("DbInsert"));
  148.   debug6(DBS, D, "DbInsert(%s, %s, %s, %s, %s, %s, dfpos)",
  149.     string(db), bool(gall), SymName(sym), tag, seq,
  150.     dfnum == NO_FILE ? AsciiToFull(".") : FileName(dfnum));
  151.   if( reading(db) )  Error(INTERN, &fpos(db), "insert into reading database!");
  152.   if( filep(db) == null )
  153.   { if( StringLength(string(db)) + StringLength(NEW_INDEX_SUFFIX) >= MAX_LINE )
  154.       Error(FATAL, no_fpos, "database file name %s%s is too long",
  155.     string(db), NEW_INDEX_SUFFIX);
  156.     StringCopy(buff, string(db));
  157.     StringCat(buff, NEW_INDEX_SUFFIX);
  158.     filep(db) = StringFOpen(buff, "w");
  159.     if( filep(db) == null )
  160.       Error(FATAL, &fpos(db), "cannot write to database file %s", buff);
  161.   }
  162.   if( dfnum != NO_FILE )
  163.   { StringCopy(buff, FileName(dfnum));
  164.     StringCopy(&buff[StringLength(buff)-StringLength(DATA_SUFFIX)], STR_EMPTY);
  165.   }
  166.   else StringCopy(buff, AsciiToFull("."));
  167.   SymToNum(db, sym, symnum, gall);
  168.   ifdebug(DBS, D,
  169.   fprintf(stderr, "  -> %s%d&%s\t%s\t%ld\t%s\n", gall ? "&" : "", symnum,
  170.     tag, seq, dfpos, buff);
  171.   );
  172.   fprintf(filep(db), "%s%d&%s\t%s\t%ld\t%s\n", gall ? "&" : "", symnum,
  173.     tag, seq, dfpos, buff);
  174.   debug0(DBS, D, "DbInsert returning.");
  175.   ifdebug(DPP, D, ProfileOff("DbInsert"));
  176. } /* end DbInsert */
  177.  
  178.  
  179. /*@::DbConvert(), DbClose()@**************************************************/
  180. /*                                                                           */
  181. /*  DbConvert(db, full_name)                                                 */
  182. /*                                                                           */
  183. /*  Convert database db from writable to readable, then dispose it.          */
  184. /*  full_name is TRUE if symbols are to be known by their full path name.    */
  185. /*                                                                           */
  186. /*****************************************************************************/
  187.  
  188. DbConvert(db, full_name)
  189. OBJECT db;  BOOLEAN full_name;
  190. { FULL_CHAR oldname[MAX_LINE+10], newname[MAX_LINE];
  191.   char buff[2*MAX_LINE + 20];
  192.   OBJECT link, y;
  193.   ifdebug(DPP, D, ProfileOn("DbConvert"));
  194.   debug2(DBS, D, "DbConvert( %d %s )", (int) db,string(db));
  195.   if( reading(db) )  Error(INTERN, &fpos(db), "DbConvert: reading database!");
  196.   StringCopy(newname, string(db));
  197.   StringCat(newname, INDEX_SUFFIX);
  198.   StringCopy(oldname, string(db));
  199.   StringCat(oldname, NEW_INDEX_SUFFIX);
  200.   if( filep(db) != null )
  201.   { for( link = Down(db);  link != db;  link = NextDown(link) )
  202.     { Child(y, link);
  203.       assert( type(y) == CROSS_SYM || type(y) == ACAT, "DbConvert: y!" );
  204.       if( type(y) != CROSS_SYM )  continue;
  205.       fprintf(filep(db), "%s %d %s\n",
  206.     db_targ(link) ? "#target" : "#symbol",
  207.     number(link),
  208.     full_name ? FullSymName(symb(y), AsciiToFull(" ")) : SymName(symb(y)));
  209.     }
  210.     fclose(filep(db));
  211.     sprintf(buff, "sort -o %s %s", newname, oldname);
  212.     system(buff);
  213.   }
  214.   else StringUnlink(newname);
  215.   StringUnlink(oldname);
  216.   DeleteNode(db);
  217.   debug0(DBS, D, "DbConvert returning.");
  218.   ifdebug(DPP, D, ProfileOff("DbConvert"));
  219. } /* end DbConvert */
  220.  
  221.  
  222. /*****************************************************************************/
  223. /*                                                                           */
  224. /*  DbClose(db)                                                              */
  225. /*                                                                           */
  226. /*  Close readable database db.                                              */
  227. /*                                                                           */
  228. /*****************************************************************************/
  229.  
  230. DbClose(db)
  231. OBJECT db;
  232. { if( db != nil && filep(db) != NULL )
  233.   {  fclose(filep(db));
  234.      filep(db) = NULL;
  235.   }
  236. } /* end DbClose */
  237.  
  238.  
  239. /*@::DbLoad()@****************************************************************/
  240. /*                                                                           */
  241. /*  OBJECT DbLoad(stem, fpath, create, symbs)                                */
  242. /*                                                                           */
  243. /*  Open for reading the database whose index file name is string(stem).li.  */
  244. /*  This file has not yet been defined; its search path is fpath.  If it     */
  245. /*  will not open and create is true, try creating it from string(stem).ld.  */
  246. /*                                                                           */
  247. /*  symbs is an ACAT of CLOSUREs showing the symbols that the database may   */
  248. /*  contain; or nil if the database may contain any symbol.                  */
  249. /*                                                                           */
  250. /*****************************************************************************/
  251.  
  252. OBJECT DbLoad(stem, fpath, create, symbs)
  253. OBJECT stem;  int fpath;  BOOLEAN create;  OBJECT symbs;
  254. { FILE *fp;  OBJECT db, t, res, tag, par, sym, link, y;
  255.   int i, lnum, num, count;  FILE_NUM index_fnum, dfnum;  long dfpos;
  256.   BOOLEAN gall;  FULL_CHAR line[MAX_LINE], sym_name[MAX_LINE];
  257.   ifdebug(DPP, D, ProfileOn("DbLoad"));
  258.   debug3(DBS, D, "DbLoad(%s, %d, %s, -)", string(stem), fpath, bool(create));
  259.  
  260.   /* open or else create index file fp */
  261.   index_fnum = DefineFile(string(stem), INDEX_SUFFIX, &fpos(stem), INDEX_FILE,
  262.          fpath);
  263.   fp = OpenFile(index_fnum, create, FALSE);
  264.   if( fp == null && create )
  265.   { db = nil;
  266.     dfnum = DefineFile(string(stem), DATA_SUFFIX, &fpos(stem),
  267.       DATABASE_FILE, DATABASE_PATH);
  268.     dfpos = 0L;  LexPush(dfnum, 0, DATABASE_FILE);  t = LexGetToken();
  269.     while( type(t) == LBR )
  270.     { res = Parse(&t, StartSym, FALSE, FALSE);
  271.       if( t != nil || type(res) != CLOSURE )  Error(FATAL, &fpos(res),
  272.     "syntax error in database file %s", FileName(dfnum));
  273.       assert( symbs != nil, "DbLoad: create && symbs == nil!" );
  274.       if( symbs != nil )
  275.       {    for( link = Down(symbs);  link != symbs;  link = NextDown(link) )
  276.     { Child(y, link);
  277.       if( type(y) == CLOSURE && actual(y) == actual(res) )  break;
  278.     }
  279.     if( link == symbs )  Error(FATAL, &fpos(res),
  280.       "%s found in database but not declared in %s line",
  281.       SymName(actual(res)), KW_DATABASE);
  282.       }
  283.       for( tag = nil, link = Down(res);  link != res;  link = NextDown(link) )
  284.       {    Child(par, link);
  285.     if( type(par) == PAR && is_tag(actual(par)) && Down(par) != par )
  286.     { Child(tag, Down(par));
  287.       break;
  288.     }
  289.       }
  290.       if( tag == nil )
  291.     Error(FATAL, &fpos(res), "database symbol %s has no tag", SymName(res));
  292.       tag = ReplaceWithTidy(tag);
  293.       if( !is_word(type(tag)) )
  294.     Error(FATAL, &fpos(res), "database symbol tag is not a simple word");
  295.       if( StringEqual(string(tag), STR_EMPTY) )
  296.     Error(FATAL, &fpos(res), "database symbol tag is an empty word");
  297.       if( db == nil )
  298.       {    StringCopy(line, FileName(dfnum));
  299.     i = StringLength(line) - StringLength(INDEX_SUFFIX);
  300.     assert( i > 0, "DbLoad: FileName(dfnum) (1)!" );
  301.     StringCopy(&line[i], STR_EMPTY);
  302.     db = DbCreate(MakeWord(WORD, line, &fpos(stem)));
  303.       }
  304.       DbInsert(db, FALSE, actual(res), string(tag), STR_ZERO, NO_FILE, dfpos);
  305.       DisposeObject(res);  dfpos = LexNextTokenPos();  t = LexGetToken();
  306.     }
  307.     if( type(t) != END )
  308.       Error(FATAL, &fpos(t), "%s or end of file expected here", KW_LBR);
  309.     LexPop();
  310.     if( db == nil )
  311.     { StringCopy(line, FileName(dfnum));
  312.       i = StringLength(line) - StringLength(INDEX_SUFFIX);
  313.       assert( i > 0, "DbLoad: FileName(dfnum) (2)!" );
  314.       StringCopy(&line[i], STR_EMPTY);
  315.       db = DbCreate(MakeWord(WORD, line, &fpos(stem)));
  316.     }
  317.     DbConvert(db, FALSE);
  318.     if( (fp = OpenFile(index_fnum, FALSE, FALSE)) == null )
  319.       Error(FATAL, &fpos(db), "cannot open database file %s",
  320.       FileName(index_fnum));
  321.   }
  322.  
  323.   /* set up database record */
  324.   StringCopy(line, FileName(index_fnum));
  325.   i = StringLength(line) - StringLength(INDEX_SUFFIX);
  326.   assert( i > 0, "DbLoad: FileName(index_fnum)!" );
  327.   StringCopy(&line[i], STR_EMPTY);
  328.   db = MakeWord(WORD, line, &fpos(stem));
  329.   reading(db) = TRUE;  filep(db) = fp;
  330.   if( symbs != nil )
  331.   { assert( type(symbs) = ACAT, "DbLoad: type(symbs)!" );
  332.     Link(db, symbs);
  333.   }
  334.   if( fp == null )
  335.   { debug1(DBS, D, "DbLoad returning (empty) %s", string(db));
  336.     ifdebug(DPP, D, ProfileOff("DbLoad"));
  337.     return db;
  338.   }
  339.  
  340.   /* read header lines of index file, find its symbols */
  341.   left_pos(db) = 0;  lnum = 0;
  342.   while( StringFGets(line, MAX_LINE, fp) != NULL && line[0] == '#' )
  343.   { lnum++;
  344.     left_pos(db) = (int) ftell(fp);
  345.     gall = StringBeginsWith(line, AsciiToFull("#target "));
  346.     sscanf( (char *) line, gall ? "#target %d" : "#symbol %d", &num);
  347.     for( i = 8;  line[i] != CH_SPACE && line[i] != '\0';  i++ );
  348.     if( symbs == nil )
  349.     {
  350.       /* any symbols are possible, full path names in index file required */
  351.       count = 0;  sym = StartSym;
  352.       while( line[i] != CH_NEWLINE && line[i] != '\0' )
  353.       {    PushScope(sym, FALSE, FALSE);  count++;
  354.     sscanf( (char *) &line[i+1], "%s", sym_name);
  355.     sym = SearchSym(sym_name, StringLength(sym_name));
  356.     i += StringLength(sym_name) + 1;
  357.       }
  358.       for( i = 1;  i <= count;  i++ )  PopScope();
  359.     }
  360.     else
  361.     {
  362.       /* only symbs symbols possible, full path names not required */
  363.       sym = nil;
  364.       sscanf( (char *) &line[i+1], "%s", sym_name);
  365.       for( link = Down(symbs);  link != symbs;  link = NextDown(link) )
  366.       {    Child(y, link);
  367.     assert( type(y) == CLOSURE, "DbLoad: type(y) != CLOSURE!" );
  368.     if( StringEqual(sym_name, SymName(actual(y))) )
  369.     { sym = actual(y);
  370.       break;
  371.     }
  372.       }
  373.     }
  374.     if( sym != nil && sym != StartSym )
  375.     { if( cross_sym(sym) == nil )  CrossInit(sym);
  376.       link = Link(db, cross_sym(sym));
  377.       number(link) = num;  db_targ(link) = gall;
  378.       if( gall )  is_extern_target(sym) = uses_extern_target(sym) = TRUE;
  379.     }
  380.     else
  381.     { Error(WARN, &fpos(db), "undefined symbol in database file %s (line %d)",
  382.             FileName(index_fnum), lnum);
  383.       debug1(DBS, D, "DbLoad returning %s (error)", string(db));
  384.       fclose(filep(db));  filep(db) = null;  /* effectively an empty database */
  385.       ifdebug(DPP, D, ProfileOff("DbLoad"));
  386.       return db;
  387.     }
  388.   }
  389.   debug1(DBS, D, "DbLoad returning %s", string(db));
  390.   ifdebug(DPP, D, ProfileOff("DbLoad"));
  391.   return db;
  392. } /* end DbLoad */
  393.  
  394.  
  395. /*@::SearchFile()@************************************************************/
  396. /*                                                                           */
  397. /*  static BOOLEAN SearchFile(fp, left, right, str, line)                    */
  398. /*                                                                           */
  399. /*  File fp is a text file.  left is the beginning of a line, right is the   */
  400. /*  end of a line.   Search the file by binary search for a line beginning   */
  401. /*  with str.  If found, return it in line, else return FALSE.               */
  402. /*                                                                           */
  403. /*****************************************************************************/
  404.  
  405. static BOOLEAN SearchFile(fp, left, right, str, line)
  406. FILE *fp;  int left, right;  FULL_CHAR *str, *line;
  407. { int l, r, mid, mid_end;  FULL_CHAR buff[MAX_LINE];  BOOLEAN res;
  408.   ifdebug(DPP, D, ProfileOn("SearchFile"));
  409.   debug3(DBS, DD, "SearchFile(fp, %d, %d, %s, line)", left, right, str);
  410.  
  411.   l = left;  r = right;
  412.   while( l <= r )
  413.   {
  414.     /* loop invt: (l==0 or fp[l-1]==CH_NEWLINE) and (fp[r] == CH_NEWLINE)    */
  415.     /* and first key >= str lies in the range fp[l..r+1]                     */
  416.  
  417.     /* find line near middle of the range; mid..mid_end brackets it */
  418.     debug2(DBS, DD, "  start loop: l = %d, r = %d", l, r);
  419.     mid = (l + r)/2;
  420.     fseek(fp, (long) mid, 0);
  421.     do { mid++; } while( getc(fp) != CH_NEWLINE );
  422.     if( mid == r + 1 )
  423.     { mid = l;
  424.       fseek(fp, (long) mid, 0);
  425.     }
  426.     StringFGets(line, MAX_LINE, fp);
  427.     mid_end = (int) ftell(fp) - 1;
  428.     debug3(DBS, DD, "  mid: %d, mid_end: %d, line: %s", mid, mid_end, line);
  429.     assert( l <= mid,      "SearchFile: l > mid!"        );
  430.     assert( mid < mid_end, "SearchFile: mid >= mid_end!" );
  431.     assert( mid_end <= r,  "SearchFile: mid_end > r!"    );
  432.  
  433.     /* compare str with this line and prepare next step */
  434.     debug2(DBS, DD, "  comparing key %s with line %s", str, line);
  435.     if( StringLessEqual(str, line) )  r = mid - 1;
  436.     else l = mid_end + 1;
  437.   } /* end while */
  438.  
  439.   /* now first key >= str lies in fp[l]; compare it with str */
  440.   if( l < right )
  441.   { fseek(fp, (long) l, 0);
  442.     StringFGets(line, MAX_LINE, fp);
  443.     sscanf( (char *) line, "%s", buff);
  444.     res = StringEqual(str, buff);
  445.   }
  446.   else res = FALSE;
  447.   debug1(DBS, DD, "SearchFile returning %s", bool(res));
  448.   ifdebug(DPP, D, ProfileOff("SearchFile"));
  449.   return res;
  450. } /* end SearchFile */
  451.  
  452.  
  453. /*@::DbRetrieve()@************************************************************/
  454. /*                                                                           */
  455. /*  BOOLEAN DbRetrieve(db, gall, sym, tag, seq, dfnum, dfpos, cont)          */
  456. /*                                                                           */
  457. /*  Retrieve the first entry of database db with the given gall, sym and     */
  458. /*  tag.  Set *seq, *dfnum, *dfpos to the associated value.                  */
  459. /*  Set *cont to a private value for passing to DbRetrieveNext.              */
  460. /*                                                                           */
  461. /*****************************************************************************/
  462.  
  463. BOOLEAN DbRetrieve(db, gall, sym, tag, seq, dfnum, dfpos, cont)
  464. OBJECT db;  BOOLEAN gall;  OBJECT sym;  FULL_CHAR *tag, *seq;
  465. FILE_NUM *dfnum;  long *dfpos;  long *cont;
  466. { int symnum;  FULL_CHAR line[MAX_LINE], buff[MAX_LINE];  OBJECT y;
  467.   ifdebug(DPP, D, ProfileOn("DbRetrieve"));
  468.   debug4(DBS, D, "DbRetrieve(%s, %s%s&%s)", string(db), gall ? "&" : "",
  469.     SymName(sym), tag);
  470.   if( !reading(db) || filep(db) == null )
  471.   { debug0(DBS, D, "DbRetrieve returning FALSE (empty or not reading)");
  472.     ifdebug(DPP, D, ProfileOff("DbRetrieve"));
  473.     return FALSE;
  474.   }
  475.   SymToNum(db, sym, symnum, FALSE);
  476.   sprintf( (char *) buff, "%s%d&%s", gall ? "&" : "", symnum, tag);
  477.   fseek(filep(db), 0L, 2);
  478.   if( !SearchFile(filep(db), (int) left_pos(db), (int) ftell(filep(db)) - 1,
  479.     buff, line) )
  480.   { debug0(DBS, D, "DbRetrieve returning FALSE (key not present)");
  481.     ifdebug(DPP, D, ProfileOff("DbRetrieve"));
  482.     return FALSE;
  483.   }
  484.   sscanf( (char *) line, "%*s\t%s\t%ld\t%[^\n]", seq, dfpos, buff);
  485.   if( StringEqual(buff, AsciiToFull(".")) )
  486.   { StringCopy(buff, string(db));
  487.   }
  488.   *dfnum = FileNum(buff, DATA_SUFFIX);
  489.   if( *dfnum == NO_FILE )  /* can only occur in cross reference database */
  490.     *dfnum = DefineFile(buff, DATA_SUFFIX, &fpos(db),
  491.       DATABASE_FILE, SOURCE_PATH);
  492.   *cont = ftell(filep(db));
  493.   Child(y, Down(db));
  494.   debug2(DBS, D, "DbRetrieve returning TRUE (in %s at %ld)",
  495.     FileName(*dfnum), *dfpos);
  496.   ifdebug(DPP, D, ProfileOff("DbRetrieve"));
  497.   return TRUE;
  498. } /* end DbRetrieve */
  499.  
  500.  
  501. /*@::DbRetrieveNext()@********************************************************/
  502. /*                                                                           */
  503. /*  BOOLEAN DbRetrieveNext(db, gall, sym, tag, seq, dfnum, dfpos, cont)      */
  504. /*                                                                           */
  505. /*  Retrieve the entry of database db pointed to by *cont.                   */
  506. /*  Set *gall, *sym, *tag, *seq, *dfnum, *dfpos to the value of the entry.   */
  507. /*  Reset *cont to the next entry for passing to the next DbRetrieveNext.    */
  508. /*                                                                           */
  509. /*****************************************************************************/
  510.  
  511. BOOLEAN DbRetrieveNext(db, gall, sym, tag, seq, dfnum, dfpos, cont)
  512. OBJECT db;  BOOLEAN *gall;  OBJECT *sym;  FULL_CHAR *tag, *seq;
  513. FILE_NUM *dfnum;  long *dfpos;  long *cont;
  514. { FULL_CHAR line[MAX_LINE], fname[MAX_LINE]; int symnum;
  515.   ifdebug(DPP, D, ProfileOn("DbRetrieveNext"));
  516.   debug2(DBS, D, "DbRetrieveNext( %s, %ld )", string(db), *cont);
  517.   if( !reading(db) )  Error(INTERN, &fpos(db), "DbRetrieveNext: writing!");
  518.   if( filep(db) == null )
  519.   { debug0(DBS, D, "DbRetrieveNext returning FALSE (empty database)");
  520.     ifdebug(DPP, D, ProfileOff("DbRetrieveNext"));
  521.     return FALSE;
  522.   }
  523.   fseek(filep(db), *cont == 0L ? (long) left_pos(db) : *cont, 0);
  524.   if( StringFGets(line, MAX_LINE, filep(db)) == NULL )
  525.   { debug0(DBS, D, "DbRetrieveNext returning FALSE (no successor)");
  526.     ifdebug(DPP, D, ProfileOff("DbRetrieveNext"));
  527.     return FALSE;
  528.   }
  529.   *gall = (line[0] == '&' ? 1 : 0);
  530.   sscanf( (char *) &line[*gall], "%d&%s\t%s\t%ld\t%[^\n]",
  531.     &symnum, tag, seq,dfpos,fname);
  532.   if( StringEqual(fname, AsciiToFull(".")) )
  533.   { StringCopy(fname, string(db));
  534.   }
  535.   *dfnum = FileNum(fname, DATA_SUFFIX);
  536.   if( *dfnum == NO_FILE )  /* can only occur in cross reference database */
  537.     *dfnum = DefineFile(fname, DATA_SUFFIX, &fpos(db),
  538.       DATABASE_FILE, SOURCE_PATH);
  539.   NumToSym(db, symnum, *sym);  *cont = ftell(filep(db));
  540.   debug2(DBS, D, "DbRetrieveNext returning TRUE (in %s at %ld)",
  541.     FileName(*dfnum), *dfpos);
  542.   ifdebug(DPP, D, ProfileOff("DbRetrieveNext"));
  543.   return TRUE;
  544. } /* end DbRetrieveNext */
  545.