View Single Post
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