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

  1. unit KOLE2Stream;
  2.  
  3. interface
  4. uses SysUtils, Classes, KOLE2Import;
  5.  
  6. type
  7.   TEnumOle2Open = (Ole2_Read, Ole2_Write);
  8.  
  9.   TMsOleDirInfo = record
  10.     Name: Widestring;
  11.     OleType: TMsOleType;
  12.     Size: TMsOlePos;
  13.   end;
  14.  
  15.   TMsOleDirInfoArray = Array of TMsOleDirInfo;
  16.  
  17.   TOle2Storage = class
  18.   private
  19.     Ffs: PMsOle;
  20.     FMode: TEnumOle2Open;
  21.     FPath: Widestring;
  22.   public
  23.     constructor Create(const AFileName: string; const aMode: TEnumOle2Open);
  24.     destructor Destroy;override;
  25.  
  26.     procedure GetDirectories(var DirInfo: TMsOleDirInfoArray);
  27.     procedure CdUp;
  28.     procedure CdDown(const Dir: Widestring; const CreateIfNeeded: boolean);
  29.  
  30.     property Fs: PMsOle read Ffs;
  31.     property Mode: TEnumOle2Open read FMode;
  32.     property Path: Widestring read FPath write FPath;
  33.   end;
  34.  
  35.   TOle2Stream = class (TStream)
  36.   protected
  37.     FStorage: TOle2Storage;
  38.     FStream: PMsOleStream;
  39.   public
  40.     constructor Create(const AStorage: TOle2Storage; const StreamName: Widestring);
  41.     destructor Destroy; override;
  42.     function Read(var Buffer; Count: Longint): Longint; override;
  43.     function Write(const Buffer; Count: Longint): Longint; override;
  44.     function Seek(Offset: Longint; Origin: Word): Longint; override;
  45.   end;
  46.  
  47.   procedure OLE2Check(const Err: TMsOleErr);
  48. implementation
  49.  
  50. resourcestring
  51.   ErrLibOle2='Error in OLE file: ERR_%s';
  52.   Txt_OLE_ERR_OK='OK';
  53.   Txt_OLE_ERR_EXIST='EXIST';
  54.   Txt_OLE_ERR_INVALID='INVALID';
  55.   Txt_OLE_ERR_FORMAT='FORMAT';
  56.   Txt_OLE_ERR_PERM='PERMISSIONS';
  57.   Txt_OLE_ERR_MEM,='MEMORY';
  58.   Txt_OLE_ERR_SPACE='SPACE';
  59.   Txt_OLE_ERR_NOTEMPTY)='NOT EMPTY';
  60.   Txt_OLE_ERR_BADARG='BAD ARGUMENT';
  61.  
  62.   TxtUndefined='UNDEFINED';
  63.  
  64. const
  65.   ArrayErrTxt: array[TMsOleErr] of string= (
  66.     Txt_OLE_ERR_OK,
  67.     Txt_OLE_ERR_EXIST,
  68.     Txt_OLE_ERR_INVALID,
  69.     Txt_OLE_ERR_FORMAT,
  70.     Txt_OLE_ERR_PERM,
  71.     Txt_OLE_ERR_MEM,
  72.     Txt_OLE_ERR_SPACE,
  73.     Txt_OLE_ERR_NOTEMPTY,
  74.     Txt_OLE_ERR_BADARG
  75.   );
  76.  
  77. { TOle2Storage }
  78. procedure TOle2Storage.CdDown(const Dir: Widestring; const CreateIfNeeded: boolean);
  79. begin
  80.   //CreateIfNeeded not used with libole2
  81.   if (Length(Path)>0) and (Path[Length(Path)]=PathDelim) then Path:=Path+Dir
  82.     else Path:=Path+PathDelim+Dir;
  83.  
  84. end;
  85.  
  86. procedure TOle2Storage.CdUp;
  87. begin
  88.   Path:=copy(Path, 1, LastDelimiter(PathDelim,Path)-1);
  89.   if Path='' then Path:=PathDelim;
  90. end;
  91.  
  92. constructor TOle2Storage.Create(const AFileName: string; const aMode: TEnumOle2Open);
  93. begin
  94.   inherited Create;
  95.   if aMode= Ole2_Write then OLE2Check(ms_ole_create(@Ffs, PCHAR(AFileName)))
  96.   else
  97.   if aMode= Ole2_Read then OLE2Check(ms_ole_open(@Ffs, PCHAR(AFileName)));
  98.  
  99.   Path:=PathDelim;
  100.   FMode:=aMode;
  101. end;
  102.  
  103. destructor TOle2Storage.Destroy;
  104. begin
  105.   if Ffs<>nil then ms_ole_destroy(@Ffs);
  106.   inherited;
  107. end;
  108.  
  109. procedure TOle2Storage.GetDirectories(var DirInfo: TMsOleDirInfoArray);
  110. var
  111.   DirStats: TMsOleStat;
  112.   nPath: string;
  113.   Names: PPChar;
  114.   NamesArray: PPointerArray;
  115.   i: integer;
  116. begin
  117.   nPath:=Path;
  118.   OLE2Check(ms_ole_directory(@Names, Ffs, PCHAR(nPath)));
  119.   NamesArray:=PPointerArray(Names);
  120.   try
  121.     SetLength(DirInfo,0);
  122.     while NamesArray[Length(DirInfo)]<>nil do
  123.     begin
  124.       SetLength(DirInfo,Length(DirInfo)+1);
  125.       DirInfo[Length(DirInfo)-1].Name:=PChar(NamesArray[Length(DirInfo)-1]);
  126.       OLE2Check(ms_ole_stat(@(DirStats), Ffs, PCHAR(nPath), NamesArray[Length(DirInfo)-1]));
  127.       DirInfo[Length(DirInfo)-1].OleType:=DirStats.OleType;
  128.       DirInfo[Length(DirInfo)-1].Size:=DirStats.Size;
  129.     end;
  130.   finally
  131.     i:=0;
  132.     while NamesArray[i]<>nil do
  133.     begin
  134.       FreeMemory(NamesArray[i]);
  135.       inc(i);
  136.     end;
  137.     FreeMemory(Names);
  138.   end; //finally
  139.  
  140. end;
  141.  
  142.  
  143. { TOle2Stream }
  144.  
  145. constructor TOle2Stream.Create(const AStorage: TOle2Storage; const StreamName: Widestring);
  146. var
  147.   m:Char;
  148.   nPath, nStreamName: string;
  149. begin
  150.   inherited Create;
  151.   FStorage:= AStorage;
  152.   nPath:=FStorage.Path;nStreamName:=StreamName;
  153.   if Fstorage.Mode=Ole2_Read then m:= 'r' else m:='w';
  154.   OLE2Check(ms_ole_stream_open(@FStream, FStorage.Fs, PCHAR(nPath), PCHAR(nStreamName), m));
  155. end;
  156.  
  157. destructor TOle2Stream.Destroy;
  158. begin
  159.   if FStream<> nil then ms_ole_stream_close(@FStream);
  160.   inherited;
  161. end;
  162.  
  163. function TOle2Stream.Read(var Buffer; Count: Longint): Longint;
  164. begin
  165.   if FStream=nil then begin; Result:=0; exit; end;
  166.   if FStream.read_copy( FStream, @Buffer, Count)<>0 then     //Here 0 is NIL = error
  167.     Result := Count else Result := 0;
  168. end;
  169.  
  170. function TOle2Stream.Write(const Buffer; Count: Longint): Longint;
  171. begin
  172.   if FStream=nil then begin; Result:=0; exit; end;
  173.   if FStream.write( FStream, @Buffer, Count)<>0 then
  174.     Result := Count else Result := 0;
  175. end;
  176.  
  177. function TOle2Stream.Seek(Offset: Longint; Origin: Word): Longint;
  178. var
  179.   oleType: TMsOleSeek;
  180. begin
  181.   if FStream=nil then begin; Result:=-1; exit; end;
  182.   case Origin of
  183.     soFromBeginning: oleType:=MsOleSeekSet;
  184.     soFromCurrent  : oleType:=MsOleSeekCur;
  185.     soFromEnd      : oleType:=MsOleSeekEnd;
  186.     else begin; Result:=-1; exit; end;
  187.   end; //case
  188.  
  189.   Result:= FStream.lseek( FStream, Offset, oleType);
  190. end;
  191.  
  192. procedure OLE2Check(const Err: TMsOleErr);
  193. begin
  194.   case Err of
  195.     MS_OLE_ERR_OK: exit;
  196.         
  197.     MS_OLE_ERR_EXIST,
  198.     MS_OLE_ERR_INVALID,
  199.     MS_OLE_ERR_FORMAT,
  200.     MS_OLE_ERR_PERM,
  201.     MS_OLE_ERR_MEM,
  202.     MS_OLE_ERR_SPACE,
  203.     MS_OLE_ERR_NOTEMPTY,
  204.     MS_OLE_ERR_BADARG: raise Exception.CreateFmt(ErrLibOle2, [ArrayErrTxt[Err]]);
  205.         else raise Exception.CreateFmt(ErrLibOle2, [TxtUndefined]);
  206.   end; //case;
  207. end;
  208.  
  209.  
  210.  
  211. end.
  212.