View Single Post
Old 29 August 2017, 03:31   #16
chocsplease
Registered User

 
Join Date: Dec 2016
Location: london
Posts: 157
Hi All,

Well almost there but not quite. I am having an odd problem when lining up columns in a window. I am basically outputting an updated version of the info command but things are not quite lining up correctly.

This is the output straight to the shell window:

Code:
Unit       DOS    Size       Used       Free   Full  Errors     Status        Name
SMBFS0:    DOS0   488Gb      223Gb      264Gb   45%     0       Read & Write  PCbackup
PC0:       MSD0   720Kb       11Kb      709Kb    1%     0       Read only     EMPTY
CF0:       No disk in drive
RAM:       DOS0   686Kb      686Kb      121Mb    0%     0       Read & Write  Ram Disk
DH0:       PFS3    94Mb       93Mb      654Kb   99%     0       Read & Write  WB
DF0:       PC Disk
CC0:       No disk in drive
DF1:       DOS0   837Kb      789Kb       48Kb   94%     0       Read & Write  EdWordPro_2
DH1:       PFS3  3471Mb     3353Mb      117Mb   96%     0       Read & Write  Games
DH2:       PFS3  3567Mb     3487Mb       80Mb   97%     0       Read & Write  Apps

Available Volumes:
PCbackup    Mounted
EMPTY       Mounted
Ram Disk    Mounted
Apps        Mounted
Games       Mounted
EdWordPro_2 Mounted
In order to get this onto a window I am building a linked list with this structure
Code:
struct window_keeper
{
	struct window_keeper *next;
	char* line;
	int body;
	int linetype;
	unsigned int lineno;
};
*next points to the next line
line is the line of text eg -DH2: PFS3 3567Mb 3487Mb 80Mb 97% 0 Read & Write Apps
body is unused at pres
linetype is 1 for the 1st heading, 2 for the device list, 3 for the Volumes heading and 4 for the volumes list
lineno is the line's number starting at 1.

This structure is the var winhead in the below code.

This is the code I am using to output to the window.
Code:
int write_win()
{
/*	struct textattr TextAttr;*/
	long win_width=0,win_height=0;
	long line_len=0,doscol=0,currentdoscol;
	long numpix=0, avpix=0, fitchar=0,tempfitchar=0;
	struct window_keeper *currentline=NULL;
	unsigned char *buffer;	/*used for lines*/
	unsigned char *buffer_part; /*used for colours*/
	unsigned int charpos=0, percentpos=0;
int zz=0;


	win_width=mywindow->Width-mywindow->BorderRight;
	win_height=mywindow->Height-mywindow->BorderBottom;

	/*dirty way to find how may chars will fit into window*/

	myIText.IText="M";
	numpix=IntuiTextLength(&myIText);

	fitchar=(win_width/numpix) -4; /* -4 for safety*/

	printf("number of pix %d width %d max=%d\n",numpix,win_width,fitchar);


	if (fitchar==0) /*cant get any so give up*/
		return 0;

	if (winhead)	/*if we have something*/
	{

		if (currentline==NULL)
			currentline=winhead;
	}
	else
		return 0; /*nothing saved!*/

	buffer=malloc((maxlinelen+1)*sizeof(char)); /*make memory buffers for line plus null char*/
	buffer_part=malloc((maxlinelen+1)*sizeof(char));


	while ((currentline) && ((10*currentline->lineno)<win_height)) /*now dump saved text rough attempt stopping at end of data or window*/
	{

/*		printf("BUFFER is %d\n",&buffer);
		printf("Line -%s-\n",currentline->line);	*/
		if (fitchar>strlen(currentline->line))
		{
			
			memcpy(buffer, (char *)currentline->line, strlen(currentline->line));
			(char)buffer[strlen(currentline->line)]='\0';	/*teminate string*/
		}
		else
		{	

			memcpy(buffer, (char *)currentline->line,fitchar );
			(char)buffer[fitchar]='\0';	/*terminate string*/
		}

		switch (currentline->linetype)
		{
			case 1:
				myIText.FrontPen = white;
				doscol=finddoscol(buffer);
				break;

			case 2:
				charpos=findchar(buffer,':');
				currentdoscol=finddoscol(buffer);
				if (charpos!=-1)
				{
					
					myIText.FrontPen = white;
					leftstr(buffer_part,currentline->line,charpos);
					myIText.IText=buffer_part;
					PrintIText(mywindow->RPort,&myIText,10,10*currentline->lineno);

					/*now the rest*/

					/*but can it fit?*/

					tempfitchar=(win_width/numpix) -4;/* - (strlen(buffer_part)); -4 for safety*/
					
					if (tempfitchar>0)
					{
						myIText.FrontPen = black;
						midstr(buffer,currentline->line,currentdoscol,tempfitchar-(currentdoscol));




						/*now look for %*/
						
						percentpos=findchar(currentline->line,'%');
						if (percentpos!=-1)
						{
							percentpos=percentpos-3;
							/*dump up to percent*/

							midstr(buffer_part,currentline->line,currentdoscol,percentpos);
							myIText.IText=buffer_part;

					printf("doscol=%d currentdoscol=%d buffer-%s-\n",doscol,currentdoscol,buffer_part);

							PrintIText(mywindow->RPort,&myIText,10+(10*(doscol-3)),10*currentline->lineno);  /*<<<<< why do I have to subtract 3????*/

							/*colour for percent*/
							myIText.FrontPen = white;
							midstr(buffer_part,currentline->line,percentpos,percentpos+3);
							myIText.IText=buffer_part;

							printf("PERCENT BUFFER -%s- %d\n",buffer_part,percentpos);

							[B]PrintIText(mywindow->RPort,&myIText,10+(10*(percentpos-9)),10*currentline->lineno); /*<<<<<< no idea why i need to subtract 9*/[/B]
	
							myIText.FrontPen = black;
							/*set up to print remainder - do we need to check size?*/

							midstr(buffer,currentline->line,percentpos+4,tempfitchar);
							myIText.IText=buffer;

							charpos=percentpos+3;

							PrintIText(mywindow->RPort,&myIText,10+(10*(charpos-9)),10*currentline->lineno);


						}
						else
						{
							/*now print out*/
						
							myIText.IText=buffer;
							PrintIText(mywindow->RPort,&myIText,10+(10*charpos),10*currentline->lineno);
						}
					}
				}
				else /* cant find so dump*/
				{
					myIText.FrontPen = black;
					myIText.IText=buffer;

					PrintIText(mywindow->RPort,&myIText,10,10*currentline->lineno);
				}
				break;

			case 3:
				myIText.FrontPen = white;
				break;

			default:
				myIText.FrontPen = black;
				break;
		}

		if (currentline->linetype!=2)
		{
			myIText.IText=buffer;

			PrintIText(mywindow->RPort,&myIText,10,10*currentline->lineno);
		}

		currentline=currentline->next;

	}
	free(buffer);	/*clear the buffers*/
	free(buffer_part);

			printf("waiting press space\n");
				zz=0;
				while ((zz!=32) && (zz!='a'))
				{
					zz=getchar();
					if (zz=='a')
						return 1;
					putchar(zz);
				}

return 0;
}
This takes the info from the linked list in a while loop and using a couple of buffers (I need these as I do not know how long the lines could be) outputs the header, device name and percentage used in white and the rest of the text in black.
The 4 functions, findchar, midstr, leftstr and finddoscol are below:
Code:
int findchar(char string[], char letter)
{
	int result=-1;
	int i=0;
	while (string[i]!='\0')
	{
		if (string[i]==letter)
			result=i;
		i++;
	}
	return result;
}

