\File{viewbrowser.c},{05:35},{May 19 1991}
\L{\LB{}}
\L{\LB{\C{}\/* test.c tests out viewbrowser interface.}}
\L{\LB{   two critical functions:}}
\L{\LB{      Get\_Subjects() \-\-\- gets a list of up to 4096 subjects and their}}
\L{\LB{      corresponding transaction numbers.}}
\L{\LB{}}
\L{\LB{      got everything working!!!!}}
\L{\LB{      }}
\L{\LB{   \-\- ukim *\/\CE{}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\K{\#include} \<stdio.h\>   }}
\L{\LB{\K{\#include} \<discuss\/discuss.h\>}}
\L{\LB{\K{\#include} \S{}\"lucy.h\"\SE{}}}
\L{\LB{\K{\#include} \S{}\"params.h\"\SE{}}}
\L{\LB{}}
\L{\LB{\K{\#define} False 0}}
\L{\LB{\K{\#define} True 1}}
\L{\LB{}}
\L{\LB{\K{typedef} \K{int} Boolean;}}
\L{\LB{}}
\L{\LB{\K{extern} tfile unix\_tfile();}}
\L{\LB{}}
\L{\LB{\K{struct} browse\_struct \{}}
\L{\LB{  \K{char} subject[LINE\_SIZE];}}
\L{\LB{  \K{int} tnum; \C{}\/* transaction number *\/\CE{}}}
\L{\LB{\} browser[BROWSER\_MAX];}}
\L{\LB{\C{}\/* up to 4096 possible transactions handled *\/\CE{}}}
\L{\LB{}}
\L{\LB{\K{int} num\_browser = 0; \C{}\/* keeps track of how many subjects are in the browser *\/\CE{}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\K{void} Get\_Subjects()}}
\L{\LB{\C{}\/* works *\/\CE{}}}
\L{\LB{\{}}
\L{\LB{  \K{int} i;}}
\L{\LB{  Boolean exit\_flag;}}
\L{\LB{  \K{int} cur\_tnum, next\_tnum; \C{}\/* current and next transaction numbers *\/\CE{}}}
\L{\LB{  ltrn *tran\_info;  \C{}\/* transaction info structure *\/\CE{}}}
\L{\LB{  \K{int} err\_code; \C{}\/* error code *\/\CE{}}}
\L{\LB{}}
\L{\LB{  \C{}\/* initialize luc\_init() to get in touch with the browser meeting.*\/\CE{}}}
\L{\LB{  luc\_init(); \C{}\/* initialize lucy discuss meetings *\/\CE{}}}
\L{\LB{  luv\_curmtgp = \&luv\_bmtg; \C{}\/* the current meeting will be the browser *\/\CE{}}}
\L{\LB{  \C{}\/* luv\_curmtgp is a virtual constant after this point *\/\CE{}}}
\L{\LB{  \C{}\/* algo: get 1st transaction using luc\_next().  and also get}}
\L{\LB{     trn number for each transaction.  iterate until no more}}
\L{\LB{     transactions or browser is full *\/\CE{}}}
\L{\LB{  }}
\L{\LB{  \C{}\/* am assuming that an error indicates that no more transactions}}
\L{\LB{     are available *\/\CE{}}}
\L{\LB{}}
\L{\LB{  num\_browser = 0; \C{}\/* initialize *\/\CE{}}}
\L{\LB{  next\_tnum = 0;  \C{}\/* next transaction number *\/\CE{}}}
\L{\LB{  exit\_flag = False;}}
\L{\LB{  \K{while} (exit\_flag == False) \{}}
\L{\LB{    cur\_tnum = next\_tnum;}}
\L{\LB{    luc\_next(luv\_curmtgp, cur\_tnum, \&next\_tnum);}}
\L{\LB{    \K{if} (next\_tnum != 0) \{}}
\L{\LB{      luc\_trn\_info(luv\_curmtgp, next\_tnum, \&tran\_info);}}
\L{\LB{}}
\L{\LB{      \C{}\/* assign data to browser *\/\CE{}}}
\L{\LB{      strncpy(browser[num\_browser].subject, tran\_info\-\>subject, 128);}}
\L{\LB{      browser[num\_browser].tnum = next\_tnum;}}
\L{\LB{}}
\L{\LB{      num\_browser++;}}
\L{\LB{    \} \K{else} \{ \C{}\/* no more transactions available *\/\CE{}}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{      fprintf(stderr, \S{}\"Get\_Subjects: no more transactions available.\!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{}}
\L{\LB{      }}
\L{\LB{      exit\_flag = True;}}
\L{\LB{    \}}}
\L{\LB{}}
\L{\LB{    \K{if} (num\_browser == BROWSER\_MAX) \{}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{      fprintf(stderr, \S{}\"Get\_Subjects: Browser is full.\!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{      }}
\L{\LB{      }}
\L{\LB{      exit\_flag = True;}}
\L{\LB{    \}}}
\L{\LB{}}
\L{\LB{  \} \C{}\/* end while *\/\CE{}}}
\L{\LB{}}
\L{\LB{\}  \C{}\/* end Get\_Subject() *\/\CE{}}}
\L{\LB{    }}
\L{\LB{    }}
\L{\LB{}}
\L{\LB{\K{void} Get\_Transaction(\K{int} tnum, \K{char} buffer[], \K{int} buflen)}}
\L{\LB{\C{}\/* retries the contents of a transaction, given the transaction}}
\L{\LB{   number.  buflen should be maximum size of buffer being passed.}}
\L{\LB{   Be sure to call Get\_Subjects() to initialize lucy meetings beforehand.  *\/\CE{}}}
\L{\LB{     }}
\L{\LB{\{}}
\L{\LB{  \K{char} tmpfname[LINE\_SIZE]; \C{}\/* name of temporary file *\/\CE{}}}
\L{\LB{  FILE *tempfile; \C{}\/* temporary file *\/\CE{}}}
\L{\LB{  tfile tf;  \C{}\/* need a tfile store contents of transaction in *\/\CE{}}}
\L{\LB{  \K{int} terror; \C{}\/* place to hold error numbers that meetings server}}
\L{\LB{}\Tab{16}{ might return *\/\CE{}}}
\L{\LB{  \C{}\/* creat a tfile :}}
\L{\LB{     create a temporary file, and open it.}}
\L{\LB{     use unix\_tfile to translate it into a tfile.}}
\L{\LB{     call get\_trn, then read the temporary file }}
\L{\LB{     to output to fprintf *\/\CE{}}}
\L{\LB{}}
\L{\LB{  tmpnam(tmpfname); \C{}\/* create a temporary file name *\/\CE{}}}
\L{\LB{}}
\L{\LB{  tempfile = freopen(tmpfname, \S{}\"w+\"\SE{}, stdout); }}
\L{\LB{}}
\L{\LB{  \K{if} (tempfile == NULL) \{}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{    fprintf(stderr, \S{}\"could not create a temporary file.\!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{    }}
\L{\LB{    }}
\L{\LB{    exit(1);}}
\L{\LB{  \}}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{}}
\L{\LB{  fprintf(stderr, \S{}\"creating unix wrapper \!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  tf = unix\_tfile(1); \C{}\/* put a tfile wrapper around the tempfile *\/\CE{}}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"finished creating unix wrapper \!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{  dsc\_get\_trn(\&(luv\_curmtgp\-\>nb), tnum, tf, \&terror);}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"get\_trn returned error code \%d \!n\"\SE{}, terror);}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{}}
\L{\LB{  tdestroy(tf); \C{}\/* free the memory allocated to the tfile *\/\CE{}}}
\L{\LB{  }}
\L{\LB{  fclose(tempfile);  \C{}\/* flush out to disk before reading it back in *\/\CE{}}}
\L{\LB{  \C{}\/* need to close the file, to flush out the data, so that we}}
\L{\LB{     can read it back in. *\/\CE{}}}
\L{\LB{}}
\L{\LB{  \C{}\/* read from buffer *\/\CE{}}}
\L{\LB{  }}
\L{\LB{  tempfile = fopen(tmpfname, \S{}\"r\"\SE{});}}
\L{\LB{  fread(buffer, \K{sizeof}(\K{char}), buflen, tempfile); }}
\L{\LB{  }}
\L{\LB{  \C{}\/* display transaction contents *\/\CE{}}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"Contents of transaction are \!n\%s\!n\-\-End of File\-\-\!n\"\SE{},}}
\L{\LB{}\Tab{8}{  buffer);}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{  fclose(tempfile); }}
\L{\LB{  unlink(tmpfname); \C{}\/* get rid of our temporary file *\/\CE{}}}
\L{\LB{}}
\L{\LB{\}  \C{}\/* end of get\_transaction() *\/\CE{}  }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{\Proc{prev\_obsolete\_code}prev\_obsolete\_code()}}
\L{\LB{\{}}
\L{\LB{  ltrn *tran\_info;  \C{}\/* transaction info structure *\/\CE{}}}
\L{\LB{  \K{char} subject[LINE\_SIZE];  \C{}\/* subject of transaction *\/\CE{}}}
\L{\LB{  \K{int} next;}}
\L{\LB{  \K{int} tnum; \C{}\/* transaction number *\/\CE{}}}
\L{\LB{  tfile tf;  \C{}\/* need a tfile store contents of transaction in *\/\CE{}}}
\L{\LB{  \K{int} terror; \C{}\/* place to hold error numbers that meetings server}}
\L{\LB{}\Tab{16}{ might return *\/\CE{}}}
\L{\LB{  \K{char} buffer[QUESTION\_SIZE]; \C{}\/* place to hold contents of transaction *\/\CE{}}}
\L{\LB{  \K{char} text[QUESTION\_SIZE]; \C{}\/* buffer for tfile *\/\CE{}}}
\L{\LB{  \K{char} tmpfname[LINE\_SIZE]; \C{}\/* name of temporary file *\/\CE{}}}
\L{\LB{  FILE *tempfile; \C{}\/* temporary file *\/\CE{}}}
\L{\LB{  }}
\L{\LB{  luc\_init();  \C{}\/* initialize lucy discuss meetings *\/\CE{}}}
\L{\LB{  luv\_curmtgp = \&luv\_bmtg; \C{}\/* the current meeting will be the browser *\/\CE{}}}
\L{\LB{  luc\_next(luv\_curmtgp, 0, \&next);  \C{}\/* get first (next) transaction. *\/\CE{}}}
\L{\LB{  luc\_trn\_info(luv\_curmtgp, next, \&tran\_info);}}
\L{\LB{}}
\L{\LB{  strcpy(subject, tran\_info\-\>subject);}}
\L{\LB{  tnum = tran\_info\-\>current; \C{}\/* transaction number of current transaction *\/\CE{}}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"Subject of transaction: \%s \!n\"\SE{}, subject);}}
\L{\LB{  fprintf(stderr, \S{}\"Transaction \# is \%d \!n\"\SE{}, tnum);}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{}}
\L{\LB{  \C{}\/* creat a tfile :}}
\L{\LB{     create a temporary file, and open it.}}
\L{\LB{     use unix\_tfile to translate it into a tfile.}}
\L{\LB{     call get\_trn, then read the temporary file }}
\L{\LB{     to output to fprintf *\/\CE{}}}
\L{\LB{}}
\L{\LB{  tmpnam(tmpfname); \C{}\/* create a temporary file name *\/\CE{}}}
\L{\LB{}}
\L{\LB{  tempfile = freopen(tmpfname, \S{}\"w+\"\SE{}, stdout); }}
\L{\LB{}}
\L{\LB{  \K{if} (tempfile == NULL) \{}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{    fprintf(stderr, \S{}\"could not create a temporary file.\!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{    }}
\L{\LB{    }}
\L{\LB{    exit(1);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"creating unix wrapper \!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{  tf = unix\_tfile(1); \C{}\/* put a tfile wrapper around the tempfile *\/\CE{}}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"finished creating unix wrapper \!n\"\SE{});}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{  dsc\_get\_trn(\&(luv\_curmtgp\-\>nb), tnum, tf, \&terror);}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"get\_trn returned error code \%d \!n\"\SE{}, terror);}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{}}
\L{\LB{  tdestroy(tf); \C{}\/* free the memory allocated to the tfile *\/\CE{}}}
\L{\LB{  }}
\L{\LB{  fclose(tempfile);  \C{}\/* flush out to disk before reading it back in *\/\CE{}}}
\L{\LB{  \C{}\/* need to close the file, to flush out the data, so that we}}
\L{\LB{     can read it back in. *\/\CE{}}}
\L{\LB{}}
\L{\LB{  \C{}\/* read from buffer *\/\CE{}}}
\L{\LB{  }}
\L{\LB{  tempfile = fopen(tmpfname, \S{}\"r\"\SE{});}}
\L{\LB{  fread(buffer, \K{sizeof}(\K{char}), 1024, tempfile); }}
\L{\LB{  }}
\L{\LB{  \C{}\/* display transaction contents *\/\CE{}}}
\L{\LB{\K{\#ifdef} DEBUG\_LUCY}}
\L{\LB{  fprintf(stderr, \S{}\"Contents of transaction are \!n\%s\!n\-\-End of File\-\-\!n\"\SE{},}}
\L{\LB{}\Tab{8}{  buffer);}}
\L{\LB{\K{\#endif}      }}
\L{\LB{  }}
\L{\LB{  }}
\L{\LB{}}
\L{\LB{  fclose(tempfile); }}
\L{\LB{  unlink(tmpfname); \C{}\/* get rid of our temporary file *\/\CE{}}}
\L{\LB{}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{main()}}
\L{\LB{}}
\L{\LB{\{}}
\L{\LB{  int i; }}
\L{\LB{  int tnum;}}
\L{\LB{}}
\L{\LB{  char buffer[QUESTION\_SIZE]; }}
\L{\LB{}}
\L{\LB{  Get\_Subjects(); }}
\L{\LB{}}
\L{\LB{  for (i=0; i\<num\_browser; i++) \{}}
\L{\LB{\#ifdef DEBUG\_LUCY}}
\L{\LB{    fprintf(stderr, \"[\%d] =\> \%s\!n\", browser[i].tnum, browser[i].subject);}}
\L{\LB{\#endif      }}
\L{\LB{    }}
\L{\LB{    }}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  i = 1;}}
\L{\LB{  }}
\L{\LB{  while (i != 0) \{}}
\L{\LB{    printf(\"Enter Transaction \#: \");}}
\L{\LB{    scanf(\"\%d\", \&tnum);}}
\L{\LB{}}
\L{\LB{    Get\_Transaction(tnum, buffer, 1024);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{\}}}
\L{\LB{*\/\CE{}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{  }}
