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

namespace ScanToolLib
{
   public static class modScanToolLib
   {
      private const string MOD_NAME = "modScanToolLib";
      //  here is a command line string for testing /optRpt=DirList /Root="c:\projects\" /LOG="ScanTool.log"
      public static string readNode(System.Xml.XmlDocument dom,string xpath)
      {
         string readNode = "";

         const string FUNC_NAME = "readNode";
         System.Xml.XmlNode nod = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            nod = dom.SelectSingleNode(xpath);

            if (nod.FirstChild == null)
            {
               readNode = "";
            }
            else
            {
               readNode = nod.FirstChild.Value;
            }

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

         nod = null;

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            gmRTL.Core.ErrorHandling.RaiseGlobalException(gmRTL.Core.GlobalException.Instance.Number,String.Empty,MOD_NAME + "::" + FUNC_NAME + " line= " + gmRTL.Core.GlobalException.Erl + gmRTL.Core.GlobalException.Instance.Description);
         }

         return readNode;
      }
      public static System.Xml.XmlElement writeTextNode(System.Xml.XmlElement elmRoot,string name,string value)
      {
         System.Xml.XmlElement writeTextNode = default;

         const string FUNC_NAME = "writeNode";
         System.Xml.XmlElement elmNew = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            elmNew = elmRoot.OwnerDocument.CreateElement(name);
            elmRoot.AppendChild(elmNew);
            elmNew.AppendChild(elmNew.OwnerDocument.CreateTextNode(value));
            writeTextNode = elmNew;

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

         elmNew = null;

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            gmRTL.Core.ErrorHandling.RaiseGlobalException(gmRTL.Core.GlobalException.Instance.Number,String.Empty,MOD_NAME + "::" + FUNC_NAME + " line= " + gmRTL.Core.GlobalException.Erl + gmRTL.Core.GlobalException.Instance.Description);
         }

         return writeTextNode;
      }
      public static string getPath(string sFileName)
      {
         string getPath = "";


         int c = 0;
         c = sFileName.InStrRev("\\",-1,StringComparison.CurrentCulture);
         if (c > 0)
         {
            getPath = sFileName.Left(c);
         }

         return getPath;
      }
      public static string filename(ref string filepath)
      {
         string filename = "";


         const string FUNC_NAME = "fileName";
         try
         {
            gmRTL.Core.GlobalException.Clear();

            int c = 0;
            c = filepath.InStrRev("\\",-1,StringComparison.CurrentCulture);
            if (c > 0)
            {
               filepath = filepath.Mid((c + 1));
            }
            c = filepath.InStrRev(".",-1,StringComparison.CurrentCulture);
            if (c > 0)
            {
               filepath = filepath.Left((c - 1));
            }

            filename = filepath;

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

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            gmRTL.Core.ErrorHandling.RaiseGlobalException(gmRTL.Core.GlobalException.Instance.Number,String.Empty,MOD_NAME + "::" + FUNC_NAME + " " + gmRTL.Core.GlobalException.Instance.Description);
         }
         return filename;
      }
      public static System.IO.StreamReader openInputText(string sinpFileName)
      {
         System.IO.StreamReader openInputText = default;

         const string FUNC_NAME = "openInputText";
         try
         {
            gmRTL.Core.GlobalException.Clear();

            openInputText = null;
            openInputText = new System.IO.StreamReader(new System.IO.FileStream(sinpFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite));

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }
         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            gmRTL.Core.ErrorHandling.RaiseGlobalException(gmRTL.Core.GlobalException.Instance.Number,String.Empty,MOD_NAME + "::" + FUNC_NAME + " line= " + gmRTL.Core.GlobalException.Erl + gmRTL.Core.GlobalException.Instance.Description);
         }
         return openInputText;
      }
      public static System.IO.StreamWriter openOutputText(string soutFileName)
      {
         System.IO.StreamWriter openOutputText = default;

         const string FUNC_NAME = "openOutputText";
         try
         {
            gmRTL.Core.GlobalException.Clear();

            openOutputText = null;
            openOutputText = new System.IO.StreamWriter(new System.IO.FileStream(soutFileName,System.IO.FileMode.Create,System.IO.FileAccess.ReadWrite,System.IO.FileShare.ReadWrite));

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }
         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            gmRTL.Core.ErrorHandling.RaiseGlobalException(gmRTL.Core.GlobalException.Instance.Number,String.Empty,MOD_NAME + "::" + FUNC_NAME + " " + gmRTL.Core.GlobalException.Instance.Description);
         }

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


         int i = 0;

         for(i = sSource.Len(); i >= 1; i += -1)
         {
            if (sSource.Mid(i, 1) == sSearch)
            {
               RInStr = i;
               return RInStr;
            }
         }

         RInStr = 0;


         return RInStr;
      }
      public static string Chr1Tran(string sSrc,string sOld,string sNew)
      {
         string Chr1Tran = "";

         int i = 0;
         int l = 0;
         string c = "";

         if (sNew != "")
         {
            if (sOld != sNew && sOld != "")
            {
               i = 1;
               do
               {
                  i = sSrc.InStr(sOld,i,StringComparison.CurrentCulture);
                  if (i == 0)
                  {
                     break;  // Exit Do
                  }
                  sSrc = sSrc.MidCmd(sNew,i,1);
               } while(true);
            }
            Chr1Tran = sSrc;
         }
         else
         {
            string sOutput = "";
            sOutput = "";
            l = sSrc.Len();
            for(i = 1; i <= l; i += 1)
            {
               c = sSrc.Mid(i, 1);
               if (c != sOld)
               {
                  sOutput = sOutput + c;
               }
            }
            Chr1Tran = sOutput;
         }
         return Chr1Tran;
      }
      public static void writeRpt(System.IO.StreamWriter rpt,string str)
      {
         System.Diagnostics.Debug.WriteLine(str);
         rpt.Write(str);
      }
   }
}
