/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of SanKit -- A multiplatform set of C code
 * for reading and writing PGN formatted chess games. In addition
 * it can be used as an engine to determine in real time if proposed
 * moves are legal or not. It appears to generate near perfect PGN
 * from incorrectly formatted files.
 *
 * Copyright 1994 Steven J. Edwards All Rights Reserved.
 * Copyright 2009-2011 David Hoerl All Rights Reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*>>> sanpgc.c: SAN project PGC routines */

/* Revised: 1994.02.06 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sandef.h"
#include "sanvar.h"

#include "sanpgc.h"
#include "santag.h"

/*--> SANPGNWriteInt1: write an int-1 to a file */
static
void SANPGNWriteInt1(FILEptrT fptr, byteT int1)
{
	fprintf(fptr, "%c", int1);

	return;
}

/*--> SANPGNWriteInt2: write an int-2 to a file */
static
void SANPGNWriteInt2(FILEptrT fptr, siT int2)
{
	fprintf(fptr, "%c%c", (int2 & 0x00ff), ((int2 >> 8) & 0x00ff));

	return;
}

#if 0
/*--> SANPGNWriteInt4: write an int-4 to a file */
static
void SANPGNWriteInt4(FILEptrT fptr, liT int4)
{
	fprintf(fptr, "%c%c%c%c",
		(int4 & 0x00ff), ((int4 >> 8) & 0x00ff),
		((int4 >> 16) & 0x00ff), ((int4 >> 24) & 0x00ff));

	return;
}

#endif

/*--> SANPGNWriteString1: write a string-1 to a file */
static
void SANPGNWriteString1(FILEptrT fptr, liT length, charptrT s)
{
	liT i;

	SANPGNWriteInt1(fptr, length);
	for(i = 0; i < length; i++) {
		SANPGNWriteInt1(fptr, *(s + i));
	}

	return;
}

#if 0
/*--> SANPGNWriteString2: write a string-2 to a file */
static
void SANPGNWriteString2(FILEptrT fptr, liT length, charptrT s)
{
	liT i;

	SANPGNWriteInt2(fptr, length);
	for(i = 0; i < length; i++) {
		SANPGNWriteInt1(fptr, *(s + i));
	}

	return;
}

/*--> SANPGNWriteString4: write a string-4 to a file */
static
void SANPGNWriteString4(FILEptrT fptr, liT length, charptrT s)
{
	liT i;

	SANPGNWriteInt4(fptr, length);
	for(i = 0; i < length; i++) {
		SANPGNWriteInt1(fptr, *(s + i));
	}

	return;
}

#endif

/*--> SANPGCWriteRPGC: write the current game in (reduced) PGC to a file */
static
void SANPGCWriteRPGC(FILEptrT fptr)
{
	rsvtagT rsvtag;
	tagptrT tagptr;
	siT hcn;
	hptrT hptr;

	SANPGNWriteInt1(fptr, 0x01);

	for(rsvtag = 0; rsvtag < rsvtagL; rsvtag++) {
		tagptr = SANTagLocate(rsvtagv[rsvtag]);
		SANPGNWriteString1(fptr, (liT)strlen(tagptr->tag_value), tagptr->tag_value);
	}

	SANPGNWriteInt2(fptr, hn);
	hcn = 0;
	hptr = hbase;
	while(hcn < hn) {
		SANPGNWriteInt1(fptr, hptr->h_o);
		hcn++;
		hptr++;
	}

	return;
}

/*--> SANPGCWriteCurrent: write the current game in PGC to a file */
nonstatic
void SANPGCWriteCurrent(FILEptrT fptr)
{
	SANPGCWriteRPGC(fptr);

	return;
}

/*<<< sanpgc.c: EOF */
