This is a code strip/restructure tool. Here's the short story:
I'm writing a C++ program in VS2003, decided to test it in linux, opened it in gedit and all the idents are messed up. So i wrote a tool that strips out leading spaces and then restructure the code with 4 spaces per ident.
Some things to keep in mind:
- This is still under testing, don't trust it with valuable code
- No support yet for semi-colon identing
- I only tested this under Ubuntu Linux.
- You can use it to prep your code prior to posting on PFO.
Feedback welcome.
/******************************************************************
* Code Restructure Tool
*
* Copyright (C) 2006 Ali Cheaito. acheaito@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* to receive a copy of the GNU General Public License visit the
* GNU website at http://www.gnu.org/copyleft/gpl.html
*******************************************************************/
#include <iostream>
#include <fstream>
#include <string>
#define MAXLINE 255
#define IDENTSIZE 4
std::string stripString(std::string target)
{
std::string temp = "";
int i = 0;
while (target[i] == ' ' || target[i] == '\t'){i++;}
while (target[i] != '\0') {temp += target[i++];}
return temp;
}
std::string identLine(std::string currLine, int &identLevel)
{
std::string identSpaces = "", outLine = "";
bool incString = false, incSubString = false, skipIdent = false;
for (int i = 0; currLine[i] != '\0' && i < MAXLINE; i++)
{
if (currLine[i] == '"') incString=(incString?false:true);
if (currLine[i] == 39) incSubString=(incSubString?false:true);
if (currLine[i] == '{' && (!incString && !incSubString))
{
skipIdent = true;
identLevel++;
}
if (currLine[i] == '}' && (!incString && !incSubString))
{
skipIdent = false;
identLevel--;
}
}
for (int j = 0; j < (skipIdent?(IDENTSIZE*(identLevel-1)):(IDENTSIZE*identLevel))
&& j < (MAXLINE/2) ; j++)
identSpaces += ' ';
outLine += identSpaces;
outLine += currLine;
return outLine;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
std::cout<<"Usage: "<<argv[0]<<" InputFile OutputFile\n";
exit(1);
}
std::string currLine, outLine;
std::ifstream fin(argv[1]);
std::ofstream fout(argv[2]);
int identLevel = 0;
while (fin)
{
currLine = "";
getline(fin,currLine);
currLine = stripString(currLine);
outLine = identLine(currLine,identLevel);
fout<<outLine<<'\n';
}
fin.close();
fout.close();
std::cout<<"Done, check "<<argv[2]<<"\n";
return 0;
}