Recent

Author Topic: File Handler Beta... A Simple Unit to Make File Handling Easier  (Read 8385 times)

captian jaster

  • Guest
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

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: File Handler Beta... A Simple Unit to Make File Handling Easier
« Reply #1 on: July 02, 2010, 01:02:03 am »
i wonder what do the "returnFileData" function does
Too late to escape fate

eny

  • Hero Member
  • *****
  • Posts: 1618
Re: File Handler Beta... A Simple Unit to Make File Handling Easier
« Reply #2 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' and make as much use of your library as possible.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

captian jaster

  • Guest
Re: File Handler Beta... A Simple Unit to Make File Handling Easier
« Reply #3 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.

Bart

  • Hero Member
  • *****
  • Posts: 4217
    • Bart en Mariska's Webstek
Re: File Handler Beta... A Simple Unit to Make File Handling Easier
« Reply #4 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

bubulemaster

  • New Member
  • *
  • Posts: 44
Re: File Handler Beta... A Simple Unit to Make File Handling Easier
« Reply #5 on: July 09, 2010, 01:30:28 pm »
Good idea.

Where can download it ?

mas steindorff

  • Sr. Member
  • ****
  • Posts: 444
Re: File Handler Beta... A Simple Unit to Make File Handling Easier
« Reply #6 on: July 09, 2010, 08:26:43 pm »
Where can download it ?

it's the zip file under the 1st post
windows 7/10 - laz 2.0 / 1.2.6 general releases

 

TinyPortal © 2005-2018