home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / SRC / MiscPMOS.mod < prev    next >
Text File  |  1998-01-27  |  4KB  |  106 lines

  1. IMPLEMENTATION MODULE MiscPMOS;
  2.  
  3.         (************************************************)
  4.         (*                                              *)
  5.         (*      Miscellaneous PMOS procedures           *)
  6.         (*                                              *)
  7.         (*  Programmer:         P. Moylan               *)
  8.         (*  Last edited:        27 January 1998         *)
  9.         (*  Status:             OK                      *)
  10.         (*                                              *)
  11.         (************************************************)
  12.  
  13. IMPORT SYSTEM;
  14.  
  15. FROM OS2 IMPORT
  16.     (* const*)  SEM_INDEFINITE_WAIT,
  17.     (* type *)  HMTX,
  18.     (* proc *)  DosCreateMutexSem, DosRequestMutexSem, DosReleaseMutexSem;
  19.  
  20. VAR mutex: HMTX;
  21.  
  22. (************************************************************************)
  23. (*                              STRING COPY                             *)
  24. (************************************************************************)
  25.  
  26. PROCEDURE CopyString (source: ARRAY OF CHAR;  VAR (*OUT*) dest: ARRAY OF CHAR);
  27.  
  28.     (* Copies a string, with truncation or null termination as needed.  *)
  29.     (* This function is provided in order to help software portability, *)
  30.     (* i.e. to avoid having to rewrite code for no reason other than    *)
  31.     (* a change of compilers.                                           *)
  32.  
  33.     VAR j, last: CARDINAL;  AddNull: BOOLEAN;
  34.  
  35.     BEGIN
  36.         last := HIGH(dest);
  37.         AddNull := HIGH(source) < last;
  38.         IF AddNull THEN last := HIGH(source) END (*IF*);
  39.         FOR j := 0 TO last DO dest[j] := source[j] END (*FOR*);
  40.         IF AddNull THEN dest[last+1] := CHR(0) END (*IF*);
  41.     END CopyString;
  42.  
  43. (************************************************************************)
  44. (*                          STRING COMPARISON                           *)
  45. (************************************************************************)
  46.  
  47. PROCEDURE Compare (first, second: ARRAY OF SYSTEM.LOC): INTEGER;
  48.  
  49.     (* Returns >0 if first>second, 0 if first=second, <0 if             *)
  50.     (* first<second.                                                    *)
  51.  
  52.     VAR j: CARDINAL;  val1, val2: SYSTEM.CARD8;
  53.  
  54.     BEGIN
  55.         j := 0;
  56.         LOOP
  57.             IF j > HIGH(first) THEN
  58.                 IF j <= HIGH(second) THEN RETURN -1
  59.                 ELSE RETURN 0;
  60.                 END (*IF*);
  61.             ELSIF j > HIGH(second) THEN RETURN +1
  62.             ELSE
  63.                 val1 := SYSTEM.CAST(SYSTEM.CARD8, first[j]);
  64.                 val2 := SYSTEM.CAST(SYSTEM.CARD8, second[j]);
  65.                 IF val1 > val2 THEN RETURN +1
  66.                 ELSIF val1 < val2 THEN RETURN -1
  67.                 ELSE INC(j);
  68.                 END (*IF*);
  69.             END (*IF*);
  70.         END (*LOOP*);
  71.     END Compare;
  72.  
  73. (************************************************************************)
  74. (*                   MISCELLANEOUS LOW-LEVEL OPERATIONS                 *)
  75. (************************************************************************)
  76.  
  77. PROCEDURE EnterCriticalSection (): CARDINAL;
  78.  
  79.     (* Saves the processor flags word, including the current "interrupt *)
  80.     (* enable" status, on the caller's stack, and returns with          *)
  81.     (* interrupts disabled.   NOTE: this procedure and the following    *)
  82.     (* one should be used as a matched pair.                            *)
  83.  
  84.     BEGIN
  85.         DosRequestMutexSem (mutex, SEM_INDEFINITE_WAIT);
  86.         RETURN 0;
  87.     END EnterCriticalSection;
  88.  
  89. (************************************************************************)
  90.  
  91. PROCEDURE LeaveCriticalSection (SavedProcessorStatus: CARDINAL);
  92.  
  93.     (* Restores the processor flags word, including the "interrupt      *)
  94.     (* enable" status, from the stack.  NOTE: this procedure and the    *)
  95.     (* one above should be used as a matched pair.                      *)
  96.  
  97.     BEGIN
  98.         DosReleaseMutexSem (mutex);
  99.     END LeaveCriticalSection;
  100.  
  101. (************************************************************************)
  102.  
  103. BEGIN
  104.     DosCreateMutexSem (NIL, mutex, 0, FALSE);
  105. END MiscPMOS.
  106.