Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
int ServiceMethod_EditSource(int fileRoot)
{

   return 0; // off for now
   
   
   tInfoFile   fileInfo;
   Handle      fileStream;
   int         rai; 
   int         length;
   string      record;  
   int         nRecord;
   int         curRecord;
   int         iPos;
   int         iEnd;

   fileInfo = Store.GetVector(fileRoot);
   if(fileInfo.rawtext == 0) return 0;
   string fileName = Store.GetName(fileRoot)
   System.LogMessage("filename = " +  fileName);
   
   fileStream = Text.Open(Store.GetHandle(),fileInfo.rawtext);
   nRecord = Text.Maximum(fileStream);
   curRecord = 0;
   while(curRecord < nRecord)
   {
      // look for #include statements to be inlined to the code
      
      curRecord = curRecord + 1;
      Text.Position(fileStream,curRecord);
      record = Text.Access(fileStream, length, rai, 0);
      iPos = Character.FindFirst(record, 0, "<!-- #include file=");
      if(iPos == 0) continue;
      
      if(Select.Progress) System.LogMessage("INLINE " + record  + ">" + Store.GetIdent(fileRoot) + " @" + curRecord);

      // find the include file   
      
      string includePath = "C:\gmSpec\ASP\ScriptTagInclude\TestSite2\include.inc";
      int incRoot = System.LoadFile(includePath,1);
      if (incRoot == 0) return 0;

      // remove the include statement
      
      Text.Position(fileStream,curRecord);
      Text.Delete(fileStream);
      Text.Position(fileStream,curRecord - 1);

      // copy records from the include into the file text buffer

      Handle incStream = Text.Open(Store.GetHandle(),incRoot);
      int incRecords = Text.Maximum(incStream);
      int incRecord = 0;      
      for(incRecord = 1; incRecord <= incRecords; incRecord = incRecord + 1)
      {
         Text.Position(incStream,incRecord);
         string incText = Text.Access(incStream, length, rai, 0);
         Text.Insert(fileStream, incText);
      }
      Text.Close(incStream);      
   }
   Text.Close(fileStream);
   return 1;
}

Another sample

Code Block
I want to to apply an edit to every file in an ASP site.  The edit should simplify script tags that contain "clientEventHandlersVBS". 

<script id=clientEventHandlersVBS LANGUAGE="javascript">
<SCRIPT ID=clientEventHandlersVBS LANGUAGE="javascript" defer>
<script id=clientEventHandlersVBS LANGUAGE="VBscript">
<SCRIPT ID=clientEventHandlersVBS LANGUAGE="VBscript" defer>

Also if it was a vbscript tag, insert a comment above.
Code Block
int ServiceMethod_EditSource(int fileRoot)
{
    
   tInfoFile   fileInfo;
   Handle      fileStream;
   int         rai;
   int         length;
   string      record; 
   int         nRecord;
   int         curRecord;
   int         iPos;
   int         iEnd;
   string      lang;
 
   fileInfo = Store.GetVector(fileRoot);
   if(fileInfo.rawtext == 0) return 0;
   string fileName = Store.GetName(fileRoot)
   
   //return 0; // off for now
   
   fileStream = Text.Open(Store.GetHandle(),fileInfo.rawtext);
   nRecord = Text.Maximum(fileStream);
   curRecord = 0;
   while(curRecord < nRecord)
   {
      // look for <script statements with clientEventHandlersVBS (these are all client-side)
       
      curRecord = curRecord + 1;
      Text.Position(fileStream,curRecord);
      record = Text.Access(fileStream, length, rai, 0);
      iPos = Character.FindFirst(record, 0, "<script ");
      if(iPos == 0) continue;
      iPos = Character.FindFirst(record, 0, "clientEventHandlersVBS");
      if(iPos == 0) continue;
      iPos = Character.FindFirst(record, 0, "javascript");
      if(iPos == 0) lang = "vbscript";
      else lang = "javascript";
      
      // Replace script tag with a simplified one that
      // has neither id nor defer attribute.  
      // Also insert a comment before the vbscript tags
      // to assist with review.

      Text.Position(fileStream,curRecord);
      Text.Delete(fileStream);
      Text.Position(fileStream,curRecord-1);
      Text.Insert(fileStream, "<script language='"+lang+"'>");
      if (lang == "vbscript")
      {
         System.LogMessage("updated " + filename + "@" + curRecord);
         Text.Position(fileStream,curRecord-1);
         Text.Insert(fileStream, "<!-- UPGRADE_INFO: VBStoJVS -->");
      }
   }
   Text.Close(fileStream);
   return 1;
}