473,549 Members | 2,346 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 2094
"__PPS__" <i-*********@yande x.ru> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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-*********@yande x.ru> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.or g/check?uri=refer er\">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-*********@yande x.ru> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.or g/check?uri=refer er\">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_di v().addattr("at tr1","x").addat tr("attr1","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="ye s" 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_di v().addattr("at tr1","x").addat tr("attr1","y") .value("hello
world"))


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

x.tr(' bgColor="#{Parc hment}"'){
x.td{x.small{x. em{ 'Page' }}}
x.td{x.small{
x.em{ 'Citation for ' }
x.escape(@rx.in spect)
}}
}

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-*********@yande x.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="ye s" 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="#{Parc hment}"'){
x.td{x.small{x. em{ 'Page' }}}
x.td{x.small{
x.em{ 'Citation for ' }
x.escape(@rx.in spect)
}}
}
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 generic design patterns that can be used and shared amongst many sub-schemas. For example, the grouping of entities. I may have the following tables:...
9
2913
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the...
2
2436
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 experts. I've been asked to create a Daily Log sheet to be distributed to some of our clerks. For each day, the clerk is to log tasks worked on for the...
6
2107
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
17
2678
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 know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to...
17
4830
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 Page Is Valid XHTML 1.0 Transitional!" but it still wouldn't look the same. It is on http://www.dvdnowkiosks.com/new/theproduct.php scroll down and...
6
2126
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 times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
0
2066
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 end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently...
19
3141
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 certification, possibly that involves C++, but, if not, C++ and UML. All I could find was Java + UML design certifications (one such is detailed on...
8
2210
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, port number, or both. How should we design the classes to represent these filters? My answer was: class FilterRule {
0
7471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7985
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7830
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5387
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1962
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 we have to send another system
0
784
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.