Lazarus

Announcements => Third party => Topic started by: captian jaster on July 01, 2010, 04:01:12 pm

Title: File Handler Beta... A Simple Unit to Make File Handling Easier
Post by: captian jaster on July 01, 2010, 04:01:12 pm
So i made a new unit.. Figured id release it.
It simplifies File Use and if you happen to notice some Random Types. i was toying around... SO it has the Following Functions\Procedures
Code: Pascal  [Select][+][-]
  1. Function CountFilesInDir(const Dir:String):LongInt;//Beta
  2. Procedure ReturnFileInfo(Var FT:TFileThingsREC; const FPath:String);
  3. Function ReturnFileAge(const FPath:String):TDateTime;//Beta
  4. Function FileAttrIs(const FPath:String):TFileAttr;//Beta
  5. Function CountFileLines(const FPath:String):LongInt;
  6. Procedure EditFile(const FPath:String; Data:String);
  7. Procedure MakeFile(const FPath:String; Data:String);
  8. Function ReturnFileData(const FPAth:String):DataHolder;
  9. Function FileIsNil(const FPath:String):Boolean;
  10.  
So.... Here it is
Title: Re: File Handler Beta... A Simple Unit to Make File Handling Easier
Post by: hinst on July 02, 2010, 01:02:03 am
i wonder what do the "returnFileData" function does
Title: Re: File Handler Beta... A Simple Unit to Make File Handling Easier
Post by: eny on July 02, 2010, 08:34:36 pm
i wonder what do the "returnFileData" function does
Supposedley it returns data from a file  :D

@CJ: now 'eat your own dogfood (http://en.wikipedia.org/wiki/Eating_your_own_dog_food)' and make as much use of your library as possible.
Title: Re: File Handler Beta... A Simple Unit to Make File Handling Easier
Post by: captian jaster on July 02, 2010, 09:33:58 pm
i wonder what do the "returnFileData" function does
It returns Data From a file Line By Line.
@eny... i still have allot of improvements to do on the Handler
If you have any ideas just tell me.. K.
Title: Re: File Handler Beta... A Simple Unit to Make File Handling Easier
Post by: Bart on July 03, 2010, 12:33:43 am
Code: [Select]
Function FileAttrIs(const FPath:String):TFileAttr;
Var
 F:LongInt;
begin
  F := FileGetAttr(FPath);
  IF(F<>-1)Then
  begin
    IF(F AND FaReadOnly)<>0Then Result := ReadOnly;
    IF(F AND FaSysFile)<>0Then Result := SysFile;
    IF(F AND FaVolumeID)<>0Then Result := DLabel;
    IF(F AND FaDirectory)<>0Then Result := Dir;
    IF(F AND FaHidden)<>0Then Result := Hidden;
    IF(F AND FaArchive)<>0Then Result := Archive;
  end Else Result := FError;
end;

And what about files that are read-only, hidden, archived and have the systemattribute set?

Your function will return Archive  :o

Also for a filename with no attribute set the function will return FError.

Code: [Select]
Function ReturnFileData(const FPAth:String):DataHolder;
Var FileVar:TextFile;
begin
  AssignFile(FileVar,FPath);
  Reset(FileVar);
  Repeat
    Readln(FileVar,Result);
  until(EoF(FileVar));
  CloseFile(FileVar);
end;

Result will always hold the last line of the textfile.
Probably not what you want?

Code: [Select]
Function CountFileLines(const FPath:String):LongInt;
Var
 FileVar:TextFile;
 Counter:LongInt;
 ReadStr:DataHolder;
begin
  Counter := 0;
  AssignFile(FileVar,FPath);
  Reset(FileVar);
  Repeat
  Readln(FileVar,ReadStr);
  Inc(Counter);
  until(EoF(FileVar));
  CloseFile(FileVar);
  Result := Counter;
end;

The result will be 1 for an empty file. Doesn't sound right to me.
Hint: change the "repeat until" loop to a "while do" loop.

In general.
Be aware that in none of your functions/procedures do you handle file IO errors.

For better readability you might want to indent the repeat/until and while/do loops.

Bart
Title: Re: File Handler Beta... A Simple Unit to Make File Handling Easier
Post by: bubulemaster on July 09, 2010, 01:30:28 pm
Good idea.

Where can download it ?
Title: Re: File Handler Beta... A Simple Unit to Make File Handling Easier
Post by: mas steindorff on July 09, 2010, 08:26:43 pm
Where can download it ?

it's the zip file under the 1st post
TinyPortal © 2005-2018