home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / SCRPTEXM.PAK / LONGLINE.SPP < prev    next >
Encoding:
Text File  |  1997-05-06  |  3.2 KB  |  125 lines

  1. //--------------------------------------------------------------------------
  2. // Object Scripting
  3. // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
  4. //
  5. // LONGLINE.SPP: Long Line Finder. Searches all .C, .H, .CPP, .HPP, and .SPP
  6. //   files in the specified directory and reports all lines that are longer
  7. //   than a given threshold value to the message database. Double-click a
  8. //   message to edit the referenced file.
  9. //
  10. // USE: Run script, and enter the line length threshold and search directory.
  11. //
  12. // FILES: FILE.SPP, MSG.SPP
  13. //--------------------------------------------------------------------------
  14. print typeid(module());
  15.  
  16. //
  17. // IDE import(s).
  18. //
  19. import scriptEngine;
  20. import IDE;
  21.  
  22. //
  23. // Load support module(s).
  24. //
  25. if (!scriptEngine.IsLoaded("file")) scriptEngine.Load("file");
  26. if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg");
  27.  
  28. //
  29. // Constant for the default line length threshold.
  30. //
  31. #define DEFAULT_THRESHOLD  "78"
  32.  
  33. longline()
  34. {
  35.   declare msg = new TMsg();
  36.  
  37.   // Get the threshold line length.
  38.   //
  39.   declare limit;
  40.   limit = IDE.SimpleDialog("Enter the threshold line length.", DEFAULT_THRESHOLD);
  41.   if (limit == "") return;
  42.  
  43.   // Get the search directory.
  44.   //
  45.   declare dir;
  46.   dir = IDE.SimpleDialog("Enter the search directory.", IDE.CurrentDirectory);
  47.   if (dir == "") return;
  48.  
  49.   // Walk the directory, searching files that are source.
  50.   //
  51.   IDE.StartWaitCursor();
  52.   declare gotOne = FALSE;
  53.   declare file = FindFirstFile(dir + "\\*.*");
  54.   while (file != "") {
  55.     if (IsSource(file)) {
  56.       if (CheckLongLines(dir + "\\" + file, limit)) {
  57.         gotOne = TRUE;
  58.       }
  59.     }
  60.     file = FindNextFile();
  61.   }
  62.   IDE.EndWaitCursor();
  63.  
  64.   // Show the Longline page of the message MPD if something to report.
  65.   //
  66.   if (gotOne) {
  67.     IDE.ViewMessage("Longline");
  68.   }
  69.   else {
  70.     msg.Info("No lines exceeding threshold found.");
  71.   }
  72. }
  73.  
  74. //
  75. // Returns TRUE if the filename passed is .C, .H, .CPP, .HPP, or .SPP;
  76. // FALSE otherwise.
  77. //
  78. IsSource(file)
  79. {
  80.   declare sTmp = new String(file);
  81.   sTmp = sTmp.Lower();
  82.  
  83.   // Look for extension as last characters of the string.
  84.   //
  85.   if (sTmp.Index(".c") + 1 == sTmp.Length ||
  86.       sTmp.Index(".h") + 1 == sTmp.Length ||
  87.       sTmp.Index(".cpp") + 3 == sTmp.Length ||
  88.       sTmp.Index(".hpp") + 3 == sTmp.Length ||
  89.       sTmp.Index(".spp") + 3 == sTmp.Length) {
  90.     return TRUE;
  91.   }
  92.   return FALSE;
  93. }
  94.  
  95. //
  96. // Searches the file for lines exceeding the threshold length, and
  97. // reports them to the message database. Returns TRUE if at least one
  98. // violation is found.
  99. //
  100. CheckLongLines(file, threshold)
  101. {
  102.   declare checkFile = new TFlatFile(file);
  103.   declare sLine = new String();
  104.   declare gotOne = FALSE;
  105.   declare length;
  106.   for (declare i = 0; i < checkFile.EdPos.LastRow; i++) {
  107.     checkFile.EdPos.Move(i + 1, 1);
  108.     sLine.Text = checkFile.EdPos.Read();
  109.     length = sLine.Length;
  110.     if (sLine.Index("\r\n")) length -= 2;  // Account for new line characters.
  111.     if (length > threshold) {
  112.       IDE.MessageCreate("Longline", "", INFORMATION, 0, file, i + 1, 1,
  113.                         "  " + length, NULL, NULL);
  114.       gotOne = TRUE;
  115.     }
  116.   }
  117.   checkFile.Close();
  118.   return gotOne;
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.