Re: [fpc-pascal] uses in '' relative paths

2020-10-07 Thread Ryan Joseph via fpc-pascal


> On Oct 7, 2020, at 11:02 AM, Mattias Gaertner via fpc-pascal 
>  wrote:
> 
> Only in FPC:
> 
> {$unitpath sources}
> interface
> uses
>  TOMLParser, TOMLTypes;

Some interesting stuff here, thanks. Almost there but for some reason 
Scanner.pas is not found even though it's in the same directory as 
TOMLParser.pas. The compiler did find TOMLParser despite /sources not being 
explicitly included as -Fu option.


unit TOMLParser;
interface
uses
  Classes, 
  Scanner,// error: Can't find unit Scanner used by TOMLParser
  TOMLTypes;

We get to TOMLParser like this:


unit TOML;
interface
uses
  TOMLParser in 'sources/TOMLParser.pas',


Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] uses in '' relative paths

2020-10-07 Thread Mattias Gaertner via fpc-pascal
On Wed, 7 Oct 2020 10:53:16 -0600
Ryan Joseph via fpc-pascal  wrote:

> I'm trying "uses in" which I never knew existed before.
> 
> The idea is that I provide a path to TOML.pas using -Fu then use the
> "in" syntax to reference the units which are in a subdirectory. This
> makes it safe from the project importing the unit so any private
> units in the subdirectory don't override units from the importing
> project.
> 
> unit TOML;
> interface
> uses
>   TOMLParser in '/sources',
>   TOMLTypes in '/sources';

uses
  TOMLParser in 'sources/TOMLParser.pas',
  TOMLTypes in 'sources/TOMLTypes.pas';

Not Delphi compatible.
 
> This doesn't seem to be working however. I was hoping that I could
> then omit an extra -Fu flag to the subdirectory "/sources" thus
> making it easier to include the main unit but maybe that's not how
> the "uses in" syntax works. 

Only in FPC:

{$unitpath sources}
interface
uses
  TOMLParser, TOMLTypes;

Mattias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] uses in '' relative paths

2020-10-07 Thread Ryan Joseph via fpc-pascal
I'm trying "uses in" which I never knew existed before.

The idea is that I provide a path to TOML.pas using -Fu then use the "in" 
syntax to reference the units which are in a subdirectory. This makes it safe 
from the project importing the unit so any private units in the subdirectory 
don't override units from the importing project.

unit TOML;
interface
uses
  TOMLParser in '/sources',
  TOMLTypes in '/sources';

This doesn't seem to be working however. I was hoping that I could then omit an 
extra -Fu flag to the subdirectory "/sources" thus making it easier to include 
the main unit but maybe that's not how the "uses in" syntax works. 

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Adding file to string to the RTL

2020-10-07 Thread Ryan Joseph via fpc-pascal
Here's another interesting option I considered. You can call Scandir which 
returns a record with an enumerator. You can then use this to drop right into 
the for loop. It doesn't allocate memory and you can break the loop to stop the 
iteration. The benefit is you can avoid costly memory allocations for large 
directories but I don't think it's a replacement for a function that returns an 
array of TStringList.

type  
TDirectoryEnumerator = record
  path: string;
  count: integer;
name: string;
info: TSearchRec;
function GetEnumerator: TDirectoryEnumerator;
function MoveNext: boolean;  
property Current: string read name;  
end;

function TDirectoryEnumerator.GetEnumerator: TDirectoryEnumerator;
begin
result := self;
end;

Function TDirectoryEnumerator.MoveNext: Boolean;  
begin  
  if count = 0 then
begin
if FindFirst(path, faAnyFile, info) = 0 then
begin
name := info.name;
inc(count);
result := true;
end
else
result := false;
end
  else if FindNext(info) <> 0 then
begin
FindClose(info);
result := false;
end
else
begin
name := info.name;
inc(count);
result := true;
end;
end;  

function ScanDir(path: string): TDirectoryEnumerator;
begin
if not DirectoryExists(path) then
raise Exception.Create('Directory "'+path+'"" doesn''t exist');
result.path := path+DirectorySeparator+'*';
result.count := 0;
end;



Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Adding file to string to the RTL

2020-10-07 Thread Ryan Joseph via fpc-pascal


> On Oct 7, 2020, at 2:19 AM, Michael Van Canneyt via fpc-pascal 
>  wrote:
> 
> I see Delphi has something similar, so I will add an implementation. But I 
> need to study the options they provide so we can make a reasonably
> compatible version :)

Excellent thanks. It looks like Lazarus has FindAllFiles but it doesn't return 
a dynamic array (the important part so it can be dropped into for..in loops). 
Many scripting languages have a similar function (I can think of C#, PHP, 
Python and Javascript off the top of my head).

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Adding file to string to the RTL

2020-10-07 Thread Michael Van Canneyt via fpc-pascal



On Tue, 6 Oct 2020, Ryan Joseph via fpc-pascal wrote:


Since we're on the topic how about another one-liner for reading all the files 
in directory into a dynamic array? This has the added benefit of getting 
enumeration for free. This is standard stuff for working with files in 
scripting languages so I think the FPC RTL should include something like this 
also.



type
 TStringArray = array of string;

function FindAllFiles(path: string): TStringArray;
var
 info: TSearchRec;
begin
 if not DirectoryExists(path) then
   raise Exception.Create('Directory "'+path+'"" doesn''t exist');
 path := path+DirectorySeparator+'*';
 result := [];
 if FindFirst(path, faAnyFile, info) = 0 then
   begin
 repeat
   result += [info.name];
 until FindNext(info) <> 0;
 FindClose(info);
   end;
end;

begin
 // fastest way to read all text files in a directory.
 // only 2 lines doesn't leak memory
 for name in FindAllFiles('/usr/local/lib/fpc') do
   writeln(GetFileAsString('/usr/local/lib/fpc/'+name));
end.


I see Delphi has something similar, so I will add an implementation. 
But I need to study the options they provide so we can make a reasonably

compatible version :)

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal