![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Looks good. A few points:
- it is indent, not ident - The stripstring could use the substr and find_first_not_of string methods, instead of appending the chars back one by one. std::string stripString(std::string target)
{
string temp;
int i = target.find_first_not_of(" \t");
if (i != npos)
temp = target.substr(i);
return temp;
}- In VS you can use Edit->Advanced->Format Selection to reformat, and use the options to Tools->Options->(i forget) to change the editor to use spaces instead of tabs. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Yes 'indent', thank you. I don't know why 'ident' sounds so familiar...
Since we're on the bool subject i have a question: does this modify the flag? bool y = false;
int x = 1;
if (x == 1)
!y; |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Nope, it just gets the value of y, does a "not" operation on it, and throws the result away.
You would have to put y = !y; |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I see... I did this when writing the program and it didn't work. That's why i switched to the ternary.
|
|
|
|
|
|
#6 |
|
Professional Programmer
|
I think you could have opened it in vim, then done:
:set ff=unix :set tabstop=4 :retab and been done with it. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Nevertheless, it was no doubt a good exercise for you. I didn't look, but you'll probably find things you don't like if you use it. A lot of people wrote entab and detab filters in the olden days, even when we had vi.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Looks nice. Here is what The Dark said + some checks so it doesn't overwrite.
/************************************************** ****************
* 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 = target.find_first_not_of(" \t");
if (i != std::string::npos)
temp = target.substr(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;
if (currLine[i] == 39) incSubString = !incSubString;
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" << std::endl;
exit(1);
}
std::string currLine, outLine;
std::ifstream fin(argv[1]);
if(!(fin.good()))
{
std::cout << "Could not open input file" << std::endl;
exit(1);
}
std::ofstream fout(argv[2], std::ios::in);
if(fout.is_open())
{
fout.close();
std::cout << "Ouput file already exists" << std::endl;
exit(1);
}
fout.close();
fout.clear();
fout.open(argv[2], std::ios::out | std::ios::trunc);
if(!(fout.good()))
{
std::cout << "Could not open output file" << std::endl;
exit(1);
}
int identLevel = 0;
while (fin)
{
currLine = "";
getline(fin, currLine);
currLine = stripString(currLine);
outLine = identLine(currLine,identLevel);
fout << outLine << std::endl;
}
fin.close();
fout.close();
std::cout << "Done, check " << argv[2] << std::endl;
return 0;
}Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"Ident" is what I said when my mom asked who did the Bad Thang.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 | |
|
Newbie
|
kp4621@gmail.com
Quote:
__________________
Help me out and get rewards , K.O. Hosting #include <iostream>
using namespace std;
int main()
{
int name;
cout << "Please enter your name: " << endl;
cin >> name;
cout << "Hello " << name << " Please go jump off a cliff !!" << endl;
return 0;
} |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|