/*
 *	String.c - Copyright © 1991 by S.R. & P.C.
 *
 *	Created:	10 Mar 1991  16:47:13
 *	Modified:	01 May 1991  15:14:32
 *
 *	Make>> make
 */

#include "Global.h"
#include "DosVar.h"
#include "proto/String.h"


/*
 *	size of sub-strings. Theses values are also in SPrintf() format strings, and
 *	they need to be same. Changes MUST be made in all places if these values have
 *	to be modified.
 */

#define SIZE_LEN	9
#define BLOCKS_LEN	7
#define PROTECT_LEN	10
#define DATE_LEN	19
#define KEY_LEN		10


/* Copy and convert to upper case a string */

void UStrcpy(char *dst, char *src)
{
	while(*dst++ = Toupper(*src++));
}


void Date2String(struct DateStamp *ds, char *dest)
{
	struct DateTime dt;

	dt.dat_Stamp = *ds;
	dt.dat_Format = FORMAT_DOS;
	dt.dat_Flags = DTF_FUTURE;	/* Print real dates, not "Future", "Tomorrow", ... */
	dt.dat_StrDay = NULL;
	dt.dat_StrDate = dest;
	dt.dat_StrTime = dest + LEN_DATSTRING;
	if (StamptoStr(&dt))
		strcpy(dest, "  Invalid         ");
	dest[9] = ' ';		/* replace '\0' with a space between date and time strings */
	dest[18] = '\0';	/* It seems not to be done by StamptoStr() ! */
}


static void MakeProtectString(LONG Protection, char *dest)
{
	static char *ProtectChar = "chsparwed";
	short i;

	for( i=0 ; i<9; i++ )
		*dest++ = (Protection & 0x100>>i) ? ProtectChar[i] : '-';
	*dest = '\0';
}


void FreePrintStrings(struct BrowserWindow *Win)
{
	struct ScrollEntry *S;
	short i;

	for( i=0 ; i<Win->bw_ShownEntries ; i++ ) {
		S = Win->bw_EntryArray[i];
		if (S->se_Print) {
			FreeMem(S->se_Print, Win->bw_PrintStringLen+1);
			S->se_Print = NULL;
		}
	}
}


void BuildPrintString(struct BrowserWindow *Win, struct ScrollEntry *S)
{
	char *s;
	char buf[20];

	s = S->se_Print;
	SPrintf(s, Win->bw_Fmt, S->se_FileInfo.fi_Name);
	s += Win->bw_MaxFilenameLen;
	if (Win->bw_EntryInfoFlags & DISPLAY_KEY) {
		SPrintf(buf, "[%ld]", S->se_FileInfo.fi_DiskKey);
		SPrintf(s, "%10s", buf);
		s += KEY_LEN;
	}
	if (Win->bw_EntryInfoFlags & DISPLAY_SIZE) {
		if (S->se_FileInfo.fi_Type == DLX_FILE)
			SPrintf(s, "%9ld", S->se_FileInfo.fi_Size);
		else
			strcpy(s, "    (dir)");
		s += SIZE_LEN;
	}
	if (Win->bw_EntryInfoFlags & DISPLAY_BLOCK) {
		SPrintf(s, "%7ld", S->se_FileInfo.fi_NumBlocks);
		s += BLOCKS_LEN;
	}
	if (Win->bw_EntryInfoFlags & DISPLAY_PROTECT) {
		*s++ = ' ';
		MakeProtectString(S->se_FileInfo.fi_Protection, s);
		s += PROTECT_LEN-1;
	}
	if (Win->bw_EntryInfoFlags & DISPLAY_DATE) {
		*s++ = ' ';
		Date2String(&S->se_FileInfo.fi_Date, s);
	}
}


BOOL BuildPrintStrings(struct BrowserWindow *Win)
{
	static short LenTab[] = { SIZE_LEN, BLOCKS_LEN, PROTECT_LEN, DATE_LEN, KEY_LEN };
	struct ScrollEntry *S;
	short len, i;

	FreePrintStrings(Win);
	if (Win->bw_NewMaxFilenameLen > Win->bw_MaxFilenameLen)
		Win->bw_MaxFilenameLen = Win->bw_NewMaxFilenameLen;
	Win->bw_NewMaxFilenameLen = 0;
	len = Win->bw_MaxFilenameLen;
	SPrintf(Win->bw_Fmt, "%%-%ds", len);
	for( i=0 ; i<5 ; i++ ) {
		if (Win->bw_EntryInfoFlags & 1<<i)
			len += LenTab[i];
	}
	Win->bw_PrintStringLen = len;
	len++;
	for( i=0 ; i<Win->bw_ShownEntries ; i++ ) {
		S = Win->bw_EntryArray[i];
		if (!(S->se_Print = AllocMem(len, MEMF_PUBLIC)))
			return FALSE;
		BuildPrintString(Win, S);
	}
	return TRUE;
}


