Напишим простой CGI спомощью моей библиотеки см. https://cloud.mail.ru/public/UWRh/PfovNYdbx, так будет выглядеть результат:
Файл tag.h:
#ifndef TAG_H
#define TAG_H
#include "control.hpp"
#include <iostream>
using namespace std;
class Tag {
private:
string id;
string tagname;
string attribute;
string innerHTML;
public:
Tag(string tagname);
Tag(string tagname, string id);
void setAttribute(string attribute);
string getAttribute();
void setInnerHTML(string innerHTML);
string getInnerHTML();
string render();
};
#endif
Файл tag.cpp:
#include "tag.h"
Tag::Tag(string tagname) {
this->tagname = tagname;
}
Tag::Tag(string tagname, string id) {
this->tagname = tagname;
this->id = id;
}
void Tag::setAttribute(string attribute) {
this->attribute = attribute;
}
string Tag::getAttribute() {
return this->attribute;
}
void Tag::setInnerHTML(string innerHTML) {
this->innerHTML = innerHTML;
}
string Tag::getInnerHTML() {
return this->innerHTML;
}
string Tag::render() {
string temp = "";
if(this->innerHTML.length() > 0)
temp = this->innerHTML;
string buffer = "<" + this->tagname + " " + this->attribute + " " + ">"+ temp + "<" + this->tagname + "/>";
return buffer;
}Файл textbox.h:
#ifndef TEXTBOX_H
#define TEXTBOX_H
#include "control.hpp"
#include <iostream>
using namespace std;
class TextBox {
private:
string id;
string text;
string attribute;
public:
TextBox(string id);
void setText(string text);
string getText();
void setAttribute(string attribute);
string getAttribute();
string render();
};
#endifФайл textbox.cpp:
#include "textbox.h"
TextBox::TextBox(string id) {
this->id = id;
}
void TextBox::setText(string text) {
this->text = text;
}
string TextBox::getText() {
return this->text;
}
void TextBox::setAttribute(string attribute) {
this->attribute = attribute;
}
string TextBox::getAttribute() {
return this->attribute;
}
string TextBox::render() {
string temp = "";
if(this->text.length() > 0)
temp = "value=\"" + this->text + "\"";
string buffer = "<input type=\"text\" " + this->attribute + " " + temp + "/>";
return buffer;
}Файл button.h:
#ifndef BUTTON_H
#define BUTTON_H
#include "control.hpp"
#include <iostream>
using namespace std;
class Button {
private:
string id;
string text;
string attribute;
public:
Button(string id);
void setText(string text);
string getText();
void setAttribute(string attribute);
string getAttribute();
string render();
};
#endif
Файл button.cpp:
#include "button.h"
Button::Button(string id) {
this->id = id;
}
void Button::setText(string text) {
this->text = text;
}
string Button::getText() {
return this->text;
}
void Button::setAttribute(string attribute) {
this->attribute = attribute;
}
string Button::getAttribute() {
return this->attribute;
}
string Button::render() {
string temp = "";
if(this->text.length() > 0)
temp = "value=\"" + this->text + "\"";
string buffer = "<input type=\"button\" " + this->attribute + " " + temp + "/>";
return buffer;
}
Файл main.cpp, это главный файл:
#include "textbox.h"
#include "button.h"
#include "tag.h"
#include <control.hpp>
#include <response.hpp>
#include <request.hpp>
#include <htmlinput.hpp>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
const string header = "<!DOCTYPE html>\n"
"<html>"
"\n<head>"
"\n<title>Demo CGI</title>"
"\n</head>"
"\n<body>"
"\n";
const string footer = "\n</body>"
"\n</html>";
try {
Response *r = new Response();
r->set_content_type("text/html");
r->begin_html();
r->add_text(header);
Tag *div1 = new Tag("div", "div1");
div1->setInnerHTML("Hello!");
Tag *bold = new Tag("b");
bold->setInnerHTML(div1->render());
r->add_text(bold->render());
Tag *h1 = new Tag("h1");
h1->setInnerHTML("Hello World!");
r->add_text(h1->render());
r->add_text(footer);
r->render();
}
catch(const exception& e) {
cout << header
<< "Exception "
<< e.what()
<< "position:"
<< footer
<< endl;
}
return 0;
}