Author Topic: How to add ifdef around a function using macros  (Read 7502 times)

gpavlov

  • New Community Member
  • Posts: 1
  • Hero Points: 0
How to add ifdef around a function using macros
« on: March 03, 2017, 02:14:56 AM »
I am trying to figure out how to write a script that finds a function definition using it's name in all project files and adds #ifdef right before the function and #endif just after the function. I have a list of those functions and I want to automate the process. I noticed there is "csh_proc_search" macro function but I can't find a good example of how to use it.

 

Graeme

  • Senior Community Member
  • Posts: 2812
  • Hero Points: 347
Re: How to add ifdef around a function using macros
« Reply #1 on: March 03, 2017, 10:49:24 AM »
I am trying to figure out how to write a script that finds a function definition using it's name in all project files and adds #ifdef right before the function and #endif just after the function. I have a list of those functions and I want to automate the process. I noticed there is "csh_proc_search" macro function but I can't find a good example of how to use it. 

The code below is completely untested.  It's a modified version of a macro I wrote to beautify all files in a project.  next_proc() finds the next function / procedure.  If the function "signature" spans multiple lines, the code won't work because it looks only in the first line for the function name.  You could optimise xifdef_file() by putting the call to next_proc in the outer loop.

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

#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)




static void xifdef_file(_str func_names[])
{
   int k1 = func_names._length();
   for ( k1 = 0; k1 < func_names._length(); ++k1 ) {
      top();
      while ( next_proc('quiet') == 0 ) {
         _str s1;
         get_line(s1);
         if ( pos(func_names[k1], s1) ) {
            up();
            insert_line("#ifdef whatever");
            down();
            search('{','I?');
            _deselect();
            find_matching_paren(true);
            insert_line("#endif");
         }
      }
   }
}


_command void xifdef_project(_str func_names[]=null, boolean ask = true, boolean no_preview = false, boolean autosave = true) name_info(',')
{
   _str files_to_xifdef [];

   //_GetWorkspaceFiles(_workspace_filename, files_to_xifdef);
   _getProjectFiles( _workspace_filename, _project_get_filename(), files_to_xifdef, 1);

   if (ask && !no_preview) {
      activate_preview();
   }

   int k;
   for (k = 0; k < files_to_xifdef._length(); ++k) {
      if (ask) {

         if (!no_preview) {
            struct VS_TAG_BROWSE_INFO cm;
            tag_browse_info_init(cm);
            cm.member_name = files_to_xifdef[k];
            cm.file_name = files_to_xifdef[k];
            cm.line_no = 1;
            cb_refresh_output_tab(cm, true, false, false);
            _UpdateTagWindowDelayed(cm, 0);
         }

         _str res = _message_box("Process " :+ files_to_xifdef[k], "ifdef", MB_YESNOCANCEL|IDYESTOALL);
         if (res == IDCANCEL) return;
         if (res == IDNO) continue;
         if (res == IDYESTOALL) ask = false;
      }

      if (edit("+B " :+ files_to_xifdef[k]) == 0) {
         xifdef_file(func_names);
         if (autosave) save();
      }
      else
      {
         edit(files_to_xifdef[k]);
         xifdef_file(func_names);
         if (autosave) save();
         quit();
      }
   }
}