unixutil.pp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. <What does this file>
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit unixutil;
  12. interface
  13. var
  14. Tzseconds : Longint;
  15. Type
  16. ComStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  17. PathStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  18. DirStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  19. NameStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  20. ExtStr = String[255] deprecated 'Clean up shortstring use, or use same type from unit dos.';
  21. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  22. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  23. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  24. Function GetFS(var T:Text):longint; deprecated;
  25. Function GetFS(Var F:File):longint; deprecated; // use sysutils.getfilehandle
  26. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  27. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  28. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  29. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  30. implementation
  31. {$I textrec.inc}
  32. {$i filerec.inc}
  33. function ArrayStringToPPchar(const S:Array of AnsiString;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:AnsiString;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. Function GetFS (var T:Text):longint;
  133. {
  134. Get File Descriptor of a text file.
  135. }
  136. begin
  137. if textrec(t).mode=fmclosed then
  138. exit(-1)
  139. else
  140. GETFS:=textrec(t).Handle
  141. end;
  142. Function GetFS(Var F:File):longint;
  143. {
  144. Get File Descriptor of an unTyped file.
  145. }
  146. begin
  147. { Handle and mode are on the same place in textrec and filerec. }
  148. if filerec(f).mode=fmclosed then
  149. exit(-1)
  150. else
  151. GETFS:=filerec(f).Handle
  152. end;
  153. Const
  154. {Date Translation}
  155. C1970=2440588;
  156. D0 = 1461;
  157. D1 = 146097;
  158. D2 =1721119;
  159. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  160. Var
  161. YYear,XYear,Temp,TempMonth : LongInt;
  162. Begin
  163. Temp:=((JulianDN-D2) shl 2)-1;
  164. JulianDN:=Temp Div D1;
  165. XYear:=(Temp Mod D1) or 3;
  166. YYear:=(XYear Div D0);
  167. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  168. Day:=((Temp Mod 153)+5) Div 5;
  169. TempMonth:=Temp Div 153;
  170. If TempMonth>=10 Then
  171. Begin
  172. inc(YYear);
  173. dec(TempMonth,12);
  174. End;
  175. inc(TempMonth,3);
  176. Month := TempMonth;
  177. Year:=YYear+(JulianDN*100);
  178. end;
  179. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  180. {
  181. Transforms Epoch time into local time (hour, minute,seconds)
  182. }
  183. Var
  184. DateNum: LongInt;
  185. Begin
  186. inc(Epoch,TZSeconds);
  187. Datenum:=(Epoch Div 86400) + c1970;
  188. JulianToGregorian(DateNum,Year,Month,day);
  189. Epoch:=Abs(Epoch Mod 86400);
  190. Hour:=Epoch Div 3600;
  191. Epoch:=Epoch Mod 3600;
  192. Minute:=Epoch Div 60;
  193. Second:=Epoch Mod 60;
  194. End;
  195. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  196. {
  197. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  198. (seconds since 00:00, january 1 1970, corrected for local time zone)
  199. }
  200. Begin
  201. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  202. (LongInt(Hour)*3600)+(Longint(Minute)*60)+Second-TZSeconds;
  203. End;
  204. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  205. Var
  206. Century,XYear: LongInt;
  207. Begin
  208. If Month<=2 Then
  209. Begin
  210. Dec(Year);
  211. Inc(Month,12);
  212. End;
  213. Dec(Month,3);
  214. Century:=(longint(Year Div 100)*D1) shr 2;
  215. XYear:=(longint(Year Mod 100)*D0) shr 2;
  216. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  217. End;
  218. end.