Code formatter help

This is a discussion on Code formatter help within the C++ Programming forums, part of the General Programming Boards category; I decided to make a program that keeps the same formatting of the c++ code but inserts the color tags ...

  1. #1
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282

    Code formatter help

    I decided to make a program that keeps the same formatting of the c++ code but inserts the color tags for this forum.
    I got that all to work right, but now I am trying to make it not colorize the keywords that are in between the quotation.
    I was running into some trouble so I decided before implementing it with quotes I would try and make it not highlight anything between %. But it is not working right

    Here is my code, and here it is also ran through my code formatter so you can not only see the code but the problem (at the start in the commented section with the %)

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    // % double int char bool % char bool
     
    using namespace std;
     
    int main()
    {
    
    	string item;
    	bool hold = false;
        
    	
    	ifstream in_file;
    	ofstream out_file;
    
    	in_file.open("raw.txt");
    	if ( in_file.fail() )
    	{
    		cout << "Error opening input file";
    		cin.get();
    		return 1;
    	}
    
    	out_file.open("formated.txt");
    	if ( out_file.fail() )
    	{
    		cout << "Error opening input file";
    		cin.get();
    		return 1;
    	}
    
    
    	
    	while ( !in_file.eof() )
    	{
    
    	
    
    		if ( in_file.peek() == '\n')
    		{
    			out_file << '\n';
    			in_file.get();
    		}
    
    	
    		
    		else if ( in_file.peek() == ' ')
    		{
    		
    			out_file << " ";
    			in_file.get();
    		}
    
    		else if ( in_file.peek() == '\t')
    		{
    			out_file << '\t';
    			in_file.get();
    		}
    		
    
    
    
    
    
    
    	
    		else
    		{
    
    
    		
    		in_file >> item;
    
    		
    		char temp1 = item[0] ;
    		int i = item.length();
    	
    
    		if (temp1 == '%' || item[i-1] == '%' || item == "%")
    		{
    
    			if (hold == false ) hold = true;
    			else if (hold == true ) hold = false;
    		}
    
    	
    
    
    	
    		
    			if ( item == "int" || item == "double" || item == "char" 
    				|| item == "using" || item == "namespace" || item == "#include"
    				|| item == "return" || item == "void" || item == "class" 
    				|| item == "delete" || item == "#ifndef" || item == "#define"
    				|| item == "#endif" || item == "public" || item == "protected"
    				|| item == "const" || item == "private" || item == "string" 
    				|| item == "bool" || item == "true" || item == "true;" || item == "false" || item == "false;"
    				|| item == "if" || item == "if(" || item == "else" || item == "do" || item == "while" || item == "while("
    				|| item == "friend" || item == "operator"
    				
    				&& hold == false )
    			{
    		
    				item.insert(0,""); //Ignore these 2 lines of code
    				item.insert(item.length(),""); //The board is detecting the insert color string and it is causing trouble
    	
    			}
    
    
    
    			
    	
    			out_file << item;
    
    
    	
    		}//End of else
    
    
    	}//End of while
    
    	in_file.close();
    	out_file.close();
    
    	system("formated.txt");
    
    
    
    
    return 0;
    }
    Last edited by Enahs; 12-02-2005 at 10:09 AM.

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    First of all:-

    Code:
    !in_file.eof()
    Anything with EOF is generally bad news. Get rid of it. Use this instead

    Code:
    while(infile>>variable)
    .

    Next, think about what has priority.

    For example, if you encounter a quote you should turn off the rest of your code formatting to ignore highlighting key words, such as while, for etc.

    And likewise, if your comment has quotes in them, you would ignore the formatting for the quotations, since comments have priority over quotes.

    Once you have your rules of precedence in place the rest should be easy.

    Oh btw this has been done before:
    But don't bother looking at this link if you want to do this as a exercise or just for fun.



    Looking for a few people willing to test!
    Last edited by treenef; 12-02-2005 at 10:27 AM.

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Yeah, I was just out walking my dog and thinking about the problem and I was luke "duh".
    And yes I know someone had already made one, I was just doing it for fun. I will still be to lazy to use it when I post though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux C source code formatter utility
    By BobS0327 in forum Tech Board
    Replies: 2
    Last Post: 04-08-2006, 06:53 PM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  3. C++ Code Formatter
    By Visual Develope in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2002, 04:18 PM
  4. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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