using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data;
using VBNET = Microsoft.VisualBasic;
using gmRTL.Core;
using gmRTL.GUI;
using Microsoft.VisualBasic.CompilerServices;
using System.Linq;
using System.Collections.Generic;

namespace VBSCAN
{
   public static class modVBSCAN
   {
      public static string gsMAKName = "";     //  full name of project mak file
      public static string gsMAKPath = "";     //  path of project make file
      public static string[] gsModPath = Array.Empty<string>();     //  full path and name of module files
      public static int giModCnt = 0;     //  number of modules
      public static string[] gsSrch = Array.Empty<string>();     //  array of strings to search for
      public static string gsDtlView = "";     //  Detail View
      public static int gNumHits = 0;     //  number of mathing lines
      public static int giCase = 0;     //  ignore case flag
      public static int giCmnts = 0;     //  ignore comments flag
      public static int giStopScan = 0;     //  abort scanning
      public const int DEFAULT = 0;     //  0 - Default
      public const int HOURGLASS = 11;     //  11 - Hourglass
      public static string gsDocComment = "";
      public static string gsDocFile = "";
      public static string gsDocType = "";
      [STAThread]
      public static void Main()
      {
         gsDocComment = "'!d";
         gsDocFile = "c:\\temp\\vbscan.htm";
         gsDocType = "HTML";
         System.Windows.Forms.Application.Run(frmVbScan.DefInstance);
      }
      public static int rdPrjFile(string sFName)
      {
         int rdPrjFile = 0;


         double inf = 0.0;
         string sbuf = "";
         try
         {
            rdPrjFile = -1;

            inf = 1;
            string c = "";
            string sExt = "";
            int l = 0;
            int i = 0;
            int rc = 0;
            string sfullname = "";
            string sshortName = "";
            string fType = "";
            MSOutl.Outline ol = null;

            ol = frmVbScan.DefInstance.OL1;
            ol.Clear();

            //  add project

            giModCnt = 0;
            gsModPath = null;
            giModCnt = giModCnt + 1;

            gmRTL.Core.FileSystemHelper.FileOpen(inf.ToInt(),sFName,gmRTL.Core.FileSystemHelper.OpenMode.Input,gmRTL.Core.FileSystemHelper.OpenAccess.Default,gmRTL.Core.FileSystemHelper.OpenShare.Default,-1);
            sbuf = "";
            while (!(gmRTL.Core.FileSystemHelper.EOF(inf.ToInt()) && giStopScan == 0))
            {
               System.Windows.Forms.Application.DoEvents();
               c = gmRTL.Core.FileSystemHelper.InputString(inf.ToInt(),1);
               if (c != 10.Char())
               {
                  sbuf = sbuf + c;
               }
               else
               {
                  sExt = (sbuf.Right(4).Left(3)).UCase();
                  if (sExt == "BAS" || sExt == "FRM" || sExt == "CLS" || sExt == "CTL")
                  {

                     cleanupNames(ref sbuf,ref sshortName,ref sfullname);

                     ol.AddItem(sshortName);
                     ol.set_Indent(ol.ListCount - 1,1);
                     fType = Word(sbuf,1,"=");
                     ol.set_List(ol.ListCount - 1,fType + ": " + sshortName);
                     frmVbScan.DefInstance.Refresh();

                     //  save index of gsModPath in ItemData

                     giModCnt = giModCnt + 1;
                     Array.Resize(ref gsModPath, giModCnt + 1);
                     gsModPath[giModCnt] = sfullname;
                     ol.set_ItemData(ol.ListCount - 1,giModCnt);

                     rc = rdMODFile(sfullname);
                     if (rc != 0)
                     {
                        rdPrjFile = 1;
                        throw new System.Exception();
                     }
                  }
                  sbuf = "";

               }
            }     //  while not EOF

            rdPrjFile = 0;

         }
         catch{}

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            System.Windows.Forms.MessageBox.Show("Error [rdPrjFile] " + "\r\n" + gmRTL.Core.GlobalException.Instance.Description, "", MessageBoxButtons.OK);
         }

