📄 astyle_main.cpp
字号:
else if ( IS_OPTION(arg, "style=gnu") ) { formatter.setSpaceIndentation(2); formatter.setBracketFormatMode(BREAK_MODE); formatter.setBlockIndent(true); formatter.setClassIndent(false); formatter.setSwitchIndent(false); formatter.setNamespaceIndent(false); } else if ( IS_OPTION(arg, "style=java") ) {// formatter.setJavaStyle();// g_modeManuallySet = true; formatter.setSpaceIndentation(4); formatter.setBracketFormatMode(ATTACH_MODE); formatter.setBracketIndent(false); formatter.setSwitchIndent(false); } else if ( IS_OPTION(arg, "style=kr") ) { //manuallySetCStyle(formatter); formatter.setSpaceIndentation(4); formatter.setBracketFormatMode(ATTACH_MODE); formatter.setBracketIndent(false); formatter.setClassIndent(false); formatter.setSwitchIndent(false); formatter.setNamespaceIndent(false); } else if ( IS_OPTION(arg, "style=linux") ) { formatter.setSpaceIndentation(8); formatter.setBracketFormatMode(BDAC_MODE); formatter.setBracketIndent(false); formatter.setClassIndent(false); formatter.setSwitchIndent(false); formatter.setNamespaceIndent(false); } // must check for mode=cs before mode=c !!! else if ( IS_OPTION(arg, "mode=cs") ) { formatter.setSharpStyle(); g_modeManuallySet = true; } else if ( IS_OPTION(arg, "mode=c") ) { formatter.setCStyle(); g_modeManuallySet = true; } else if ( IS_OPTION(arg, "mode=java") ) { formatter.setJavaStyle(); g_modeManuallySet = true; } else if ( isParamOption(arg, "t", "indent=tab=") ) { int spaceNum = 4; string spaceNumParam = GET_PARAMS(arg, "t", "indent=tab="); if (spaceNumParam.length() > 0) spaceNum = atoi(spaceNumParam.c_str()); if (spaceNum < 1 || spaceNum > 20) isOptionError(arg, errorInfo); else formatter.setTabIndentation(spaceNum, false); } else if ( isParamOption(arg, "T", "force-indent=tab=") ) { int spaceNum = 4; string spaceNumParam = GET_PARAMS(arg, "T", "force-indent=tab="); if (spaceNumParam.length() > 0) spaceNum = atoi(spaceNumParam.c_str()); if (spaceNum < 1 || spaceNum > 20) isOptionError(arg, errorInfo); else formatter.setTabIndentation(spaceNum, true); } else if ( IS_OPTION(arg, "indent=tab") ) { formatter.setTabIndentation(4); } else if ( isParamOption(arg, "s", "indent=spaces=") ) { int spaceNum = 4; string spaceNumParam = GET_PARAMS(arg, "s", "indent=spaces="); if (spaceNumParam.length() > 0) spaceNum = atoi(spaceNumParam.c_str()); if (spaceNum < 1 || spaceNum > 20) isOptionError(arg, errorInfo); else formatter.setSpaceIndentation(spaceNum); } else if ( IS_OPTION(arg, "indent=spaces") ) { formatter.setSpaceIndentation(4); } else if ( isParamOption(arg, "m", "min-conditional-indent=") ) { int minIndent = 8; string minIndentParam = GET_PARAMS(arg, "m", "min-conditional-indent="); if (minIndentParam.length() > 0) minIndent = atoi(minIndentParam.c_str()); if (minIndent > 40) isOptionError(arg, errorInfo); else formatter.setMinConditionalIndentLength(minIndent); } else if ( isParamOption(arg, "M", "max-instatement-indent=") ) { int maxIndent = 40; string maxIndentParam = GET_PARAMS(arg, "M", "max-instatement-indent="); if (maxIndentParam.length() > 0) maxIndent = atoi(maxIndentParam.c_str()); if (maxIndent > 80) isOptionError(arg, errorInfo); else formatter.setMaxInStatementIndentLength(maxIndent); } else if ( IS_OPTIONS(arg, "B", "indent-brackets") ) { formatter.setBracketIndent(true); } else if ( IS_OPTIONS(arg, "G", "indent-blocks") ) { formatter.setBlockIndent(true); } else if ( IS_OPTIONS(arg, "N", "indent-namespaces") ) { formatter.setNamespaceIndent(true); } else if ( IS_OPTIONS(arg, "C", "indent-classes") ) { formatter.setClassIndent(true); } else if ( IS_OPTIONS(arg, "S", "indent-switches") ) { formatter.setSwitchIndent(true); } else if ( IS_OPTIONS(arg, "K", "indent-cases") ) { formatter.setCaseIndent(true); } else if ( IS_OPTIONS(arg, "L", "indent-labels") ) { formatter.setLabelIndent(true); } else if ( IS_OPTIONS(arg, "y", "brackets=break-closing") ) { formatter.setBreakClosingHeaderBracketsMode(true); } else if ( IS_OPTIONS(arg, "b", "brackets=break") ) { formatter.setBracketFormatMode(BREAK_MODE); } else if ( IS_OPTIONS(arg, "a", "brackets=attach") ) { formatter.setBracketFormatMode(ATTACH_MODE); } else if ( IS_OPTIONS(arg, "l", "brackets=linux") ) { formatter.setBracketFormatMode(BDAC_MODE); } else if ( IS_OPTIONS(arg, "O", "one-line=keep-blocks") ) { formatter.setBreakOneLineBlocksMode(false); } else if ( IS_OPTIONS(arg, "o", "one-line=keep-statements") ) { formatter.setSingleStatementsMode(false); } else if ( IS_OPTIONS(arg, "P", "pad=paren") ) { formatter.setParensOutsidePaddingMode(true); formatter.setParensInsidePaddingMode(true); } else if ( IS_OPTIONS(arg, "d", "pad=paren-out") ) { formatter.setParensOutsidePaddingMode(true); } else if ( IS_OPTIONS(arg, "D", "pad=paren-in") ) { formatter.setParensInsidePaddingMode(true); } else if ( IS_OPTIONS(arg, "U", "unpad=paren") ) { formatter.setParensUnPaddingMode(true); } else if ( IS_OPTIONS(arg, "p", "pad=oper") ) { formatter.setOperatorPaddingMode(true); } else if ( IS_OPTIONS(arg, "E", "fill-empty-lines") ) { formatter.setEmptyLineFill(true); } else if ( IS_OPTIONS(arg, "w", "indent-preprocessor") ) { formatter.setPreprocessorIndent(true); } else if ( IS_OPTIONS(arg, "c", "convert-tabs") ) { formatter.setTabSpaceConversionMode(true); } else if ( IS_OPTIONS(arg, "F", "break-blocks=all") ) { formatter.setBreakBlocksMode(true); formatter.setBreakClosingHeaderBlocksMode(true); } else if ( IS_OPTIONS(arg, "f", "break-blocks") ) { formatter.setBreakBlocksMode(true); } else if ( IS_OPTIONS(arg, "e", "break-elseifs") ) { formatter.setBreakElseIfsMode(true); }#ifdef ASTYLE_LIB // End of options used by GUI else isOptionError(arg, errorInfo);#else // Options used by only console else if ( IS_OPTIONS(arg, "n", "suffix=none") ) { g_noBackup = true; } else if ( isParamOption(arg, "suffix=") ) { string suffixParam = GET_PARAM(arg, "suffix="); if (suffixParam.length() > 0) { g_origSuffix = suffixParam; } } else if ( isParamOption(arg, "exclude=") ) { string suffixParam = GET_PARAM(arg, "exclude="); if (suffixParam.length() > 0) { g_excludeVector.push_back(suffixParam); g_excludeHitsVector.push_back(false); } } else if ( IS_OPTIONS(arg, "r", "R") || IS_OPTION(arg, "recursive") ) { g_isRecursive = true; } else if ( IS_OPTIONS(arg, "Z", "preserve-date") ) { g_preserveDate = true; } else if ( IS_OPTIONS(arg, "v", "verbose") ) { if (g_isQuiet) error("Cannot use both verbose and quiet", ""); g_isVerbose = true; } else if ( IS_OPTIONS(arg, "q", "quiet") ) { if (g_isVerbose) error("Cannot use both verbose and quiet", ""); g_isQuiet = true; } else if ( IS_OPTIONS(arg, "X", "errors-to-stdout") ) { _err = &cout; } else { (*_err) << errorInfo << arg << endl; return false; // invalid option }#endif// End of parseOption function return true; //o.k.}#ifdef ASTYLE_JNI// ************************* JNI functions *****************************************************// called by a java program to get the version number// the function name is constructed from method names in the calling java programextern "C" EXPORT jstring STDCALL Java_AStyleInterface_GetVersion(JNIEnv* env, jclass){ return env->NewStringUTF(_version);}// called by a java program to format the source code// the function name is constructed from method names in the calling java programextern "C" EXPORT jstring STDCALL Java_AStyleInterface_AStyleMain(JNIEnv* env, jobject obj, jstring textInJava, jstring optionsJava){ g_env = env; // make object available globally g_obj = obj; // make object available globally jstring textErr = env->NewStringUTF(""); // zero length text returned if an error occurs // get the method ID jclass cls = env->GetObjectClass(obj); g_mid = env->GetMethodID(cls, "ErrorHandler","(ILjava/lang/String;)V"); if (g_mid == 0) { cout << "Cannot find java method ErrorHandler" << endl; return textErr; } // convert jstring to char* const char* textIn = env->GetStringUTFChars(textInJava, NULL); const char* options = env->GetStringUTFChars(optionsJava, NULL); // call the C++ formatting function char* textOut = AStyleMain(textIn, options, javaErrorHandler, javaMemoryAlloc); // if an error message occurred it was displayed by errorHandler if (textOut == NULL) return textErr; // release memory jstring textOutJava = env->NewStringUTF(textOut); delete [] textOut; env->ReleaseStringUTFChars(textInJava, textIn); env->ReleaseStringUTFChars(optionsJava, options); return textOutJava;}// Call the Java error handlervoid STDCALL javaErrorHandler(int errorNumber, char* errorMessage){ jstring errorMessageJava = g_env->NewStringUTF(errorMessage); g_env->CallVoidMethod(g_obj, g_mid, errorNumber, errorMessageJava);}// Allocate memory for the formatted textchar* STDCALL javaMemoryAlloc(unsigned long memoryNeeded){ // error condition is checked after return from AStyleMain char* buffer = new(nothrow) char [memoryNeeded]; return buffer;}#endif#ifdef ASTYLE_LIB// ************************* GUI functions *****************************************************/* * IMPORTANT VC DLL linker must have the parameter /EXPORT:AStyleMain=_AStyleMain@16 * /EXPORT:AStyleGetVersion=_AStyleGetVersion@0 * For Dll only - "warning C4702: unreachable code" in the <vector> header * is caused by using the Optimization options. * /O2 Maximize speed * /O1 Minimize size * /Ob2 Inline Expansion * This is a bug in the Microsoft compiler. The program runs about twice as fast * with the options set. There haven't been any problems so far.*/extern "C" EXPORT char* STDCALL AStyleMain(const char* pSourceIn, // pointer to the source to be formatted const char* pOptions, // pointer to AStyle options, separated by \n fpError fpErrorHandler, // pointer to error handler function fpAlloc fpMemoryAlloc) // pointer to memory allocation function{ if (fpErrorHandler == NULL) // cannot display a message if no error handler return NULL; if (pSourceIn == NULL) { fpErrorHandler(101, (char*)"No pointer to source input."); return NULL; } if (pOptions == NULL) { fpErrorHandler(102, (char*)"No pointer to AStyle options."); return NULL; } if (fpMemoryAlloc == NULL) { fpErrorHandler(103, (char*)"No pointer to memory allocation function."); return NULL; } ASFormatter formatter; string arg; vector<string> optionsVector; istringstream opt(pOptions); _err = new stringstream; g_modeManuallySet = false; importOptions(opt, optionsVector); parseOptions(formatter, optionsVector.begin(), optionsVector.end(), "Invalid Artistic Style options.\n" "The following options were not processed:"); if (_err->str().length() > 0) fpErrorHandler(210, (char*) _err->str().c_str()); delete _err; _err = NULL; istringstream in(pSourceIn); ASStreamIterator<istringstream> streamIterator(&in); ostringstream out; formatter.init(&streamIterator); while (formatter.hasMoreLines()) { out << formatter.nextLine(); if (formatter.hasMoreLines()) out << streamIterator.getOutputEOL(); } unsigned long textSizeOut = out.str().length(); char* pTextOut = fpMemoryAlloc(textSizeOut + 1); // call memory allocation function// pTextOut = NULL; // for testing if (pTextOut == NULL) { fpErrorHandler(110, (char*)"Allocation failure on output."); return NULL; } strcpy(pTextOut, out.str().c_str()); return pTextOut;}extern "C" EXPORT const char* STDCALL AStyleGetVersion (void){ return _version;}#else// *********************** console functions ***************************************************void preserveFileDate(const char *oldFileName, const char *newFileName){ struct stat stBuf; bool statErr = false; if (stat (oldFileName, &stBuf) == -1) statErr = true; else { struct utimbuf outBuf; outBuf.actime = stBuf.st_atime; // add 1 so 'make' will recoginze a change outBuf.modtime = stBuf.st_mtime + 1; if (utime (newFileName, &outBuf) == -1) statErr = true; } if (statErr) (*_err) << " Could not preserve file date" << endl;}bool stringEndsWith(const string &str, const string &suffix){ int strIndex = (int) str.length() - 1; int suffixIndex = (int) suffix.length() - 1; while (strIndex >= 0 && suffixIndex >= 0) { if (tolower(str[strIndex]) != tolower(suffix[suffixIndex])) return false; --strIndex; --suffixIndex; } return true;}#ifdef _WIN32 // Windows specific/** * WINDOWS function to resolve wildcards and recurse into sub directories. * The fileName vector is filled with the path and names of files to process. * * @param directory The path of the directory to be processed. * @param wildcard The wildcard to be processed (e.g. *.cpp). * @param filenam An empty vector which will be filled with the path and names of files to process. */void getFileNames(const string &directory, const string &wildcard, vector<string> &fileName){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -