// aniscrl.h --- Provides Page by Page output like cout with buffer for DOS.
/*+------------------------------------------------+*/
/*|Copyright (C) 2003 Anirudh Sasikumar            |*/
/*|                                                |*/
/*|Author: Anirudh Sasikumar <an1sk@hotmail.com>   |*/
/*|Created: January 3 2003                         |*/
/*+------------------------------------------------+*/
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */

/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
/* GNU General Public License for more details. */

/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* Commentary: */
/* This was written and compiled in Turbo C++ 3.0. This is one of the modules*/
/* for anishell. There are still some bugs to be worked out.*/
/* You can declare an object of aniscroll and use it just like cout.*/
/* Eg:  */
/* aniscroll obj1; */
/* obj1 << "A lot of strings and " << 1212 << 2.4 << var; */

#ifndef ANISCRL_H
#define ANISCRL_H
#define ANIMAXPAGES 50

#ifndef __IOSTREAM_H
#include<iostream.h>
#endif

#ifndef __CONIO_H
#include<conio.h>
#endif

#ifndef __STRING_H
#include<string.h>
#endif

#ifndef __STDLIB_H
#include<stdlib.h>
#endif

class aniscroll
{
int pos,page,totpage;//screen has to be cleared for first view
int upd,esc;//upd for smart navig display,esc for escape support
int index[ANIMAXPAGES];
char *data;
int display(int tot=0); //main menu printing func
public:
aniscroll()
{
pos=0;
page=0;
totpage=0;
data=NULL;
index[0]=0;
upd=0;
esc=0;
}
~aniscroll()
{
if(data!=NULL)
free(data);
}
friend aniscroll& operator<< (aniscroll& obj,const char *text);
friend aniscroll& operator<< (aniscroll& obj,int no);
friend aniscroll& operator<< (aniscroll& obj,char ch);
friend aniscroll& operator<< (aniscroll& obj,aniscroll& (*funcptr)(aniscroll&));
friend aniscroll& endl(aniscroll& os);
int prevpage();
int nextpage();
void lastpage();
void view();
void flush(void);
int size()
{
return strlen(data);
}
const char *getdata(void)
{
return data;
}
int currpage()
{
return page;
}
int totpages()
{
return totpage;
}
};
aniscroll& operator<< (aniscroll& obj,const char *text)
	{
	if(obj.data==NULL)
		{obj.data=strdup(text);
		clrscr();
		}
	else
		{obj.pos=strlen(obj.data);
		obj.data=(char *)realloc(obj.data,strlen(obj.data)+1+strlen(text)+1);
		strcat(obj.data,text);
		}
	while(obj.pos<strlen(obj.data))
	{
	if(wherey()>=24)
		{obj.index[++obj.page]=obj.pos;
		obj.totpage++;
		if(obj.esc!=27)
		obj.esc=obj.display();
		clrscr();
		}
	else
		cout << obj.data[obj.pos++];
	}
	return obj;
	};
aniscroll& operator<< (aniscroll& obj,int no)
	{
	char temp[6];
	itoa(no,temp,10);
	if(obj.data==NULL)
		{
		obj.data=strdup(temp);
		clrscr();
		}
	else
		{obj.pos=strlen(obj.data);
		obj.data=(char *)realloc(obj.data,strlen(obj.data)+1+strlen(temp)+1);
		strcat(obj.data,temp);}

	while(obj.pos<strlen(obj.data))
	{
	if(wherey()>=24)
		{obj.index[++obj.page]=obj.pos;
		obj.totpage++;
		if(obj.esc!=27)
		obj.esc=obj.display();
		clrscr();
		}
	else
		cout << obj.data[obj.pos++];
	}
	return obj;
	};
aniscroll& operator<< (aniscroll& obj,char ch)
	{
	char temp[2];
	temp[0]=ch;
	temp[1]='\0';
	if(obj.data==NULL)
		{obj.data=strdup(temp);
		clrscr();
		}
	else
		{obj.data=(char *)realloc(obj.data,strlen(obj.data)+1+2);
		strcat(obj.data,temp);
		}
	while(obj.pos<strlen(obj.data))
	{
	if(wherey()>=24)
		{
		obj.index[++obj.page]=obj.pos;
		obj.totpage++;
		if(obj.esc!=27)
		obj.esc=obj.display();
		clrscr();
		}
	else
		cout << obj.data[obj.pos++];
	}
	return obj;
	};

aniscroll& operator<< (aniscroll& obj,aniscroll& (*funcptr)(aniscroll&))
	{
	funcptr(obj);
	return obj;
	};
int aniscroll::prevpage()
{
if(page==1) return 0;
if(page==2)
	{
	pos=0;
	}
if(page>2)
{pos=index[page-2];
}
page--;
clrscr();
while(pos<index[page])
	{
		cout << data[pos++];
	}
cout << endl << "||More|| (Page: " << page << "/" <<totpage;
cout << ")Prev Page:PageUp Next Page: PageDown";
return 1;
}
int aniscroll::nextpage()
{
int lim=0;
if(page>=totpage) return 0;
pos=index[page];
page++;
if(page==totpage) {lim=size();}
else lim=index[page];
clrscr();
while(pos<lim)
	{
		if((wherey())==24)
		  break;//to prevent extra line during incomplete traversal nextpage
		cout << data[pos++];
	}
cout << endl << "||More|| (Page: " << page << "/" <<totpage;
	cout << ")Previous Page:PageUp Next Page: PageDown";
return 1;
}
int aniscroll::display(int tot)
	{int ch=13;
	if(!upd)
	{
	if(tot)
	{cout << endl << "||More|| (Page: " << page << "/" <<totpage;
	cout << ")Prev Page:PageUp Next Page: PageDown";}
	else
	{cout << endl <<  "||More|| (Page: " << page << ")";
	 cout << "Prev Page:PageUp Next Page: PageDown";}
	}//end of upd
	while(ch!=27)
		{
		ch=getch();
		if(ch==73)
			{prevpage();}
		if(ch==13||ch==81)
			{if(nextpage()==0) break;
			}

		}
	return ch;
	}
void aniscroll::lastpage()
	{
	int ch;
	page++;
	totpage++;
	upd=0;
		do{
		ch=display(1);
		if(ch==81) upd=1; else upd=0;
		}
		while(ch!=13&&ch!=27);

	}

void aniscroll::view()
	{
	int ch;
	pos=0;
	page=1;
	while(pos<index[page])
	cout << data[pos++];
	upd=0;
		do{
		ch=display(1);
		if(ch==81) upd=1; else upd=0;
		}
		while(ch!=13&&ch!=27);

	}
void aniscroll::flush(void)
{
free(data);
pos=0;
page=0;
totpage=0;
data=NULL;
index[0]=0;
upd=0;
esc=0;
}
aniscroll& endl(aniscroll& os) //overloading endl for scroll class
	{
	os << "\n";
	return os;
	}
#endif

Copyright © 2004-2011 Anirudh Sasikumar. All rights reserved.
Last Updated: November 29, 2011 4:41 PM