/********************************************************************
                         THIS_FILE

   Description:
   Syntax:      THIS_FILE

   Adds THIS_FILE block to a CPP file (if it is not already added),
   just after last include statement

   #ifdef _DEBUG
   #define new DEBUG_NEW
   #undef THIS_FILE
   static char THIS_FILE[] = __FILE__;
   #endif
********************************************************************/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <editor/doc.h>
#include <editor/macros.h>
#include <editor/options.h>
//#include <editor/dlg.h>
//#include <editor/app.h>

#define CPP_INCLUDE_REGEXPR   " *# *include *\t*[<\"][^>\"]+[>\"]"  // reg expresion for searching include statements


// function prototypes
void ScriptMain(HDOC);


/*-------------------------------------------------------------------
                    main function
-------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
   HDOC hDoc;
   char szExt[5];

   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;
   }
   Gte_GetFileExtension(hDoc, szExt, sizeof(szExt));
   if(strcmpi(szExt, "cpp") != 0)
   {
      MsgBox("This is not a C++ cpp file", MB_OK);
      return 1;
   }

   // turn off line drawing and/or selecting mode
   GM_EndLineDraw(hDoc);
   GM_EndSelect(hDoc);

   {
      BOOL bOverwriteMode;

      // save original editor options
      bOverwriteMode = Gte_IsOverwriteMode();

      // set new editor options
      Gte_SetOverwriteMode(FALSE);

      // call main script function
      ScriptMain(hDoc);

      // restore original editor options
      Gte_SetOverwriteMode(bOverwriteMode);
   }

   return 0;
}


/*-------------------------------------------------------------------
                         ScriptMain

  Main script function.

  PARAMETERS

     hDoc - Handle of the active document.
-------------------------------------------------------------------*/

void ScriptMain(HDOC hDoc)
{
   long nLine;

   // first search if we already have a Visual C++ "THIS_FILE" block
   // in the source file
   if(Gte_Find(hDoc, "^ *# *undef *THIS_FILE", 1, 1, FALSE, TRUE, TRUE, TRUE) == 0)
   {
      // we already have THIS_FILE block
      return;
   }

   // Search for last include statement in file
   nLine = 1;
   while(Gte_Find(hDoc, CPP_INCLUDE_REGEXPR, 1, nLine, FALSE, TRUE, TRUE, TRUE) == 0)
   {
      Gte_GetCursorPos(hDoc, NULL, &nLine);
      nLine++;
   }
   // nLine points to first line after last valid include statement in the file

	// set cursor pos and destroy selection
	Gte_SetCursorPos(hDoc, 1, nLine, FALSE);
   Gte_InsertText(hDoc, "\n"
	                     "#ifdef _DEBUG\n"
                        "#define new DEBUG_NEW\n"
                        "#undef THIS_FILE\n"
                        "static char THIS_FILE[] = __FILE__;\n"
                        "#endif\n");
}


