📄 astyle_main.cpp
字号:
(*_err) << "-------------------\n"; (*_err) << " --break-blocks OR -f\n"; (*_err) << " Insert empty lines around unrelated blocks, labels, classes, ...\n"; (*_err) << endl; (*_err) << " --break-blocks=all OR -F\n"; (*_err) << " Like --break-blocks, except also insert empty lines \n"; (*_err) << " around closing headers (e.g. 'else', 'catch', ...).\n"; (*_err) << endl; (*_err) << " --break-elseifs OR -e\n"; (*_err) << " Break 'else if()' statements into two different lines.\n"; (*_err) << endl; (*_err) << " --pad=oper OR -p\n"; (*_err) << " Insert space paddings around operators.\n"; (*_err) << endl; (*_err) << " --pad=paren OR -P\n"; (*_err) << " Insert space padding around parenthesis on both the outside\n"; (*_err) << " and the inside.\n"; (*_err) << endl; (*_err) << " --pad=paren-out OR -d\n"; (*_err) << " Insert space padding around parenthesis on the outside only.\n"; (*_err) << endl; (*_err) << " --pad=paren-in OR -D\n"; (*_err) << " Insert space padding around parenthesis on the inside only.\n"; (*_err) << endl; (*_err) << " --unpad=paren OR -U\n"; (*_err) << " Remove unnecessary space padding around parenthesis. This\n"; (*_err) << " can be used in combination with the 'pad' options above.\n"; (*_err) << endl; (*_err) << " --one-line=keep-statements OR -o\n"; (*_err) << " Don't break lines containing multiple statements into\n"; (*_err) << " multiple single-statement lines.\n"; (*_err) << endl; (*_err) << " --one-line=keep-blocks OR -O\n"; (*_err) << " Don't break blocks residing completely on one line.\n"; (*_err) << endl; (*_err) << " --convert-tabs OR -c\n"; (*_err) << " Convert tabs to spaces.\n"; (*_err) << endl; (*_err) << " --fill-empty-lines OR -E\n"; (*_err) << " Fill empty lines with the white space of their\n"; (*_err) << " previous lines.\n"; (*_err) << endl; (*_err) << " --mode=c\n"; (*_err) << " Indent a C or C++ source file (this is the default).\n"; (*_err) << endl; (*_err) << " --mode=java\n"; (*_err) << " Indent a Java source file.\n"; (*_err) << endl; (*_err) << " --mode=cs\n"; (*_err) << " Indent a C# source file.\n"; (*_err) << endl; (*_err) << "Other options:\n"; (*_err) << "--------------\n"; (*_err) << " --suffix=####\n"; (*_err) << " Append the suffix #### instead of '.orig' to original filename.\n"; (*_err) << endl; (*_err) << " --suffix=none OR -n\n"; (*_err) << " Do not retain a backup of the original file.\n"; (*_err) << endl; (*_err) << " --options=####\n"; (*_err) << " Specify an options file #### to read and use.\n"; (*_err) << endl; (*_err) << " --options=none\n"; (*_err) << " Disable the default options file.\n"; (*_err) << " Only the command-line parameters will be used.\n"; (*_err) << endl; (*_err) << " --recursive OR -r OR -R\n"; (*_err) << " Process subdirectories recursively.\n"; (*_err) << endl; (*_err) << " --exclude=####\n"; (*_err) << " Specify a file or directory #### to be excluded from processing.\n"; (*_err) << endl; (*_err) << " --preserve-date OR -Z\n"; (*_err) << " The date and time modified will not be changed in the formatted file.\n"; (*_err) << endl; (*_err) << " --verbose OR -v\n"; (*_err) << " Verbose mode. Extra informational messages will be displayed.\n"; (*_err) << endl; (*_err) << " --errors-to-stdout OR -X\n"; (*_err) << " Print errors and help information to standard-output rather than\n"; (*_err) << " to standard-error.\n"; (*_err) << endl; (*_err) << " --version OR -V\n"; (*_err) << " Print version number.\n"; (*_err) << endl; (*_err) << " --help OR -h OR -?\n"; (*_err) << " Print this help message.\n"; (*_err) << endl; (*_err) << "Default options file:\n"; (*_err) << "---------------------\n"; (*_err) << " Artistic Style looks for a default options file in the\n"; (*_err) << " following order:\n"; (*_err) << " 1. The contents of the ARTISTIC_STYLE_OPTIONS environment\n"; (*_err) << " variable if it exists.\n"; (*_err) << " 2. The file called .astylerc in the directory pointed to by the\n"; (*_err) << " HOME environment variable ( i.e. $HOME/.astylerc ).\n"; (*_err) << " 3. The file called astylerc in the directory pointed to by the\n"; (*_err) << " USERPROFILE environment variable ( i.e. %USERPROFILE%\\astylerc ).\n"; (*_err) << " If a default options file is found, the options in this file\n"; (*_err) << " will be parsed BEFORE the command-line options.\n"; (*_err) << " Long options within the default option file may be written without\n"; (*_err) << " the preliminary '--'.\n"; (*_err) << endl;}/** * Open input file, format it, and close the output. * * @param fileName The path and name of the file to be processed. * @param formatter The formatter object. * @return true if the file was formatted, false if it was not (no changes). */bool formatFile(const string &fileName, ASFormatter &formatter){ bool isFormatted = false; // return value // open input file ifstream in(fileName.c_str(), ios::binary); if (!in) error("Could not open input file", fileName.c_str()); // open tmp file string tmpFileName = fileName + g_tempSuffix; remove(tmpFileName.c_str()); // remove the old .tmp if present ofstream out(tmpFileName.c_str(), ios::binary); if (!out) error("Could not open output file", tmpFileName.c_str()); // Unless a specific language mode has been set, set the language mode // according to the file's suffix. if (!g_modeManuallySet) { if (stringEndsWith(fileName, string(".java"))) formatter.setJavaStyle(); else if (stringEndsWith(fileName, string(".cs"))) formatter.setSharpStyle(); else formatter.setCStyle(); } // cout << "formatting " << fileName.c_str() << endl; // save the filename used by the trace macros size_t fname = fileName.find_last_of(g_fileSeparator); if (fname == string::npos) fname = 0; else fname +=1; // filename is used by the trace macros formatter.traceFileName = fileName.substr(fname); // format the file ASStreamIterator<istream> streamIterator(&in); formatter.init(&streamIterator); bool filesAreIdentical = true; // input and output files are identical string nextLine; // next output line while (formatter.hasMoreLines()) { nextLine = formatter.nextLine(); out << nextLine; if (formatter.hasMoreLines()) out << streamIterator.getOutputEOL(); else streamIterator.saveLastInputLine(); // to compare the last input line if (filesAreIdentical) { if (!streamIterator.compareToInputBuffer(nextLine)) filesAreIdentical = false; } } out.flush(); out.close(); in.close(); // if input and output are identical, don't change anything if (filesAreIdentical) { remove(tmpFileName.c_str()); } else { // create a backup string origFileName = fileName + g_origSuffix; remove(origFileName.c_str()); // remove the old .orig if present if (rename(fileName.c_str(), origFileName.c_str()) < 0) error("Could not create backup file", origFileName.c_str()); // change tmp name to original (reformatted) if (rename(tmpFileName.c_str(), fileName.c_str()) < 0) error("Could not rename tmp file", tmpFileName.c_str()); // change date modified to original file date if (g_preserveDate) preserveFileDate(origFileName.c_str(), fileName.c_str()); if (g_noBackup) remove(origFileName.c_str()); isFormatted = true; } return isFormatted;}int main(int argc, char *argv[]){ ASFormatter formatter; vector<string> fileNameVector; // file paths and names from the command line vector<string> optionsVector; // options from the command line vector<string> fileOptionsVector; // options from the options vector string optionsFileName = ""; // file path and name of the options file to use string arg; bool ok = true; bool shouldParseOptionsFile = true; g_modeManuallySet = false; // get command line options for (int i = 1; i < argc; i++) { arg = string(argv[i]); if ( IS_OPTION(arg, "--options=none") ) { shouldParseOptionsFile = false; } else if ( isParamOption(arg, "--options=") ) { optionsFileName = GET_PARAM(arg, "--options="); g_optionsFileRequired = true; if (optionsFileName.compare("") == 0) optionsFileName = ' '; } else if ( IS_OPTION(arg, "-h") || IS_OPTION(arg, "--help") || IS_OPTION(arg, "-?") ) { printHelp(); return EXIT_SUCCESS; } else if ( IS_OPTION(arg, "-V" ) || IS_OPTION(arg, "--version") ) { (*_err) << "Artistic Style Version " << _version << endl; exit(EXIT_SUCCESS); } else if (arg[0] == '-') { optionsVector.push_back(arg); } else // file-name { fileNameVector.push_back(arg); } } // get options file path and name 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("USERPROFILE"); if (env != NULL) optionsFileName = string(env) + string("/astylerc"); } if (optionsFileName.compare("") != 0) standardizePath(optionsFileName); } // create the options file vector and parse the options for errors if (optionsFileName.compare("") != 0) { ifstream optionsIn(optionsFileName.c_str()); if (optionsIn) { importOptions(optionsIn, fileOptionsVector); ok = parseOptions(formatter, fileOptionsVector.begin(), fileOptionsVector.end(), string("Invalid option in default options file: ")); } else { if (g_optionsFileRequired) error("Could not open options file", optionsFileName.c_str()); optionsFileName.clear(); } optionsIn.close(); } if (!ok) { (*_err) << "For help on options, type 'astyle -h' " << endl; return EXIT_FAILURE; } // parse the command line options vector for errors ok = parseOptions(formatter, optionsVector.begin(), optionsVector.end(), string("Invalid command line option: ")); if (!ok) { (*_err) << "For help on options, type 'astyle -h' \n" << endl; return EXIT_FAILURE; } // if no files have been given, use cin for input and cout for output // this is used to format text for text editors like TextWrangler // do NOT display any console messages when this branch is used if (fileNameVector.empty()) { ASStreamIterator<istream> streamIterator(&cin); // create iterator for cin formatter.init(&streamIterator); while (formatter.hasMoreLines()) { cout << formatter.nextLine(); if (formatter.hasMoreLines()) cout << streamIterator.getOutputEOL(); } cout.flush(); return EXIT_SUCCESS; } // indent the given files // standarize the exclude names for (size_t ix = 0; ix < g_excludeVector.size(); ix++) standardizePath(g_excludeVector[ix], true); if (g_isVerbose) { cout << "Artistic Style " << _version << endl; if (optionsFileName.compare("") != 0) cout << "Using default options file " << optionsFileName << endl; } // loop thru input fileNameVector formatting the files clock_t startTime = clock(); // start time of file formatting int filesFormatted = 0; // number of files formatted int filesUnchanged = 0; // number of files unchanged for (size_t i = 0; i < fileNameVector.size(); i++) { vector<string> fileName; // files to be processed including path string targetDirectory; // path to the directory being processed string targetFilename; // file name being processed standardizePath(fileNameVector[i]); // standardize the file separators string fileSpec = fileNameVector[i]; // separate directory and file name size_t separator = fileSpec.find_last_of(g_fileSeparator); if (separator == string::npos) { // if no directory is present, use the currently active directory targetDirectory = getCurrentDirectory(fileSpec); targetFilename = fileSpec; g_mainDirectoryLength = targetDirectory.length() + 1; // +1 includes trailing separator } else { targetDirectory = fileSpec.substr(0, separator); targetFilename = fileSpec.substr(separator+1); g_mainDirectoryLength = targetDirectory.length() + 1; // +1 includes trailing separator } if (targetFilename.length() == 0) error("Missing filename in", fileSpec.c_str()); // check filename for wildcards g_hasWildcard = false; if (targetFilename.find_first_of( "*?") != string::npos) g_hasWildcard = true; // clear exclude hits vector for (size_t ix = 0; ix < g_excludeHitsVector.size(); ix++) g_excludeHitsVector[ix] = false; // display directory name for wildcard processing if (g_hasWildcard && ! g_isQuiet) { cout << "--------------------------------------------------" << endl; cout << "directory " << targetDirectory << g_fileSeparator << targetFilename << endl; } // create a vector of paths and file names to process getFileNames(targetDirectory, targetFilename, fileName); if (g_hasWildcard && ! g_isQuiet) cout << "--------------------------------------------------" << endl; // check for unprocessed excludes for (size_t ix = 0; ix < g_excludeHitsVector.size(); ix++) if (g_excludeHitsVector[ix] == false) error("Unmatched exclude", g_excludeVector[ix].c_str()); // check if files were found (probably an input error if not) if (fileName.size() == 0) (*_err) << "No file to process " << fileSpec.c_str() << endl; // loop thru fileName vector to format the files for (size_t j = 0; j < fileName.size(); j++) { // format the file bool isFormatted = formatFile(fileName[j], formatter); // remove targetDirectory from filename if required string displayName; if (g_hasWildcard) displayName = fileName[j].substr(targetDirectory.length() + 1); else displayName = fileName[j]; if (isFormatted) { filesFormatted++; if (!g_isQuiet) cout << "formatted " << displayName.c_str() << endl; } else { filesUnchanged++; if (!g_isQuiet) cout << "unchanged* " << displayName.c_str() << endl; } } } // files are processed, display stats if (g_isVerbose) { if (g_hasWildcard) cout << "--------------------------------------------------" << endl; cout << filesFormatted << " formatted, "; cout << filesUnchanged << " unchanged, "; // show processing time clock_t stopTime = clock(); float secs = (float) (stopTime - startTime) / CLOCKS_PER_SEC; if (secs < 60) { // show tenths of a second if time is less than 20 seconds cout.precision(2); if (secs >= 10 && secs < 20) cout.precision(3); cout << secs << " seconds" << endl; cout.precision(0); cout << endl; } else { // show minutes and seconds if time is greater than one minute int min = (int) secs / 60; secs -= min * 60; int minsec = int (secs + .5); cout << min << " min " << minsec << " sec" << endl; } } return EXIT_SUCCESS;}#endif// ************************* end of console functions *****************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -