Recent

Author Topic: Improvements to SysGetEnvironmentList implementations  (Read 630 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 390
Improvements to SysGetEnvironmentList implementations
« on: August 08, 2023, 11:57:03 am »
packages/fcl-base/src/custapp.pp has the following procedure
Code: Pascal  [Select][+][-]
  1. Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  2.  
  3. var
  4.    s : string;
  5.    i,j,count : longint;
  6.  
  7. begin
  8.   count:=GetEnvironmentVariableCount;
  9.   for j:=1 to count  do
  10.     begin
  11.     s:=GetEnvironmentString(j);
  12.     If NamesOnly then
  13.       begin
  14.       I:=pos('=',s);
  15.       If (I>1) then
  16.         S:=Copy(S,1,I-1);
  17.       end;
  18.     List.Add(S);
  19.     end;
  20. end;

S:=Copy(S,1,I-1); has been replaced with SetLength(S,I-1);
After that, variables have been changed from longints to integers and named to uppercase in order to mimic the variable names of the other routines found in the same unit. Added and removed some spaces, too.
After that I've searched for Procedure SysGetEnvironmentList in the fpc directory in order to make sure the modified code looks like other procedures. So, I've had to replace S:=Copy(S,1,I-1); with S:=SetLength(S,I-1); in other routines, too. In addition to that, the OS2 version of SysGetEnvironmentList had a bug: the S variable was untyped.
The netwlibc version had the If (I>0) then condition instead of If (I>1) then. I've changed it to match all other implementations. It's this modification that I'd like FPC developers to double check. If NamesOnly was true, for a string like "=value", the original code would have considered an empty('') string as the name, in contradiction with the other implementations which would consider the entire "=value" string as a name. In this scenario, the modified code considers the entire "=value" line as the string name, as it is considered in all other implementations. I'd like FPC developers to make sure that this is the right behaviour.
Now all of them should look alike. Have a look and test.
I attach a patch with the following content:
Code: Pascal  [Select][+][-]
  1. diff --git a/packages/fcl-base/src/custapp.pp b/packages/fcl-base/src/custapp.pp
  2. index 45a37c8854..abc36a4ff4 100644
  3. --- a/packages/fcl-base/src/custapp.pp
  4. +++ b/packages/fcl-base/src/custapp.pp
  5. @@ -183,22 +183,22 @@ function TCustomApplication.GetExeName: string;
  6.  end;
  7.  {$endif darwin}
  8.  
  9. -Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  10. +Procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);
  11.  
  12.  var
  13. -   s : string;
  14. -   i,j,count : longint;
  15. +   S : String;
  16. +   I,J,Count : Integer;
  17.  
  18.  begin
  19. -  count:=GetEnvironmentVariableCount;
  20. -  for j:=1 to count  do
  21. +  Count:=GetEnvironmentVariableCount;
  22. +  for J:=1 to Count do
  23.      begin
  24. -    s:=GetEnvironmentString(j);
  25. +    S:=GetEnvironmentString(J);
  26.      If NamesOnly then
  27.        begin
  28. -      I:=pos('=',s);
  29. +      I:=Pos('=',S);
  30.        If (I>1) then
  31. -        S:=Copy(S,1,I-1);
  32. +        SetLength(S,I-1);
  33.        end;
  34.      List.Add(S);
  35.      end;
  36. diff --git a/packages/fcl-base/src/go32v2/custapp.inc b/packages/fcl-base/src/go32v2/custapp.inc
  37. index d684059fb9..9ef83aff17 100644
  38. --- a/packages/fcl-base/src/go32v2/custapp.inc
  39. +++ b/packages/fcl-base/src/go32v2/custapp.inc
  40. @@ -13,7 +13,7 @@
  41.  
  42.   **********************************************************************}
  43.  
  44. -Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  45. +Procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);
  46.  
  47.  Var
  48.    P : PPAnsiChar;
  49. @@ -31,7 +31,7 @@
  50.          begin
  51.          I:=Pos('=',S);
  52.          If (I>1) then
  53. -          S:=Copy(S,1,I-1);
  54. +          SetLength(S,I-1);
  55.          end;
  56.        List.Add(S);
  57.        Inc(P);
  58. diff --git a/packages/fcl-base/src/netwlibc/custapp.inc b/packages/fcl-base/src/netwlibc/custapp.inc
  59. index 9911f3ed74..5bb53d8523 100644
  60. --- a/packages/fcl-base/src/netwlibc/custapp.inc
  61. +++ b/packages/fcl-base/src/netwlibc/custapp.inc
  62. @@ -13,7 +13,7 @@
  63.  
  64.   **********************************************************************}
  65.  
  66. -Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  67. +Procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);
  68.  
  69.  Var
  70.    P : PPAnsiChar;
  71. @@ -30,8 +30,8 @@
  72.        If NamesOnly then
  73.          begin
  74.          I:=Pos('=',S);
  75. -        If (I>0) then
  76. -          S:=Copy(S,1,I-1);
  77. +        If (I>1) then
  78. +          SetLength(S,I-1);
  79.          end;
  80.        List.Add(S);
  81.        Inc(P);
  82. diff --git a/packages/fcl-base/src/os2/custapp.inc b/packages/fcl-base/src/os2/custapp.inc
  83. index 77224ca05f..a2561b383b 100644
  84. --- a/packages/fcl-base/src/os2/custapp.inc
  85. +++ b/packages/fcl-base/src/os2/custapp.inc
  86. @@ -30,10 +30,10 @@ function EnvStr (Index: longint): ;
  87.  end;
  88.  
  89.  
  90. -procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  91. +procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);
  92.  
  93.  Var
  94. -  S : ;
  95. +  S : String;
  96.    J,I : Integer;
  97.  
  98.  begin
  99. @@ -45,7 +45,7 @@ procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);
  100.        begin
  101.        I:=Pos('=',S);
  102.        If (I>1) then
  103. -        S:=Copy(S,1,I-1);
  104. +        SetLength(S,I-1);
  105.        end;
  106.      List.Add(S);
  107.      end;


 

TinyPortal © 2005-2018