unixutil.pp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. var
  23. Tzseconds : Longint;
  24. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  25. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  26. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  27. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint; deprecated 'use DateUtils.DateTimeToUnix';
  28. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word); deprecated 'use DateUtils.UnixToDateTime';
  29. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word); deprecated 'use DateUtils.DateTimetoJulianDate';
  30. Function GregorianToJulian(Year,Month,Day:Longint):LongInt; deprecated 'use DateUtils.JulianDateToDateTime';
  31. implementation
  32. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  33. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  34. // Note: for internal use by skilled programmers only
  35. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  36. var p : ppchar;
  37. i : LongInt;
  38. begin
  39. if High(s)<Low(s) Then Exit(NIL);
  40. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  41. // for cmd
  42. if p=nil then
  43. begin
  44. {$ifdef xunix}
  45. fpseterrno(ESysEnomem);
  46. {$endif}
  47. exit(NIL);
  48. end;
  49. for i:=low(s) to high(s) do
  50. p[i+Reserveentries]:=pchar(s[i]);
  51. p[high(s)+1+Reserveentries]:=nil;
  52. ArrayStringToPPchar:=p;
  53. end;
  54. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  55. {
  56. Create a PPChar to structure of pchars which are the arguments specified
  57. in the string S. Especially useful for creating an ArgV for Exec-calls
  58. }
  59. begin
  60. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  61. end;
  62. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  63. var
  64. i,nr : longint;
  65. Buf : ^char;
  66. p : ppchar;
  67. begin
  68. buf:=s;
  69. nr:=1;
  70. while (buf^<>#0) do // count nr of args
  71. begin
  72. while (buf^ in [' ',#9,#10]) do // Kill separators.
  73. inc(buf);
  74. inc(nr);
  75. if buf^='"' Then // quotes argument?
  76. begin
  77. inc(buf);
  78. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  79. inc(buf);
  80. if buf^='"' then // skip closing quote.
  81. inc(buf);
  82. end
  83. else
  84. begin // else std
  85. while not (buf^ in [' ',#0,#9,#10]) do
  86. inc(buf);
  87. end;
  88. end;
  89. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  90. StringToPPChar:=p;
  91. if p=nil then
  92. begin
  93. {$ifdef xunix}
  94. fpseterrno(ESysEnomem);
  95. {$endif}
  96. exit;
  97. end;
  98. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  99. buf:=s;
  100. while (buf^<>#0) do
  101. begin
  102. while (buf^ in [' ',#9,#10]) do // Kill separators.
  103. begin
  104. buf^:=#0;
  105. inc(buf);
  106. end;
  107. if buf^='"' Then // quotes argument?
  108. begin
  109. inc(buf);
  110. p^:=buf;
  111. inc(p);
  112. p^:=nil;
  113. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  114. inc(buf);
  115. if buf^='"' then // skip closing quote.
  116. begin
  117. buf^:=#0;
  118. inc(buf);
  119. end;
  120. end
  121. else
  122. begin
  123. p^:=buf;
  124. inc(p);
  125. p^:=nil;
  126. while not (buf^ in [' ',#0,#9,#10]) do
  127. inc(buf);
  128. end;
  129. end;
  130. end;
  131. Const
  132. {Date Translation}
  133. C1970=2440588;
  134. D0 = 1461;
  135. D1 = 146097;
  136. D2 =1721119;
  137. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  138. Var
  139. YYear,XYear,Temp,TempMonth : LongInt;
  140. Begin
  141. Temp:=((JulianDN-D2) shl 2)-1;
  142. JulianDN:=Temp Div D1;
  143. XYear:=(Temp Mod D1) or 3;
  144. YYear:=(XYear Div D0);
  145. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  146. Day:=((Temp Mod 153)+5) Div 5;
  147. TempMonth:=Temp Div 153;
  148. If TempMonth>=10 Then
  149. Begin
  150. inc(YYear);
  151. dec(TempMonth,12);
  152. End;
  153. inc(TempMonth,3);
  154. Month := TempMonth;
  155. Year:=YYear+(JulianDN*100);
  156. end;
  157. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  158. {
  159. Transforms Epoch time into local time (hour, minute,seconds)
  160. }
  161. Var
  162. DateNum: LongInt;
  163. Begin
  164. inc(Epoch,TZSeconds);
  165. Datenum:=(Epoch Div 86400) + c1970;
  166. JulianToGregorian(DateNum,Year,Month,day);
  167. Epoch:=Abs(Epoch Mod 86400);
  168. Hour:=Epoch Div 3600;
  169. Epoch:=Epoch Mod 3600;
  170. Minute:=Epoch Div 60;
  171. Second:=Epoch Mod 60;
  172. End;
  173. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  174. {
  175. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  176. (seconds since 00:00, january 1 1970, corrected for local time zone)
  177. }
  178. Begin
  179. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  180. (LongInt(Hour)*3600)+(Longint(Minute)*60)+Second-TZSeconds;
  181. End;
  182. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  183. Var
  184. Century,XYear: LongInt;
  185. Begin
  186. If Month<=2 Then
  187. Begin
  188. Dec(Year);
  189. Inc(Month,12);
  190. End;
  191. Dec(Month,3);
  192. Century:=(longint(Year Div 100)*D1) shr 2;
  193. XYear:=(longint(Year Mod 100)*D0) shr 2;
  194. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  195. End;
  196. end.