2
0

unixutil.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2013 by the Free Pascal development team
  4. DO NOT ADD ROUTINES TO THIS FILE!
  5. THE ROUTINES IN THIS FILE ARE INTERNAL AND NOT FOR END USER USAGE!
  6. Background: This unit contains leftovers from the unix restructure that
  7. shouldn't be in the interface of unit baseunix/unix, but are needed
  8. in these units. (at the time routines were still being moved
  9. from baseunix to unix, and unit baseunix couldn't depend on unix)
  10. The routines are fairly OS independent but can't move to
  11. OS independent because the lowlevel units baseunix/unix depend
  12. on them. If they need to be generally accessable, copy these
  13. functions to a general purpose, OS independent, supportable unit.
  14. See the file COPYING.FPC, included in this distribution,
  15. for details about the copyright.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. **********************************************************************}
  20. unit unixutil;
  21. interface
  22. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  23. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  24. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  25. implementation
  26. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  27. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  28. // Note: for internal use by skilled programmers only
  29. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  30. var p : ppchar;
  31. i : LongInt;
  32. begin
  33. if High(s)<Low(s) Then Exit(NIL);
  34. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  35. // for cmd
  36. if p=nil then
  37. begin
  38. {$ifdef xunix}
  39. fpseterrno(ESysEnomem);
  40. {$endif}
  41. exit(NIL);
  42. end;
  43. for i:=low(s) to high(s) do
  44. p[i+Reserveentries]:=pchar(s[i]);
  45. p[high(s)+1+Reserveentries]:=nil;
  46. ArrayStringToPPchar:=p;
  47. end;
  48. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  49. {
  50. Create a PPChar to structure of pchars which are the arguments specified
  51. in the string S. Especially useful for creating an ArgV for Exec-calls
  52. }
  53. begin
  54. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  55. end;
  56. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  57. var
  58. i,nr : longint;
  59. Buf : ^char;
  60. p : ppchar;
  61. begin
  62. buf:=s;
  63. nr:=1;
  64. while (buf^<>#0) do // count nr of args
  65. begin
  66. while (buf^ in [' ',#9,#10]) do // Kill separators.
  67. inc(buf);
  68. inc(nr);
  69. if buf^='"' Then // quotes argument?
  70. begin
  71. inc(buf);
  72. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  73. inc(buf);
  74. if buf^='"' then // skip closing quote.
  75. inc(buf);
  76. end
  77. else
  78. begin // else std
  79. while not (buf^ in [' ',#0,#9,#10]) do
  80. inc(buf);
  81. end;
  82. end;
  83. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  84. StringToPPChar:=p;
  85. if p=nil then
  86. begin
  87. {$ifdef xunix}
  88. fpseterrno(ESysEnomem);
  89. {$endif}
  90. exit;
  91. end;
  92. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  93. buf:=s;
  94. while (buf^<>#0) do
  95. begin
  96. while (buf^ in [' ',#9,#10]) do // Kill separators.
  97. begin
  98. buf^:=#0;
  99. inc(buf);
  100. end;
  101. if buf^='"' Then // quotes argument?
  102. begin
  103. inc(buf);
  104. p^:=buf;
  105. inc(p);
  106. p^:=nil;
  107. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  108. inc(buf);
  109. if buf^='"' then // skip closing quote.
  110. begin
  111. buf^:=#0;
  112. inc(buf);
  113. end;
  114. end
  115. else
  116. begin
  117. p^:=buf;
  118. inc(p);
  119. p^:=nil;
  120. while not (buf^ in [' ',#0,#9,#10]) do
  121. inc(buf);
  122. end;
  123. end;
  124. end;
  125. end.