#include "pdb.h"
#include "../h/cdefs.h"

char* pdbTypeStr(type)
int type;
{
	static struct { int type;
	    char* name;
	  } typeNames[] = 
	   { PDB_NONE,       "none",
	     PDB_INTEGER,    "integer", 
	     PDB_DOUBLE,     "double", 
	     PDB_STRING,     "string",
	     PDB_SET,        "set",
	   };
	int k;

	for (k=0; k<ARRAY_SIZE(typeNames); k++)
	 { if (type == typeNames[k].type)
	    { return (typeNames[k].name);
	    }
	 }
	return ("???");
}

int pdbSetDouble(a, p)
double *a;
PdbPar *p;
{
	if (p->type == PDB_INTEGER)
	 { *a = p->val.i;
	   return (1);
	 }
	else if (p->type == PDB_DOUBLE)
	 { *a = p->val.d;
	   return (1);
	 }
	else
	 { return (0);
	 }
}

int pdbSetFloat(a, p)
float *a;
PdbPar *p;
{
	if (p->type == PDB_INTEGER)
	 { *a = p->val.i;
	   return (1);
	 }
	else if (p->type == PDB_DOUBLE)
	 { *a = p->val.d;
	   return (1);
	 }
	else
	 { return (0);
	 }
}

int *pdbBuildVecI(set)
PdbSet *set;
{
	PdbPar *p;
	int *vec;
	int k;
	
	vec = (int*)malloc(sizeof(int)*set->len);
	for (k=0, p=set->list; k<set->len; k++, p=p->next)
	 { if (p->type == PDB_INTEGER)
	    { vec[k] = p->val.i;
	    }
	   else if (p->type == PDB_DOUBLE)
	    { vec[k] = p->val.d;
	    }
	   else
	    { vec[k] = 0.0;
	    }
	 }
	return (vec);
}

double *pdbBuildVecD(set)
PdbSet *set;
{
	PdbPar *p;
	double *vec;
	int k;
	
	vec = (double*)malloc(sizeof(double)*set->len);
	for (k=0, p=set->list; k<set->len; k++, p=p->next)
	 { if (p->type == PDB_INTEGER)
	    { vec[k] = p->val.i;
	    }
	   else if (p->type == PDB_DOUBLE)
	    { vec[k] = p->val.d;
	    }
	   else
	    { vec[k] = 0.0;
	    }
	 }
	return (vec);
}

float *pdbBuildVecF(set)
PdbSet *set;
{
	PdbPar *p;
	float *vec;
	int k;
	
	vec = (float*)malloc(sizeof(float)*set->len);
	for (k=0, p=set->list; k<set->len; k++, p=p->next)
	 { if (p->type == PDB_INTEGER)
	    { vec[k] = p->val.i;
	    }
	   else if (p->type == PDB_DOUBLE)
	    { vec[k] = p->val.d;
	    }
	   else
	    { vec[k] = 0.0;
	    }
	 }
	return (vec);
}

int *pdbSetVecI(set, vec)
PdbSet *set;
int *vec;
{
	PdbPar *p;
	int k;
	
	for (k=0, p=set->list; k<set->len; k++, p=p->next)
	 { if (p->type == PDB_INTEGER)
	    { vec[k] = p->val.i;
	    }
	   else if (p->type == PDB_DOUBLE)
	    { vec[k] = p->val.d;
	    }
	 }
	return (vec);
}

double *pdbSetVecD(set, vec)
PdbSet *set;
double *vec;
{
	PdbPar *p;
	int k;
	
	for (k=0, p=set->list; k<set->len; k++, p=p->next)
	 { if (p->type == PDB_INTEGER)
	    { vec[k] = p->val.i;
	    }
	   else if (p->type == PDB_DOUBLE)
	    { vec[k] = p->val.d;
	    }
	 }
	return (vec);
}

float *pdbSetVecF(set, vec)
PdbSet *set;
float *vec;
{
	PdbPar *p;
	int k;
	
	for (k=0, p=set->list; k<set->len; k++, p=p->next)
	 { if (p->type == PDB_INTEGER)
	    { vec[k] = p->val.i;
	    }
	   else if (p->type == PDB_DOUBLE)
	    { vec[k] = p->val.d;
	    }
	 }
	return (vec);
}
