home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vp21beta.zip / ARTLSRC.RAR / WINDOS.PAS < prev    next >
Pascal/Delphi Source File  |  2000-08-15  |  17KB  |  537 lines

  1. //█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
  2. //█                                                       █
  3. //█      Virtual Pascal Runtime Library.  Version 2.1.    █
  4. //█      WINDOS interface unit for OS/2 & Win32           █
  5. //█      ─────────────────────────────────────────────────█
  6. //█      Copyright (C) 1995-2000 vpascal.com              █
  7. //█                                                       █
  8. //▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  9.  
  10. {$S-,R-,Q-,I-,H-,T-,X+}                     // Common compiler directive
  11. {&Cdecl-,OrgName-,AlignRec-,Delphi+,Use32+} // VP specific compiler directives
  12.  
  13. unit WinDos;
  14.  
  15. interface
  16.  
  17. uses VpSysLow;
  18.  
  19. const
  20.  
  21. { Flags bit masks }
  22.  
  23.   fCarry     = $0001;
  24.   fParity    = $0004;
  25.   fAuxiliary = $0010;
  26.   fZero      = $0040;
  27.   fSign      = $0080;
  28.   fOverflow  = $0800;
  29.  
  30. { File mode magic numbers }
  31.  
  32.   fmClosed = $A55AD7B0;
  33.   fmInput  = $A55AD7B1;
  34.   fmOutput = $A55AD7B2;
  35.   fmInOut  = $A55AD7B3;
  36.  
  37. { File attribute constants }
  38.  
  39.   faReadOnly  = $01;
  40.   faHidden    = $02;
  41.   faSysFile   = $04;
  42.   faVolumeID  = $08;    // N/A under OS/2
  43.   faDirectory = $10;
  44.   faArchive   = $20;
  45. {$IFDEF OS2}
  46.   faAnyFile   = $37;
  47. {$ELSE}
  48.   faAnyFile   = $3F;
  49. {$ENDIF}
  50.  
  51. { Maximum file name component string lengths }
  52.  
  53. const
  54.   fsPathName  = 259;
  55.   fsDirectory = 255;
  56.   fsFileName  = 255;
  57.   fsExtension = 255;
  58.  
  59. { FileSplit return flags }
  60.  
  61. const
  62.   fcExtension = $0001;
  63.   fcFileName  = $0002;
  64.   fcDirectory = $0004;
  65.   fcWildcards = $0008;
  66.  
  67. { Typed-file and untyped-file record }
  68.  
  69. type
  70.   TFileRec = record
  71.     Handle:   Longint;                  // File Handle
  72.     Mode:     Longint;                  // Current file mode
  73.     RecSize:  Longint;                  // I/O operation record size
  74.     Private:  array [1..28] of Byte;    // Reserved
  75.     UserData: array [1..32] of Byte;    // User data area
  76.     Name:     array [0..259] of Char;   // File name (ASCIIZ)
  77.   end;
  78.  
  79. { Textfile record }
  80.  
  81. type
  82.   PTextBuf = ^TTextBuf;
  83.   TTextBuf = array[0..127] of Char;
  84.   TTextRec = record
  85.     Handle:    Longint;                 // File Handle
  86.     Mode:      Longint;                 // Current file mode
  87.     BufSize:   Longint;                 // Text File buffer size
  88.     BufPos:    Longint;                 // Buffer current position
  89.     BufEnd:    Longint;                 // Buffer ending position
  90.     BufPtr:    PTextBuf;                // Pointer to the buffer
  91.     OpenFunc:  Pointer;                 // Open Text File function @
  92.     InOutFunc: Pointer;                 // In/Out ...
  93.     FlushFunc: Pointer;                 // Flush ...
  94.     CloseFunc: Pointer;                 // Close ...
  95.     UserData:  array [1..32] of Byte;   // User data area
  96.     Name:      array [0..259] of Char;  // File name (ASCIIZ)
  97.     Buffer:    TTextBuf;                // Default I/O buffer
  98.   end;
  99.  
  100. { Search record used by FindFirst and FindNext }
  101.  
  102. type
  103.   TSearchRec = record
  104.     Handle: Longint;
  105.     Filler1: Longint;
  106.     Attr: Byte;
  107.     Time: Longint;
  108.     Size: Longint;
  109.     Name: array[0..259] of Char;
  110. {$IFDEF WIN32}
  111.     Filler3: array[0..321] of Char;
  112. {$ENDIF}
  113. {$IFDEF DPMI32}
  114.     Private_data: array[1..sizeof(TOSSearchRec)-4-4-1-4-4-256-4] of Byte;
  115. {$ENDIF}
  116. {$IFDEF LINUX}
  117.     Private_data: array[1..sizeof(TOSSearchRec)-4-4-1-4-4-256-4] of Byte;
  118. {$ENDIF}
  119.   end;
  120.  
  121. { Date and time record used by PackTime and UnpackTime }
  122.  
  123. type
  124.   TDateTime = record
  125.     Year, Month, Day, Hour, Min, Sec: Word;
  126.   end;
  127.  
  128. { Error status variable }
  129.  
  130. threadvar
  131.   DosError: Integer;
  132.  
  133. function DosVersion: Word;
  134. procedure GetDate(var Year, Month, Day, DayOfWeek: Word);
  135. procedure SetDate(Year, Month, Day: Word);
  136. procedure GetTime(var Hour, Minute, Second, Sec100: Word);
  137. procedure SetTime(Hour, Minute, Second, Sec100: Word);
  138. procedure GetVerify(var Verify: Boolean);
  139. procedure SetVerify(Verify: Boolean);
  140. function DiskFree(Drive: Byte): Longint;
  141. function DiskSize(Drive: Byte): Longint;
  142. procedure GetFAttr(var F; var Attr: Word);
  143. procedure SetFAttr(var F; Attr: Word);
  144. procedure GetFTime(var F; var Time: Longint);
  145. procedure SetFTime(var F; Time: Longint);
  146. procedure FindFirst(Path: PChar; Attr: Word; var F: TSearchRec);
  147. procedure FindNext(var F: TSearchRec);
  148. procedure UnpackTime(P: Longint; var T: TDateTime);
  149. procedure PackTime(var T: TDateTime; var P: Longint);
  150. function FileSearch(Dest, Name, List: PChar): PChar;
  151. function FileExpand(Dest, Name: PChar): PChar;
  152. function FileSplit(Path, Dir, Name, Ext: PChar): Word;
  153. function GetCurDir(Dir: PChar; Drive: Byte): PChar;
  154. procedure SetCurDir(Dir: PChar);
  155. procedure CreateDir(Dir: PChar);
  156. procedure RemoveDir(Dir: PChar);
  157. function GetArgCount: Integer;
  158. function GetArgStr(Dest: PChar; Index: Integer; MaxLen: Word): PChar;
  159. function GetEnvVar(VarName: PChar): PChar;
  160.  
  161. { The following procedures are not implemented
  162.  
  163. procedure Intr(IntNo: Byte; var Regs: TRegisters);
  164. procedure MsDos(var Regs: TRegisters);
  165. procedure GetCBreak(var Break: Boolean);
  166. procedure SetCBreak(Break: Boolean);
  167. procedure GetIntVec(IntNo: Byte; var Vector: Pointer);
  168. procedure SetIntVec(IntNo: Byte; Vector: Pointer);
  169.  
  170. }
  171.  
  172. { The following procedure has been added }
  173.  
  174. procedure FindClose(var F: TSearchRec);
  175.  
  176. implementation
  177.  
  178. uses Strings;
  179.  
  180. type
  181.   TDateTimeRec = record
  182.     FTime,FDate: SmallWord;
  183.   end;
  184.  
  185. // Assigns the value of the last error code to DosError and returns it.
  186.  
  187. function SetDosError(ErrCode: Integer): Integer;
  188. begin
  189.   DosError := ErrCode;
  190.   Result := ErrCode;
  191. end;
  192.  
  193. // Returns the version number of the operating system. The low byte of the
  194. // result is the major version number, and the high byte is the minor version
  195. // number. For example, OS/2 2.10 returns $0A14, i.e. 20 in the low byte, and
  196. // 10 in the high byte.
  197.  
  198. function DosVersion: Word;
  199. begin
  200.   Result := SysOsVersion;
  201. end;
  202.  
  203. // Returns the current date set in the operating system. Ranges of the
  204. // values returned are: Year 1980-2099, Month 1-12, Day 1-31 and
  205. // DayOfWeek 0-6 (0 corresponds to Sunday).
  206.  
  207. procedure GetDate(var Year,Month,Day,DayOfWeek: Word);
  208. begin
  209.   SysGetDateTime(@Year, @Month, @Day, @DayOfWeek, nil, nil, nil, nil);
  210. end;
  211.  
  212. // Sets the current date set in the operating system. Valid parameter
  213. // ranges are: Year 1980-2099, Month 1-12 and Day 1-31. If the date is
  214. // not valid, the function call is ignored.
  215.  
  216. procedure SetDate(Year,Month,Day: Word);
  217. begin
  218.   SysSetDateTime(@Year, @Month, @Day, nil, nil, nil, nil);
  219. end;
  220.  
  221. // Returns the current time set in the operating system. Ranges of the
  222. // values returned are: Hour 0-23, Minute 0-59, Second 0-59 and Sec100
  223. // (hundredths of seconds) 0-99.
  224.  
  225. procedure GetTime(var Hour,Minute,Second,Sec100: Word);
  226. begin
  227.   SysGetDateTime(nil, nil, nil, nil, @Hour, @Minute, @Second, @Sec100);
  228.   Sec100 := Sec100 div 10;
  229. end;
  230.  
  231. procedure SetTime(Hour,Minute,Second,Sec100: Word);
  232. begin
  233.   Sec100 := Sec100 * 10;
  234.   SysSetDateTime(nil, nil, nil, @Hour, @Minute, @Second, @Sec100);
  235. end;
  236.  
  237. // GetVerify returns the state of the verify flag in OS/2. When off
  238. // (False), disk writes are not verified. When on (True), all disk
  239. // writes are verified to insure proper writing.
  240.  
  241. procedure GetVerify(var Verify: Boolean);
  242. begin
  243.   Verify := SysVerify(False, False);
  244. end;
  245.  
  246. // SetVerify sets the state of the verify flag in OS/2.
  247.  
  248. procedure SetVerify(Verify: Boolean);
  249. begin
  250.   SysVerify(True, Verify);
  251. end;
  252.  
  253. // Returns the number of free bytes on the specified drive number
  254. // (0=Default,1=A,2=B,..). Returns -1 if the drive number is invalid.
  255.  
  256. function DiskFree(Drive: Byte): Longint;
  257. begin
  258.   Result := SysDiskFree(Drive);
  259. end;
  260.  
  261. // Returns the size in bytes of the specified drive number (0=Default,
  262. // 1=A,2=B,..). Returns -1 if the drive number is invalid.
  263.  
  264. function DiskSize(Drive: Byte): Longint;
  265. begin
  266.   Result := SysDiskSize(Drive);
  267. end;
  268.  
  269. // Returns the attributes of a file. F must be a file variable (typed,
  270. // untyped or textfile) which has been assigned a name. The attributes
  271. // are examined by ANDing with the attribute masks defined as constants
  272. // above. Errors are reported in DosError.
  273.  
  274. procedure GetFAttr(var F; var Attr: Word);
  275. begin
  276.   SetDosError(SysGetFileAttr(TFileRec(F).Name, Attr));
  277. end;
  278.  
  279. // Sets the attributes of a file. F must be a file variable (typed,
  280. // untyped or textfile) which has been assigned a name. The attribute
  281. // alue is formed by adding (or ORing) the appropriate attribute masks
  282. // efined as constants above. Errors are reported in DosError.
  283.  
  284. procedure SetFAttr(var F; Attr: Word);
  285. begin
  286.   SetDosError(SysSetFileAttr(TFileRec(F).Name, Attr));
  287. end;
  288.  
  289. // Returns the date and time a file was last written. F must be a file
  290. // variable (typed, untyped or textfile) which has been assigned and
  291. // opened. The Time parameter may be unpacked throgh a call to
  292. // UnpackTime. Errors are reported in DosError.
  293.  
  294. procedure GetFTime(var F; var Time: Longint);
  295. begin
  296.   SetDosError(SysGetFileTime(TFileRec(F).Handle, Time));
  297. end;
  298.  
  299. // Sets the date and time a file was last written. F must be a file
  300. // variable (typed, untyped or textfile) which has been assigned and
  301. // opened. The Time parameter may be created through a call to PackTime.
  302. // Errors are reported in DosError.
  303.  
  304. procedure SetFTime(var F; Time: Longint);
  305. begin
  306.   SetDosError(SysSetFileTime(TFileRec(F).Handle, Time));
  307. end;
  308.  
  309. // Converts a 4-byte packed date/time returned by FindFirst, FindNext or
  310. // GetFTime into a TDateTime record.
  311.  
  312. procedure UnpackTime(P: Longint; var T: TDateTime);
  313. var
  314.   FDateTime: TDateTimeRec absolute P;
  315. begin
  316.   with T,FDateTime do
  317.   begin
  318.     Year  := (FDate and $FE00) shr 9 + 1980;
  319.     Month := (FDate and $01E0) shr 5;
  320.     Day   := (FDate and $001F);
  321.     Hour  := (FTime and $F800) shr 11;
  322.     Min   := (FTime and $07E0) shr 5;
  323.     Sec   := (FTime and $001F) * 2;
  324.   end;
  325. end;
  326.  
  327. // Converts a TDateTime record into a 4-byte packed date/time used by
  328. // SetFTime.
  329.  
  330. procedure PackTime(var T: TDateTime; var P: Longint);
  331. var
  332.   FDateTime: TDateTimeRec absolute P;
  333. begin
  334.   with T,FDateTime do
  335.   begin
  336.     FDate := (Year - 1980) shl 9 + Month shl 5 + Day;
  337.     FTime := Hour shl 11 + Min shl 5 + (Sec div 2);
  338.   end;
  339. end;
  340.  
  341. // Splits the file name specified by Path into its three components. Dir
  342. // Searches the specified (or current) directory for the first entry
  343. // that matches the specified filename and attributes. The result is
  344. // returned in the specified search record. Errors (and no files found)
  345. // are reported in DosError.
  346.  
  347. procedure FindFirst(Path: PChar; Attr: Word; var F: TSearchRec);
  348. begin
  349.   SetDosError(SysFindFirst(Path, Attr, TOSSearchRec(F), True));
  350. end;
  351.  
  352. // Returns the next entry that matches the name and attributes specified
  353. // in a previous call to FindFirst. The search record must be one passed
  354. // to FindFirst. Errors (and no more files) are reported in DosError.
  355.  
  356. procedure FindNext(var F: TSearchRec);
  357. begin
  358.   SetDosError(SysFindNext(TOSSearchRec(F), True));
  359. end;
  360.  
  361. // Ends the search, closes the search record. FindClose should be issued
  362. // whenever search record is no longer needed.
  363.  
  364. procedure FindClose(var F: TSearchRec);
  365. begin
  366.   SetDosError(SysFindClose(TOSSearchRec(F)));
  367. end;
  368.  
  369. // FileSearch searches for the file given by Name in the list of
  370. // directories given by List. The directory paths in List must be
  371. // eparated by semicolons. The search always starts with the current
  372. // directory of the current drive. If the file is found, FileSearch
  373. // stores a concatenation of the directory path and the file name in
  374. // Dest. Otherwise FileSearch stores an empty string in Dest. The
  375. // maximum length of the result is defined by the fsPathName constant.
  376. // The returned value is Dest.
  377.  
  378. function FileSearch(Dest, Name, List: PChar): PChar;
  379. begin
  380.   Result := SysFileSearch(Dest, Name, List);
  381. end;
  382.  
  383. // FileExpand fully expands the file name in Name, and stores the result
  384. // in Dest. The maximum length of the result is defined by the
  385. // fsPathName constant. The result is an all upper case string
  386. // consisting of a drive letter, a colon, a root relative directory path,
  387. // and a file name. Embedded '.' and '..' directory references are
  388. // removed. The returned value is Dest.
  389.  
  390. function FileExpand(Dest, Name: PChar): PChar;
  391. begin
  392.   Result := SysFileExpand(Dest, Name);
  393. end;
  394.  
  395. // FileSplit splits the file name specified by Path into its three
  396. // components. Dir is set to the drive and directory path with any
  397. // leading and trailing backslashes, Name is set to the file name, and
  398. // Ext is set to the extension with a preceding period. If a component
  399. // string parameter is NIL, the corresponding part of the path is not
  400. // stored. If the path does not contain a given component, the returned
  401. // component string is empty. The maximum lengths of the strings
  402. // returned in Dir, Name, and Ext are defined by the fsDirectory,
  403. // fsFileName, and fsExtension constants. The returned value is a
  404. // combination of the fcDirectory, fcFileName, and fcExtension bit masks,
  405. // indicating which components were present in the path. If the name or
  406. // extension contains any wildcard characters (* or ?), the fcWildcards
  407. // flag is set in the returned value.
  408.  
  409. function FileSplit(Path, Dir, Name, Ext: PChar): Word;
  410. var
  411.   DirLen, NameLen, Flags: Word;
  412.   NamePtr, ExtPtr: PChar;
  413. begin
  414.   NamePtr := StrRScan(Path, '\');
  415.   if NamePtr = nil then NamePtr := StrRScan(Path, ':');
  416.   if NamePtr = nil then NamePtr := Path else Inc(NamePtr);
  417.   ExtPtr := StrScan(NamePtr, '.');
  418.   if ExtPtr = nil then ExtPtr := StrEnd(NamePtr);
  419.   DirLen := NamePtr - Path;
  420.   if DirLen > fsDirectory then DirLen := fsDirectory;
  421.   NameLen := ExtPtr - NamePtr;
  422.   if NameLen > fsFilename then NameLen := fsFilename;
  423.   Flags := 0;
  424.   if (StrScan(NamePtr, '?') <> nil) or (StrScan(NamePtr, '*') <> nil) then
  425.     Flags := fcWildcards;
  426.   if DirLen <> 0 then Flags := Flags or fcDirectory;
  427.   if NameLen <> 0 then Flags := Flags or fcFilename;
  428.   if ExtPtr[0] <> #0 then Flags := Flags or fcExtension;
  429.   if Dir <> nil then
  430.     begin
  431.       Dir^:=#0;
  432.       StrLCopy(Dir, Path, DirLen);
  433.     end;
  434.   if Name <> nil then
  435.     begin
  436.       Name^:=#0;
  437.       StrLCopy(Name, NamePtr, NameLen);
  438.     end;
  439.   if Ext <> nil then
  440.     begin
  441.       Ext^:=#0;
  442.       StrLCopy(Ext, ExtPtr, fsExtension);
  443.     end;
  444.   FileSplit := Flags;
  445. end;
  446.  
  447. // GetCurDir returns the current directory of a specified drive.
  448. // Drive = 0 indicates the current drive, 1 indicates drive A, 2
  449. // indicates drive B, and so on. The string returned in Dir always
  450. // starts with a drive letter, a colon, and a backslash. The maximum
  451. // ength of the resulting string is defined by the fsDirectory constant.
  452. // The returned value is Dir. Errors are reported in DosError.
  453.  
  454. function GetCurDir(Dir: PChar; Drive: Byte): PChar;
  455. begin
  456.   SetDosError(SysDirGetCurrent(Drive, Dir));
  457.   Result := Dir;
  458. end;
  459.  
  460. // SetCurDir changes the current directory to the path specified by Dir.
  461. // If Dir specifies a drive letter, the current drive is also changed.
  462. // Errors are reported in DosError.
  463.  
  464. procedure SetCurDir(Dir: PChar);
  465. begin
  466.   SetDosError(SysDirSetCurrent(Dir));
  467. end;
  468.  
  469. // CreateDir creates a new subdirectory with the path specified by Dir.
  470. // Errors are reported in DosError.
  471.  
  472. procedure CreateDir(Dir: PChar);
  473. begin
  474.   SetDosError(SysDirCreate(Dir));
  475. end;
  476.  
  477. // RemoveDir removes the subdirectory with the path specified by Dir.
  478. // Errors are reported in DosError.
  479.  
  480. procedure RemoveDir(Dir: PChar);
  481. begin
  482.   SetDosError(SysDirDelete(Dir));
  483. end;
  484.  
  485. // GetArgCount returns the number of parameters passed to the program on
  486. // the command line.
  487.  
  488. function GetArgCount: Integer;
  489. begin
  490.   GetArgCount := SysCmdlnCount;
  491. end;
  492.  
  493. // GetArgStr returns the Index'th parameter from the command line, or an
  494. // empty string if Index is less than zero or greater than GetArgCount.
  495. // If Index is zero, GetArgStr returns the filename of the current
  496. // module. The maximum length of the string returned in Dest is given by
  497. // the MaxLen parameter. The returned value is Dest.
  498.  
  499. function GetArgStr(Dest: PChar; Index: Integer; MaxLen: Word): PChar;
  500. var
  501.   S: ShortString;
  502. begin
  503.   if MaxLen > 255 then
  504.     MaxLen := 255;
  505.   SysCmdlnParam(Index, S);
  506.   if Length(S) > MaxLen then S[0] := Chr(MaxLen);
  507.   GetArgStr := StrPCopy(Dest, S);
  508. end;
  509.  
  510. // GetEnvVar returns a pointer to the value of a specified environment
  511. // variable, i.e. a pointer to the first character after the equals sign
  512. // (=) in the environment entry given by VarName. VarName is case
  513. // insensitive. GetEnvVar returns NIL if the specified environment
  514. // variable does not exist.
  515.  
  516. function GetEnvVar(VarName: PChar): PChar;
  517. var
  518.   L: Word;
  519.   P: PChar;
  520. begin
  521.   L := StrLen(VarName);
  522.   P := SysGetEnvironment;
  523.   if P <> nil then
  524.     while P^ <> #0 do
  525.     begin
  526.       if (StrLIComp(P, VarName, L) = 0) and (P[L] = '=') then
  527.       begin
  528.         Result := P + L + 1;
  529.         Exit;
  530.       end;
  531.       Inc(P, StrLen(P) + 1);
  532.     end;
  533.   Result := nil;
  534. end;
  535.  
  536. end.
  537.