Jump to content

C++ to HTML?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts
I'm trying to create a C++ program, that will take a .cpp file, and output it as .html, I don't know if I actually have to go through this file and read it line by line, and output with <pre> tags - or what...

Any ideas?

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
What are the program going to do?
Shall it actually interpret the C++ source (*.cpp) as a source, and instead of normal output, it would be as HTML. Or are you simply going to make the C++ source readable online, like replacing '<' with &#60;, etc.
The first possibility is much harder to do, than the second. You need to make a fully workable parser for C++. The second is easier. You can go through the source character-by-character, and do whatever you want with it. It needs a little more work, if you're going to make syntax highlighting, though.

#3
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts

v0id said:

What are the program going to do?
Shall it actually interpret the C++ source (*.cpp) as a source, and instead of normal output, it would be as HTML. Or are you simply going to make the C++ source readable online, like replacing '<' with <, etc.
The first possibility is much harder to do, than the second. You need to make a fully workable parser for C++. The second is easier. You can go through the source character-by-character, and do whatever you want with it. It needs a little more work, if you're going to make syntax highlighting, though.

Nope no need for the syntax highlighting, just replacing simple '<' with < etc.

I figure I'll just take the cpp as a file, go through it line by line looking for special characters and replacing them with the & icon - Surrounding the whole thing in <pre> tags..

That's the only logic I can come up with, am I missing anything? (I don't need code, unless you really want to post it ;P)

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
In about 5 lines of PHP, you can probably open the file, replace all the special characters with their proper entity, and output [echo] the source [html].

There is also a great PHP class called GeSHi [thats what this forum uses for its syntax highlighting], you can set it up to read from files and highlight the syntax too with 20 or so lines of PHP.

#5
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts

Sidewinder said:

In about 5 lines of PHP, you can probably open the file, replace all the special characters with their proper entity, and output [echo] the source [html].

There is also a great PHP class called GeSHi [thats what this forum uses for its syntax highlighting], you can set it up to read from files and highlight the syntax too with 20 or so lines of PHP.

Yeah.. I've been writing php for a few years now.

Any idea for C++ Anyone? xD

#6
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
The hardest part is actually making the replace-function, but it's pretty easy if you're using C++-strings. This is how one could look:
std::string ReplaceCharacter(std::string Haystack, char Needle, std::string Replace)
{
	size_t Position = 0;
	
	while((Position = Haystack.find(Needle, Position)) != std::string::npos)
		Haystack.replace(Position, 1, Replace);
	
	return Haystack;
}
The function only replaces one character, but that's also fine. Like you said yourself, you only need to replace character-by-character. Here's a full example:
#include <iostream>
#include <fstream>
#include <string>

std::string ReplaceCharacter(std::string Haystack, char Needle, std::string Replace)
{
	size_t Position = 0;
	
	while((Position = Haystack.find(Needle, Position)) != std::string::npos)
		Haystack.replace(Position, 1, Replace);
	
	return Haystack;
}

int main(int argc, char *argv[])
{
	std::string Temporary;
	std::string HTMLSource;
	std::ifstream InputFile;
	std::ofstream OutputFile;
	
	if(argc != 3)
	{
		std::cout << "Two parameters shall be given to the program." << std::endl;
		std::cout << " " << argv[0] << " <source> <destination>" << std::endl;
		return -1;
	}
	else
	{
		InputFile.open(argv[1]);
		if(InputFile.is_open())
		{
			while(!InputFile.eof())
			{
				std::getline(InputFile, Temporary);
				HTMLSource += (Temporary + '\n');
			}
			InputFile.close();
			
			OutputFile.open(argv[2]);
			if(OutputFile.is_open())
			{
				HTMLSource = ReplaceCharacter(HTMLSource, 60, "<");
				HTMLSource = ReplaceCharacter(HTMLSource, 32, " ");
				HTMLSource = ReplaceCharacter(HTMLSource, 10, "<br />");
				OutputFile << HTMLSource;
				OutputFile.close();
			}
			else
			{
				std::cout << "Unable to open destination file..." << std::endl;
				return -1;
			}
		}
		else
		{
			std::cout << "Unable to open source file..." << std::endl;
			return -1;
		}
	}
	
	return 0;
}


#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
For C++ all you need is a replacement table and read character by character. Newlines get replaced by <p>, etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
I'd use a predictive scanner. It could be done in about 200 lines of code. You could probably add syntax highlighting with little effort too.

You literally just scan through, character by character, outputing until you find a character that needs replacing. Then you call a procedure that eats that character from the input stream and replaces it with the necessary HTML. You could implement syntax highlighting by detecting when an identifier starts, collecting the entire identifier, then comparing it to a hash table of keywords. If it is in the hash table output the necessary HTML to change the colour, otherwise just output the identifier.

#9
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
Isn't C++ a program code, and HTML a web code? How do you convert the two?

#10
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts

DevilsCharm said:

Isn't C++ a program code, and HTML a web code? How do you convert the two?

It could actually be done provided you created some conventions about what stdin, stdout and stderr mean in the domain of the web.

In this case though it means modifying the source code so it will display nicely in a web browser.

#11
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Quote

It could actually be done provided you created some conventions about what stdin, stdout and stderr mean in the domain of the web.

Its a hell of alot harder then that! allocation and deallocation, pointers, refrences, IO, ring security, a vm on the host machine executing the code so it doesn't murder the rest of the machine...

no C program consists of nothing but printf() and scanf()!!!

#12
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts

TkTech said:

Its a hell of alot harder then that! allocation and deallocation, pointers, refrences, IO, ring security, a vm on the host machine executing the code so it doesn't murder the rest of the machine...

no C program consists of nothing but printf() and scanf()!!!

Indeed, I was simply giving an example of the sort of issue that needed to be solved.

With some effort you could get C++ to compile for the JVM and run it via an applet.