Re: A86: Strings??


[Prev][Next][Index][Thread]

Re: A86: Strings??



It would probably help if I actually attached it...

>I've attached a little demo on writting strings.  I just cut all the
// this is basically my mapeditor CnCEdit with the actual editor
// functions cut out...it just shows how to save a string
// saves a blank string 4k along with the equivalent asm file
//
// code by David Phillips
// thanks to... 
//    Dan Eble for ideas from string85.c 
//    Tom Singer for the file format in ti86prot.txt


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

//======== TYPEDEFS/STRUCTS ===============================================
typedef struct
{
	char  sig[8],         // "**TI86**"
				ext_sig[3],     // EOF, CR, NULL byte (1a, 0a, 00)
				comment[42];    // padded with NULL bytes to len of 42
	short file_len;       // file length minus 0x39
} FileHeader;

typedef struct
{
	short data_ptr,       // points to var_len2 at end of entry
				var_len;        // length of variable
	char  type,           // variable type
				name_len,       // variable name length
				name[8];        // variable name
	short var_len2;       // exactly the same as var_len
} FileEntry;

typedef struct
{
	short len;            // length of the program
	char  token[2];       // for a compiled asm prgm, (8e, 28)
} VarPrgm;

typedef struct
{
	short len;            // length of the string
} VarString;

//======== GLOBAL VARIABLES ===============================================

char map[64][64];           // maps are 64x64

//======== MAP FUNCTIONS ==================================================
void new_map()
{
	short x, y;

	for(x = 0; x < 64; x++)
		for(y = 0; y < 64; y++)
			map[x][y] = 0;
}

void save_map_asm(char *name)   // extension needed, *name is actual name!
{
	FILE  *fp;
	short x, y;
	printf("Saving map as %s...", name);
	fp = fopen(name, "w");
	if (fp == NULL)
		printf("\nError opening %s.  Map not saved!\n", name);
	else
	{
		fprintf(fp, "; created with CnCEdit\n\n");
		for(y = 0; y < 64; y++)    
		{
			fprintf(fp, " .db ");
			for(x = 0; x < 31; x++)
				fprintf(fp, "$%02x,", map[x][y]);
			fprintf(fp, "$%02x\n", map[x][y]);
			fprintf(fp, " .db ");
			for(x = 32; x < 63; x++)
				fprintf(fp, "$%02x,", map[x][y]);
			fprintf(fp, "$%02x\n", map[x][y]);
		}
		fclose(fp);
		if (errno != 0)
		{
			printf("\nError writting %s.  Map not saved!\n", name);
			return;
		}
		printf("done!\n");
	}
}

void save_map_str(char *name)   // call without extension!
{
	FILE  *fp;
	short x, y; 
	unsigned short checksum;
	char *p;
	FileHeader header;
	char fname[12];

	strcpy(fname, name);
	strcat(fname, ".86s");
	
	memcpy(header.sig, "**TI86**", 8);
	header.ext_sig[0] = 0x1a;
	header.ext_sig[1] = 0x0a;
	header.ext_sig[2] = 0x00;
	memset(header.comment, 0, 42);
	memcpy(header.comment, "cnc86map", 8); //CnCEdit map for CnC86
	header.file_len = 10 + strlen(name) + 4096;

	printf("Saving map as %s...", fname);
	fp = fopen(fname, "wb");
	if (fp == NULL)
		printf("\nError opening %s.  Map not saved!\n", fname);
	else
	{
		fwrite(&header, sizeof(header), 1, fp);

		checksum = 0;
		fputc_sum(4 + strlen(name), fp, &checksum);
		fputc_sum(0, fp, &checksum);
		fputc_sum((4096 + 2) % 256, fp, &checksum);
		fputc_sum((4096 + 2) >> 8, fp, &checksum);
		fputc_sum(0x0c, fp, &checksum);
		fputc_sum(strlen(name), fp, &checksum);
		for(x = 0; x < strlen(name); x++)
			fputc_sum(name[x], fp, &checksum);
		fputc_sum((4096 + 2) % 256, fp, &checksum);
		fputc_sum((4096 + 2) >> 8, fp, &checksum);
		fputc_sum(4096 % 256, fp, &checksum);
		fputc_sum(4096 >> 8, fp, &checksum);

		for(y = 0; y < 64; y++)
			for(x = 0; x < 64; x++)
				fputc_sum(map[x][y], fp, &checksum);
		fwrite(&checksum, 2, 1, fp);

		fclose(fp);
		if (errno != 0)
		{
			printf("\nError writting %s.  Map not saved!\n", fname);
			return;
		}
		printf("done!\n");
	}
}

fputc_sum(char c, FILE *fp, unsigned short *sum)
{
	*sum += c;
	fputc(c, fp);
}

//======== MAIN() =========================================================
void main()
{
	new_map();
	save_map_asm("cncmap.asm");
	save_map_str("cncmap");
}


--
David Phillips
mailto:electrum@tfs.net
ICQ: 13811951
AOL/AIM: Electrum32