473,498 Members | 703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cpp for cgi, design question

Hello everybody, that's already not a quiz question from school :)

I wanted to design simple stuff that would allow me to program cgi
programs with cpp. My goal is to make reading input from user much like
traditional cpp programming with iostreams:

cout << "Enter date of birth and name: ";
cin >> dob >> name;
....

I wrote a cgi library and sometimes I was adding some new stuff when I
had some free time. Today, I had an idea and really liked it - in about
a couple of hours I wrote something that looks like traditional
iostream programming

here's the result: http://block111.servehttp.com/fcgid/lib_test/x
(take a look at the cpp source link)

the only problem with it is that I have to put promts in the sequence
of input parameters, something like

cgi::cin >> "enter this" >> This >> "enter that" >> That;

What could you suggest on how to improve on this; one of the ways could
be creating some reader object that handles this stuff like this:
date dob;
reader<date> date_reader(dob, "Enter DoB: ",...)
cin >> date_reader >> ...
thank you

Oct 29 '05 #1
7 2091
"__PPS__" <i-*********@yandex.ru> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
the only problem with it is that I have to put promts in the sequence
of input parameters, something like

cgi::cin >> "enter this" >> This >> "enter that" >> That;

What could you suggest on how to improve on this; one of the ways could
be creating some reader object that handles this stuff like this:


I once wrote a Form class that had this exact problem. My solution was to
override both the input and output operators and use them in the same
expression. For example:

Form f;
string name;
int age;
f << "Name: " >> name << "Age:" >> age;

The Form class built up a form by using the << and >> operators. Since they
have the same precedence, they are executed in the correct order. The <<
operator only added an item to the form, while the >> operator added an
input control to the form.

The beauty of this system is that you can control whether something is input
or output by the operator. For example, if you have a string containing
"Hello", then << would put "Hello" on the form, while >> would input a new
value and overwrite the "Hello" in the variable.
Oct 30 '05 #2

"__PPS__" <i-*********@yandex.ru> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hello everybody, that's already not a quiz question from school :)

I wanted to design simple stuff that would allow me to program cgi
programs with cpp. My goal is to make reading input from user much like
traditional cpp programming with iostreams:

cout << "Enter date of birth and name: ";
cin >> dob >> name;
...

I wrote a cgi library and sometimes I was adding some new stuff when I
had some free time. Today, I had an idea and really liked it - in about
a couple of hours I wrote something that looks like traditional
iostream programming

here's the result: http://block111.servehttp.com/fcgid/lib_test/x
(take a look at the cpp source link)

the only problem with it is that I have to put promts in the sequence
of input parameters, something like

cgi::cin >> "enter this" >> This >> "enter that" >> That;

What could you suggest on how to improve on this; one of the ways could
be creating some reader object that handles this stuff like this:
date dob;
reader<date> date_reader(dob, "Enter DoB: ",...)
cin >> date_reader >> ...


I fear your design falls prey to "syntactic sugar".

