Recent

Author Topic: for achar := 'A' to 'Z' do  (Read 1607 times)

Jorg3000

  • Jr. Member
  • **
  • Posts: 64
Re: for achar := 'A' to 'Z' do
« Reply #15 on: October 24, 2022, 08:55:27 am »
Hi!
And if you need information about the type of a drive, see ...

Code: Pascal  [Select][+][-]
  1. aType:=Windows.GetDriveType(PChar(aRootPath));

DRIVE_REMOVABLE=2; DRIVE_FIXED=3; DRIVE_REMOTE=4; DRIVE_CDROM=5; DRIVE_RAMDISK=6;

https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdrivetypea
Jörg

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: for achar := 'A' to 'Z' do
« Reply #16 on: October 24, 2022, 09:03:36 am »
4 Definitional conventions
[…]
The characters required to form Pascal programs shall be those implicitly required to form the tokens and separators defined in 6.1.

But that is distinct from your earlier

Quote
6.4.2.2 Required simple-types

This was the point in the discussion a few years ago of a mainframe port: FPC could quite happily have EBCDIC-based string types (i.e. section 6), but changing the compiler itself to use anything other than an ASCII representation for the program (i.e. section 4) would have been decidedly non-trivial.

So a cross-compiler would have been viable, at least for newer variants of the IBM mainframe architecture, but a native compiler much less so.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: for achar := 'A' to 'Z' do
« Reply #17 on: October 24, 2022, 06:43:08 pm »
By the way, the goal is to get all the devices on a Windows system with something like this:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3. {$LONGSTRINGS ON}
  4.  
  5. uses SysUtils, Windows;
  6.  
  7. procedure PrintDrives;
  8. var
  9.   BufSize: DWORD;
  10.   P, Buffer: PUnicodeChar;
  11.   S: UnicodeString;
  12. begin
  13.   BufSize := GetLogicalDriveStringsW(0, nil);
  14.   if BufSize = 0 then
  15.     RaiseLastOSError;
  16.   GetMem(Buffer, BufSize * SizeOf(UnicodeChar));
  17.   try
  18.     if GetLogicalDriveStringsW(BufSize, Buffer) = 0 then
  19.       RaiseLastOSError;
  20.     P := Buffer;
  21.     while P[0] <> #0 do
  22.     begin
  23.       S := P;
  24.       Inc(P, Length(S) + 1);
  25.       Writeln(S);
  26.     end;
  27.   finally
  28.     FreeMem(Buffer);
  29.   end;
  30. end;
  31.  
  32. begin
  33.   PrintDrives;
  34.   Readln;
  35. end.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: for achar := 'A' to 'Z' do
« Reply #18 on: October 24, 2022, 07:52:37 pm »
@Jorge3000 and @ASerge and @Bart: many thanks for that execellent options.

But I forgot to note that I prefer a cross-os code and not use window unit .
So I will stay with one of the options using a loop with if directoryexists((achar + ':\') in it.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

wildfire

  • Full Member
  • ***
  • Posts: 109
Re: for achar := 'A' to 'Z' do
« Reply #19 on: October 24, 2022, 08:12:36 pm »
@Jorge3000 and @ASerge and @Bart: many thanks for that execellent options.

But I forgot to note that I prefer a cross-os code and not use window unit .
So I will stay with one of the options using a loop with if directoryexists((achar + ':\') in it.

Fre;D

Which in itself is not cross OS.
A halo is a mere circle, when does it end?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: for achar := 'A' to 'Z' do
« Reply #20 on: October 24, 2022, 08:37:46 pm »
Which in itself is not cross OS.

I have to agree, since I'm sure that Fred is interested in more than Windows and DOS. But allowing that the other OSes tend to be unix-based, it's difficult to decide on an appropriate alternative unless we try to do something like enumerating / plus any other filesystems that some system component can access without actually mounting it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: for achar := 'A' to 'Z' do
« Reply #21 on: October 24, 2022, 08:45:39 pm »
@Jorge3000 and @ASerge and @Bart: many thanks for that execellent options.

But I forgot to note that I prefer a cross-os code and not use window unit .
So I will stay with one of the options using a loop with if directoryexists((achar + ':\') in it.

Fre;D

Which in itself is not cross OS.

It depend what you call cross OS, the previous code of course will have no result on Unix.
Here part of the code to populate a grid, like a filedialog.  (of course using {$ifdef windows} for the afphabet drivers and {$ifdef unix} for "/" in path will make code lighter but will not generate crash without it.)

Code: Pascal  [Select][+][-]
  1. // this will be for Windows only
  2. {$ifdef windows}  // optional
  3.   for achar := 'A' to 'Z' do
  4.     begin
  5.      if directoryexists(achar + ':\') then
  6.        begin
  7.         Inc(x);
  8.         fo.places.rowcount := x+1;
  9.          fo.places[1][x] := string(achar + ':\');
  10.        end;
  11.    end;
  12.    
  13.     if directoryexists('C:\users') then  // only Windows
  14.     begin
  15.       Inc(x);
  16.       fo.places.rowcount := x+1;
  17.       fo.places[1][x] := string('C:\users');
  18.     end;
  19. {$endif}  
  20.  
  21. {$ifdef unix} // optional
  22.     if directoryexists('/') then // only Unix
  23.     begin
  24.       Inc(x);
  25.       fo.places.rowcount := x+1;
  26.       fo.places[1][x] := string('/');
  27.     end;
  28.     if directoryexists('/usr') then // only unix
  29.     begin
  30.       Inc(x);
  31.       fo.places.rowcount := x+1;
  32.       fo.places[1][x] := string('/usr');
  33.     end;
  34.     {$endif}
  35.  
  36.     if directoryexists(tosysfilepath(sys_getuserhomedir)) then // Windows and Unix
  37.     begin
  38.       Inc(x);
  39.       fo.places.rowcount := x+1;
  40.       fo.places[1][x] := string(tosysfilepath(sys_getuserhomedir));
  41.     end;
  42.     if directoryexists(tosysfilepath(sys_getuserhomedir + directoryseparator + 'Desktop')) then // Windows and Unix
  43.     begin
  44.       Inc(x);
  45.       fo.places.rowcount := x+1;
  46.       fo.places[1][x] := string(tosysfilepath(sys_getuserhomedir + directoryseparator + 'Desktop'));
  47.     end;
  48. ....
  49.  
« Last Edit: October 24, 2022, 09:06:22 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: for achar := 'A' to 'Z' do
« Reply #22 on: October 25, 2022, 07:47:44 am »
@Jorge3000 and @ASerge and @Bart: many thanks for that execellent options.

But I forgot to note that I prefer a cross-os code and not use window unit .
So I will stay with one of the options using a loop with if directoryexists((achar + ':\') in it.

Fre;D

Which in itself is not cross OS.

If you need to work with drive letters you already are not cross platform. And sometimes you need to provide a platform specific view (e.g. displaying the full root on *nix systems, but displaying the drive letters on Windows for the user to find themselves at home).

 

TinyPortal © 2005-2018