473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Implementing pageup pagedown feature using C language

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)+strle n(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
Oct 28 '08 #1
2 2835

"ssubbarayan" <ss****@gmail.comwrote in message
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.
You can speed this up a lot.

Hold the message in a single, contiguousm, NUL-terminated buffer (i.e. a big
string).
Then provide a function.

int getNpages(char *message, int width, int height)

Basically you count one column for a character, one line for a newline. Then
you divide the answer by the number of lines per page.

Now we have

void printpage(FILE *fp, char *message, int page)

it is essentially the same function as the previous one, counting columns
and lines, except that when you get to the page you want, you start calling
putchar(). No temporary buffers are required.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Oct 28 '08 #2
On 2008-10-28, ssubbarayan <ss****@gmail.comwrote:
>
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.
How did you design this code? In a sense that is a rhetorical
question because I already know how you wrote it - you opened up
your text editor and started typing, right? It shows in the code.
If it was me and I wanted to have some pride in my work I would
delete it now and start over. I'd probably end up saving time in
the long run because this code simply isn't maintainable.

I suspect you have already spent hours bashing this code into
shape. Next time, invest a little time in design first. Turn
off your computer, find a pad of paper, a pencil and a rubber and
don't go back to your computer until you know how every aspect of
your code is going to work. Your code will go together far more
quickly, and be much tidier and more compact to boot.

Returning to your code, reading between the lines it appears that
arr is supposed to represent a memory-mapped display device - this
isn't mentioned anywhere and that is one of the first things you
should specify in an early stage of designing your code. Similarly
I spent some time looking around for where you receive input from
the user, before realising that you weren't doing this, but
running through a predefined test sequence; again something that
should have been mentioned. I also take it that all those printfs
littering your code are debug code to try and get your code
working - they shouldn't be needed for something like this.

I _really_ doubt arr should be 161 characters long and your trial
and error comment does little to inspire confidence. 160 is much
more likely, quite possibly on a 40x4 matrix. If that's the case
then what about line breaks - presumably we need to wrap at word
boundaries which you aren't doing here. I can understand why you
got 161 - you need one for the trailing NULL character to treat
the display buffer as a string. Big mistake - it is a character
array, not a string, and if you start writing to arr[161] that
may well be some special function register associated with the
display.

Looking through your code in a little more detail, a few more
specific issues do come to mind.

Don't:
#include <stdio.h>
#include "string.h"

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

[Always use angle brackets for standard libraries]
[malloc() is defined in <stdlib.h>]

Don't:
totlen=strlen(stringdata)+strlen(stringdata)+strle n(stringdata2);

Do:
totlen = strlen(stringdata) + strlen(stringdata1) + strlen(stringdata2);

[Silly mistake: we all make them]

Don't:
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);

Do:
numpages = (totlen + (bytesperpage - 1)) / bytesperpage;
printf("total number of pages is %d\n", numpages);

[More compact and faster]
[This is a standard pattern whenever you need to round a division up]
[Test it with a pen and paper to prove to yourself it works]

--
Andrew Smallshaw
an*****@sdf.lonestar.org
Oct 29 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yihan | last post by:
Hi, I would like to make a pageUp and pageDown button to go up or down in the text ( it is like PgUp/PgDown on the keyboard). But it works on the IE but not on netscape 7.1. Could somebody tell me...
1
by: Harald Solvberg | last post by:
I want a datagrid object that will not intercept PageUp or PageDown keystrokes so that instead the tabcontrol it's on can pick it them up and change TabPages. Any idea how to create a new version...
0
by: nm | last post by:
I'm using the Web Browser control in my app and notice that when I enter a new URL to view via text box, then give focus to the browser control, I can't page up or down within the browser until I...
18
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am...
2
by: Phil Galey | last post by:
I have a Panel control docked on all sides on a form and the panel control contains a PictureBox. I'm using the KeyDown event of the form to respond to the and keys for resizing the image and the...
6
by: Nate | last post by:
I am in a slight predicament trying to determine the most efficient and effective way to connect/disconnect from a database within a business object (c# dll). I'm also keeping in mind the concept...
0
by: =?Utf-8?B?SGFyYWxk?= | last post by:
I have a datagrid on a tabcontrol and want PageUp and PageDown to change tabs on the tabcontrol but when on the datagrid the keystrokes get captured by the datagrid. I've created my own datagrid...
27
by: ssubbarayan | last post by:
Hi all, I am looking for some sample code which shows how to implement pageup/ pagedown feature using C language similar to the ones we encounter in our mobile devices.In mobiles if we dont have...
9
by: ssubbarayan | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.