home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd1.bin / zkuste / delphi / kolekce / d567 / FLEXCEL.ZIP / XLSAdapter / WOLE2Stream.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-10-05  |  4.1 KB  |  149 lines

  1. unit WOLE2Stream;
  2.  
  3. //  This is the windows unit for reading OLE-2 files.
  4. // Uses IStream and IStorage
  5.  
  6. interface
  7. uses Windows, SysUtils, Classes, ActiveX, ComObj, XlsMessages, axctrls;
  8.  
  9. const
  10.   OptionsReadStorage = STGM_DIRECT or STGM_SHARE_EXCLUSIVE or STGM_READ; //Storages should be opened in EXCLUSIVE MODE
  11.   OptionsReadRoot = STGM_DIRECT or STGM_SHARE_DENY_WRITE or STGM_READ;
  12.   OptionsWrite = STGM_DIRECT or STGM_SHARE_EXCLUSIVE or STGM_WRITE;
  13.  
  14.   MsOleStreamT= STGTY_STREAM;
  15.   MsOleStorageT= STGTY_STORAGE;
  16.  
  17. type
  18.   TEnumOle2Open = (Ole2_Read, Ole2_Write);
  19.  
  20.   TMsOleDirInfo= record
  21.     Name: WideString;
  22.     OleType: integer;
  23.     Size: integer;
  24.   end;
  25.  
  26.   TIStorageArray= array of IStorage;
  27.  
  28.   TMsOleDirInfoArray = Array of TMsOleDirInfo;
  29.  
  30.   TOle2Storage = class
  31.   private
  32.     FMode: TEnumOle2Open;
  33.     FStorage: IStorage;
  34.     StorageList: TIStorageArray;
  35.   public
  36.     constructor Create(const AFileName: string; const aMode: TEnumOle2Open);
  37.     destructor Destroy;override;
  38.  
  39.     procedure GetDirectories(var DirInfo: TMsOleDirInfoArray);
  40.     procedure CdUp;
  41.     procedure CdDown(const Dir: Widestring; const CreateIfNeeded: boolean);
  42.  
  43.     property Storage: IStorage read FStorage;
  44.     property Mode: TEnumOle2Open read FMode;
  45.   end;
  46.  
  47.   TOle2Stream = class (TOleStream)
  48.   protected
  49.     FStorage: TOle2Storage;
  50.   public
  51.     constructor Create(const AStorage: TOle2Storage; const StreamName: Widestring);
  52.   end;
  53.  
  54. implementation
  55.  
  56. { TOle2Storage }
  57. procedure TOle2Storage.CdDown(const Dir: Widestring; const CreateIfNeeded: boolean);
  58. begin
  59.   SetLength(StorageList, Length(StorageList)+1);
  60.   StorageList[Length(StorageList)-1]:=FStorage;
  61.   if (FMode= Ole2_Write) then
  62.     OleCheck(StorageList[Length(StorageList)-1].CreateStorage(PWideChar(Dir), OptionsWrite, 0, 0, FStorage))
  63.   else
  64.     OleCheck(StorageList[Length(StorageList)-1].OpenStorage(PWideChar(Dir), nil, OptionsReadStorage, nil, 0, FStorage));
  65. end;
  66. procedure TOle2Storage.CdUp;
  67. begin
  68.   FStorage:=StorageList[Length(StorageList)-1];
  69.   SetLength(StorageList, Length(StorageList)-1);
  70. end;
  71.  
  72. constructor TOle2Storage.Create(const AFileName: string; const aMode: TEnumOle2Open);
  73. var
  74.   WideFileName: Widestring;
  75. begin
  76.   inherited Create;
  77.   WideFileName:=AFileName;
  78.   if aMode= Ole2_Write then
  79.     OleCheck(StgCreateDocfile(PWideChar(WideFileName), OptionsWrite, 0, FStorage))
  80.  
  81.   else if aMode= Ole2_Read then
  82.   begin
  83.     if StgIsStorageFile(PWideChar(WideFileName)) <> S_OK then
  84.       raise Exception.CreateFmt(ErrFileIsNotXLS, [WideFileName]);
  85.  
  86.     OleCheck(StgOpenStorage(PWideChar(WideFileName), nil, OptionsReadRoot, nil, 0, FStorage));
  87.   end;
  88.  
  89.   FMode:=aMode;
  90.   SetLength(StorageList,0);
  91. end;
  92.  
  93. destructor TOle2Storage.Destroy;
  94. begin
  95.   inherited;
  96. end;
  97.  
  98. procedure TOle2Storage.GetDirectories(var DirInfo: TMsOleDirInfoArray);
  99. var
  100.   Enum: IEnumStatStg;
  101.   NumFetched: integer;
  102.   StatStg: TStatStg;
  103.   Malloc: IMalloc;
  104.  
  105. begin
  106.   SetLength(DirInfo, 0);
  107.   OleCheck(CoGetMalloc(1, Malloc));
  108.   if FStorage.EnumElements(0, nil, 0, Enum) <> S_OK then
  109.   begin
  110.     FStorage.Stat(StatStg, 0);
  111.     try
  112.       raise Exception.CreateFmt(ErrCantReadFile, [StatStg.pwcsName]);
  113.     finally
  114.       Malloc.Free(StatStg.pwcsName);
  115.     end; //finally
  116.   end;
  117.  
  118.   while Enum.Next(1, StatStg, @NumFetched) = S_OK do
  119.   begin
  120.     try
  121.       SetLength(DirInfo, Length(DirInfo)+1);
  122.       DirInfo[Length(DirInfo)-1].Name:= StatStg.pwcsName;
  123.       DirInfo[Length(DirInfo)-1].OleType:= StatStg.dwType;
  124.       DirInfo[Length(DirInfo)-1].Size:= StatStg.cbSize;
  125.     finally
  126.        Malloc.Free(StatStg.pwcsName);
  127.     end; //finally
  128.   end; //while
  129. end;
  130.  
  131.  
  132. { TOle2Stream }
  133.  
  134. constructor TOle2Stream.Create(const AStorage: TOle2Storage; const StreamName: Widestring);
  135. var
  136.   aStream: IStream;
  137. begin
  138.   if AStorage.Mode=Ole2_Read then
  139.     OleCheck(AStorage.Storage.OpenStream( PWideChar(StreamName), nil, OptionsReadStorage, 0, aStream))
  140.   else
  141.     OleCheck(AStorage.Storage.CreateStream( PWideChar(StreamName), OptionsWrite, 0, 0, aStream));
  142.  
  143.   inherited Create(aStream);
  144.   FStorage:= AStorage;
  145. end;
  146.  
  147.  
  148. end.
  149.