/********************************************************************
                         InclOnce

  Description: Prevents compilation of C/C++ header two times
  Syntax:      inc_once

  This macro inserts:  #ifdef __XXX_H #define __XXX_H #endif
  statements to prevents compilation of the C/C++ header file
  more than one time.

  To map InclOnce command to an shortcut key:

  1. Save this script in the GwdTextEditor\script directory.
  2. From the Macro menu, choose Macro command.
  3. Choose the New button.
  4. In the New Macro Name input box, type a name for the macro
     (e.g. Script_InclOnce). In this dialog you can also define a
     shortcut key for macro.
  5. Choose the OK button.
  6. In the Edit Macro dialog editor, enter the following two
     lines:
        Param1 "InclOnce"
        ExecScript
  7. Choose the Close button.
  8. You can now define shortcut key for Script_InclOnce macro
     in the Key Mapping dialog box.

  Author: Vedran Gaco
********************************************************************/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <editor/doc.h>
#include <editor/macros.h>
#include <editor/options.h>


// function prototypes
void ScriptMain(HDOC);

/*-------------------------------------------------------------------
                    main function
-------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
   HDOC hDoc;

   hDoc = Gte_GetActiveDocument();
   if(hDoc == NULL) {
      MsgBox("Cannot get handle of the active document!", MB_OK);
      return 1;
   }
   // check if document is read only
   if(Gte_IsReadOnly(hDoc)) {
      MsgBox("Document is read only!", MB_OK);
      return 1;
   }
   // this script works only with C/C++ header file
   {
      char szFileExt[8];

      Gte_GetFileExtension(hDoc, szFileExt, sizeof(szFileExt));
      if(stricmp(szFileExt, "h") != 0 && stricmp(szFileExt, "hpp") &&
         stricmp(szFileExt, "hxx"))
      {
         MsgBox("This is not C/C++ header file", MB_OK);
         return 1;
      }
   }

   // turn off line drawing and/or selecting mode
   GM_EndLineDraw(hDoc);
   GM_EndSelect(hDoc);

   {
      BOOL bOverwriteMode;
      BOOL bAutoIndentMode;

      // save original editor options
      bOverwriteMode = Gte_IsOverwriteMode();
      bAutoIndentMode = Gte_IsAutoIndentMode();
      // set new editor options
      Gte_SetOverwriteMode(FALSE);
      Gte_SetAutoIndentMode(FALSE);
      // save cursor position
      GM_SaveCursorPos(hDoc);

      // call main script function
      ScriptMain(hDoc);

      // restore original editor options
      Gte_SetOverwriteMode(bOverwriteMode);
      Gte_SetAutoIndentMode(bAutoIndentMode);
      GM_RestoreCursorPos(hDoc);
   }

   return 0;
}


/*-------------------------------------------------------------------
                         ScriptMain

  Main script function.

  PARAMETERS

     hDoc - Handle of the active document.
-------------------------------------------------------------------*/

void ScriptMain(HDOC hDoc)
{
   char szBuf[128+2*FILENAME_MAX];
   char szFileName[FILENAME_MAX];
   char *pszName;
   char *psz;

   Gte_GetFileName(hDoc, szFileName, FILENAME_MAX);
   pszName = max(strrchr(szFileName, '/'), strrchr(szFileName, '\\'));
   if(pszName == NULL)
      pszName = szFileName;
   else
      pszName++;
   psz = strchr(pszName, '.');
   if(psz)
      *psz = 0;
   strupr(pszName);

   sprintf(szBuf, "#ifndef __%s_H\n#define __%s_H\n\n", pszName, pszName);
   Gte_SetCursorPos(hDoc, 1, 1, FALSE);
   Gte_InsertText(hDoc, szBuf);
   Gte_SetCursorPos(hDoc, 4096, Gte_GetNumberOfLines(hDoc), FALSE);
   Gte_InsertText(hDoc, "\n\n#endif");
}
