unixutil.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. unit unixutil;
  2. interface
  3. Type
  4. ComStr = String[255];
  5. PathStr = String[255];
  6. DirStr = String[255];
  7. NameStr = String[255];
  8. ExtStr = String[255];
  9. Function Dirname(Const path:pathstr):pathstr;
  10. Function StringToPPChar(S: PChar):ppchar;
  11. Function StringToPPChar(Var S:String):ppchar;
  12. Function StringToPPChar(Var S:AnsiString):ppchar;
  13. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  14. Function FNMatch(const Pattern,Name:string):Boolean;
  15. Function GetFS (var T:Text):longint;
  16. Function GetFS(Var F:File):longint;
  17. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  18. implementation
  19. {$I textrec.inc}
  20. {$i filerec.inc}
  21. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  22. Var
  23. DotPos,SlashPos,i : longint;
  24. Begin
  25. SlashPos:=0;
  26. DotPos:=256;
  27. i:=Length(Path);
  28. While (i>0) and (SlashPos=0) Do
  29. Begin
  30. If (DotPos=256) and (Path[i]='.') Then
  31. begin
  32. DotPos:=i;
  33. end;
  34. If (Path[i]='/') Then
  35. SlashPos:=i;
  36. Dec(i);
  37. End;
  38. Ext:=Copy(Path,DotPos,255);
  39. Dir:=Copy(Path,1,SlashPos);
  40. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  41. End;
  42. Function Dirname(Const path:pathstr):pathstr;
  43. {
  44. This function returns the directory part of a complete path.
  45. Unless the directory is root '/', The last character is not
  46. a slash.
  47. }
  48. var
  49. Dir : PathStr;
  50. Name : NameStr;
  51. Ext : ExtStr;
  52. begin
  53. FSplit(Path,Dir,Name,Ext);
  54. if length(Dir)>1 then
  55. Delete(Dir,length(Dir),1);
  56. DirName:=Dir;
  57. end;
  58. Function StringToPPChar(Var S:String):ppchar;
  59. {
  60. Create a PPChar to structure of pchars which are the arguments specified
  61. in the string S. Especially usefull for creating an ArgV for Exec-calls
  62. Note that the string S is destroyed by this call.
  63. }
  64. begin
  65. S:=S+#0;
  66. StringToPPChar:=StringToPPChar(@S[1]);
  67. end;
  68. Function StringToPPChar(Var S:AnsiString):ppchar;
  69. {
  70. Create a PPChar to structure of pchars which are the arguments specified
  71. in the string S. Especially usefull for creating an ArgV for Exec-calls
  72. }
  73. begin
  74. StringToPPChar:=StringToPPChar(PChar(S));
  75. end;
  76. Function StringToPPChar(S: PChar):ppchar;
  77. var
  78. nr : longint;
  79. Buf : ^char;
  80. p : ppchar;
  81. begin
  82. buf:=s;
  83. nr:=0;
  84. while(buf^<>#0) do
  85. begin
  86. while (buf^ in [' ',#9,#10]) do
  87. inc(buf);
  88. inc(nr);
  89. while not (buf^ in [' ',#0,#9,#10]) do
  90. inc(buf);
  91. end;
  92. getmem(p,nr*4);
  93. StringToPPChar:=p;
  94. if p=nil then
  95. begin
  96. {$ifdef xunix}
  97. fpseterrno(ESysEnomem);
  98. {$endif}
  99. exit;
  100. end;
  101. buf:=s;
  102. while (buf^<>#0) do
  103. begin
  104. while (buf^ in [' ',#9,#10]) do
  105. begin
  106. buf^:=#0;
  107. inc(buf);
  108. end;
  109. p^:=buf;
  110. inc(p);
  111. p^:=nil;
  112. while not (buf^ in [' ',#0,#9,#10]) do
  113. inc(buf);
  114. end;
  115. end;
  116. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  117. {
  118. This function returns the filename part of a complete path. If suf is
  119. supplied, it is cut off the filename.
  120. }
  121. var
  122. Dir : PathStr;
  123. Name : NameStr;
  124. Ext : ExtStr;
  125. begin
  126. FSplit(Path,Dir,Name,Ext);
  127. if Suf<>Ext then
  128. Name:=Name+Ext;
  129. BaseName:=Name;
  130. end;
  131. Function FNMatch(const Pattern,Name:string):Boolean;
  132. Var
  133. LenPat,LenName : longint;
  134. Function DoFNMatch(i,j:longint):Boolean;
  135. Var
  136. Found : boolean;
  137. Begin
  138. Found:=true;
  139. While Found and (i<=LenPat) Do
  140. Begin
  141. Case Pattern[i] of
  142. '?' : Found:=(j<=LenName);
  143. '*' : Begin
  144. {find the next character in pattern, different of ? and *}
  145. while Found and (i<LenPat) do
  146. begin
  147. inc(i);
  148. case Pattern[i] of
  149. '*' : ;
  150. '?' : begin
  151. inc(j);
  152. Found:=(j<=LenName);
  153. end;
  154. else
  155. Found:=false;
  156. end;
  157. end;
  158. {Now, find in name the character which i points to, if the * or ?
  159. wasn't the last character in the pattern, else, use up all the
  160. chars in name}
  161. Found:=true;
  162. if (i<=LenPat) then
  163. begin
  164. repeat
  165. {find a letter (not only first !) which maches pattern[i]}
  166. while (j<=LenName) and (name[j]<>pattern[i]) do
  167. inc (j);
  168. if (j<LenName) then
  169. begin
  170. if DoFnMatch(i+1,j+1) then
  171. begin
  172. i:=LenPat;
  173. j:=LenName;{we can stop}
  174. Found:=true;
  175. end
  176. else
  177. inc(j);{We didn't find one, need to look further}
  178. end;
  179. until (j>=LenName);
  180. end
  181. else
  182. j:=LenName;{we can stop}
  183. end;
  184. else {not a wildcard character in pattern}
  185. Found:=(j<=LenName) and (pattern[i]=name[j]);
  186. end;
  187. inc(i);
  188. inc(j);
  189. end;
  190. DoFnMatch:=Found and (j>LenName);
  191. end;
  192. Begin {start FNMatch}
  193. LenPat:=Length(Pattern);
  194. LenName:=Length(Name);
  195. FNMatch:=DoFNMatch(1,1);
  196. End;
  197. Function GetFS (var T:Text):longint;
  198. {
  199. Get File Descriptor of a text file.
  200. }
  201. begin
  202. if textrec(t).mode=fmclosed then
  203. exit(-1)
  204. else
  205. GETFS:=textrec(t).Handle
  206. end;
  207. Function GetFS(Var F:File):longint;
  208. {
  209. Get File Descriptor of an unTyped file.
  210. }
  211. begin
  212. { Handle and mode are on the same place in textrec and filerec. }
  213. if filerec(f).mode=fmclosed then
  214. exit(-1)
  215. else
  216. GETFS:=filerec(f).Handle
  217. end;
  218. end.