         gmRTL.Core.FileSystemHelper.FileClose(inf.ToInt());

         return rdPrjFile;
      }
      private static void cleanupNames(ref string sbuf,ref string sshortName,ref string sfullname)
      {
         int l = 0;
         int i = 0;

         l = sbuf.Len() - 1;
         sbuf = (sbuf.Left(l)).TrimB();     //  get rid of CR

         i = sbuf.InStr("=",1,StringComparison.CurrentCulture);
         if (i != 0)
         {
            sbuf = (sbuf.Mid((i + 1))).TrimB();
         }

         i = sbuf.InStr(";",1,StringComparison.CurrentCulture);
         if (i != 0)
         {
            sbuf = (sbuf.Mid((i + 1))).TrimB();
         }

         l = sbuf.Len();
         i = RInStr(sbuf,"\\");
         if (i == 0)
         {
            sshortName = sbuf;
            sfullname = gsMAKPath + sbuf;
         }
         else
         {
            sshortName = (sbuf.Right((l - i))).TrimB();
            sfullname = sbuf;
         }
      }
      public static int rdMODFile(string sFName)
      {
         int rdMODFile = 0;


         double inf = 0.0;
         try
         {
            rdMODFile = -1;
            inf = 2;
            string sbuf = "";
            string c = "";
            string sExt = "";
            int l = 0;
            string w = "";
            int rc = 0;
            string w2 = "";
            MSOutl.Outline ol = null;
            string sn = "";
            int iNoComments = 0;

            ol = frmVbScan.DefInstance.OL1;

            gmRTL.Core.FileSystemHelper.FileOpen(inf.ToInt(),sFName,gmRTL.Core.FileSystemHelper.OpenMode.Input,gmRTL.Core.FileSystemHelper.OpenAccess.Default,gmRTL.Core.FileSystemHelper.OpenShare.Default,-1);
            sbuf = "";
            sn = "General";
            while (!gmRTL.Core.FileSystemHelper.EOF(inf.ToInt()))
            {
               c = gmRTL.Core.FileSystemHelper.InputString(inf.ToInt(),1);
               if (c != 10.Char())
               {
                  //  keep adding characters until new line encountered
                  sbuf = sbuf + c;
               }
               else
               {
                  sbuf = sbuf.Left((sbuf.Len() - 1));
                  if (sbuf.Right(1) != "_")
                  {
                     // Debug.Print sbuf
                     w = Word(sbuf,1);

                     if (w == "Public" || w == "Private" || w == "Friend")
                     {
                        l = 2;
                     }
                     else
                     {
                        l = 1;
                     }

                     w = Word(sbuf,l);

                     switch(w)
                     {
                     case "Function":
                     case "Sub":
                        w = w.Left(3) + " " + Word(sbuf,l + 1);
                        break;
                     case "Property":
                        w = w.Left(3) + " " + Word(sbuf,l + 1) + " " + Word(sbuf,l + 2);
                        break;
                     default:
                        l = 0;
                        break;
                     }

                     if (l > 0)
                     {
                        l = w.InStr("(",1,StringComparison.CurrentCulture);
                        if (l > 0)
                        {
                           w = w.Left((l - 1));
                        }
                        // sn = w ' mej modified to show full function declaration
                        sn = sbuf;
                     }
                     if (giCmnts != 0)
                     {
                        l = sbuf.InStr("'",1,StringComparison.CurrentCulture);
                        if (l != 0)
                        {
                           sbuf = sbuf.Left(l);
                        }
                     }
                     rc = ScanLine(sbuf,gsSrch);
                     if (rc != 0)
                     {
                        if (sn != "")
                        {
                           ol.AddItem(sn);
                           ol.set_Indent(ol.ListCount - 1,2);
                           sn = "";
                        }
                        ol.AddItem(TrimWS(sbuf));
                        ol.set_Indent(ol.ListCount - 1,3);
                        gNumHits = gNumHits + 1;
                     }
                     sbuf = "";
                  }
                  else
                  {

                     //  trim off line continuation character

                     sbuf = sbuf.Left((sbuf.Len() - 1));
                  }

               }
            }     //  while not EOF

            ol = null;


            rdMODFile = 0;

         }
         catch{}
         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            if (gmRTL.GUI.ControlHelper.MsgBox("Error [rdModFile] " + sFName + " " + gmRTL.Core.GlobalException.Instance.Description,gmRTL.Core.Enums.MsgBoxStyle.OkCancel,"") == System.Windows.Forms.DialogResult.Cancel)
            {
               rdMODFile = 1;
            }
            else
            {
               rdMODFile = 0;
            }
         }

