home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 45 / SuperCD45.iso / talleres / prog_alternat / Obs2 / OBS2.MOD
Encoding:
Text File  |  1999-10-31  |  4.2 KB  |  131 lines

  1. MODULE Obs2;
  2. (* 
  3. Test program to try out techniques needed to implement a string format procedure.
  4. *)
  5.    
  6. IMPORT In,Out,st := Strings;
  7.  
  8. TYPE
  9.    STRING50 = ARRAY 50 OF CHAR;
  10.  
  11.    someP = POINTER TO someR;   
  12.    someR = RECORD                     
  13.      datatype : STRING50;
  14.    END;
  15.    
  16.    intP = POINTER TO intR;
  17.    intR = RECORD(someR)
  18.        i : INTEGER;
  19.    END;
  20.  
  21.    chP = POINTER TO chR;
  22.    chR = RECORD(someR);
  23.        c : CHAR;
  24.    END;
  25.  
  26.    strP = POINTER TO strR;
  27.    strR = RECORD(someR);
  28.         s : STRING50;
  29.    END; 
  30.  
  31.   arr10T = ARRAY 10 OF someP;
  32.   arrT = ARRAY OF someP;
  33.  
  34.       
  35. PROCEDURE ProcessArray( VAR s : st.StringT; arr : arrT );
  36. VAR
  37.   pos, ActualLen, i, SearchStart : LONGINT;
  38.   msg, ints : STRING50;
  39. BEGIN
  40.  (* Display the total length of the array, arr                                                    *)
  41.  (* This is for testing purposes only                                                             *)
  42.   Out.Ln; 
  43.   Out.String( "Array Length = " );
  44.   Out.Int( LEN(arr), 0 );    
  45.  (* Now find the number, ActualLen, of the array-slots that contain data (i.e. that aren't NIL )  *)
  46.   ActualLen := 0;
  47.   WHILE arr[ActualLen] # NIL DO INC(ActualLen);  END;
  48.   Out.Ln;
  49.   Out.String( "Actual Array Length = " );
  50.   Out.Int( ActualLen, 0 );   
  51.   Out.Ln;
  52.  (* ===== Start of string formatting code ===== *)
  53.   SearchStart := 0;                            (* initially search for % from index 0 in string   *)
  54.  
  55.   FOR i := 0 TO ActualLen-1 DO                 (* FOR each object in the array...                 *)
  56.      pos := st.PosChar('%', s, SearchStart );  (* ...find the pos of the % char...                *)
  57.      IF arr[i] IS chP THEN                     (* IF array item is chP...                         *)
  58.           (*--- some debug output ---*)
  59.           Out.String( "It's a char! - " );
  60.           Out.Char( arr[i](chP)^.c ); (* ^ here is optional *)    
  61.           Out.Ln;  
  62.           (*-------------------------*)
  63.         s[pos-1] := arr[i](chP)^.c;            (* ...insert char, c, at pos of % in string         *)
  64.      ELSIF arr[i] IS intP THEN                 (* ELSE IF array item is intP...                    *) 
  65.           (*--- some debug output ---*)
  66.           Out.String( "It's an integer! - " );
  67.           Out.Int( arr[i](intP)^.i, 2 );  
  68.           Out.Ln;
  69.           (*-------------------------*)
  70.         st.Delete(s, pos, 1);                  (* ...convert i to string and insert it at pos of % *)
  71.         st.Str(arr[i](intP)^.i, ints ); 
  72.         st.Insert(ints, s, pos );
  73.      ELSIF arr[i] IS strP THEN                 (* ELSE IF array item is strP...                    *) 
  74.           (*--- some debug output ---*)
  75.           Out.String( "It's a string! - " );
  76.           Out.String( arr[i](strP)^.s );  
  77.           Out.Ln;                                               
  78.           (*-------------------------*)
  79.         st.Delete(s, pos, 1);                  (* ...insert it at pos of % in the string           *)
  80.         st.Insert(arr[i](strP)^.s, s, pos );        
  81.      ELSE                                      (* ELSE array item is something else, so say so!    *)
  82.         Out.String( "It's summat else!" );  
  83.   END;
  84.      SearchStart := pos + 1;                   (* reset search index when looking for % next time  *) 
  85.   END; (* FOR 0 TO... *)
  86. END ProcessArray;
  87.  
  88.  
  89. PROCEDURE ProgMain*;
  90. VAR
  91.   chPtr : chP;
  92.   intPtr : intP;
  93.   strPtr : strP;
  94.   arr : arr10T;
  95.   s : STRING50;
  96.   i : INTEGER;
  97. BEGIN
  98.   (* create and init some records *)
  99.   NEW(chPtr);
  100.   NEW(intPtr);
  101.   NEW(strPtr);
  102.   chPtr.datatype := 'char'; 
  103.   chPtr^.c := 'X';         (* ^ here is optional *)
  104.   intPtr.datatype := 'integer';
  105.   intPtr.i := 100;
  106.   strPtr.datatype := 'string';
  107.   strPtr.s := 'Hello there, folks!';
  108.   
  109.   (* create a format string, s *)
  110.   s := '% "%" is worth รบ%!';
  111.   Out.String( 'Before processing, the string is: ' );
  112.   Out.String( s );
  113.   Out.Ln;
  114.   
  115.   (* init the array, arr, by filling the slots with NILs *)
  116.   FOR i := 0 TO LEN(arr)-1 DO
  117.       arr[i] := NIL;
  118.   END;
  119.   
  120.   arr[0] := strPtr;
  121.   arr[1] := chPtr;
  122.   arr[2] := intPtr;
  123.   ProcessArray( s, arr );
  124.   Out.Ln;
  125.   Out.String( 'After processing, the string is: ' );  
  126.   Out.String( s );
  127.    
  128. END ProgMain;
  129.  
  130. END Obs2.
  131.