#ifndef MYLIB_H
#define MYLIB_H	17
#include <stdio.h>
#include "gpk_std.h"


PLAIN_C_START
struct alloc_size	{int rows; int cols;};
extern  double ***double3alloc(C int m,C int n, C int q);
extern  double **double2alloc(C int m,C int n);
extern double **double2add(C int m, C int n);
extern  void double3free(double ***q);
extern  void double2free(double **q);
extern  double *double1alloc(C int n);
extern  void double1free(double *q);
extern  int double1test(C double *q);
extern  long double3test(double *C*C*q);
extern  int double2test(double *C*q);
extern  int double1size(C double *q);
extern  struct alloc_size double2size(double * C *q);
extern void double1set(double *q, double v);
extern void double2set(double **q, double v);
extern void double3set(double *** q, double v);

extern  float ***float3alloc(C int m,C int n, C int q);
extern  float **float2alloc(C int m,C int n);
extern float **float2add(C int m,C int n);
extern  void float3free(float ***q);
extern  void float2free(float **q);
extern  float *float1alloc(C int n);
extern  void float1free(float *q);
extern  int float3test(float *C*C*q);
extern  int float2test(float *C*q);
extern  int float1test(C float *q);
extern  struct alloc_size float2size(float * C *q);
extern int float1size(C float *q);
extern void float1set(float *q, float v);
extern void float2set(float **q, float v);
extern void float3set(float *** q, float v);

extern  int ***int2alloc(C int m,C int n, C int q);
extern  int **int2alloc(C int m,C int n);
extern  int **int2add(C int m,C int n);
extern  void int3free(int ***p);
extern  void int2free(int **p);
extern  long int3test(int *C*C*p);
extern  int int2test(int *C*p);
extern  int int1test(C int *p);
extern  int *int1alloc(C int n);
extern  void int1free(int *p);
extern  int * * *int3alloc(C int c,C int m,C int n);
extern  int * * *int3add(C int c,C int m,C int n);
extern long int3test(int ***p);
extern  void int3free(int * * *p);
extern  struct alloc_size int2size(int * C *q);
extern int int1size(C int *q);
extern void int1set(int *q, int v);
extern void int2set(int **q, int v);
extern void int3set(int *** q, int v);

extern  short ***short3alloc(C int m,C int n, C int q);
extern  short * *short2alloc(C int m,C int n);
extern  short * *short2add(C int m,C int n);
extern  void short3free(short ***q);
extern  void short2free(short **q);
extern  long short3test(short ***p);
extern  int short2test(short *C*q);
extern  int short1test(C short *q);
extern  short *short1alloc(C int n);
extern  void short1free(short *q);
extern  short * * *short3alloc(C int c,C int m,C int n);
extern  short * * *short3add(C int c,C int m,C int n);
extern  void short3free(short * * *p);
extern  struct alloc_size short2size(short * C *q);
extern int short1size(C short *q);
extern void short1set(short *q, short v);
extern void short2set(short **q, short v);
extern void short3set(short *** q, short v);

extern  long ***long3alloc(C int m,C int n, C int q);
extern  long * *long2alloc(C int m,C int n);
extern  long * *long2add(C int m,C int n);
extern  void long3free(long ***q);
extern  void long2free(long **q);
extern  long long3test(long *C*C*p);
extern  int long2test(long *C*q);
extern  int long1test(C long *q);
extern  long *long1alloc(C int n);
extern  void long1free(long *q);
extern  long * * *long3alloc(C int c,C int m,C int n);
extern  long * * *long3add(C int c,C int m,C int n);
extern  void long3free(long * * *p);
extern  struct alloc_size long2size(long * C *q);
extern int long1size(C long *q);
extern void long1set(long *q, long v);
extern void long2set(long **q, long v);
extern void long3set(long *** q, long v);

extern  TBOOL **TBOOL3alloc(C int m,C int n,C int q);
extern  TBOOL * *TBOOL2alloc(C int m,C int n);
extern  TBOOL * *TBOOL2add(C int m,C int n);
extern  void TBOOL3free(TBOOL ***q);
extern  void TBOOL2free(TBOOL **q);
extern  TBOOL *TBOOL1alloc(C int n);
extern  void TBOOL1free(TBOOL *q);
extern  long TBOOL3test(TBOOL *C*C*q);
extern  int TBOOL2test(TBOOL *C*q);
extern  int TBOOL1test(C TBOOL *q);
extern  struct alloc_size TBOOL2size(TBOOL * C *q);
extern int TBOOL1size(C TBOOL *q);
extern void TBOOL1set(TBOOL *q, TBOOL v);
extern void TBOOL2set(TBOOL **q, TBOOL v);
extern void TBOOL3set(TBOOL *** q, TBOOL v);

extern  unsigned char ***uchar3alloc(C int m,C int n, C int q);
extern  unsigned char * *uchar2alloc(C int m,C int n);
extern  unsigned char * *uchar2add(C int m,C int n);
extern  void uchar3free(unsigned char ***q);
extern  void uchar2free(unsigned char **q);
extern  long uchar2test(unsigned char *C*C*q);
extern  int uchar2test(unsigned char *C*q);
extern  int uchar1test(C unsigned char *q);
extern  unsigned char *uchar1alloc(C int n);
extern  void uchar1free(unsigned char *q);
extern  unsigned char * * *uchar3alloc(C int c,C int m,C int n);
extern  unsigned char * * *uchar3add(C int c,C int m,C int n);
extern  void uchar3free(unsigned char * * *p);
extern  struct alloc_size uchar2size(unsigned char * C *q);
extern int unsigned char1size(C unsigned char *q);
extern void uchar1set(unsigned char *q, unsigned char v);
extern void uchar2set(unsigned char **q, unsigned char v);
extern void uchar3set(unsigned char *** q, unsigned char v);

