/*
   <p>Test harness for the tempstore functions.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include "environ.h"

#ifdef OS9
#include <strings.h>
#else
#include <string.h>
#endif

main(int argc, char **argv)
{
    char        c;
    FILE        *f;
    tempstore*  t;
    long        n;
    char        temp[MAXLINE+1];

    setprog(argv[0]);
    
    if (argc<2)
        error(progname(),"must have at least two arguments:", "filename data");

    n = filesize(argv[1]);

    f = fopen(argv[1],"r");
    if (f == NULL)
        error(progname(),"can't open",argv[1]);

    printf("\nInput is %ld, RAM limit is %d\n",n,TSLIM);
    t = tsopen(n,TRUE);
    if (t == NULL)
        error(progname(),"can't open temp store","");
    printf("Opened tempstore; storing:\n");

    while ((c = getc(f)) != EOF)
    {
        putchar(c);
        if (!tsputc(t,c))
            error(progname(),"bad reply from tsput()","");
    }

    fclose(f);
    printf("==================================stored\n");

    while ((c = tsgetc(t)) != EOF)
        putchar(c);
    printf("==================================retrieved\n");

    tsclose(t);
    printf("Closed tempstore\n");

    if (argc>=3)
    {
        printf("Storing argument: %s\n",argv[2]);
        t = tsopen(strlen(argv[2])+1, TRUE);
        if (t == NULL)
            error(progname(),"can't open string store","");
        printf("Storing\n");
        if (tsputstr(t,argv[2]))
        {
            printf("Retrieving\n");
            if (tsgetstr(t,temp,MAXLINE))
                printf("Got [%s]\n",temp);
            else
                printf("Bad reply from tsgetstr\n");
        }
        else
            printf("Bad reply from tsputstr\n");
        printf("Closing string store\n");
        tsclose(t);
        printf("Closed\n");
    }
    printf("Test ended\n");
    return(0);
}
