Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 6:50 AM.

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