Author Topic: If_Include macro - Insert #ifndef/#define/#endif markers in .h files  (Read 10886 times)

ewjax

  • Community Member
  • Posts: 13
  • Hero Points: 0
From the source file (see attached):

Code: [Select]
#include "slick.sh"

//
// If_Include
//
// simple macro for C++ programmers, to automatically insert the #define markers
// at the start of each .h file, to prevent multiple inclusion and multiple
// definitions
//
// example:  after opening new file Test.h, after running the If_Include macro,
// the file will contain the following lines:
//
//        #ifndef __TEST_H
//        #define __TEST_H
//       
//       
//       
//        #endif   // __TEST_H
//
//
// see "Large Scale C++ Design" by Lakos
//
// original author:  Elliott W. Jackson
//

_command If_Include()
{
    _str name;
    _str ext;

    // get filename and extension, convert to upper case
    name = upcase(strip_filename(p_buf_name, "PE"));
    ext = upcase(get_extension(p_buf_name));

//    messageNwait("buf is "p_buf_name);
//    messageNwait("name is "name);
//    messageNwait("ext is "ext);


    // save insert mode, and set mode to insert
    int insertstate;
    insertstate = _insert_state();
    _insert_state(1);

    // goto file begin
    top_of_buffer();

    // insert first text line
    _insert_text("\n");
    _insert_text("#ifndef __"name"_"ext"\n");

    // insert second text line
    _insert_text("#define __"name"_"ext"\n");

    // insert some blanks
    _insert_text("\n");
    _insert_text("\n");
    _insert_text("\n");

    // save position
    cursor_up();
    cursor_up();
    push_bookmark();

    // goto file end
    bottom_of_buffer();

    // insert last text line
    _insert_text("\n");
    _insert_text("#endif   // __"name"_"ext"\n");
    _insert_text("\n");

    // jump back to saved pos
    pop_bookmark();

    // restore original insert mode
    _insert_state(insertstate);

    // done
    message("If_Include - Written by Elliott W. Jackson");
}


« Last Edit: July 14, 2006, 02:54:11 AM by ewjax »

jbezem

  • Community Member
  • Posts: 87
  • Hero Points: 8
Re: If_Include macro - Insert #ifndef/#define/#endif markers in .h files
« Reply #1 on: July 17, 2006, 08:27:43 AM »
Referring to John Lakos:
Some years ago I created a more encompassing macro to do the following:
- if in an H-file, insert 'internal' include guards (the normal variety) in the file;
- if in a C file, on an include directive with "" (supposedly your 'own' source code), insert 'external' include guards around the include statement, created from the same rule set as the first bullet;
- if in a C file, on an include directive with <> (foreign code), go into the file, extract the macro name used for include guard, and if found, insert that macro as 'external' include guards around the include directive in the original file.
Precondition: You need to have defined a project, and correctly provided all include paths. It uses the Slick-C macro 'cursor-error', which can also find include files.

External include guards are identical guards as inside the file to be included, but then around the include statement. This prevents the file from being opened, read and closed if the contents aren't needed anyway.
Some people deem this obsolete in times of C++, precompiled headers, #pragma once and similar compiler fetaures, but in my (embedded) experience compile times can be dramatically reduced in most cases while it doesn't harm in all others. Read Lakos for details.

I advertised once on LazyHacker, but aside from one enthousiastic reaction, no one seemed interested. If different here, let me know. I can post the source. It works for me since VSE 6, slightly modified since VSE 8.

FWIW,

Johan

suevian

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: If_Include macro - Insert #ifndef/#define/#endif markers in .h files
« Reply #2 on: July 19, 2006, 08:42:50 AM »
Please do attach the code.

Thanks.

jbezem

  • Community Member
  • Posts: 87
  • Hero Points: 8
Re: If_Include macro - Insert #ifndef/#define/#endif markers in .h files
« Reply #3 on: July 19, 2006, 10:20:51 AM »
I hope the comments are sufficient, if not, ask.

Johan