
/*
 * group.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id: group.c,v 1.11 2001/10/12 13:51:11 root Exp $";

/*
 * $Log: group.c,v $
 * Revision 1.11  2001/10/12 13:51:11  root
 * #
 *
 * Revision 1.10  2001/10/04 18:21:16  root
 * #
 *
 * Revision 1.9  2001/01/30 06:16:18  root
 * #
 *
 * Revision 1.8  2000/12/01 04:42:10  root
 * #
 *
 * Revision 1.7  2000/11/23 23:45:19  root
 * #
 *
 * Revision 1.6  2000/10/07 18:30:03  root
 * #
 *
 * Revision 1.5  2000/09/24 01:58:21  root
 * #
 *
 * Revision 1.4  2000/09/24 01:48:12  root
 * #
 *
 * Revision 1.3  2000/09/24 01:44:47  root
 * #
 *
 * Revision 1.2  2000/09/24 01:12:43  root
 * #
 *
 * Revision 1.1  2000/09/19 05:06:22  root
 * #
 *
 */

#include "project.h"

static char magic[] = "GRP";

INTERNAL void make_group(char *filename, Flock el)
{
  Eweid e;
  FILE *f;
  int n;
  unsigned char s[4];

  f = fopen(filename, "w");
  e = el->head;

  n = 0;
  while (e) {
    n++;
    e = e->next;
  }

  fwrite(magic, 1, sizeof(magic), f);

  s[0] = n & 0xff;
  s[1] = (n >> 8) & 0xff;
  s[2] = (n >> 16) & 0xff;
  s[3] = (n >> 24) & 0xff;

  fwrite(s, 1, 4, f);



  e = el->head;
  while (e) {
//char *s=eweidtoa(e);
//printf("%s<=>%s\n",filename,s);
//free(s);

    fwrite(&e->e, 1, sizeof(e->e), f);
    e = e->next;
  }

  fclose(f);

}


EXTERNAL int files_is_group(CFH fh)
{
  unsigned char imagic[sizeof(magic)];

  if (fh->tell(fh)) {
    fh->seek(fh, 0, SEEK_SET);
  }

  if (fh->read(fh, imagic, sizeof(magic)) != sizeof(magic))
    return 0;

  if (!bcmp(imagic, magic, sizeof(magic)))
    return 1;

  fh->seek(fh, 0, SEEK_SET);

  return 0;


}


INTERNAL int is_group(Eweid e)
{
  unsigned char imagic[sizeof(magic)];

  char *n;
  FILE *f;

  if (!e)
    return 0;
  n = eweid_to_filename(e);
  if (!n)
    return 0;
  f = fopen(n, "r");

  free(n);

  if (!f) {
    return 0;
  }



  if (fread(imagic, 1, sizeof(magic), f) != sizeof(magic)) {
    fclose(f);
    return 0;
  }

  if (!bcmp(imagic, magic, sizeof(magic))) {
    fclose(f);
    return 1;
  }

  fclose(f);

  return 0;


}

INTERNAL Flock read_group(Eweid g)
{
  unsigned char s[4];
  int n;
  Flock ret;
  Eweid e;
  char *nm;
  FILE *f;

  if (!g)
    return NULL;
  nm = eweid_to_filename(g);
  if (!nm)
    return NULL;
  f = fopen(nm, "r");
  free(nm);
  if (!f) {
    return NULL;
  }

  if (ftell(f) != sizeof(magic)) {
    fseek(f, sizeof(magic), SEEK_SET);
  }

  if (fread(s, 1, 4, f) != 4) {
    return NULL;
  }


  n = s[3];
  n <<= 8;
  n |= s[2];
  n <<= 8;
  n |= s[1];
  n <<= 8;
  n |= s[0];

  ret = CREATE_LIST(Eweid);

  while (n--) {
    e = CREATE_OBJ(Eweid);

    if (fread(e->e, 1, sizeof(e->e), f) != sizeof(e->e)) {
      Obj_Delete(e);
      fclose(f);
      return ret;
    }

    Obj_Insert(ret, e, ObjInsertTail);

  }



  fclose(f);


  return ret;
}


EXTERNAL Flock files_read_group(CFH fh)
{
  unsigned char s[4];
  int n;
  Flock ret;
  Eweid e;

  if (fh->tell(fh) != sizeof(magic)) {
    fh->seek(fh, sizeof(magic), SEEK_SET);
  }



  if (fh->read(fh, s, 4) != 4) {
    return NULL;
  }


  n = s[3];
  n <<= 8;
  n |= s[2];
  n <<= 8;
  n |= s[1];
  n <<= 8;
  n |= s[0];

  ret = CREATE_LIST(Eweid);

  while (n--) {
    e = CREATE_OBJ(Eweid);

    if (fh->read(fh, e->e, sizeof(e->e)) != sizeof(e->e)) {
      Obj_Delete(e);
      return ret;
    }

    Obj_Insert(ret, e, ObjInsertTail);

  }





  return ret;
}
