
/* test.c tests out viewbrowser interface.
   two critical functions:
      Get_Subjects() --- gets a list of up to 4096 subjects and their
      corresponding transaction numbers.

      got everything working!!!!
      
   -- ukim */


#include <stdio.h>   
#include <discuss/discuss.h>
#include "lucy.h"
#include "params.h"

#define False 0
#define True 1

typedef int Boolean;

extern tfile unix_tfile();

struct browse_struct {
  char subject[LINE_SIZE];
  int tnum; /* transaction number */
} browser[BROWSER_MAX];
/* up to 4096 possible transactions handled */

int num_browser = 0; /* keeps track of how many subjects are in the browser */


void Get_Subjects()
/* works */
{
  int i;
  Boolean exit_flag;
  int cur_tnum, next_tnum; /* current and next transaction numbers */
  ltrn *tran_info;  /* transaction info structure */
  int err_code; /* error code */

  /* initialize luc_init() to get in touch with the browser meeting.*/
  luc_init(); /* initialize lucy discuss meetings */
  luv_curmtgp = &luv_bmtg; /* the current meeting will be the browser */
  /* luv_curmtgp is a virtual constant after this point */
  /* algo: get 1st transaction using luc_next().  and also get
     trn number for each transaction.  iterate until no more
     transactions or browser is full */
  
  /* am assuming that an error indicates that no more transactions
     are available */

  num_browser = 0; /* initialize */
  next_tnum = 0;  /* next transaction number */
  exit_flag = False;
  while (exit_flag == False) {
    cur_tnum = next_tnum;
    luc_next(luv_curmtgp, cur_tnum, &next_tnum);
    if (next_tnum != 0) {
      luc_trn_info(luv_curmtgp, next_tnum, &tran_info);

      /* assign data to browser */
      strncpy(browser[num_browser].subject, tran_info->subject, 128);
      browser[num_browser].tnum = next_tnum;

      num_browser++;
    } else { /* no more transactions available */
#ifdef DEBUG_LUCY
      fprintf(stderr, "Get_Subjects: no more transactions available.\n");
#endif      

      
      exit_flag = True;
    }

    if (num_browser == BROWSER_MAX) {
#ifdef DEBUG_LUCY
      fprintf(stderr, "Get_Subjects: Browser is full.\n");
#endif      
      
      
      exit_flag = True;
    }

  } /* end while */

}  /* end Get_Subject() */
    
    

void Get_Transaction(int tnum, char buffer[], int buflen)
/* retries the contents of a transaction, given the transaction
   number.  buflen should be maximum size of buffer being passed.
   Be sure to call Get_Subjects() to initialize lucy meetings beforehand.  */
     
{
  char tmpfname[LINE_SIZE]; /* name of temporary file */
  FILE *tempfile; /* temporary file */
  tfile tf;  /* need a tfile store contents of transaction in */
  int terror; /* place to hold error numbers that meetings server
		 might return */
  /* creat a tfile :
     create a temporary file, and open it.
     use unix_tfile to translate it into a tfile.
     call get_trn, then read the temporary file 
     to output to fprintf */

  tmpnam(tmpfname); /* create a temporary file name */

  tempfile = freopen(tmpfname, "w+", stdout); 

  if (tempfile == NULL) {
#ifdef DEBUG_LUCY
    fprintf(stderr, "could not create a temporary file.\n");
#endif      
    
    
    exit(1);
  }
#ifdef DEBUG_LUCY

  fprintf(stderr, "creating unix wrapper \n");
#endif      
  
  tf = unix_tfile(1); /* put a tfile wrapper around the tempfile */
#ifdef DEBUG_LUCY
  fprintf(stderr, "finished creating unix wrapper \n");
#endif      
  
  

  dsc_get_trn(&(luv_curmtgp->nb), tnum, tf, &terror);
#ifdef DEBUG_LUCY
  fprintf(stderr, "get_trn returned error code %d \n", terror);
#endif      
  
  


  tdestroy(tf); /* free the memory allocated to the tfile */
  
  fclose(tempfile);  /* flush out to disk before reading it back in */
  /* need to close the file, to flush out the data, so that we
     can read it back in. */

  /* read from buffer */
  
  tempfile = fopen(tmpfname, "r");
  fread(buffer, sizeof(char), buflen, tempfile); 
  
  /* display transaction contents */
#ifdef DEBUG_LUCY
  fprintf(stderr, "Contents of transaction are \n%s\n--End of File--\n",
	  buffer);
#endif      
  
  

  fclose(tempfile); 
  unlink(tmpfname); /* get rid of our temporary file */

}  /* end of get_transaction() */  
  
  

