⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 astyle_main.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    << "    Break brackets before closing headers (e.g. 'else', 'catch', ..)." << endl
    << "    Should be appended to --brackets=attach or --brackets=linux." << endl;
    cout << endl
    << "    -o\tOR\t--one-line=keep-statements" << endl
    << "    Don't break lines containing multiple statements into" << endl
    << "    multiple single-statement lines." << endl;
    cout << endl
    << "    -O\tOR\t--one-line=keep-blocks" << endl
    << "    Don't break blocks residing completely on one line" << endl;
    cout << endl
    << "    -p\tOR\t--pad=oper" << endl
    << "    Insert space paddings around operators only." << endl;
    cout << endl
    << "    --pad=paren" << endl
    << "    Insert space paddings around parenthesies only." << endl;
    cout << endl
    << "    -P\tOR\t--pad=all" << endl
    << "    Insert space paddings around operators AND parenthesies." << endl;
    cout << endl
    << "    --convert-tabs" << endl
    << "    Convert tabs to spaces." << endl;
    cout << endl
    << "    --break-blocks" << endl
    << "    Insert empty lines around unrelated blocks, labels, classes, ..." << endl;
    cout << endl
    << "    --break-blocks=all" << endl
    << "    Like --break-blocks, except also insert empty lines " << endl
    << "    around closing headers (e.g. 'else', 'catch', ...)." << endl;
    cout << endl
    << "    --break-elseifs" << endl
    << "    Break 'else if()' statements into two different lines." << endl;
    cout << endl
    << "Other options:" << endl
    << "-------------" << endl;
    cout << endl
    << "    --suffix=####" << endl
    << "    Append the suffix #### instead of '.orig' to original filename." << endl;
    cout << endl
    << "    -n\tOR\t--suffix=none" << endl
    << "    Tells Astyle not to keep backups of the original source files." << endl
    << "    WARNING: Use this option with care, as Astyle comes with NO WARRANTY..." << endl;
    cout << endl
    << "    -X\tOR\t--errors-to-standard-output" << endl
    << "    Print errors to standard-output rather than standard-error." << endl;
    cout << endl
    << "    -v\tOR\t--version" << endl
    << "    Print version number" << endl;
    cout << endl
    << "    -h\tOR\t-?\tOR\t--help" << endl
    << "    Print this help message" << endl;
    cout << endl
    << "Default options file:" << endl
    << "---------------------" << endl;
    cout << endl
    << "    Artistic Style looks for a default options file in the" << endl
    << "    following order:" << endl
    << "    1. The contents of the ARTISTIC_STYLE_OPTIONS environment" << endl
    << "       variable if it exists." << endl
    << "    2. The file called .astylerc in the directory pointed to by the" << endl
    << "       HOME environment variable ( i.e. $HOME/.astylerc )." << endl
    << "    3. The file called .astylerc in the directory pointed to by the" << endl
    << "       HOMEDRIVE and HOMEPATH environment variables ( i.e.," << endl
    << "       %HOMEDRIVE%%HOMEPATH%\\.astylerc )." << endl
    << "    If a default options file is found, the options in this file" << endl
    << "    will be parsed BEFORE the command-line options." << endl
    << "    Options within the default option file may be written without" << endl
    << "    the preliminary '-' or '--'." << endl
    << endl;
}

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;

    // manage flags
    for (int i=1; i<argc; i++)
    {
        arg = string(argv[i]);
        
        if ( BEGINS_WITH(arg ,"--options=none", 14) )
        {
            TRACE( INFO, "options=none" );
            shouldParseOptionsFile = false;
        }
        else if ( BEGINS_WITH(arg ,"--options=", 10) )
        {
            TRACE( ENTRY, "options=" );
            optionsFileName = arg.substr(strlen("--options="));
            TRACE( EXIT, "options=" << optionsFileName );
        }
        else if ( (arg == "-h") || (arg == "--help") || (arg == "-?") )
        {
            TRACE( INFO, "help" );
            shouldPrintHelp = true;
        }
        else if (arg[0] == '-')
        {
            TRACE( INFO, "option: " << arg );
            optionsVector.push_back(arg);
        }
        else // file-name
        {
            TRACE( INFO, "source: " << arg );
            fileNameVector.push_back(arg);
        }
    }

    // parse options file
    if (shouldParseOptionsFile)
    {
        TRACE( INFO, "Options file should be read..." );
        if (optionsFileName.empty())
        {
            char* env = getenv("ARTISTIC_STYLE_OPTIONS");
            if (env != NULL)
                optionsFileName = string(env);
            TRACE( INFO, "Taken filename '" << optionsFileName << "' from $ARTISTIC_STYLE_OPTIONS." );
        }
        if (optionsFileName.empty())
        {
            char* env = getenv("HOME");
            if (env != NULL)
                optionsFileName = string(env) + string("/.astylerc");
            TRACE( INFO, "Assembled filename '" << optionsFileName << "' from $HOME/.astylerc." );
        }
        if (optionsFileName.empty())
        {
            char* drive = getenv("HOMEDRIVE");
            char* path = getenv("HOMEPATH");
            if (path != NULL)
                optionsFileName = string(drive) + string(path) + string("/.astylerc");
            TRACE( INFO, "Assembled filename '" << optionsFileName << "' from %HOMEDRIVE%%HOMEPATH%/.astylerc." );
        }

        if (!optionsFileName.empty())
        {
            ifstream optionsIn(optionsFileName.c_str());
            TRACE( INFO, "Reading options file " << optionsFileName );
            if (optionsIn)
            {
                vector<string> fileOptionsVector;
                // reading (whitespace seperated) strings from file into string vector
                string buffer;
                while (optionsIn)
                {
                    getline(optionsIn, buffer);
                    TRACE( INFO, "Read line: '" << buffer << "'" );
                    if (optionsIn.fail() || (buffer.size() == 0) || (buffer[0] == '#'))
                    {
                        TRACE( INFO, "Invalid line (EOF, empty line, or comment line)" );
                        continue;
                    }
                    istringstream buf(buffer);
                    copy(istream_iterator<string>(buf), istream_iterator<string>(), back_inserter(fileOptionsVector));
                }
                TRACE( ENTRY, "Options read:" );
#ifndef NDEBUG
                for ( unsigned int i = 0; i < fileOptionsVector.size(); ++i )
                    TRACE( INFO, fileOptionsVector[i] );
                TRACE( EXIT, "End of options." );
#endif
                ok = parseOptions(formatter,
                                  fileOptionsVector.begin(),
                                  fileOptionsVector.end(),
                                  "Unknown option in default options file: ");
            }
            else
            {
                TRACE( INFO, "Could not open options file " << optionsFileName );
            }

            optionsIn.close();
            if (!ok)
            {
                (*_err) << "For help on options, type 'astyle -h' " << endl;
            }
        }
    }
    else
    {
        TRACE( INFO, "Options file disabled." );
    }

    // parse options from command line

    ok = parseOptions(formatter,
                      optionsVector.begin(),
                      optionsVector.end(),
                      "Unknown command line option: ");
    if (!ok)
    {
        (*_err) << "For help on options, type 'astyle -h' " << endl;
        exit(1);
    }

    if (shouldPrintHelp)
    {
        printHelp();
        return 1;
    }

    // if no files have been given, use cin for input and cout for output
    if (fileNameVector.empty())
    {
        formatter.init( cin );

        while (formatter.hasMoreLines() )
        {
            cout << formatter.nextLine();
            if (formatter.hasMoreLines())
                cout << endl;
        }
        cout.flush();
    }
    else
    {
        // indent the given files
        for (unsigned i=0; i<fileNameVector.size(); i++)
        {
            string originalFileName = fileNameVector[i];
            string inFileName = originalFileName + _suffix;

            if ( ! isWriteable(originalFileName.c_str()) )
            {
                (*_err) << "File '" << originalFileName << "' does not exist, or is read-only." << endl;
                continue;
            }

            remove(inFileName.c_str());

            if ( rename(originalFileName.c_str(), inFileName.c_str()) < 0)
            {
                (*_err) << "Could not rename " << originalFileName << " to " << inFileName << endl;
                exit(1);
            }

            TRACE( INFO, "Processing file " << originalFileName << "..." );
            ifstream in(inFileName.c_str());
            if (!in)
            {
                (*_err) << "Could not open input file" << inFileName << endl;
                exit(1);
            }

            ofstream out(originalFileName.c_str());
            if (!out)
            {
                (*_err) << "Could not open output file" << originalFileName << endl;
                exit(1);
            }

            // Unless a specific language mode has been set, set the language mode
            // according to the file's suffix.
            if (!formatter.modeSetManually)
            {
                if (stringEndsWith(originalFileName, ".java"))
                {
                    TRACE( INFO, "Setting style=java according to filename extension on " << originalFileName );
                    formatter.sourceStyle = STYLE_JAVA;
                }
                else if (stringEndsWith(originalFileName, ".sc") || stringEndsWith(originalFileName, ".cs"))
                {
                    TRACE( INFO, "Setting style=csharp according to filename extension on " << originalFileName );
                    formatter.sourceStyle = STYLE_CSHARP;
                }
                else
                {
                    TRACE( INFO, "Setting style=c according to non-java/csharp filename extension on " << originalFileName );
                    formatter.sourceStyle = STYLE_C;
                }
            }

            formatter.init( in );
            while (formatter.hasMoreLines() )
            {
                out << formatter.nextLine();
                if (formatter.hasMoreLines())
                    out << endl;
            }

            out.flush();
            out.close();

            in.close();
            if ( ! shouldBackupFile )
            {
                TRACE( INFO, "Removing backup file as requested..." );
                remove( inFileName.c_str() );
            }
        }
    }
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -