Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 9th, 2006, 7:38 PM   #1
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Lightbulb Code Restructure, Finally!

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;

}
OpenLoop is offline   Reply With Quote
Old May 9th, 2006, 8:38 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
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;
}
- incString=(incString?false:true); can be written as incString=!incString;
- 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.
The Dark is offline   Reply With Quote
Old May 9th, 2006, 8:58 PM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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;
OpenLoop is offline   Reply With Quote
Old May 9th, 2006, 9:16 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
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;
The Dark is offline   Reply With Quote
Old May 9th, 2006, 9:23 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
I see... I did this when writing the program and it didn't work. That's why i switched to the ternary.
OpenLoop is offline   Reply With Quote
Old May 9th, 2006, 10:02 PM   #6
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
I think you could have opened it in vim, then done:
:set ff=unix
:set tabstop=4
:retab

and been done with it.
andro is offline   Reply With Quote
Old May 9th, 2006, 10:29 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 10th, 2006, 5:27 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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:
Originally Posted by OpenLoop
Yes 'indent', thank you. I don't know why 'ident' sounds so familiar...
Ident is a internet protocol often used on IRC.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 10th, 2006, 6:47 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"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
DaWei is offline   Reply With Quote
Old May 14th, 2006, 8:28 PM   #10
kp4621
Newbie
 
kp4621's Avatar
 
Join Date: May 2006
Posts: 12
Rep Power: 0 kp4621 is on a distinguished road
Send a message via Yahoo to kp4621
kp4621@gmail.com

Quote:
Originally Posted by DaWei
"Ident" is what I said when my mom asked who did the Bad Thang.
For a old guy your pretty funny... NOT... What makes you think you can criticize what people say. You have no control over what people have to say and they can say it any way that they feel like it.
__________________
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;
}
kp4621 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC