unixutil.pp 6.7 KB

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