int midstr(char str1[], char str2[],int start,int last)
{
	int count,count2;
	count=start;count2=0;
	if (last!=NULL)
	{
		while ((str2[count]!='\0') && (count<=last))
		{
			str1[count2]=str2[count];
			count++;
			count2++;
		}
	}
	else
	{
		while ((str2[count]!='\0'))
		{
			str1[count2]=str2[count];
			count++;
			count2++;
		}
		
	}
	str1[count2]='\0';
	return count2;
}

int leftstr(char str1[],char str2[],int last)
{
	return midstr(str1,str2,0,last);
}

long finddoscol(char str[])
{
	int i=0;

	while((str[i]!='\0') && (str[i]!=' '))
		i++;

	if (str[i]=='\0')
		return i;

	while ((str[i]==' ') && (str[i]!='\0'))
		i++;

	return i;
}
The problem lines in write_win are marked, and below for clarity..
.
.
myIText.IText=buffer_part;

printf("doscol=%d currentdoscol=%d buffer-%s-\n",doscol,currentdoscol,buffer_part);

PrintIText(mywindow->RPort,&myIText,10+(10*(doscol-3)),10*currentline->lineno); /*<<<<< why do I have to subtract 3????*/

/*colour for percent*/
.
.

and
.
.
myIText.IText=buffer_part;

printf("PERCENT BUFFER -%s- %d\n",buffer_part,percentpos);

PrintIText(mywindow->RPort,&myIText,10+(10*(percentpos-9)),10*currentline->lineno); /*<<<<<< no idea why i need to subtract 9*/

myIText.FrontPen = black;
/*set up to print remainder - do we need to check size?*/
.
.
.
If I don't subtract 3 and 9 then the dos columns are 3 chars to the right of the header and the errors, status etc are 9 chars to the right of their cols.

Sorry this is such a long post, but could someone suggest where I have gone wrong? The values for doscol and currentdoscol are correct at 11, but the dos column etc is outputting starting 140 pixels from the left of the window (10+(currentdoscol*10)) should be 110! (I'm counting as the 1st char being 0)

I have no idea why
chocsplease is offline  
 
Page generated in 0.05689 seconds with 10 queries