Click here to Skip to main content
14,980,532 members
Articles / Programming Languages / C++
Tip/Trick
Posted 12 Jul 2021

Tagged as

Stats

3.7K views
1 bookmarked

An awk Program to Indent Source

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
21 Jul 2021CPOL
On certain occasions, Visual Studio does not indent as intended so here is an awk program which does the trick.
In this tip, you will see an awk program that fixes the problem of VS not indenting code in C++ as intended when previous line does not end in a semi-colon.

Introduction

Visual Studio auto-indent on C++ files does not work as I intend if the previous line does not end in a semi-colon. This can result in intolerably indented code as can be seen below. The awk program here solves it easily and can be automated for the current document via a batch file invoked via Tools->External Tools. The same file post-processing is shown below. You, of course, can modify the code to utilize the indent string you prefer and can set the block open/close characters to that which your language requires. Be sure to invoke this on a saved file, otherwise you will be prompted to choose if you wish to reload it i.e. the processed file and lose any prior changes you have edited. Please note the code has been updated 7/21/2021 to repair a bug.

C++
BEGIN {INDENT_STRING="\t"; OPEN_STRING  = "{"; CLOSE_STRING = "}"; DEPTH=0; NEED_NEWLINE=0}

{OPEN = NUMBER_OF_OPEN(); DEPTH+=OPEN}
{CLOSE = NUMBER_OF_CLOSE(); DEPTH-=CLOSE}
{INDENT()}

function NUMBER_OF_OPEN() { return NUMBER_OF(OPEN_STRING)}
function NUMBER_OF_CLOSE() { return NUMBER_OF(CLOSE_STRING)}
function NUMBER_OF(_SUB_STRING)
{
    N = 0
    INDEX = 0
    STRING = $0
    while((INDEX = index(STRING, _SUB_STRING)) > 0)
    {
        ++N
        if(INDEX < length(STRING))
            STRING = substr(STRING, INDEX+1)
        else
            break
    }
    return N
}
function INDENT()
{
    if(NEED_NEWLINE!=0) printf("\n")
    if(length($0) == 0) { printf("\n"); NEED_NEWLINE=0 }
    else
    {
        # find first non-blank character
        if(match($0, "[^[:blank:]]"))
        {
            # RSTART is where the match of the pattern starts
            # RLENGTH is the length of the matched pattern
            # before = substr($0,1,RSTART-1)
            matched_pattern = substr($0,RSTART)
            # after = substr($0,RSTART+RLENGTH)
            i=0;
            if(OPEN > CLOSE) i = 1
            for(;i<DEPTH;++i) printf("%s", INDENT_STRING);
            printf("%s", matched_pattern)
            NEED_NEWLINE = 1
        }
    }
}

Before processing with the above awk program liberally copied from Mr. Bruce Barnett: Correcting this manually for a five-hundred line file is intolerable.

C++
}
template<typename scalarType> requires CONCEPT_scalar<scalarType>
    this_type& write(file_offset_type offset,
    const unique_ptr<scalarType[]>&buffer, size_t count)
    {
        HANDLE_NO_HANDLE
            seekp(offset);
        return write(buffer, count);
    }
    template<typename scalarType> requires CONCEPT_scalar<scalarType>
        this_type& write(file_offset_type offset,
        const csmart_unique_ptr<scalarType[]>&buffer, size_t count)
        {
            HANDLE_NO_HANDLE
                seekp(offset);
            return write(buffer, count);
        }
        template<typename scalarType> requires CONCEPT_scalar<scalarType>
            this_type& write(file_offset_type offset,
            const csmart_unique_ptr<scalarType[]>&buffer)
            {
                HANDLE_NO_HANDLE
                    seekp(offset);
                return write(buffer);
            }
            this_type& write(const void* buffer, size_t nbytes)
            {
                HANDLE_NO_HANDLE
                    if (nbytes == 0) return *this;
                DEBUG_ASSERT(buffer != nullptr, nullptr_msg, ARGUMENT_VA_ARG(nbytes));
                DWORD nbytes_written = 0;
                osapi::WriteFile(m_handle, buffer, nbytes, &nbytes_written, NULL);
                return *this;

The same lines after processing with the above awk program: Voila! All with a mere two clicks via Tools.

C++
}
template<typename scalarType> requires CONCEPT_scalar<scalarType>
this_type& write(file_offset_type offset,
                 const unique_ptr<scalarType[]>&buffer, size_t count)
{
    HANDLE_NO_HANDLE
    seekp(offset);
    return write(buffer, count);
}
template<typename scalarType> requires CONCEPT_scalar<scalarType>
this_type& write(file_offset_type offset,
const csmart_unique_ptr<scalarType[]>&buffer, size_t count)
{
    HANDLE_NO_HANDLE
    seekp(offset);
    return write(buffer, count);
}
template<typename scalarType> requires CONCEPT_scalar<scalarType>
this_type& write(file_offset_type offset,
const csmart_unique_ptr<scalarType[]>&buffer)
{
    HANDLE_NO_HANDLE
    seekp(offset);
    return write(buffer);
}
this_type& write(const void* buffer, size_t nbytes)
{
    HANDLE_NO_HANDLE
    if (nbytes == 0) return *this;
    DEBUG_ASSERT(buffer != nullptr, nullptr_msg, ARGUMENT_VA_ARG(nbytes));
    DWORD nbytes_written = 0;
    osapi::WriteFile(m_handle, buffer, nbytes, &nbytes_written, NULL);
    return *this;
}
this_type& write(const_byte_ptr_type buffer, size_t nbytes)
{
    HANDLE_NO_HANDLE
    if (nbytes == 0) return *this;

History

  • 13th July, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PaltryProgrammer
United States United States
No Biography provided

Comments and Discussions

 
QuestionWould clang-format be a wiser choice? Pin
scjurgen26-Jul-21 0:32
professionalscjurgen26-Jul-21 0:32 
AnswerRe: Would clang-format be a wiser choice? Pin
PaltryProgrammer26-Jul-21 7:29
MemberPaltryProgrammer26-Jul-21 7:29 
Questionerror in program Pin
PaltryProgrammer20-Jul-21 22:58
MemberPaltryProgrammer20-Jul-21 22:58 
GeneralMy vote of 5 Pin
CPallini18-Jul-21 23:08
mveCPallini18-Jul-21 23:08 
Questionis this a C++ program? Pin
Southmountain13-Jul-21 10:12
MemberSouthmountain13-Jul-21 10:12 
AnswerRe: is this a C++ program? Pin
PaltryProgrammer13-Jul-21 20:18
MemberPaltryProgrammer13-Jul-21 20:18 
GeneralRe: is this a C++ program? Pin
Southmountain14-Jul-21 8:02
MemberSouthmountain14-Jul-21 8:02 
GeneralRe: is this a C++ program? Pin
PaltryProgrammer14-Jul-21 12:48
MemberPaltryProgrammer14-Jul-21 12:48 
GeneralRe: is this a C++ program? Pin
PaltryProgrammer21-Jul-21 8:26
MemberPaltryProgrammer21-Jul-21 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.