By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

cpp for cgi, design question

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
"__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

P: n/a

"__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

P: n/a
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

P: n/a
> > ...
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

P: n/a
__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

P: n/a
"__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

P: n/a
> 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 discussion thread is closed

Replies have been disabled for this discussion.