Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

HTML and CSS C++ class

1.95/5 (6 votes)
27 Jan 2008CPOL 1   407  
Makes handling HTML and CSS in the text form in C/C++ easy

Introduction

I made these classes so that I could easily handle HTML and CSS in my programs. But because they are so useful for me, I figured they might be for someone else as well. This isn't a tutorial, so the code, my documentation, and the quick examples will have to do. If you notice any errors in my article or code, please don't hesitate to letting me know.

Using the code

HTML class documentation:

BOOL newdoc();
// Allocates necessary memory, and initializes variables.
// Call this before any other class functions.
<br /><br />
void deldoc();
// Frees allocated memory.
// Call this when you have finished with the class.
// You may bring your class back on-line with another call to newdoc().
<br /><br />
BOOL div(char *arga, char *argb);
// Adds an HTML tag section (like 'body' or 'table').
// arga - name of the tag.
// argb - the tag's parameters, can be ignored.
// To end a tag started by this function, call it again with no arguments.
<br /><br />
BOOL tag(char *arga, char *argb);
// Adds a HTML empty tag (like 'img' or 'br').
// arga - name of the tag.
// argb - the tag's parameters, can be ignored.
<br /><br />
BOOL extmem(DWORD arga, DWORD argb);
// Extends the allocated buffer.
// arga - amount of bytes to be extended.
// argb - amount of bytes that have to be in use in order to extended, can be ignored.
<br /><br />
char *text();
// Returns the complete HTML text buffer.
<br /><br />
BOOL raw(char *arga);
// Adds raw text to the HTML text. Call this to add text or special tags.
CSS class documentation:
BOOL newdoc();
// Same as in the HTML class.
<br /><br />
void deldoc();
// Same as in the HTML class.
<br /><br />
BOOL div(char *arga);
// Adds an CSS style section (like 'body' or '#myclass').
// arga - name of the style section.
// To end a section started by this function, call it again with 0 as the first argument.
// If you start a section without ending the previous one, the class will automatically end it for you.
<br /><br />
BOOL tag(char *arga, char *argb);
// Adds a CSS style attribute (like 'background' or 'border').
// arga - name of the attribute.
// argb - the attribute's value.
// A call to tag() will fail if called outside of a style section.
<br /><br />
BOOL extmem(DWORD arga, DWORD argb);
// Same as in the HTML class.
<br /><br />
char *text();
// Same as in the HTML class.
All of the boolean functions return TRUE or FALSE depending on their success.

An example of how to use the HTML class:
HTML html; // Declare our class
html.newdoc(); // Start it up
html.raw("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); // A special tag
html.div("html"); // Start the html page
html.div("head"); // Start the header
html.tag("meta", "http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\""); // A meta tag
html.div("title"); // Start the title
html.raw("Hello World"); // Our title
html.div(); // End the title
html.tag("link", "rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" media=\"screen\""); // Link to a CSS file
html.div(); // End the header
html.div("body"); // Start the body
html.raw("Hello World."); // Print our body's text
html.tag("br"); // Make a break
html.div(); // End the body
html.div(); // End the html page
SetWindowText(hChild, html.text()); // Set a edit control to contain the html document we just compiled.
html.deldoc(); // End our stuff
An example of how to use the CSS class:
CSS css; // Declare our class
css.newdoc(); // Start it up
css.div("body"); // We are going to edit the body's style.
css.tag("background-color", "#2f2f2f"); // set the background color
css.tag("font-size", "1.01em"); // set the font size
css.tag("margin", "0"); // set the page's margin
css.tag("padding", "0"); // set the page's padding
css.div("table"); // stop editing the body's style, and start editing the table's style
css.tag("background-color", "#010101"); // set the table color
css.div(); // stop editing the table's style.
SetWindowText(hChild, css.text()); // Set a edit control to contain the css document we just compiled.
css.deldoc(); // End our stuff
A nice feature of both the HTML and CSS classes, is that they automatically indent, so editing the result by hand, or viewing the results, you'll see it's really manageable.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)