/* Various random number generators: */

int uniform(int x);		/* Uniform on [0,1) */
double lograndom(void);		/* P(x) = exp(-x). */
double ran2(unsigned to_be_hashed);	/* A hash function from
					an unsigned to a real on [0,1). */
#ifdef __cplusplus
double ran1(int *q = 0);
double ran1a(int *q = 0);
double Nnoise(int *q = 0);
#else
double ran1(int *q);
double ran1a(int *q);
double Nnoise(int *q);
#endif



#if 0
extern  int round(double x);		/* Rounds to int, aborts if unrepresentable as int. */
#endif

extern  char *makename(char C *s);
extern  int csplitcount(char * *p);
extern  char * *csplit(char *s,char C *c);


PLAIN_C_END

#ifdef __cplusplus

/* The following rounding functions use the numbers defined in myNaN.h to represent
NaN and +-Inf in various integral formats. */

unsigned char roundU(float x);
/* Round to a signed byte, handling +-Inf and Not A Number. */

signed char roundC(float x);
/* Round to a signed byte, handling +-Inf and Not A Number. */

short roundS(float x);
/* Round to two signed bytes, handling +-Inf and Not A Number. */

long roundL(float x);
/* Round to a long, handling +-Inf and Not A Number. */

int roundI(float x);
/* Round to a integer, handling +-Inf and Not A Number. */

long roundL(double x);
unsigned char roundU(double x);
signed char roundC(double x);
short roundS(double x);
int roundI(double x);

double median(C double *x, int n);	/* take the median of an array. */
double median(C float *x, int n);
double median(C int *x, int n);


/* The following are misc functions to help read in ASCII files. */

void trim_comments(char *s);	/* Removes #<space> to the end of the line. */
int chomp_newline(char *s);	/* Returns the length after chomping <cr> or <nl>.
					Returns -1 if s==0. */
int chomp_whitespace(char *s);	/* Returns the length after chomping trailing whitespace,
				or -1 if s == 0. */
BOOL is_blank_line(C char *line);	/* Returns TRUE if line is empty or white space. */
BOOL is_error_line(C char *line);	/* Returns TRUE if line begins with 'ERR:'. */

double safe_atof(C char *s);	/* Returns NaN if the number isn't clean:
				e.g., "35x" will return NaN,
				while regular atof()
				will just return 35.0. */
int safe_atoi(C char *s);	/* Returns INT_NaN if the number isn't clean:
				e.g., "35x" will return INT_NaN, while regular atof()
				will just return 35.
				Also maps 'inf' into INT_Max and so forth.
				See myNaN.h .*/
long safe_atol(C char *s);	/* Similar to safe_atoi(). */
short safe_atoh(C char *s);	/* Similar to safe_atoi(). */


struct property	{
	char *attr, *val, *comment;
	struct property *next;

	unsigned refc;
	void incref();
	BOOL decref();

	property();
	property(struct property *n, C char *a, C char *v, C char *c=0);
	~property();
	property(C property& x);
	property& operator=(C property& x);

	double getd() C;
	long getl() C;
	int geti() C;
	short geth() C;

	private:
	void free();
	void make(struct property *n, C char *a, C char *v, C char *c);
	};


struct proplist	{
	struct property *first;

	proplist();
	~proplist();
	proplist(C proplist& x);
	proplist& operator=(C proplist& x);
	proplist& operator+=(C proplist& x);

	BOOL pop();
	BOOL isempty() C	{return first==0;}

	/* Adds attribute-value pairs to the property list. */
	void add(C char *attr, C char *val, C char *c = 0);
	void add(C char *attr, double val, C char *c = 0);
	void add(C char *attr, long val, C char *c = 0);
	void add(C char *attr, int val, C char *c = 0);
	void add(C char *attr, short val, C char *c = 0);

	/* Get returns a pointer to a property. */
	/* That property may be shared with other lists, so if you */
	/* change anything, it may affect values returned from other property lists. */
	property *get(C char *attr);

	C property *get(C char *attr) C;

	const char *gets(C char *attr, C char *notthere = 0) C;

	/* Notthere is used when the value is missing.
	NaN is returned for a value that is not well formed.
	The strings "NaN" and "Inf" are well formed. */

	double getd(C char *attr, double notthere) C;
	long getl(C char *attr, long notthere) C;
	int geti(C char *attr, int notthere) C;
	short geth(C char *attr, short notthere) C;

	double getd(C char *attr) C;
	long getl(C char *attr) C;
	int geti(C char *attr) C;
	short geth(C char *attr) C;

	static int matches(C char *a, C char *b);
	};


	/* Parses a string into a property list.  The string is assumed to be
	in "a=v;a=v;" format.   Errors are reported as values of the empty attribute.
	Comments are marked with '#' and extend to the end of the string.  Comments
	are reported as values of the 'COMMENT' attribute. */
proplist avparse(const char *cs);



/* Will print an attribute=value pair in a form suitable for avparse().
   Prints 'a=V;', where V=v, with nonprinting characters and '#', ';' replaced
   by '%xx' escape sequences.
   */
int print_av(FILE *f, const char *a, const char *v);
int print_avc(FILE *f, const char *a, const char *v, const char *c);

	/* Returns a copy of v, where '#', ';', and non-printing characters
	are replaced with '%xx' escape sequences.   This is suitable for
	feedint to avparse(). */
char *encode_av(const char *v);
char *decode_av(const char *v);

#endif

INSERT HERE

#endif
