/* sorttree.c */
/******************************************************************************
  IMM version 3.0
  Written by Winston Dang <wkd@hawaii.edu>
  
  * Copyright (c) University of Hawaii 1993. All rights reserved.
  *  
  * License is granted to copy, to use, and to make and to use derivative
  * works for research, educational, government or evaluation purposes, provided
  * that the University of Hawaii is acknowledged in all documentation pertaining
  * to any such copy or derivative work. University of Hawaii grants no other 
  * licenses expressed or implied. The University of Hawaii trade name should 
  * not be used in any advertising without its written  permission.
  *  
  * UNIVERSITY OF HAWAII MAKES NO REPRESENTATIONS CONCERNING EITHER THE
  * MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE
  * FOR ANY PARTICULAR PURPOSE.  The software is provided "as is" without
  * express or implied warranty of any kind.
  *  
  * These notices must be retained in any copies of any part of this software.
  ******************************************************************************/

#include "protocol.h"
#include "immserv.h"


/******************************************************************************/
/* make a duplicate */
char *liststrdup(s1,s2)
char *s1;
char *s2;
{
    char *p;
    if (s2 == NULL) {
	p = (char *) malloc(strlen(s1) + 1);
	if (p != NULL)
	    strcpy(p,s1);
	return p;
    }
    p = (char *) malloc(strlen(s1) + strlen(s2) + 2);
    if (p != NULL)
	sprintf(p,"%s/%s",s1,s2);
    return p;	
}
/******************************************************************************/
/* make a node */
struct listdef *talloc()
{
    return (struct listdef *) malloc(sizeof(struct listdef));
}

/*****************************************************************************/
char *queueit(cmd,top,data)
int cmd;
struct listdef **top;
char *data;
{
    struct listdef *curr;
    switch (cmd) {
    case RECOV_INIT:
    	/* initialize file request queue */
	*top = NULL;
	return;
    case RECOV_PUT:
	/* push entries on file request queue via insertion sort */	
	curr = talloc();
	curr->name = liststrdup(data,NULL);
	curr->next = *top;
	*top = curr;
	return;
    case RECOV_GET:
	/* pop entry off of file request queue */
	if (*top != NULL) {
	    /* pop off stack */
	    curr = *top;
	    *top = (*top)->next;
	    free(curr->name);
	    free(curr);
	}
    }
}
/******************************************************************************/
int alphalist(cmd,top,data)
int cmd;
struct listdef **top;
char *data;
{
    struct listdef *curr, *prev;
    int found;
    switch (cmd) {
    case RECOV_INIT:
    	/* initialize file request queue */
	*top = NULL;
	return TRUE;
    case RECOV_PUT:
	/* push entries on file request queue via insertion sort */
	/* handle the case of an empty queue */
	curr = talloc();
	if (*top == NULL) {
	    curr->name = liststrdup(data,NULL);
	    curr->next = *top;
	    *top = curr;
	    if (debug) printf("alpha: top %s\n", curr->name);
	    return TRUE;
	}
	prev = *top;
	while ((prev->next != NULL) && (strcmp(prev->name,data) < 0))
	    prev = prev->next;
	found = strcmp(prev->name,data);
	if (found > 0) {
	    /* insert into list */
	    curr->next = prev->next;
	    prev->next = curr;
	    curr->name = liststrdup(prev->name,NULL);
	    free(prev->name);
	    prev->name = liststrdup(data,NULL);
	    if (debug) printf("alpha: %s < %s\n", prev->name, curr->name );
	} else {
	    /* insert entry at end */
	    /* put at end of queue */
	    curr->name = liststrdup(data,NULL);
	    curr->next = NULL;
	    prev->next = curr;
	    if (debug) printf("alpha: end %s\n", curr->name);
	}
	return TRUE;
	
    case RECOV_TEST:
	prev = *top;
	while (prev != NULL) {
	    if (debug) printf("retrans queue %s\n",prev->name);
	    prev = prev->next;
	}
	return TRUE;
	
    case RECOV_GET:
	/* pop entry off of file request queue */
	if (*top != NULL) {
	    /* extract name and directory */
	    /* pop off stack */
	    curr = *top;
	    *top = (*top)->next;
	    free(curr->name);
	    free(curr);
	    return TRUE;
	}
	return FALSE;
    }
}
