pageup/pagedown implementation using C language

S

ssubbarayan

Dear all,
I am in the process of implementing pageup/pagedown feature in our
consumer electronics project.The idea is to provide feature to the
customers to that similar to viewing a single sms message in a mobile
device.With in the given view area if the whole message does not
fit,we need to provide the ability for users to scroll through the
entire message using pageup/pagedown or key up and key down.In our
case we have the whole contents to be displayed stored in a buffer.I
am giving below one implementation I have tried:

Note:We are using only pure C code and dont have active x controls
like tabs or pageup/down buttons as in .NET or VC++.

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


void pageup();
void pagedown();
static int pagecounter=0;
/*bytes per page calculated by trial and error
considering the viewable area in my device*/
int bytesperpage=161;
int numpages=0;
int contentremaining=0;
char* customstringcopy=NULL;
char arr[161]={0};
int main(int argc, char *argv[])
{
int totlen=0;
int pagenum=0;


char* customstring=NULL;

char* data="Bloodshed Dev-C++ is a full-featured Integrated
Development Environment (IDE) for the C/C++ programming language. It
uses Mingw port of GCC (GNU Compiler Collec.";
char* stringdata="Bloodshed Dev-C++ is a full-featured Integrated
Development Environment (IDE) for the C/C++ programming language. It
uses Mingw port of GCC (GNU Compiler Collection) as it's compiler.
Dev-
C++ can also be used in combination with Cygwin or any other GCC based
compiler.";
char* stringdata1="the #bloodshed channel has recently been
created on the Undernet IRC server. I will be please to talk with you
there so feel free to join :) If you want have an IRC client you can
get one for Windows at mirc.com and for Linux at xchat.org";
char* stringdata2="You can subscribe to the Dev-C++ mailing list
(for asking and answering questions on Dev-C++ and C/C++ programming)
by clicking here and filling out the subscribe form there.";

totlen=strlen(stringdata)+strlen(stringdata)+strlen(stringdata2);
printf("total length is %d\n",totlen);
printf("length of data is %d\n",strlen(data) );
customstring=(char*)(malloc)(totlen+4);
customstringcopy=customstring;
memset(customstring,0,totlen+4);

memcpy(customstring,stringdata,strlen(stringdata));
customstring=customstring+strlen(stringdata);
customstring[0]='\n';
customstring++;
/*printf("%s\n",stringdata);
printf("%s\n",customstringcopy);*/

/*customstring[0]='\n';
customstring++;*/

memcpy(customstring,stringdata1,strlen(stringdata1));
customstring=customstring+strlen(stringdata1);
customstring[0]='\n';
customstring++;
/*printf("%s\n",stringdata1);
printf("%s\n",customstringcopy);*/

memcpy(customstring,stringdata2,strlen(stringdata2));
customstring=customstring+strlen(stringdata2);
customstring[0]='\n';
customstring++;
/* printf("%s\n",stringdata2);*/
printf("%s\n",customstringcopy);

numpages=totlen/bytesperpage;
printf("total number of pages is %d\n",numpages);
contentremaining=(totlen)%(bytesperpage);
if(contentremaining > 0)
{
numpages=numpages+1;

}
printf("total number of pages is %d\n",numpages);
for(pagenum=0;pagenum<=numpages;pagenum++)
{
pageup();
}
for(pagenum=numpages;pagenum>0;pagenum--)
{
pagedown();
}
system("PAUSE");
return EXIT_SUCCESS;
}

void pageup()
{


if(pagecounter<numpages)
{
pagecounter++;
printf("pagecounter value is %d\n",pagecounter);
memcpy(arr,customstringcopy,161);
printf("%s\n\n",arr);
customstringcopy=customstringcopy
+bytesperpage;
}


}

