Formatting C++ files

Lucaci Andrew 0 Tallied Votes 363 Views Share

This is a program which I made back in the beginnings when I was trying to figure out how to work with strings, and how to work with files.
This is suppose to format a C++ code, for example if the syntax of an if statement is

if (condition)
{
    do stuff;
}

if will format it to

if (condition){
    do stuff;
}

same as for while and for loops. It will also format the return values, if for example the syntax is

return value;

it will enclose value in open brackets () etc.

/*
 * format-files.cpp
 * format-files.cpp is licensed under GNU GENERAL PUBLIC LICENSE
 *  Created on: Feb 8, 2012
 *      Author: sin
 */
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Format{
	string line;
	ifstream fin;
	ofstream fout;

public:
	Format(){
	}
	Format(string filename){
		fin.open(filename.c_str(), ios::in);
		string _new_filename="_"+filename;
		if (check(_new_filename)){
			cout<<_new_filename<<" already exists.\nPress <RETURN> to exit.\n";
			fin.close();
			cin.ignore();
			exit(0);
		}
		else fout.open(_new_filename.c_str(), ios::app);
	}

	bool check(string filename){
		/*
		 * Checks if the file exists or not.
		 */
	    ifstream infile(filename.c_str());
	    return (infile.good());
	}

	void formatAll(int i){
		/*
		 * Format starter.
		 */
		while (getline(fin, line)){
			if (i==1) fout<<formatVoidMainArg(formatVoidMain(formatIncludes(formatS((formatReturn(line))))))<<'\n';
			else if (i==2) fout<<formatReturn(line)<<'\n';
			else if (i==3) fout<<formatS(line)<<'\n';
			else if (i==4) fout<<formatIncludes(line)<<'\n';
			else if (i==5) fout<<formatVoidMainArg(formatVoidMain(line))<<'\n';
		}
	}

	string formatReturn(string line){
		/*
		 * Formats the return statements.
		 */
		string temp=line;
		size_t found=temp.find("return ");
		if (found!=string::npos){
			size_t ff=temp.find(';');
			if (ff!=string::npos){
				temp.erase(ff, 1);
				string sub=temp.substr(found+7, found+ff-2);
				temp.replace(found+7, found+sub.size()-1, "("+sub+");");
			}
		}
		return (temp);
	}

	string formatLine(string line){
		/*
		 * Formats the line.
		 */
		string temp;
		getline(fin, temp);
		if (temp[temp.size()-1]!=';')
			line+="{";
		else line+="\n"+temp;
		return (line);
	}

	string formatVoidMain(string line){
		/*
		 * Format old main() functions.
		 */
		if (line.find("void main()")!=string::npos){
			line="int main()";
			if (line.find("{")==string::npos){
				line=formatLine(line);
			}
		}
		return (line);
	}

	string formatVoidMainArg(string line){
		if (line.find("void main(int argc, char *argv[])")!=string::npos){
			line="int main(int argc, char *argv[])";
			if (line.find("{")==string::npos){
				line=formatLine(line);
			}
		}
		return (line);
	}

	string formatS(string line){
		/*
		 * Formats loops and conditionals.
		 */
		if (line.find("if ")!=string::npos || line.find("else if")!=string::npos
				|| line.find("else")!=string::npos || line.find("for ")!=string::npos
				|| line.find("while ")!=string::npos){
			if (line[line.size()-1]!=';')
				line=formatLine(line);
		}
		return (line);
	}

	string formatIncludes(string line){
		/*
		 * Formats old/bad includes.
		 */
		if (line.find("#include <iostream.h>")!=string::npos){
			line="#include <iostream>";
		}
		if (line.find("#include <conio.h>")!=string::npos || line.find("getch();")!=string::npos){
			line="";
		}
		return (line);
	}

	~Format(){
		fin.close();
		fout.close();
	}
};

int main(){
	string filename;
	Format fs;
	cout<<"Filename: ";
	cin>>filename;
	if (fs.check(filename)){
		Format f(filename);
		cout<<"1. Format all.\n"
			<<"2. Format the returns.\n"
			<<"3. Format loops and if statements.\n"
			<<"4. Format the includes.\n"
			<<"5. Format the main function.\n"
			<<"Check file: _"<<filename<<" for the result.\n"
			<<"> ";
		string answer;
		cin>>answer;
		if (answer=="1" or answer=="1.") f.formatAll(1);
		else if (answer=="2" or answer=="2.") f.formatAll(2);
		else if (answer=="3" or answer=="3.") f.formatAll(3);
		else if (answer=="4" or answer=="4.") f.formatAll(4);
		else if (answer=="5" or answer=="5.") f.formatAll(5);
		else cout<<"Invalid command.\n";
	}
	return (0);
}
TrustyTony 888 pyMod Team Colleague Featured Poster

Good subject, unfortunately, when I tested, it only added some () for return, nothing else and did not fix bad indention (done by purpose to test the code).

Lucaci Andrew 140 Za s|n

Well, was an old project of mine from "my begginings" in C++, thou the ideea of accomplishing something like this would be awesome.
What can I say:d, it isn't bugg free.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.