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

  1. //--------------------------------------------------------------------------
  2. // Object Scripting
  3. // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
  4. //
  5. // FILE.SPP: File Classes. Includes configuration file management.
  6. //
  7. // FILES: MSG.SPP
  8. //--------------------------------------------------------------------------
  9. print typeid(module());
  10.  
  11. //
  12. // IDE imports.
  13. //
  14. import scriptEngine;
  15.  
  16. //
  17. // Load support module(s).
  18. //
  19. if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg");
  20.  
  21. //
  22. // Windows imports.
  23. //
  24. import "mmsystem.dll"{
  25.   bool   PlaySound       (char *, int, long); 
  26. }
  27.  
  28. //
  29. // Constants for PlaySound.
  30. //
  31. //#define SND_SYNC         0000  /* play synchronously (default) */
  32. #define SND_ASYNC        0001  /* play asynchronously */
  33. //#define SND_NODEFAULT    0002  /* silence (!default) if sound not found */
  34. //#define SND_MEMORY       0004  /* pszSound points to a memory file */
  35. //#define SND_LOOP         0008  /* loop the sound until next sndPlaySound */
  36. //#define SND_NOSTOP       0010  /* don't stop any currently playing sound */
  37. //#define SND_NOWAIT       00002000 /* don't wait if the driver is busy */
  38. //#define SND_ALIAS        00010000 /* name is a registry alias */
  39. //#define SND_ALIAS_ID     00110000 /* alias is a predefined ID */
  40. #define SND_FILENAME     00020000 /* name is file name */
  41. //#define SND_RESOURCE     00040004 /* name is resource name or atom */
  42. //#define SND_PURGE        0040  /* purge non-static events for task */
  43. //#define SND_APPLICATION  0080  /* look for app-specific association */
  44.  
  45. //
  46. // class TFile
  47. // ~~~~~ ~~~~~
  48. // Base class.
  49. //
  50. class TFile(fileName) {
  51.   declare FileName = fileName;
  52.  
  53.   //
  54.   // Returns TRUE is the file exists; FALSE otherwise.
  55.   //
  56.   IsValid()
  57.   {
  58.     return FileExists(FileName) ? TRUE : FALSE;
  59.   }
  60. }
  61.  
  62. //
  63. // class TFlatFile
  64. // ~~~~~ ~~~~~~~~~
  65. // TFlatFile encapsulates functionality for files placed in a private IDE
  66. // edit buffer.
  67. //
  68. class TFlatFile(fileName) : TFile(fileName) {
  69.   declare EdBuf;
  70.   declare EdPos;
  71.   declare EdBlk;
  72.  
  73.   CreateEditObjs(TRUE, FALSE);
  74.  
  75.   //
  76.   // Instantiates a buffer and other objects for the file.
  77.   //
  78.   CreateEditObjs(private, readOnly)
  79.   {
  80.     EdBuf = new EditBuffer(FileName, private, readOnly);
  81.     EdPos = new EditPosition(EdBuf);
  82.     EdBlk = new EditBlock(EdBuf);
  83.   }
  84.  
  85.   //
  86.   // Returns an array of strings representing each line of the file. Passing
  87.   // the string "containing" returns only those lines with that string in
  88.   // them. Removes any newline and return characters by default. Pass TRUE
  89.   // for "suppressTrim" to prevent this.
  90.   //
  91.   GetLines(containing, suppressTrim)
  92.   {
  93.     declare lines = new array[];
  94.     declare line = new String();
  95.  
  96.     declare i = 0;
  97.     declare row = 1;
  98.     do {
  99.       EdPos.Move(row, 1);
  100.       line.Text = EdPos.Read();
  101.       if (containing == "" || line.Index(containing)) {
  102.         if (!suppressTrim) line = line.Trim();
  103.         lines[i] = line.Text;
  104.         i++;
  105.       }
  106.       row++;
  107.     } while (row - 1 < EdPos.LastRow);
  108.  
  109.     return lines;
  110.   }
  111.  
  112.   //
  113.   // Returns the first text found between two given markers. If useEOF is set
  114.   // TRUE, EOF will be used if the second marker is not found. Returns NULL
  115.   // on failure.
  116.   //
  117.   GetSandwichedText(marker1, marker2, useEOF)
  118.   {
  119.     if (!EdPos.Search(marker1, TRUE, FALSE)) {
  120.       return NULL;
  121.     }
  122.     EdPos.MoveRelative(1, 0);
  123.     EdPos.MoveBOL();
  124.     EdBlk.Begin();
  125.     if (!EdPos.Search(marker2, TRUE, FALSE) && useEOF) {
  126.       EdPos.MoveEOF();
  127.     }
  128.     EdBlk.End();
  129.     return EdBlk.Text;
  130.   }
  131.  
  132.   //
  133.   // Deletes the contents of the file. Returns TRUE on success; false
  134.   // otherwise.
  135.   //
  136.   Delete()
  137.   {
  138.     if (!EdPos.Move(1, 1)) return FALSE;
  139.     EdBlk.Begin();
  140.     if (!EdPos.MoveEOF()) return FALSE;
  141.     EdBlk.End();
  142.     return EdBlk.Delete();
  143.   }
  144.  
  145.   //
  146.   // Appends the given text to the end of the buffer. Adds a \n\r sequence if
  147.   // EOF is not column one.
  148.   //
  149.   Append(text)
  150.   {
  151.     EdPos.MoveEOF();
  152.     if (EdPos.Column != 1) EdPos.InsertText("\n\r");
  153.     EdPos.InsertText(text);
  154.   }
  155.  
  156.   //
  157.   // Closes the buffer opened for the file. Pass TRUE to save changes
  158.   // made to the buffer.
  159.   //
  160.   Close(save)
  161.   {
  162.     if (save) EdBuf.Save();
  163.     EdBuf.Destroy();
  164.   }
  165.  
  166.   //
  167.   // Destructor.
  168.   //
  169.   ~TFlatFile()
  170.   {
  171.     Close();
  172.   }
  173. }
  174.  
  175. //
  176. // class TConfigFile
  177. // ~~~~~ ~~~~~~~~~~~
  178. // TConfigFile encapsulates functionality for configuration files.
  179. //
  180. class TConfigFile(fileName) : TFlatFile(fileName) {
  181.  
  182.   //
  183.   // Returns a string representing the value of the given key or a
  184.   // default value if the key is not found. Treats ";" as comment
  185.   // character, which must be in column 1.
  186.   //
  187.   GetValue(key, defaultVal)
  188.   {
  189.     declare sTmp = new String();
  190.     if (EdPos.Search(key, TRUE, FALSE)) {
  191.       EdPos.MoveBOL();
  192.       if (EdPos.Character == 59) {  // Check for comment indicator.
  193.         EdPos.MoveEOL();
  194.         return GetValue(key, defaultVal);  // Recursively look elsewhere.
  195.       }
  196.       sTmp.Text = EdPos.Read();
  197.       declare position = sTmp.Index("=");
  198.       if (!position) {  // No equals character.
  199.         return defaultVal;
  200.       }
  201.       sTmp = sTmp.SubString(position);
  202.       sTmp = sTmp.Trim();  // Remove trailing \r\n.
  203.       return sTmp.Text;
  204.     }
  205.     return defaultVal;
  206.   }
  207.  
  208.   //
  209.   // Writes the specified value for the specified key to the file.
  210.   // Treats ";" as comment character, which must be in column 1.
  211.   // Adds the key if not found. Returns TRUE on success.
  212.   //
  213.   WriteValue(key, value)
  214.   {
  215.     // Find existing key.
  216.     //
  217.     if (EdPos.Search(key, TRUE, FALSE)) {
  218.       EdPos.MoveBOL();
  219.       if (EdPos.Character == 59) {  // Check for comment indicator.
  220.         EdPos.MoveEOL();
  221.         return WriteValue(key, value);  // Recursively look for key elsewhere.
  222.       }
  223.  
  224.       // Add the value.
  225.       //
  226.       declare EdBlk = new EditBlock(EdBuf);
  227.       EdPos.Search("=", FALSE, FALSE);
  228.       EdBlk.Begin();
  229.       EdPos.MoveEOL();
  230.       EdBlk.End();
  231.       EdBlk.Delete();
  232.       EdPos.MoveEOL();
  233.       EdPos.InsertText(value);
  234.       return TRUE;
  235.     }
  236.  
  237.     // Key not found. Add the key and the value.
  238.     //
  239.     else {
  240.       EdPos.MoveEOF();
  241.       EdPos.InsertText("\n\r" + key + "=" + value);
  242.       return TRUE;
  243.     }
  244.   }
  245. }
  246.  
  247. //
  248. // class TWAVFile
  249. // ~~~~~ ~~~~~~~~~
  250. // TWAVFile encapsulates functionality for WAV files.
  251. //
  252. class TWAVFile(fileName) : TFile(fileName) {
  253.  
  254.   //
  255.   // Plays the file.
  256.   //
  257.   Play()
  258.   {
  259.     PlaySound(FileName, NULL, SND_FILENAME | SND_ASYNC);
  260.   }
  261. }
  262.