int main(int argc, char *argv[]) { ASFormatter formatter; vector<string> fileNameVector; vector<string> optionsVector; string optionsFileName = ""; string arg; bool ok = true; bool shouldPrintHelp = false; bool shouldParseOptionsFile = true; _err = &cerr; _suffix = ".orig"; printHelpTitle(); // manage flags for (int i=1; i<argc; i++) { arg = string(argv[i]); if ( IS_PARAM_OPTION(arg ,"--options=none") ) { shouldParseOptionsFile = false; } else if ( IS_PARAM_OPTION(arg ,"--options=") ) { optionsFileName = GET_PARAM(arg, "--options="); } else if ( (arg == "-h") || (arg == "--help") || (arg == "-?") ) { shouldPrintHelp = true; } else if (arg[0] == '-') { optionsVector.push_back(arg); } else // file-name { fileNameVector.push_back(arg); } } // parse options file if (shouldParseOptionsFile) { if (optionsFileName.compare("") == 0) { char* env = getenv("ISTYLE_OPTIONS"); if (env != NULL) optionsFileName = string(env); } if (optionsFileName.compare("") == 0) { char* env = getenv("HOME"); if (env != NULL) optionsFileName = string(env) + string("/.iStylerc"); } if (optionsFileName.compare("") == 0) { char* drive = getenv("HOMEDRIVE"); char* path = getenv("HOMEPATH"); if (path != NULL) optionsFileName = string(drive) + string(path) + string("/.iStylerc"); } if (!optionsFileName.empty()) { ifstream optionsIn(optionsFileName.c_str()); if (optionsIn) { vector<string> fileOptionsVector; // reading (whitespace seperated) strings from file into string vector importOptions(optionsIn, fileOptionsVector); ok = parseOptions(formatter, fileOptionsVector.begin(), fileOptionsVector.end(), string("Unknown option in default options file: ")); } optionsIn.close(); if (!ok) { printHelpSimple(); } } } // parse options from command line ok = parseOptions(formatter, optionsVector.begin(), optionsVector.end(), string("Unknown command line option: ")); if (!ok) { printHelpSimple(); exit(1); } if (shouldPrintHelp) { printHelpFull(); exit(1); } // if no files have been given, use cin for input and cout for output if (fileNameVector.empty() ) { printHelpSimple(1); } else { // indent the given files for (int i=0; i<fileNameVector.size(); i++) { string originalFileName = fileNameVector[i]; string inFileName = originalFileName + _suffix; if ( ! isWriteable(originalFileName.c_str()) ) { error(string("Error: File '" + originalFileName ).c_str() , "' does not exist, or is read-only."); continue; } remove(inFileName.c_str()); if ( rename(originalFileName.c_str(), inFileName.c_str()) < 0) { error(string("Error: Could not rename " + originalFileName).c_str() , string(" to " + inFileName).c_str()); exit(1); } ifstream in(inFileName.c_str()); if (!in) { error("Could not open input file", inFileName.c_str()); exit(1); } ofstream out(originalFileName.c_str()); if (!out) { error("Could not open output file", originalFileName.c_str()); exit(1); } formatter.init( new ASStreamIterator(&in) ); while (formatter.hasMoreLines() ) { out << formatter.nextLine(); if (formatter.hasMoreLines()) out << endl; } out.flush(); out.close(); in.close(); if ( ! shouldBackupFile ) { remove( inFileName.c_str() ); } // print SetColor(3,0); cout <<"Indented file -- " <<originalFileName << "."<< endl; SetColor(7,0); } } SetColor(7,0); return 0; }
main(int argc, char *argv[]) { ASFormatter formatter; vector<string> fileNameVector; vector<string> optionsVector; string optionsFileName = ""; string arg; bool ok = true; bool shouldPrintHelp = false; bool shouldParseOptionsFile = true; _err = &cerr; _suffix = ".orig"; _modeManuallySet = false; // manage flags for (int i=1; i<argc; i++) { arg = string(argv[i]); if ( IS_PARAM_OPTION(arg ,"--options=none") ) { shouldParseOptionsFile = false; } else if ( IS_PARAM_OPTION(arg ,"--options=") ) { optionsFileName = GET_PARAM(arg, "--options="); } else if ( IS_OPTION(arg, "-h") || IS_OPTION(arg, "--help") || IS_OPTION(arg, "-?") ) { shouldPrintHelp = true; } else if (arg[0] == '-') { optionsVector.push_back(arg); } else // file-name { fileNameVector.push_back(arg); } } // parse options file if (shouldParseOptionsFile) { if (optionsFileName.compare("") == 0) { char* env = getenv("ARTISTIC_STYLE_OPTIONS"); if (env != NULL) optionsFileName = string(env); } if (optionsFileName.compare("") == 0) { char* env = getenv("HOME"); if (env != NULL) optionsFileName = string(env) + string("/.astylerc"); } if (optionsFileName.compare("") == 0) { char* env = getenv("HOMEPATH"); if (env != NULL) optionsFileName = string(env) + string("/.astylerc"); } if (optionsFileName.compare("") != 0) { ifstream optionsIn(optionsFileName.c_str()); if (optionsIn) { vector<string> fileOptionsVector; importOptions(optionsIn, fileOptionsVector); ok = parseOptions(formatter, fileOptionsVector.begin(), fileOptionsVector.end(), string("Unknown option in default options file: ")); } optionsIn.close(); if (!ok) { (*_err) << "For help on options, type 'astyle -h' " << endl; } } } // parse options from command line ok = parseOptions(formatter, optionsVector.begin(), optionsVector.end(), string("Unknown command line option: ")); if (!ok) { (*_err) << "For help on options, type 'astyle -h' " << endl; exit(1); } if (shouldPrintHelp) { printHelpAndExit(); } // if no files have been given, use cin for input and cout for output if (fileNameVector.empty()) { formatter.init( new ASStreamIterator(&cin) ); while (formatter.hasMoreLines() ) { cout << formatter.nextLine(); if (formatter.hasMoreLines()) cout << endl; } cout.flush(); } else { // indent the given files for (int i=0; i<fileNameVector.size(); i++) { string originalFileName = fileNameVector[i]; string inFileName = originalFileName + _suffix; remove(inFileName.c_str()); if ( rename(originalFileName.c_str(), inFileName.c_str()) < 0) error("Could not rename ", string(originalFileName + " to " + inFileName).c_str()); ifstream in(inFileName.c_str()); if (!in) error("Could not open input file", inFileName.c_str()); ofstream out(originalFileName.c_str()); if (!out) error("Could not open output file", originalFileName.c_str()); // Unless a specific language mode has been, set the language mode // according to the file's suffix. if (!_modeManuallySet) { if (stringEndsWith(originalFileName, string(".java"))) { formatter.setJavaStyle(); } else { formatter.setCStyle(); } } formatter.init( new ASStreamIterator(&in) ); while (formatter.hasMoreLines() ) { out << formatter.nextLine(); if (formatter.hasMoreLines()) out << endl; } out.flush(); out.close(); in.close(); } } return 0; }