Building a web site using C++!
Hello there! This post is about the C++ application that I developed yesterday. This application can be used to build a Website! I had this idea in my mind for quite some time. I took the time and developed it at last :). The main purpose of the application is to build a simple customizable website using C++ to write HTML pages(more about it later).Some of the features of the application are:
- You can have as many pages as you want.
- A link is provided to each and every page in every page
- You can add content as much as you want in each page
- You can set the background colour, text colour and font style of each and every page
Here's a video of it:
If you are wondering how I might've developed this application, then here's a little information. I created a class called WebSite and another class called WebPage. WebPage holds information about each individual page's colour, font style, text colour, content etc. The WebSite class is used to hold the value of the number of pages in the website, a collection of WebPage(Which when put together form the website) objects and their common details such as the name of the website and the name and directory of the folder where generated HTML pages are to be stored.
Here are the definitions and methods of WebSite class followed by WebPage class:
NOTE: only inline methods are mentioned below, to see all the methods, see the link provided at the end of this post.
WebSite class:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WebSite { | |
int no_of_pages; //stores number of pages in the website | |
char name[20]; //stores the 'name' of the file where html pages are to be stored | |
char heading[20]; //stores the heading of the website | |
char caption[50]; //stores the caption of the website | |
char nav_content[300]; //used to store information about navigation and links | |
char directory_path[100]; //stores the 'path' to the directory where html files are to be stored | |
WebPage* site_pages; //stores a collection of WebPage objects | |
public: | |
WebSite(void); | |
char* getHeading(void) { //returns heading of the website | |
return heading; | |
} | |
char* getCaption(void) { //returns caption of the website | |
return caption; | |
} | |
char* getNavigation(void) { //returns navigation details of the website | |
return nav_content; | |
} | |
char* getDirectory(void) { //returns path of the file where html files are stored | |
return directory_path; | |
} | |
int getPagesCount(void) { //returns the value of number of pages in the website | |
return no_of_pages; | |
} | |
void setDirectoryPath(void); //used to get the directory where the file is created | |
void setPagesInfo(WebSite&, int); //used to set information about a selected page in the website | |
void setPagesTheme(int); //used to set theme details of a selected page in the website | |
void showPagesDetails(int); //used to show details about a selected page in the website | |
void addChapterToPage(int); //used to add content to a selected page | |
void buildNavigation(void); //used to build the navigation/links part of the page | |
void buildSite(WebSite&); //used to build the whole website | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WebPage { | |
char file_name[20]; //name of the webpage | |
char content_name[30]; //name of the text file to store content | |
char title[20]; //acts as both title and heading of the WebPage | |
char color[10]; //color of the page | |
char text_color[10]; //text color of the page | |
char font_style[15]; //font style of the text | |
char page_path[100]; //stores the path of the WebPage | |
char content_path[100]; //stores the path to to content text file of the WebPage | |
void setPath(WebSite&); //used to store the path of the current WebPage directory | |
public: | |
void setInfo(WebSite&); //set the information about page | |
void setTheme(void); //set the background color, text color and font style of the page | |
void showDetails(void); //displays details about the page | |
void addChapter(void); //append a new chapter into the page | |
void buildPage(WebSite&); //build the web-page | |
char* getPagePath(void) { //returns the page path as a string | |
return page_path; | |
} | |
char* getTitle(void) { //return the page title | |
return title; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void WebPage::buildPage(WebSite& site) | |
{ | |
ofstream wp(page_path); | |
//head and style part of the page | |
wp << "<html>\n"; | |
wp << "<head>\n"; | |
wp << "<title>" << title << "</title>\n"; | |
wp << "<style>\n"; | |
wp << "body{\n"; | |
wp << "background: " << color << ";\n"; | |
wp << "color: " << text_color << ";\n"; | |
wp << "text-align: " << "center" << ";\n"; | |
wp << "font-family:" << font_style << ";\n"; | |
wp << "}\n"; | |
wp << "p{\n"; | |
wp << "text-align: " << "left" << ";\n"; | |
wp << "}\n"; | |
wp << "h4{\n"; | |
wp << "text-align: " << "left" << ";\n"; | |
wp << "}\n"; | |
wp << "</style>\n"; | |
wp << "</head>\n"; | |
//body part of the page | |
wp << "<body>\n"; | |
wp << "<h1>" << site.getHeading() << "</h1>\n"; | |
wp << "<h5><i><marquee>" << site.getCaption() << "</marquee></i></h5>\n"; | |
//navigation bar | |
wp << site.getNavigation() << "\n"; | |
//title of the page | |
wp << "<h4><u>" << title << "</u></h4>\n"; | |
//displaying content | |
ifstream rp(content_path); | |
int character; | |
while ((character = rp.get()) != EOF) { | |
wp << (char) character; | |
} | |
wp << "</body>\n"; | |
wp << "</html>\n"; | |
rp.close(); | |
wp.close(); | |
} |
Apart from this, I've learned something new! I've known how to create a directory and get the current path using the system() and _getcwd() functions. I've used them in the setDirectoryPath() method of WebSite class. Here's the implementation:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void WebSite::setDirectoryPath(void) { | |
//create a new directory | |
char file_dir[30] = "mkdir "; | |
strcat_s(file_dir, name); | |
system(file_dir); | |
//change the existing directory to the newly created directory created | |
char req_file[30] = "\\"; | |
strcat_s(req_file, name); | |
strcat_s(req_file, "\\"); | |
//get the address of the new directory | |
_getcwd(directory_path, 99); | |
strcat_s(directory_path, req_file); | |
} |
text file: CLICK
project file: CLICK
Comments
Post a Comment