Thema: Delphi Dateipfad extrahieren

Einzelnen Beitrag anzeigen

Benutzerbild von implementation
implementation

Registriert seit: 5. Mai 2008
940 Beiträge
 
FreePascal / Lazarus
 
#1

Dateipfad extrahieren

  Alt 10. Mär 2009, 16:40
Ich habe eine Klasse, die einen Dateipfad (inklusive Datei) darstellen soll.
Dazu muss sie natürlich auch die Daten aus einem Windows-Dateipfad extrahieren können.
Delphi-Quellcode:
type
  TProtocol = string[4];
  TRoot = string[16];
  TDir = class
    Name: PascalString;
    Descendant: TObject;
    constructor Create(Name:PascalString; Descendant:TObject);
  end;
  TXFile = record
    Name: PascalString;
    Ext, Pref: PascalString;
    Content: TMemoriaFile;
  end;
  TFile = class
  private
    FWFID: Word;
  protected
    FProtocol: TProtocol;
    FRoot: TRoot;
    FDir: TDir;
    FXFile: TXFile;
    FWebRef: PascalString;
    function WFIDCreate:Word;virtual;
    procedure WFIDFree;virtual;
  public
    constructor Create;
    constructor CreateByWin(WindowsFilePath:PascalString);
    constructor CreateByUnix(UnixFilePath:PascalString);
    destructor Destroy;
  published
    property WFID: Word read FWFID;
  end;

const
  // Dateien
  fpWinFile = 'WINF';
  fpUnixFile= 'UNIX';
  // WWW
  fpHTTP = 'HTTP';
  fpHTTPS = 'HTTS';
  fpFTP = 'FTP-';
  fpFTPS = 'FTPS';
  fpSFTP = 'SFTP';
  fpTFTP = 'TFTP';

[...]

constructor TFile.CreateByWin(WindowsFilePath:PascalString);
var s:PascalString; f:TextFile; c:char; cdir: TDir;
label letter,exit;
begin
  Create;
  Assign(f,GetTempFile('filepath'));
  ReWrite(f);
  Write(f,WindowsFilePath);
  Close(f);
  Reset(f);
  letter:
  Read(f,c);
  while c<>':do begin
    s := s+c;
    Read(f,c);
  end;
  if Length(s)=1 then begin
    FProtocol := fpWinFile;
    FRoot := s;
  end else if s='filethen begin
    FProtocol := fpWinFile;
    Read(f,c);
    Read(f,c);
    goto letter;
  end else if s='httpthen begin
    FProtocol := fpHTTP;
    FWebRef := WindowsFilePath;
    goto exit;
  end else if s='ftpthen begin
    FProtocol := fpFTP;
    FWebRef := WindowsFilePath;
    goto exit;
  end else if s='httpsthen begin
    FProtocol := fpHTTPS;
    FWebRef := WindowsFilePath;
    goto exit;
  end else if s='ftpsthen begin
    FProtocol := fpFTPS;
    FWebRef := WindowsFilePath;
    goto exit;
  end else if s='sftpthen begin
    FProtocol := fpSFTP;
    FWebRef := WindowsFilePath;
    goto exit;
  end else if s='tftpthen begin
    FProtocol := fpTFTP;
    FWebRef := WindowsFilePath;
    goto exit;
  end;
  s := '';
  if not eof(f) then begin
    Read(f,c);
    Read(f,c);
    while (not eof(f)) and (c<>'\') do begin
      s := s+c;
      Read(f,c);
    end;
    if c<>'\then s := s+c;
    FDir := TDir.Create(s,nil);
    cdir := TDir(FDir.Descendant);
  end;
  while (not eof(f)) do begin
    Read(f,c);
    s := '';
    Read(f,c);
    while (not eof(f)) and (c<>'\') do begin
      s := s+c;
      Read(f,c);
    end;
    s := s+c;
    cdir := TDir.Create(s,nil);
    cdir := TDir(cdir.Descendant);
  end;
  Notify(NOT_NOTICE,[],FDir.Name); // PUNKT A
  exit:
  Close(f);
  Erase(f);
end;
Der Aufruf:
TestFile := TFile.CreateByWin('C:\xyz.xyz\x'); Der Konstruktor ist noch nicht fertig, ich mache bis jetzt nur Versuche, um festzustellen, ob er funktioniert.
So, wie er oben steht, funktioniert er auch noch, und liefert (mit Notify) das gewünschte 'xyz.xyz'.
Nur wenn ich an (PUNKT A) nun
   Notify(NOT_NOTICE,[],TDir(FDir.Descendant).Name); schreibe, bekomme ich eine AccessViolation.

1. Frage: Warum?
2. Verbesserungsvorschläge wären hilfreich
Marvin
  Mit Zitat antworten Zitat