void pagedown()
{

if(pagecounter==numpages)
{
pagecounter--;
printf("pagecounter value is %d
\n",pagecounter);
customstringcopy=customstringcopy-2*(bytesperpage);
memcpy(arr,customstringcopy,161);
printf("%s\n\n",arr);
}
else
{
if(pagecounter>1)
{
pagecounter--;
printf("pagecounter value is %d
\n",pagecounter);
customstringcopy=customstringcopy-(bytesperpage);
memcpy(arr,customstringcopy,161);
printf("%s\n\n",arr);
}


}
/* if(pagecounter>0)
{

customstringcopy=customstringcopy-
bytesperpage;
memcpy(arr,customstringcopy,161);
printf("%s\n\n",arr);

} */

}

Incase you find bugs in above code please let me know ways to fix it.

I believe there are much better ways then the one I have tried here.It
would be helpful if some one could provide me some sample code for
similar features you would have come across in your product or some
sample links which shows me sample code on how to implement such a
feature.

Note:I am looking for only C code and not C#,JAVA,.NET,VC++

Looking farward for all your replies and advanced thanks for the same,
Regards,
s.subbarayan
 
D

David Brown

ssubbarayan said:
Dear all,
<snip>

Here's another bit of free advice - posting the same long imprecise
question three times with slightly different wording puts you on the
fast-track to the killfiles of many Usenet regulars.
 
B

Bartc

ssubbarayan said:
Dear all,
I am in the process of implementing pageup/pagedown feature in our
consumer electronics project.The idea is to provide feature to the
customers to that similar to viewing a single sms message in a mobile
device.With in the given view area if the whole message does not
fit,we need to provide the ability for users to scroll through the
entire message using pageup/pagedown or key up and key down.In our
case we have the whole contents to be displayed stored in a buffer.I
am giving below one implementation I have tried:

Note:We are using only pure C code and dont have active x controls
like tabs or pageup/down buttons as in .NET or VC++.

I found your code as unreadable as others have suggested. Perhap
/temporarily/ use a text editor to shorten variable names then the logic
might become clearer.

I've had a go at some pageup/pagedown code. (First time I've used C for this
and was quite a painful experience, especially trying to read a single key
using standard C. If you have a choice of using a rapid development
language, then use it!)

I don't know about your display; the display here is filled up left to
right, top to bottom, without caring about splitting lines in the middle of
words, and assuming a fixed pitch font.

Also, I've split the logic into two: display the text using as input:

* The text
* A single number indicating where to start

And some logic which manipulates that number. (The example displaytext()
function just rewrites everything; probably you will want to use extra logic
to scroll most of the display where possible.)


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

char *text="The #bloodshed channel has recently been "
"created on the Undernet IRC server. I will be please to talk
with you"
" there so feel free to join :) If you want have an IRC client
you can"
" get one for Windows at mirc.com and for Linux at xchat.org";

#define rowchars 16 /* Chars displayed per row */
#define displayrows 4 /* Rows available on display */

int nlines; /* Lines required by entire message */
int startline; /* Current display starts at this line# (line 1 =
top) */

void displaytext(char *text, int nlines, int startline);

int main(void) {
char c;
int quit=0;

nlines=(strlen(text)-1)/rowchars+1;
startline=1;

while (! quit) {
displaytext(text,nlines,startline);

printf("\nPress U-Up D-Down A-Pageup B-Pagedown H-Home E-End Q-Quit then
Enter:\n");
BODGE1:
c=getc(stdin);

switch (c) {
case 'U': case 'u':
if (startline>1)
--startline;
break;

case 'D': case 'd':
if (startline<nlines)
++startline;
break;

case 'A': case 'a':
startline-=displayrows;
if (startline<1)
startline=1;
break;

case 'B': case 'b':
startline+=displayrows;
if (startline>nlines)
startline=nlines-displayrows+1;
break;

case 'H': case 'h':
startline-=displayrows;
startline=1;
break;

case 'E': case 'e':
startline=nlines-displayrows+1;
break;

case 'Q': case 'q':
quit=1;
break;

default:
goto BODGE1; /* SKIP EXTRANEOUS INPUT */
}
}
}

void displaytext(char *text, int nlines, int startline) {
int i,j,n;

text += rowchars*(startline-1);

n = nlines-startline+1; /* Remaining lines to display */
if (n>displayrows) n=displayrows;

/* clear display */
/* ... */
puts("--------------------------");

for (i=1; i<=n; ++i) {
printf("%04d: ",startline++);
for (j=1; j<=rowchars; ++j) {
if (!*text) {
puts("");
return;
}
printf("%c",*text++);
}
puts("");
}
}
 
K

Kenny McCormack

I found your code as unreadable as others have suggested. Perhap
/temporarily/ use a text editor to shorten variable names then the logic
might become clearer.

Isn't it perfectly clear that the only clc-acceptable way to do this is:

system("less");

(With, of course,all the usual extra stuff needed to make it compile and
to pass muster in CLC. And, of course, some parsing of argv[]).
 
B

Bresco

ssubbarayan said:
Dear all,
I am in the process of implementing pageup/pagedown feature in our
consumer electronics project.The idea is to provide feature to the
customers to that similar to viewing a single sms message in a mobile
device.With in the given view area if the whole message does not
fit,we need to provide the ability for users to scroll through the
entire message using pageup/pagedown or key up and key down.In our
case we have the whole contents to be displayed stored in a buffer.I
am giving below one implementation I have tried:

Note:We are using only pure C code and dont have active x controls
like tabs or pageup/down buttons as in .NET or VC++.

You're telling me you don't have a graphical display on this 'device' but
are forced to implment it using a kind of Unix terminal?

I can't imagine a device that doesn't have a graphical color display these
days, and for that you always need libraries, such as MicroWindows or some
other free open-source GUI system. And with those, you don't need to
implement a page-up / down since they are part of the textcontrols.
 
L

Lew Pitcher

ssubbarayan said:
Dear all,
I am in the process of implementing pageup/pagedown feature in our
consumer electronics project.The idea is to provide feature to the
customers to that similar to viewing a single sms message in a mobile
device.
[snip]
You're telling me you don't have a graphical display on this 'device' but
are forced to implment it using a kind of Unix terminal?

I can't imagine a device that doesn't have a graphical color display these
days
[snip]

Then I guess that you don't own or use (and have never seen) microwave ovens
or clock-radios or computer printers or watches or mp3 players.

Your lack of imagination doesn't invalidate the OP's requirements. Instead,
it points out how little you know.


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
D

Default User

David said:
<snip>

Here's another bit of free advice - posting the same long imprecise
question three times with slightly different wording puts you on the
fast-track to the killfiles of many Usenet regulars.

Google Groups went through another one of its fits. Users weren't
getting new posts displayed. Bleah.




Brian
 
H

Hagen Patzke

ssubbarayan said:
I believe there are much better ways then the one I have tried here.
It would be helpful if some one could provide me some sample code
[...]


Here is my suggestion:

/** @file scrolltest.c */
#define DEBUG
#ifdef DEBUG
#include <stdio.h>
#endif

#define DISPLAY_CHUNK 160

/* + test data */
static char *buffer = "Long data string to be scrolled. Please note"
" there are no (char)0 elements in the string, which would allow "
"use of strcat to combine buffers. "
"If everything is in one buffer anyway, the length of the buffer is"
" known, and the chunk size of output data is fix, then just one "
"pointer to the current start position of the display is "
"sufficient. "
"The C compiler is assumed to combine separate strings into "
"one long string, so for our test we skip any special code for "
"just constructing a demo.";
/* - test data */

static int curpos;
static int minpos;
static int maxpos;
static char arr[DISPLAY_CHUNK + 1]; /* trailing (char)0 */

void update_display(){
/* force legal start position */
if (curpos < minpos) { curpos = minpos; }
if (curpos > maxpos) { curpos = maxpos; }
memcpy(arr, &buffer[curpos], DISPLAY_CHUNK);
arr[DISPLAY_CHUNK]='\0';
#ifdef DEBUG
printf("pos %d display content:\n%s\n", curpos, arr);
#else
output_to_display(arr);
#endif
}

void page_up(){
curpos -= DISPLAY_CHUNK;
update_display();
}

void page_down(){
curpos += DISPLAY_CHUNK;
update_display();
}

int main(int argc, char * argv[]){
/* required initialization */
curpos = 0;
minpos = 0;
maxpos = strlen(buffer) - DISPLAY_CHUNK;
/* initial output */
update_display();
/* scroll to bottom */
while (curpos < maxpos) page_down();
/* scroll to top */
while (curpos > minpos) page_up();
}
/* end of scrolltest.c **/

Have fun! :)


totlen=strlen(stringdata)+strlen(stringdata)+strlen(stringdata2);

BUG: adding lengths of stringdata twice(!) plus of stringdata2
customstringcopy=customstringcopy-bytesperpage;
memcpy(arr,customstringcopy,161);

BUG: using variable for navigation but number literal for memcpy

WARNING: Variable "data" not used.
I found your code as unreadable as others have suggested. Perhap
/temporarily/ use a text editor to shorten variable names then the
logic might become clearer.

Recommendation: GNU indent v2.2.9 (with line length set to 68 chars)
plus short variable names.
 
S

ssubbarayan

Google Groups went through another one of its fits. Users weren't
getting new posts displayed. Bleah.

Brian

Hi,
Yes that was the reason that thinking my question did not appear I
posted it thrice!Sorry about it.
Regards,
s.subbarayan
 
E

Ed Prochak

Dear all,
I am in the process of implementing pageup/pagedown feature in our
consumer electronics project.The idea is to provide feature to the
customers to that similar to viewing a single sms message in a mobile
device.With in the given view area if the whole message does not
fit,we need to provide the ability for users to scroll through the
entire message using pageup/pagedown or key up and key down.In our
case we have the whole contents to be displayed stored in a buffer.I
am giving below one implementation I have tried:

Note:We are using only pure C code and dont have active x controls
like tabs or pageup/down buttons as in .NET or VC++.

[code deleted]
I believe there are much better ways then the one I have tried here.It
would be helpful if some one could provide me some sample code for
similar features you would have come across in your product or some
sample links which shows me sample code on how to implement such a
feature.

Note:I am looking for only C code and not C#,JAVA,.NET,VC++

Looking farward for all your replies and advanced thanks for the same,
Regards,
s.subbarayan

Even in C you would have some graphics libraries. The scrolling you
are looking for would be an easy application in most such libraries.
Given this is an embedded device, you may have to implement your own.
the key is to think of the different levels of abstraction. What does
scrolling mean at the topmost level? What functions would you need?
This is called top down programming.

HTH,
Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top