The way to improve your specific question is research the Boost Any class (I
think it's there), and research any of many "variant" classes. They allow a
variable's type to vary:

variant(0, vt_date) date_reader(...);

Then you overload operator>> to work with variants.

However, this is still sugar. The act of converting the CGI parameters into
local variables is a small trivial part of any web application. You still
need a pattern, like Model View Controller, to avoid mixing all your
business rules up with your user interface.

Check the quality of your design by seeking duplication. For example:

std::cout <<
" <div><br />\n"
" <a title=\"Check generated html for validity\"
href=\"http://validator.w3.org/check?uri=referer\">XHTML 1.0 Strict</a><br
/>\n"
" <a title=\"View syntax-colored c++ source\"
href=\"/tra/x?file=/fcgid/lib_test/x.cpp\">View cpp source</a><br />\n"
" <br /></div>\n";

That might not look like duplication, but I see <div> and <a> duplicating
</div> and </a>. Fixing that would let your compiler check that your output
will have balanced tags.

BTW props for using XHTML!

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Oct 30 '05 #3
Phlip wrote:
"__PPS__" <i-*********@yandex.ru> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hello everybody, that's already not a quiz question from school :)

I wanted to design simple stuff that would allow me to program cgi
....
reader<date> date_reader(dob, "Enter DoB: ",...)
cin >> date_reader >> ...
I fear your design falls prey to "syntactic sugar".

The way to improve your specific question is research the Boost Any class (I
think it's there), and research any of many "variant" classes. They allow a
variable's type to vary:


I use boost::any and boost::variant in this application already but in
some other places. This particular way of writing cgi apps is a quick
hack and made on top of the other library that allowed me to create
this simple "cgiio.h" in a couple of hours. This library uses alot of
boost libs, icu, and xerces for different tasks. From the beginning I
wanted to mimic some usefull php stuff like _GET, _POST, _COOKIE,
_SERVER, _SESSION (boost::shmem) and a couple of others; some design
decisions were inspired by php prado framework (I once contributed
javascript validators for them, but they still haven't integrated them
- I use parts of it in this library too, you may check them in action
here: http://block111.servehttp.com/prado/xxx/ and
http://block111.servehttp.com/prado/validator/ (sorry, for off topic
here)) Basicly, with cgiio my goal was to create some way of specifying
constraints on user input (data type, range of valid values, comparison
to other values) so that finall application looked something like
if(cin >> input){ /* input is valid, do logic here... */ }. The deal
with that constraints - they aren't meant to be specified by the
programmer programmer - they are auto generated by transforming
pseudo-html/xml templates with xerces dom parser; so by the original
idea I would be left with only one function to write where I do
calculations etc...

variant(0, vt_date) date_reader(...);

Then you overload operator>> to work with variants.

However, this is still sugar. The act of converting the CGI parameters into
local variables is a small trivial part of any web application. You still
need a pattern, like Model View Controller, to avoid mixing all your
business rules up with your user interface.
What's Model View Controller? As I mentioned - it's not intended to
help solve all kinds of complicated problems; this syntactical sugar
(such as if(cgi::get >>input >> ... is only needed for that simple
applications that are handcoded by the programmer. The original bigger
lib doesn't need it - this part of code is meant to be auto
generated...

Check the quality of your design by seeking duplication. For example:

std::cout <<
" <div><br />\n"
" <a title=\"Check generated html for validity\"
href=\"http://validator.w3.org/check?uri=referer\">XHTML 1.0 Strict</a><br
/>\n"
" <a title=\"View syntax-colored c++ source\"
href=\"/tra/x?file=/fcgid/lib_test/x.cpp\">View cpp source</a><br />\n"
" <br /></div>\n";

That might not look like duplication, but I see <div> and <a> duplicating
</div> and </a>. Fixing that would let your compiler check that your output
will have balanced tags.
If you mean solution like div("contents of div") then, surelly I don't
need it, personally I hate such functions in cgi libraries. Before
writing some parts of my code I donwloaded many cgi libs that I could
find on (sf.net) and was comparing their code with php etc., and was
simply deleating those that had half of documentation on how to use
these garbage functions like div("..."); For me it looked like it was
more difficult to learn how to use these functions than learning to
write proper html; in addition, this functions usually provide reduced
functionality (missing <div attr1="x" attr2="y" ..> or inner html
elements other than simple text) or high complexity (something like
html::create_div().addattr("attr1","x").addattr("a ttr1","y").value("hello
world"))

....

BTW props for using XHTML!


Oct 31 '05 #4
> > ...
What could you suggest on how to improve on this; one of the ways could
be creating some reader object that handles this stuff like this:
I once wrote a Form class that had this exact problem. My solution was to
override both the input and output operators and use them in the same
expression. For example:

Form f;
string name;
int age;
f << "Name: " >> name << "Age:" >> age;

The Form class built up a form by using the << and >> operators. Since they
have the same precedence, they are executed in the correct order. The <<
operator only added an item to the form, while the >> operator added an
input control to the form.

This is a good idea, I didn't think about that! Seems like combining
this idea with the input objects that I mentioned in the previous post
is what I need.
The beauty of this system is that you can control whether something is input
or output by the operator. For example, if you have a string containing
"Hello", then << would put "Hello" on the form, while >> would input a new
value and overwrite the "Hello" in the variable.


I don't understand this part about overwriting, does it mean that
plasing >> "Hello" assignegs "Hello" to the following variable?

Oct 31 '05 #5
__PPS__ wrote:
here: http://block111.servehttp.com/prado/xxx/ and
http://block111.servehttp.com/prado/validator/ (sorry, for off topic
here))
Uh, we are discussing how to use C++ (relatively platform neutrally). All
that crap about topicality is for newbies asking "how do I click on Visual
C++ to generate a class?"
Basicly, with cgiio my goal was to create some way of specifying
constraints on user input (data type, range of valid values, comparison
to other values) so that finall application looked something like
if(cin >> input){ /* input is valid, do logic here... */ }. The deal
with that constraints - they aren't meant to be specified by the
programmer programmer - they are auto generated by transforming
pseudo-html/xml templates with xerces dom parser; so by the original
idea I would be left with only one function to write where I do
calculations etc...


Again, you seem to be approaching Model View Controller; look it up. It's a
system to use Observer Pattern to reduce GUI updates to messages. This
allows you to write completely decoupled business code.

Also, one non-heinous way to generate XHTML is by writing content in XML and
markup in XSLT, then merging them at the last minute. Put put this tag at
the top of the XSLT:

<xsl:output method="xml" media-type="text/html" standalone="no"
omit-xml-declaration="yes" encoding="UTF-8" />
That might not look like duplication, but I see <div> and <a> duplicating
</div> and </a>. Fixing that would let your compiler check that your
output
will have balanced tags.


If you mean solution like div("contents of div") then, surelly I don't
need it, personally I hate such functions in cgi libraries. Before
writing some parts of my code I donwloaded many cgi libs that I could
find on (sf.net) and was comparing their code with php etc., and was
simply deleating those that had half of documentation on how to use
these garbage functions like div("..."); For me it looked like it was
more difficult to learn how to use these functions than learning to
write proper html; in addition, this functions usually provide reduced
functionality (missing <div attr1="x" attr2="y" ..> or inner html
elements other than simple text) or high complexity (something like
html::create_div().addattr("attr1","x").addattr("a ttr1","y").value("hello
world"))


Right. That latter is absurd. Here's what I meant (in [gasp] an Off Topic
Language!!):

x.tr(' bgColor="#{Parchment}"'){
x.td{x.small{x.em{ 'Page' }}}
x.td{x.small{
x.em{ 'Citation for ' }
x.escape(@rx.inspect)
}}
}

That's is Ruby code to generate one row of a table. If we want a program
with lots of string manipulation, then avoiding C++'s purposefully spartan
string handling gives us many advantages. For example, we set the background
color of our row to Parchment not by adding lots of .addattr() methods, but
by passing in a string. Ruby makes that the simplest way.

Then, the {} things evaluate their contents as closures within the calling
method. So after the last }, tr() robustly provides the </tr> tag.

That's what those CGI libraries tried to give you, and it's really hard in
C++. These kinds of block closures would be a welcome addition because they
decouple clean-up routines from the calling code.

C++ can do the same thing with destructors:

Tr tr("...");
Td td("...");
Td td("...");

But that's still less flexible.

I suppose _I_ have the chops to write elegant CGI code in C++. But why are
you trying? when there are scripting language designed for this stuff...

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Oct 31 '05 #6
"__PPS__" <i-*********@yandex.ru> wrote in message > This is a good idea, I
didn't think about that!
The beauty of this system is that you can control whether something is
input
or output by the operator. For example, if you have a string containing
"Hello", then << would put "Hello" on the form, while >> would input a
new
value and overwrite the "Hello" in the variable.


I don't understand this part about overwriting, does it mean that
plasing >> "Hello" assignegs "Hello" to the following variable?


Sorry. That was misleading. What I should have said is that Form << string
would place the contents of the string onto the form. Form >> string would
take input from the user and change the contents of the string.
Nov 1 '05 #7
> Also, one non-heinous way to generate XHTML is by writing content in XML and
markup in XSLT, then merging them at the last minute. Put put this tag at
the top of the XSLT:

<xsl:output method="xml" media-type="text/html" standalone="no"
omit-xml-declaration="yes" encoding="UTF-8" />
That's irrelevant here. I studied xslt for some time before I
understood that xslt isn't enough for me to do the necessary
transformations

Right. That latter is absurd. Here's what I meant (in [gasp] an Off Topic
Language!!):

x.tr(' bgColor="#{Parchment}"'){
x.td{x.small{x.em{ 'Page' }}}
x.td{x.small{
x.em{ 'Citation for ' }
x.escape(@rx.inspect)
}}
}
Still I don't find it even readable. I prefer writing boost::format and
then putting data into the rows like this:
//piece of some generated code
static boost::format
tmpl("<tr><td>%1%</td><td>%2%</td><td>%3%</td></tr>\n");
//piece written by the programmer
while(...) cout << tmpl % favoriteColor % favoriteMovie % favoriteCar;

C++ can do the same thing with destructors:

Tr tr("...");
Td td("...");
Td td("...");
There's unlimited number of ways it could be done in c++, but still
this example is complete crap - imagine how it would all look like if
cells of table had suppose some string content that has some link
inside... even if you find some good solution (like Td td("xxx <a
....>link</a>yyy :)"); ), you then also should be able to write spanned
cells/rows... Overall, writing plain html is simpler - even if you
write invalid markup, you still can easyli modify it, but fixing wrong
markup that's generated by c++ will be harder task... although, c++ may
easily make sure that special chars get properly escaped, and needed
tags properly closed

But that's still less flexible.

I suppose _I_ have the chops to write elegant CGI code in C++. But why are
you trying? when there are scripting language designed for this stuff...


I'm perfectly aware of this, I personally know perl and php quite well,
and still, if I were to write some web app for someone I'll choose any
of the two, however, for myself I'd use c++, because I like c++; I'm
not doing all this work wasting someone else's money and time, it's
purely my own interest in c++ ... and that's why I ask some design
questions here

Nov 1 '05 #8

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

Similar topics

5
674
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
2912
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
2423
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
2103
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
2668
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
4824
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
2120
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
2064
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
3132
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
2205
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
0
7124
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,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.