2
0

unixutil.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. {$IFNDEF FPC_DOTTEDUNITS}
  21. unit unixutil;
  22. {$ENDIF FPC_DOTTEDUNITS}
  23. interface
  24. Function StringToPPChar(S: PAnsiChar;ReserveEntries:integer):PPAnsiChar;
  25. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):PPAnsiChar;
  26. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):PPAnsiChar; // const ?
  27. implementation
  28. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):PPAnsiChar; // const ?
  29. // Extra allocate reserveentries PAnsiChar's at the beginning (default param=0 after 1.0.x ?)
  30. // Note: for internal use by skilled programmers only
  31. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  32. var p : PPAnsiChar;
  33. i : LongInt;
  34. begin
  35. if High(s)<Low(s) Then Exit(NIL);
  36. Getmem(p,sizeof(PAnsiChar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  37. // for cmd
  38. if p=nil then
  39. begin
  40. {$ifdef xunix}
  41. fpseterrno(ESysEnomem);
  42. {$endif}
  43. exit(NIL);
  44. end;
  45. for i:=low(s) to high(s) do
  46. p[i+Reserveentries]:=PAnsiChar(s[i]);
  47. p[high(s)+1+Reserveentries]:=nil;
  48. ArrayStringToPPchar:=p;
  49. end;
  50. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):PPAnsiChar;
  51. {
  52. Create a PPAnsiChar to structure of pchars which are the arguments specified
  53. in the string S. Especially useful for creating an ArgV for Exec-calls
  54. }
  55. begin
  56. StringToPPChar:=StringToPPChar(PAnsiChar(S),ReserveEntries);
  57. end;
  58. Function StringToPPChar(S: PAnsiChar;ReserveEntries:integer):PPAnsiChar;
  59. var
  60. i,nr : longint;
  61. Buf : ^AnsiChar;
  62. p : PPAnsiChar;
  63. begin
  64. buf:=s;
  65. nr:=1;
  66. while (buf^<>#0) do // count nr of args
  67. begin
  68. while (buf^ in [' ',#9,#10]) do // Kill separators.
  69. inc(buf);
  70. inc(nr);
  71. if buf^='"' Then // quotes argument?
  72. begin
  73. inc(buf);
  74. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  75. inc(buf);
  76. if buf^='"' then // skip closing quote.
  77. inc(buf);
  78. end
  79. else
  80. begin // else std
  81. while not (buf^ in [' ',#0,#9,#10]) do
  82. inc(buf);
  83. end;
  84. end;
  85. getmem(p,(ReserveEntries+nr)*sizeof(PAnsiChar));
  86. StringToPPChar:=p;
  87. if p=nil then
  88. begin
  89. {$ifdef xunix}
  90. fpseterrno(ESysEnomem);
  91. {$endif}
  92. exit;
  93. end;
  94. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  95. buf:=s;
  96. while (buf^<>#0) do
  97. begin
  98. while (buf^ in [' ',#9,#10]) do // Kill separators.
  99. begin
  100. buf^:=#0;
  101. inc(buf);
  102. end;
  103. if buf^='"' Then // quotes argument?
  104. begin
  105. inc(buf);
  106. p^:=buf;
  107. inc(p);
  108. p^:=nil;
  109. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  110. inc(buf);
  111. if buf^='"' then // skip closing quote.
  112. begin
  113. buf^:=#0;
  114. inc(buf);
  115. end;
  116. end
  117. else
  118. begin
  119. p^:=buf;
  120. inc(p);
  121. p^:=nil;
  122. while not (buf^ in [' ',#0,#9,#10]) do
  123. inc(buf);
  124. end;
  125. end;
  126. end;
  127. end.