html translator progress

This is a discussion on html translator progress within the C++ Programming forums, part of the General Programming Boards category; I've just gotten started on this and haven't gotten to the juicy parts yet but thought I'd just share some ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    html translator progress

    I've just gotten started on this and haven't gotten to the juicy parts yet but thought I'd just share some simple starting blocks to begin with (they'll be presuppositions for the complicated stuff anyway). The program compiles and works so far, and I just wondered what you guys think.

    Here's my file htmltrans.h:
    Code:
    #ifndef HTMLTRANS_H
    #define HTMLTRANS_H
    
    std::string pathNames(std::string & sourcePath);
    #endif
    I'm gathering that some want #include <iostream> or #include <string> or both in this file, although it works as is.

    Here's htmltrans.cpp:

    Code:
    #include <iostream>
    #include <string>
    #include "htmltrans.h"
    
    std::string pathNames(std::string & sourcePath)
    {
    	using namespace std;
    
    	string sourceFile, transPath;
    	char conf;
    
    	cout << "This program creates a copy of your source file\n"
    		<< "which will allow you to display code on the web.\n"
    		<< "Simply paste the text of the new file into your html file\n"
    		<< "in order to display the original coded text.\n\n";
    	cout << "You must first copy of the file you want to translate\n"
    		<< "into the directory c:\\html_trans\\\n"
    		<< "Press 'C' if you have completed this step\n"
    		<< "or any other key to end the program: ";
    	cin >> conf;
    	if (conf != 'c' && conf != 'C')
    		return "exit";
    	cout << endl;
    
    	cout << "Enter the name of the file you want to translate: ";
    	cin >> sourceFile;
    	sourcePath.append(sourceFile);
    
    	transPath = sourcePath;
    	int dot = transPath.find('.', 0), nameEnd = transPath.size();
    	int toErase = nameEnd - dot;
    	transPath.erase(dot, toErase); //chops off file extension
    
    	transPath.append("_wtrans.txt"); //adds recognizable new extension
    	
    	return transPath;
    }
    And finally htmltrans_main.cpp:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include "htmltrans.h"
    
    int main()
    {
    	using namespace std;
    
    	string sourcePath = "c:\\html_trans\\";
    	string transPath = pathNames(sourcePath);
    
    	if (transPath.at(0) != 'c')
    		return 0;
    	cout << "Here's the new path back in main:\n" << transPath << endl;
    	cout << "Here's the source path in main:\n" << sourcePath << endl;
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It works "as is" because your .cpp file includes <string> before the file. If you moved your .h file to the top it would break.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    242
    oh, ok, now I think I'm following. Basically, the #include in the header doesn't hurt anything and at the same time means that you don't have to worry as much about the order of your #include's in files that refer back to the header.

    In any case, I figure if those of you who have been doing this for years prefer it, I should change my header to this:

    Code:
    #ifndef HTMLTRANS_H
    #define HTMLTRANS_H
    
    #include <string>
    
    std::string pathNames(std::string & sourcePath);
    #endif

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    242
    Here's the decisive translation function, which I've at this point just tested on very simple source files. If anyone wants it (or the full program) after I've tested on real-world stuff, please let me know, and I'll email code or just post the whole thing here (it's not a big program by any means, but a little awkward for just sticking into a message board).

    It's NOT intended to make all code look immediately just right on the web, but I'm hoping it solves everything except the spots where you want to put your line breaks (which is going to depend also on your particular web-page). It also should solve SOME of the line-break problems, so as to leave minimal and easy editing after the program has run.

    Code:
    void transHtml(const char * sourcePName, const char * transPName)
    {
    	using namespace std;
    	fstream inFile;
    	inFile.open(sourcePName, ios::in);
    	if (inFile.fail())
    	{
    		cout << "Source file doesn't exist c:\\html_trans\\\n";
    		cout << "Ending program.\n";
    		return;
    	}
    	string trans = "";
    	char tempCh;
    
    	for (int i = 0; i < 5000; i++)
    	{
    		inFile.get(tempCh);
    		if (inFile.eof())
    			break;
    		switch (tempCh)
    		{
    		case '<':
    			trans.append("&lt;");
    			break;
    		case '>':
    			trans.append("&gt;");
    			break;
    		case '&':
    			trans.append("&amp;");
    			break;
    		case '\n':
    			trans.append("<br />\n");
    			break;
    		default:
    			trans.append(1, tempCh);
    		}
    	}
    	inFile.clear();
    	inFile.close();
    	if (inFile.fail())
    	{
    		cout << "Error closing file.\n"
    			<< "Aborting program.\n";
    		exit(EXIT_FAILURE);
    	}
    
    	fstream outFile;
    	outFile.open(transPName, ios::out);
    	if (outFile.fail())
    	{
    		cout << "Error opening file.\n"
    			<< "Aborting program.\n";
    		exit(EXIT_FAILURE);
    	}
    
    	for (int i = 0; i < trans.length(); i++)
    	{
    		outFile.put(trans.at(i));
    	}
    	outFile.close();
    	if (outFile.fail())
    	{
    		cout << "Error closing file.\n"
    			<< "Aborting program.\n"
    			<< "Translation file may be corrupt!\n";
    		exit(EXIT_FAILURE);
    	}
    	cout << "Translation complete!\n";
    }
    My exception handling is obviously pretty brutish, but I haven't gotten into how to do that properly. And here, you really DON'T want failures on opening and closing your files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. html web translator/interpreter
    By Aisthesis in forum C++ Programming
    Replies: 6
    Last Post: 08-02-2009, 02:17 PM
  2. Please Help - C code creates dynamic HTML
    By Christie2008 in forum C Programming
    Replies: 19
    Last Post: 04-02-2008, 07:36 PM
  3. Writing an HTML Preprocessor
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2007, 08:01 AM
  4. Design + HTML
    By orbitz in forum C Programming
    Replies: 8
    Last Post: 11-21-2002, 05:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21