unixutil.pp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. unit unixutil;
  2. interface
  3. var
  4. Tzseconds : Longint;
  5. Type
  6. ComStr = String[255];
  7. PathStr = String[255];
  8. DirStr = String[255];
  9. NameStr = String[255];
  10. ExtStr = String[255];
  11. Function Dirname(Const path:pathstr):pathstr;
  12. Function StringToPPChar(S: PChar):ppchar;
  13. Function StringToPPChar(Var S:String):ppchar;
  14. Function StringToPPChar(Var S:AnsiString):ppchar;
  15. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  16. Function FNMatch(const Pattern,Name:string):Boolean;
  17. Function GetFS (var T:Text):longint;
  18. Function GetFS(Var F:File):longint;
  19. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  20. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  21. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  22. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  23. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  24. implementation
  25. {$I textrec.inc}
  26. {$i filerec.inc}
  27. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  28. Var
  29. DotPos,SlashPos,i : longint;
  30. Begin
  31. SlashPos:=0;
  32. DotPos:=256;
  33. i:=Length(Path);
  34. While (i>0) and (SlashPos=0) Do
  35. Begin
  36. If (DotPos=256) and (Path[i]='.') Then
  37. begin
  38. DotPos:=i;
  39. end;
  40. If (Path[i]='/') Then
  41. SlashPos:=i;
  42. Dec(i);
  43. End;
  44. Ext:=Copy(Path,DotPos,255);
  45. Dir:=Copy(Path,1,SlashPos);
  46. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  47. End;
  48. Function Dirname(Const path:pathstr):pathstr;
  49. {
  50. This function returns the directory part of a complete path.
  51. Unless the directory is root '/', The last character is not
  52. a slash.
  53. }
  54. var
  55. Dir : PathStr;
  56. Name : NameStr;
  57. Ext : ExtStr;
  58. begin
  59. FSplit(Path,Dir,Name,Ext);
  60. if length(Dir)>1 then
  61. Delete(Dir,length(Dir),1);
  62. DirName:=Dir;
  63. end;
  64. Function StringToPPChar(Var S:String):ppchar;
  65. {
  66. Create a PPChar to structure of pchars which are the arguments specified
  67. in the string S. Especially usefull for creating an ArgV for Exec-calls
  68. Note that the string S is destroyed by this call.
  69. }
  70. begin
  71. S:=S+#0;
  72. StringToPPChar:=StringToPPChar(@S[1]);
  73. end;
  74. Function StringToPPChar(Var S:AnsiString):ppchar;
  75. {
  76. Create a PPChar to structure of pchars which are the arguments specified
  77. in the string S. Especially usefull for creating an ArgV for Exec-calls
  78. }
  79. begin
  80. StringToPPChar:=StringToPPChar(PChar(S));
  81. end;
  82. Function StringToPPChar(S: PChar):ppchar;
  83. var
  84. nr : longint;
  85. Buf : ^char;
  86. p : ppchar;
  87. begin
  88. buf:=s;
  89. nr:=0;
  90. while(buf^<>#0) do
  91. begin
  92. while (buf^ in [' ',#9,#10]) do
  93. inc(buf);
  94. inc(nr);
  95. while not (buf^ in [' ',#0,#9,#10]) do
  96. inc(buf);
  97. end;
  98. getmem(p,nr*4);
  99. StringToPPChar:=p;
  100. if p=nil then
  101. begin
  102. {$ifdef xunix}
  103. fpseterrno(ESysEnomem);
  104. {$endif}
  105. exit;
  106. end;
  107. buf:=s;
  108. while (buf^<>#0) do
  109. begin
  110. while (buf^ in [' ',#9,#10]) do
  111. begin
  112. buf^:=#0;
  113. inc(buf);
  114. end;
  115. p^:=buf;
  116. inc(p);
  117. p^:=nil;
  118. while not (buf^ in [' ',#0,#9,#10]) do
  119. inc(buf);
  120. end;
  121. end;
  122. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  123. {
  124. This function returns the filename part of a complete path. If suf is
  125. supplied, it is cut off the filename.
  126. }
  127. var
  128. Dir : PathStr;
  129. Name : NameStr;
  130. Ext : ExtStr;
  131. begin
  132. FSplit(Path,Dir,Name,Ext);
  133. if Suf<>Ext then
  134. Name:=Name+Ext;
  135. BaseName:=Name;
  136. end;
  137. Function FNMatch(const Pattern,Name:string):Boolean;
  138. Var
  139. LenPat,LenName : longint;
  140. Function DoFNMatch(i,j:longint):Boolean;
  141. Var
  142. Found : boolean;
  143. Begin
  144. Found:=true;
  145. While Found and (i<=LenPat) Do
  146. Begin
  147. Case Pattern[i] of
  148. '?' : Found:=(j<=LenName);
  149. '*' : Begin
  150. {find the next character in pattern, different of ? and *}
  151. while Found and (i<LenPat) do
  152. begin
  153. inc(i);
  154. case Pattern[i] of
  155. '*' : ;
  156. '?' : begin
  157. inc(j);
  158. Found:=(j<=LenName);
  159. end;
  160. else
  161. Found:=false;
  162. end;
  163. end;
  164. {Now, find in name the character which i points to, if the * or ?
  165. wasn't the last character in the pattern, else, use up all the
  166. chars in name}
  167. Found:=true;
  168. if (i<=LenPat) then
  169. begin
  170. repeat
  171. {find a letter (not only first !) which maches pattern[i]}
  172. while (j<=LenName) and (name[j]<>pattern[i]) do
  173. inc (j);
  174. if (j<LenName) then
  175. begin
  176. if DoFnMatch(i+1,j+1) then
  177. begin
  178. i:=LenPat;
  179. j:=LenName;{we can stop}
  180. Found:=true;
  181. end
  182. else
  183. inc(j);{We didn't find one, need to look further}
  184. end;
  185. until (j>=LenName);
  186. end
  187. else
  188. j:=LenName;{we can stop}
  189. end;
  190. else {not a wildcard character in pattern}
  191. Found:=(j<=LenName) and (pattern[i]=name[j]);
  192. end;
  193. inc(i);
  194. inc(j);
  195. end;
  196. DoFnMatch:=Found and (j>LenName);
  197. end;
  198. Begin {start FNMatch}
  199. LenPat:=Length(Pattern);
  200. LenName:=Length(Name);
  201. FNMatch:=DoFNMatch(1,1);
  202. End;
  203. Function GetFS (var T:Text):longint;
  204. {
  205. Get File Descriptor of a text file.
  206. }
  207. begin
  208. if textrec(t).mode=fmclosed then
  209. exit(-1)
  210. else
  211. GETFS:=textrec(t).Handle
  212. end;
  213. Function GetFS(Var F:File):longint;
  214. {
  215. Get File Descriptor of an unTyped file.
  216. }
  217. begin
  218. { Handle and mode are on the same place in textrec and filerec. }
  219. if filerec(f).mode=fmclosed then
  220. exit(-1)
  221. else
  222. GETFS:=filerec(f).Handle
  223. end;
  224. Const
  225. {Date Translation}
  226. C1970=2440588;
  227. D0 = 1461;
  228. D1 = 146097;
  229. D2 =1721119;
  230. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  231. Var
  232. YYear,XYear,Temp,TempMonth : LongInt;
  233. Begin
  234. Temp:=((JulianDN-D2) shl 2)-1;
  235. JulianDN:=Temp Div D1;
  236. XYear:=(Temp Mod D1) or 3;
  237. YYear:=(XYear Div D0);
  238. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  239. Day:=((Temp Mod 153)+5) Div 5;
  240. TempMonth:=Temp Div 153;
  241. If TempMonth>=10 Then
  242. Begin
  243. inc(YYear);
  244. dec(TempMonth,12);
  245. End;
  246. inc(TempMonth,3);
  247. Month := TempMonth;
  248. Year:=YYear+(JulianDN*100);
  249. end;
  250. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  251. {
  252. Transforms Epoch time into local time (hour, minute,seconds)
  253. }
  254. Var
  255. DateNum: LongInt;
  256. Begin
  257. inc(Epoch,TZSeconds);
  258. Datenum:=(Epoch Div 86400) + c1970;
  259. JulianToGregorian(DateNum,Year,Month,day);
  260. Epoch:=Abs(Epoch Mod 86400);
  261. Hour:=Epoch Div 3600;
  262. Epoch:=Epoch Mod 3600;
  263. Minute:=Epoch Div 60;
  264. Second:=Epoch Mod 60;
  265. End;
  266. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  267. {
  268. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  269. (seconds since 00:00, january 1 1970, corrected for local time zone)
  270. }
  271. Begin
  272. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  273. (LongInt(Hour)*3600)+(Longint(Minute)*60)+Second-TZSeconds;
  274. End;
  275. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  276. Var
  277. Century,XYear: LongInt;
  278. Begin
  279. If Month<=2 Then
  280. Begin
  281. Dec(Year);
  282. Inc(Month,12);
  283. End;
  284. Dec(Month,3);
  285. Century:=(longint(Year Div 100)*D1) shr 2;
  286. XYear:=(longint(Year Mod 100)*D0) shr 2;
  287. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  288. End;
  289. end.