         gmRTL.Core.FileSystemHelper.FileClose(inf.ToInt());
         return rdMODFile;
      }
      public static int rdRoutine(string sFName,string sRtn)
      {
         int rdRoutine = 0;

         double inf = 0.0;
         string sbuf = "";

         System.Diagnostics.Debug.WriteLine("rdRoutine: fname=" + sFName + " rtn=" + sRtn);

         try
         {
            rdRoutine = -1;

            inf = 2;
            string c = "";
            string w = "";
            int rc = 0;
            int l = 0;

            gmRTL.Core.FileSystemHelper.FileOpen(inf.ToInt(),sFName,gmRTL.Core.FileSystemHelper.OpenMode.Input,gmRTL.Core.FileSystemHelper.OpenAccess.Default,gmRTL.Core.FileSystemHelper.OpenShare.Default,-1);
            sbuf = "";
            while (!gmRTL.Core.FileSystemHelper.EOF(inf.ToInt()))
            {
               c = gmRTL.Core.FileSystemHelper.InputString(inf.ToInt(),1);
               if (c != 10.Char())
               {
                  sbuf = sbuf + c;
               }
               else
               {
                  sbuf = sbuf.Left((sbuf.Len() - 1));

                  w = Word(sbuf,1);

                  if (w == "Public" || w == "Private" || w == "Friend")
                  {
                     l = 2;
                  }
                  else
                  {
                     l = 1;
                  }

                  w = Word(sbuf,l);

                  switch(w)
                  {
                  case "Function":
                  case "Sub":
                     w = w.Left(3) + " " + Word(sbuf,l + 1);
                     break;
                  case "Property":
                     w = w.Left(3) + " " + Word(sbuf,l + 1) + " " + Word(sbuf,l + 2);
                     break;
                  default:
                     l = 0;
                     break;
                  }

                  if (l > 0)
                  {
                     l = w.InStr("(",1,StringComparison.CurrentCulture);
                     if (l > 0)
                     {
                        w = w.Left((l - 1));
                     }

                     if (sbuf == sRtn)
                     {

                        if (gsDtlView == "List")
                        {
                           rc = Rtn2Lst("End " + w.Left(3),inf,sbuf);
                        }
                        else
                        {
                           rc = Rtn2OL("End " + w.Left(3),inf,sbuf);
                        }

                        if (rc != 0)
                        {
                           throw new System.Exception();
                        }
                        else
                        {
                           break;  // Exit Do
                        }
                     }
                  }
                  sbuf = "";
               }
            }     //  while not EOF

            rdRoutine = 0;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [rdRoutine] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }
         gmRTL.Core.FileSystemHelper.FileClose(inf.ToInt());

         return rdRoutine;
      }
      public static int rdGeneral(string sFName)
      {
         int rdGeneral = 0;

         double inf = 0.0;

         try
         {
            rdGeneral = -1;

            inf = 2;
            gmRTL.Core.FileSystemHelper.FileOpen(inf.ToInt(),sFName,gmRTL.Core.FileSystemHelper.OpenMode.Input,gmRTL.Core.FileSystemHelper.OpenAccess.Default,gmRTL.Core.FileSystemHelper.OpenShare.Default,-1);
            int rc = 0;

            if (gsDtlView == "List")
            {
               rc = Rtn2Lst("",inf,"");
            }
            else
            {
               rc = Rtn2OL("",inf,"");
            }

            rdGeneral = 0;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [rdRoutine] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }
         gmRTL.Core.FileSystemHelper.FileClose(inf.ToInt());

         return rdGeneral;
      }
      public static int RInStr(string sSource,string sSearch)
      {
         int RInStr = 0;

         int i = 0;
         int l = 0;
         l = sSource.Len();
         for(i = l; i >= 1; i += -1)
         {
            if (sSource.Mid(i, 1) == sSearch)
            {
               RInStr = i;
               return RInStr;
            }
         }
         return 0;
      }
      public static int Rtn2Lst(string sTerm,double inf,string sRtnHdr)
      {
         int Rtn2Lst = 0;


         try
         {
            Rtn2Lst = -1;

            string sbuf = "";
            string tbuf = "";
            string c = "";
            string w = "";
            int rc = 0;
            int l = 0;
            System.Windows.Forms.ListBox Lst = null;
            int i = 0;

            Lst = frmRtnLst.DefInstance.List1;

            Lst.Items.Clear();
            if (sRtnHdr.Len() != 0)
            {
               Lst.Items.Add(sRtnHdr);
            }

            sbuf = "";
            while (!gmRTL.Core.FileSystemHelper.EOF(inf.ToInt()))
            {
               c = gmRTL.Core.FileSystemHelper.InputString(inf.ToInt(),1);
               if (c != 10.Char())
               {
                  sbuf = sbuf + c;
               }
               else
               {
                  sbuf = sbuf.Left((sbuf.Len() - 1));
                  tbuf = TrimWS(sbuf);
                  if (tbuf.Len() > 0)
                  {
                     Lst.Items.Add(sbuf);
                     Lst.SetItemData((Lst.Items.Count - 1),0);
                     if (giCmnts != 0)
                     {
                        l = tbuf.InStr("'",1,StringComparison.CurrentCulture);
                        if (l != 0)
                        {
                           tbuf = tbuf.Left(l);
                        }
                     }
                     rc = ScanLine(tbuf,gsSrch);
                     if (rc != 0)
                     {
                        Lst.SetItemData((Lst.Items.Count - 1),1);
                        Lst.SetSelected(Lst.Items.Count - 1,true);
                     }
                     if (sTerm.Len() > 0 && tbuf.Left(7) == sTerm)
                     {
                        break;  // Exit Do     //  exit at end of routine
                     }     //  exit at end of routine
                     sbuf = "";
                  }
               }

            }     //  while not EOF

            Rtn2Lst = 0;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [Rtn2Lst] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }

         gmRTL.Core.FileSystemHelper.FileClose(inf.ToInt());

         return Rtn2Lst;
      }
      public static int Rtn2OL(string sTerm,double inf,string sRtnHdr)
      {
         int Rtn2OL = 0;


         try
         {
            Rtn2OL = -1;

            string sbuf = "";
            string c = "";
            string w = "";
            int i = 0;
            int l = 0;
            string tbuf = "";
            MSOutl.Outline ol = null;
            int nest = 0;

            ol = frmRtnOL.DefInstance.OL1;
            ol.Clear();
            nest = 1;
            sbuf = "";
            if (sRtnHdr.Len() != 0)
            {
               ol.AddItem(sRtnHdr);
            }
            while (!gmRTL.Core.FileSystemHelper.EOF(inf.ToInt()))
            {
               c = gmRTL.Core.FileSystemHelper.InputString(inf.ToInt(),1);
               if (c != 10.Char())
               {
                  sbuf = sbuf + c;
               }
               else
               {
                  sbuf = sbuf.Left((sbuf.Len() - 1));
                  sbuf = TrimWS(sbuf);
                  if (sbuf.Len() > 0)
                  {

                     if (sTerm.Len() > 0 && sbuf.Left(7) == sTerm)
                     {
                        ol.AddItem(sbuf);
                        ol.set_Indent(ol.ListCount - 1,1);
                        break;  // Exit Do     //  exit at end of routine
                     }

                     w = Word(sbuf,1);

                     if (w == "End")
                     {
                        w = w + " " + Word(sbuf,2);
                     }

                     ol.AddItem(sbuf);
                     switch(w)
                     {
                     case "If":
                     case "While":
                     case "Do":
                     case "Select":
                     case "For":
                        ol.set_Indent(ol.ListCount - 1,nest);
                        i = 1;
                        if (w == "If")
                        {
                                //  check for same-line if
                           l = sbuf.InStr("'",1,StringComparison.CurrentCulture);
                           if (l == 0)
                           {
                              l = sbuf.Len() + 1;
                           }
                           tbuf = (sbuf.Left((l - 1))).TrimB();
                           if (tbuf.Right(4) != "Then")
                           {
                              i = 0;
                           }
                        }
                        nest = nest + i;
                        break;
                     case "End If":
                     case "End Select":
                     case "Wend":
                     case "Loop":
                     case "Next":
                        ol.set_Indent(ol.ListCount - 1,nest - 1);
                        nest = nest - 1;
                        break;
                     case "ElseIf":
                     case "Else":
                     case "Case":
                        ol.set_Indent(ol.ListCount - 1,nest - 1);
                        break;
                     default:
                        ol.set_Indent(ol.ListCount - 1,nest);
                        break;
                     }

                     sbuf = "";

                  }
               }

            }     //  while not EOF

            Rtn2OL = 0;

            return Rtn2OL;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);

            System.Windows.Forms.MessageBox.Show("Error [Rtn2OL] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }
         return Rtn2OL;
      }
      public static int SaveCSR(string sFile)
      {
         int SaveCSR = 0;

         int cout = 0;
         try
         {
            SaveCSR = -1;

            string oFile = "";


            oFile = sFile;

            int i = 0;
            int m = 0;
            cout = 1;
            string TABc = "";
            TABc = 9.Char();
            MSOutl.Outline ol = null;

            gmRTL.Core.FileSystemHelper.FileOpen(cout,oFile,gmRTL.Core.FileSystemHelper.OpenMode.Output,gmRTL.Core.FileSystemHelper.OpenAccess.Default,gmRTL.Core.FileSystemHelper.OpenShare.Default,-1);

            gmRTL.Core.FileSystemHelper.PrintLine(cout,"VBScan Report for " + gsMAKName + " -- " + DateTime.Now.ToStr());
            gmRTL.Core.FileSystemHelper.PrintLine(cout,new string('-',80));
            gmRTL.Core.FileSystemHelper.PrintLine(cout,"");
            gmRTL.Core.FileSystemHelper.PrintLine(cout,"Ignore Comments: ",(giCmnts == 0 ? "No" : "Yes"));
            gmRTL.Core.FileSystemHelper.PrintLine(cout,"Ignore Case    : ",(giCase == 0 ? "No" : "Yes"));
            gmRTL.Core.FileSystemHelper.PrintLine(cout,"");

            m = gsSrch.GetUpperBound(0);

            for(i = 1; i <= m; i += 1)
            {
               gmRTL.Core.FileSystemHelper.PrintLine(cout,"Search " + i + ": [" + gsSrch[i] + "]");
            }
            gmRTL.Core.FileSystemHelper.PrintLine(cout,new string('-',80));


            ol = frmVbScan.DefInstance.OL1;
            m = ol.ListCount;
            for(i = 0; i <= m - 1; i += 1)
            {
               gmRTL.Core.FileSystemHelper.PrintLine(cout,new string(TABc[0],ol.Indent(i) - 1) + ol.List(i));
            }

            SaveCSR = 0;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [SaveCSR] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }

         try
         {
            gmRTL.Core.FileSystemHelper.FileClose(cout);

         }
         catch{}
         return SaveCSR;
      }
      public static int MakeHtm(string sFile)
      {
         int MakeHtm = 0;

         int f = 0;
         try
         {
            MakeHtm = -1;

            MSOutl.Outline ol = null;

            // -------------------------------------------------------------------------------
            //  create and open a new htm file and save as the specified file name
            // -------------------------------------------------------------------------------

            f = gmRTL.Core.FileSystemHelper.FreeFile();
            gmRTL.Core.FileSystemHelper.FileOpen(f,sFile,gmRTL.Core.FileSystemHelper.OpenMode.Output,gmRTL.Core.FileSystemHelper.OpenAccess.Default,gmRTL.Core.FileSystemHelper.OpenShare.Default,-1);

            // -------------------------------------------------------------------------------
            //  Add document header
            // -------------------------------------------------------------------------------

            gmRTL.Core.FileSystemHelper.PrintLine(f,"<HTML>");
            gmRTL.Core.FileSystemHelper.PrintLine(f,"<HEAD>");
            gmRTL.Core.FileSystemHelper.PrintLine(f,"<TITLE>" + gsMAKName + "</TITLE>");
            gmRTL.Core.FileSystemHelper.PrintLine(f,"</HEAD>");

            // -------------------------------------------------------------------------------
            //  Walk the outline adding the outline contents to the word doc.  Use Header styles
            //  for indent levels < 3.
            // -------------------------------------------------------------------------------

            ol = frmVbScan.DefInstance.OL1;

            string sStyle = "";
            string sText = "";
            int iBgn = 0;
            int i = 0;

            for(i = 0; i <= ol.ListCount - 1; i += 1)
            {

               if (ol.Indent(i) == 1)
               {
                  ol.set_Expand(i,true);
                  ol.ListIndex = i;
                  ol.Refresh();
               }


               sStyle = "p>";     //  default style is none

               sText = ol.List(i);

               if (ol.Indent(i) < 3)
               {
                  sStyle = "H" + ol.Indent(i) + ">";
                  if (ol.Indent(i) == 1)
                  {
                     sText = sText.UCase();
                  }
               }
               else
               {

                  //  clean up doc comment lines

                  if (sText == gsDocComment)
                  {
                     sText = "";     //  convert blank doc comments to blank line
                  }     //  convert blank doc comments to blank line

                  iBgn = sText.InStr(gsDocComment,1,StringComparison.CurrentCultureIgnoreCase);
                  if (iBgn != 0)
                  {

                     if (iBgn == 1)
                     {

                        //  strip the comment off of begin-of-line comments

                        iBgn = gsDocComment.Len() + 1;
                        sText = (sText.Mid(iBgn)).TrimB();
                     }
                     else
                     {

                        //  Cut the comment out of in-line comments and replace with a ":"

                        sText = sText.Left((iBgn - 1)) + " : " + sText.Mid((iBgn + gsDocComment.Len()));
                     }

                  }
               }

               // sText = sText & "<p>"
               // 
               // Debug.Print sStyle, sText

               if (sStyle != "")
               {
                  gmRTL.Core.FileSystemHelper.PrintLine(f,"<" + sStyle + sText + "</" + sStyle);
               }
               else
               {
                  gmRTL.Core.FileSystemHelper.PrintLine(f,sText + "\r\n");
               }

            }

            MakeHtm = 0;

            // -------------------------------------------------------------------------------
            //  End HTML Generation
            // -------------------------------------------------------------------------------

            gmRTL.Core.FileSystemHelper.PrintLine(f,"</BODY>");
            gmRTL.Core.FileSystemHelper.PrintLine(f,"</HTML>");

         }
         catch{}

         gmRTL.Core.FileSystemHelper.FileClose(f);

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            gmRTL.Core.ErrorHandling.RaiseGlobalException(gmRTL.Core.GlobalException.Instance.Number,"Error [MakeHtm] " + gmRTL.Core.GlobalException.Instance.Description,String.Empty);
         }

         return MakeHtm;
      }
      public static int SaveRtnOL()
      {
         int SaveRtnOL = 0;

         int cout = 0;
         try
         {
            SaveRtnOL = -1;

            string oFile = "";
            gmRTL.MSComDlg.CommonDialog CMD1 = null;
            string sRtn = "";
            sRtn = frmRtnOL.DefInstance.Text.TrimB();

            CMD1 = frmVbScan.DefInstance.CMD1;

            CMD1.Filter = "VBScan Routine Files (*.CS1)|*.CS1";
            CMD1.DialogTitle = "Select Output File";
            CMD1.FilterIndex = 1;
            CMD1.FileName = "VBSCAN.CS1";
            CMD1.CancelError = false;
            CMD1.ShowSave();
            if (CMD1.FileName != "")
            {
               oFile = CMD1.FileName;
            }
            else
            {
               SaveRtnOL = 0;
               return SaveRtnOL;
            }

            int i = 0;
            int m = 0;
            cout = 1;
            string TABc = "";
            TABc = 9.Char();
            MSOutl.Outline ol = null;

            gmRTL.Core.FileSystemHelper.FileOpen(cout,oFile,gmRTL.Core.FileSystemHelper.OpenMode.Output,gmRTL.Core.FileSystemHelper.OpenAccess.Default,gmRTL.Core.FileSystemHelper.OpenShare.Default,-1);

            gmRTL.Core.FileSystemHelper.PrintLine(cout,sRtn);
            gmRTL.Core.FileSystemHelper.PrintLine(cout,"' ",DateTime.Now);
            gmRTL.Core.FileSystemHelper.PrintLine(cout,"'",new string('-',80));
            gmRTL.Core.FileSystemHelper.PrintLine(cout,"");

            ol = frmRtnOL.DefInstance.OL1;
            m = ol.ListCount;
            for(i = 0; i <= m - 1; i += 1)
            {
               gmRTL.Core.FileSystemHelper.PrintLine(cout,new string(TABc[0],ol.Indent(i) - 1) + ol.List(i));
            }

            SaveRtnOL = 0;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [SaveRtnOL] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }
         try
         {
            gmRTL.Core.FileSystemHelper.FileClose(cout);

         }
         catch{}
         return SaveRtnOL;
      }
      public static int ScanLine(string sSource,string[] sSrch)
      {
         int ScanLine = 0;


         try
         {
            ScanLine = 0;

            int i = 0;
            int m = 0;

            m = sSrch.GetUpperBound(0);

            for(i = 1; i <= m; i += 1)
            {
               if (sSource.InStr(sSrch[i],1,((StringComparison)(giCase))) != 0)
               {
                  ScanLine = 1;
                  return ScanLine;
               }
            }

            return ScanLine;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [ScanLine] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);

         }
         return ScanLine;
      }
      public static string TrimWS(string s_In)
      {
         string TrimWS = "";

         //  trim leading and trailing (tabs and spaces)

         TrimWS = "";

         int l = 0;
         int i = 0;
         string SOut = "";
         string c = "";
         string TABc = "";
         TABc = 9.Char();

         SOut = s_In.TrimB();

         l = SOut.Len();
         if (l == 0)
         {
            return TrimWS;
         }

         for(i = 1; i <= l; i += 1)
         {
            c = SOut.Left(1);
            if (c == TABc || c == " ")
            {
               SOut = SOut.Right((l - 1));
               l = l - 1;
            }
         }
         for(i = l; i <= 1; i += 1)
         {
            c = SOut.Right(1);
            if (c == TABc || c == " ")
            {
               SOut = SOut.Left((l - 1));
               l = l - 1;
            }
         }
         return SOut;
      }
      public static string Word(string ss,int i,string delim = " ")
      {
         string Word = "";

         //  Parse a blank delimeted string
         //  ss = string to be parsed
         //  i = number of the word to be returned
         // 
         //  return the i'th word in string ss.  Words are delimited by blanks
         //  if i < 1 or i > number of words in ss, blank is returned

         try
         {
            Word = "";

            string s = "";
            s = ss.TrimB();
            int l = 0;
            l = s.Len();
            int c = 0;
            c = 0;     //  word count
            int b = 0;
            b = 1;     //  begining each parsed word
            int e = 0;
            e = 0;     //  end of each parsed word

            if (l == 0)
            {
               return Word;
            }
            if (i < 1)
            {
               return Word;
            }

            while (e <= l)
            {
               e = ss.InStr(delim,b,StringComparison.CurrentCulture);

               if (e == 0)
               {
                  e = l + 1;
               }
               c = c + 1;
               if (c == i)
               {
                  Word = ss.Mid(b, e - b);
                  return Word;
               }
               for(b = e; b <= l; b += 1)
               {
                       //  skip white space
                  if (ss.Mid(b, 1) != " ")
                  {
                     break;  // Exit For
                  }
               }
            }

            return Word;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [Word] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }
         return Word;
      }
   }
}
