static char rcsid[] = "@(#)$Id: astorage.c,v 1.5.40.1 2007/08/25 07:45:27 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.5.40.1 $   $State: Exp $
 *
 *  Author: Kari Hurtta <hurtta+elm@posti.FMI.FI> (was hurtta+elm@ozone.FMI.FI)
 *****************************************************************************/

#include "def_aliases.h"

DEBUG_VAR(Debug,__FILE__,"alias");

struct current_astorage  *current_astorage = NULL;

extern void reset_astorage(astorage)
     struct current_astorage *astorage;
{
    if (astorage->aliases) {
	int i;
	
	for (i = 0; i < astorage->max_aliases; i++) {
	    if (astorage->aliases[i])
		alias_free( & (astorage->aliases[i]));
	}

	free(astorage->aliases);
	astorage->aliases = NULL;
    }
    astorage->max_aliases         = 0;
    astorage->aliascount          = 0;
}

static void free_astorage P_((struct current_astorage **astorage));
static void free_astorage(astorage)
     struct current_astorage **astorage;
{
    struct current_astorage * X = *astorage;

    reset_astorage(X);

    if (X->hash) {
	dbz_close(X->hash);
	X->hash = NULL;
    }

    if (X->filename) {
	free(X->filename);
	X->filename = NULL;
    }

    free(X);
    X = NULL;

    *astorage = X;
}

static struct current_astorage *new_astorage P_((void));
static struct current_astorage *new_astorage() 
{
    
    struct current_astorage * X = safe_malloc(sizeof (*X));

    /* bzero is defined hdrs/defs.h */
    bzero((void *)X,sizeof (*X));

    X->max_aliases          = 0;
    X->aliases              = NULL;

    X->hash                 = NULL;
    X->filename             = NULL;
    X->is_system            = 0;

    X->mtime                = 0;
    X->ctime                = 0;

    X->aliascount           = 0;
    return X;
}



#if ANSI_C
#define S_(x) static x;
#else
#define S_(x)
#endif

S_(at_init_aliasclass at_init_astorage)
static void at_init_astorage P_((struct aliasclass *c));
static void at_init_astorage(c) 
     struct aliasclass *c;
{
  c->p.astorage = new_astorage();
}

S_(at_free_aliasclass at_free_astorage)
static void at_free_astorage P_((struct aliasclass *c));
static void at_free_astorage(c) 
     struct aliasclass *c;
{
  free_astorage (& (c->p.astorage));
}

struct alias_rec  * at_astorage_give_alias(c,index)
     struct aliasclass *c;
     int index;
{
  struct current_astorage *astorage =  c->p.astorage;

    if (index < 0 || index >= astorage -> max_aliases ||
	! astorage->aliases[index]) {
	panic("ALIAS VIEW PANIC",__FILE__,__LINE__,"at_astorage_give_alias",
	      "bad internal index",0);
	return NULL;
    }

    return astorage->aliases[index];
}

S_(at_get_count_aliasclass at_get_count_astorage)
static int at_get_count_astorage P_((struct aliasclass *c));
static int at_get_count_astorage(c)
     struct aliasclass *c;
{
    return c->p.astorage -> aliascount;
}

struct alias_type at_astorage = {
  ALIASTYPE_magic,
  at_init_astorage,
  at_free_astorage,
  at_astorage_give_alias,
  at_update_astorage,
  at_get_count_astorage
};


void set_astorage_alias(astorage,index,rec)
     struct current_astorage *astorage;
     int index; 
     struct alias_rec * rec;
{
    if (index < 0 || index >= astorage -> max_aliases) {
	panic("ALIAS VIEW PANIC",__FILE__,__LINE__,"set_astorage_alias",
	      "bad internal index",0);
	return;
    }
    
    if (astorage->aliases[index])
	alias_free(& astorage->aliases[index]);

    if (!rec) {
	panic("ALIAS VIEW PANIC",__FILE__,__LINE__,"set_astorage_alias",
	      "rec not given",0);
	return;
    }
    astorage->aliases[index] = rec;
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */


syntax highlighted by Code2HTML, v. 0.9.1