prev_obsolete_code()
{
  ltrn *tran_info;  /* transaction info structure */
  char subject[LINE_SIZE];  /* subject of transaction */
  int next;
  int tnum; /* transaction number */
  tfile tf;  /* need a tfile store contents of transaction in */
  int terror; /* place to hold error numbers that meetings server
		 might return */
  char buffer[QUESTION_SIZE]; /* place to hold contents of transaction */
  char text[QUESTION_SIZE]; /* buffer for tfile */
  char tmpfname[LINE_SIZE]; /* name of temporary file */
  FILE *tempfile; /* temporary file */
  
  luc_init();  /* initialize lucy discuss meetings */
  luv_curmtgp = &luv_bmtg; /* the current meeting will be the browser */
  luc_next(luv_curmtgp, 0, &next);  /* get first (next) transaction. */
  luc_trn_info(luv_curmtgp, next, &tran_info);

  strcpy(subject, tran_info->subject);
  tnum = tran_info->current; /* transaction number of current transaction */
#ifdef DEBUG_LUCY
  fprintf(stderr, "Subject of transaction: %s \n", subject);
  fprintf(stderr, "Transaction # is %d \n", tnum);
#endif      
  
  


  /* creat a tfile :
     create a temporary file, and open it.
     use unix_tfile to translate it into a tfile.
     call get_trn, then read the temporary file 
     to output to fprintf */

  tmpnam(tmpfname); /* create a temporary file name */

  tempfile = freopen(tmpfname, "w+", stdout); 

  if (tempfile == NULL) {
#ifdef DEBUG_LUCY
    fprintf(stderr, "could not create a temporary file.\n");
#endif      
    
    
    exit(1);
  }

#ifdef DEBUG_LUCY
  fprintf(stderr, "creating unix wrapper \n");
#endif      
  
  
  tf = unix_tfile(1); /* put a tfile wrapper around the tempfile */
#ifdef DEBUG_LUCY
  fprintf(stderr, "finished creating unix wrapper \n");
#endif      
  
  

  dsc_get_trn(&(luv_curmtgp->nb), tnum, tf, &terror);
#ifdef DEBUG_LUCY
  fprintf(stderr, "get_trn returned error code %d \n", terror);
#endif      
  


  tdestroy(tf); /* free the memory allocated to the tfile */
  
  fclose(tempfile);  /* flush out to disk before reading it back in */
  /* need to close the file, to flush out the data, so that we
     can read it back in. */

  /* read from buffer */
  
  tempfile = fopen(tmpfname, "r");
  fread(buffer, sizeof(char), 1024, tempfile); 
  
  /* display transaction contents */
#ifdef DEBUG_LUCY
  fprintf(stderr, "Contents of transaction are \n%s\n--End of File--\n",
	  buffer);
#endif      
  
  

  fclose(tempfile); 
  unlink(tmpfname); /* get rid of our temporary file */

}



/*
main()

{
  int i; 
  int tnum;

  char buffer[QUESTION_SIZE]; 

  Get_Subjects(); 

  for (i=0; i<num_browser; i++) {
#ifdef DEBUG_LUCY
    fprintf(stderr, "[%d] => %s\n", browser[i].tnum, browser[i].subject);
#endif      
    
    
  }

  i = 1;
  
  while (i != 0) {
    printf("Enter Transaction #: ");
    scanf("%d", &tnum);

    Get_Transaction(tnum, buffer, 1024);
  }

}
*/


  
