home *** CD-ROM | disk | FTP | other *** search
- MODULE Obs2;
- (*
- Test program to try out techniques needed to implement a string format procedure.
- *)
-
- IMPORT In,Out,st := Strings;
-
- TYPE
- STRING50 = ARRAY 50 OF CHAR;
-
- someP = POINTER TO someR;
- someR = RECORD
- datatype : STRING50;
- END;
-
- intP = POINTER TO intR;
- intR = RECORD(someR)
- i : INTEGER;
- END;
-
- chP = POINTER TO chR;
- chR = RECORD(someR);
- c : CHAR;
- END;
-
- strP = POINTER TO strR;
- strR = RECORD(someR);
- s : STRING50;
- END;
-
- arr10T = ARRAY 10 OF someP;
- arrT = ARRAY OF someP;
-
-
- PROCEDURE ProcessArray( VAR s : st.StringT; arr : arrT );
- VAR
- pos, ActualLen, i, SearchStart : LONGINT;
- msg, ints : STRING50;
- BEGIN
- (* Display the total length of the array, arr *)
- (* This is for testing purposes only *)
- Out.Ln;
- Out.String( "Array Length = " );
- Out.Int( LEN(arr), 0 );
- (* Now find the number, ActualLen, of the array-slots that contain data (i.e. that aren't NIL ) *)
- ActualLen := 0;
- WHILE arr[ActualLen] # NIL DO INC(ActualLen); END;
- Out.Ln;
- Out.String( "Actual Array Length = " );
- Out.Int( ActualLen, 0 );
- Out.Ln;
- (* ===== Start of string formatting code ===== *)
- SearchStart := 0; (* initially search for % from index 0 in string *)
-
- FOR i := 0 TO ActualLen-1 DO (* FOR each object in the array... *)
- pos := st.PosChar('%', s, SearchStart ); (* ...find the pos of the % char... *)
- IF arr[i] IS chP THEN (* IF array item is chP... *)
- (*--- some debug output ---*)
- Out.String( "It's a char! - " );
- Out.Char( arr[i](chP)^.c ); (* ^ here is optional *)
- Out.Ln;
- (*-------------------------*)
- s[pos-1] := arr[i](chP)^.c; (* ...insert char, c, at pos of % in string *)
- ELSIF arr[i] IS intP THEN (* ELSE IF array item is intP... *)
- (*--- some debug output ---*)
- Out.String( "It's an integer! - " );
- Out.Int( arr[i](intP)^.i, 2 );
- Out.Ln;
- (*-------------------------*)
- st.Delete(s, pos, 1); (* ...convert i to string and insert it at pos of % *)
- st.Str(arr[i](intP)^.i, ints );
- st.Insert(ints, s, pos );
- ELSIF arr[i] IS strP THEN (* ELSE IF array item is strP... *)
- (*--- some debug output ---*)
- Out.String( "It's a string! - " );
- Out.String( arr[i](strP)^.s );
- Out.Ln;
- (*-------------------------*)
- st.Delete(s, pos, 1); (* ...insert it at pos of % in the string *)
- st.Insert(arr[i](strP)^.s, s, pos );
- ELSE (* ELSE array item is something else, so say so! *)
- Out.String( "It's summat else!" );
- END;
- SearchStart := pos + 1; (* reset search index when looking for % next time *)
- END; (* FOR 0 TO... *)
- END ProcessArray;
-
-
- PROCEDURE ProgMain*;
- VAR
- chPtr : chP;
- intPtr : intP;
- strPtr : strP;
- arr : arr10T;
- s : STRING50;
- i : INTEGER;
- BEGIN
- (* create and init some records *)
- NEW(chPtr);
- NEW(intPtr);
- NEW(strPtr);
- chPtr.datatype := 'char';
- chPtr^.c := 'X'; (* ^ here is optional *)
- intPtr.datatype := 'integer';
- intPtr.i := 100;
- strPtr.datatype := 'string';
- strPtr.s := 'Hello there, folks!';
-
- (* create a format string, s *)
- s := '% "%" is worth รบ%!';
- Out.String( 'Before processing, the string is: ' );
- Out.String( s );
- Out.Ln;
-
- (* init the array, arr, by filling the slots with NILs *)
- FOR i := 0 TO LEN(arr)-1 DO
- arr[i] := NIL;
- END;
-
- arr[0] := strPtr;
- arr[1] := chPtr;
- arr[2] := intPtr;
- ProcessArray( s, arr );
- Out.Ln;
- Out.String( 'After processing, the string is: ' );
- Out.String( s );
-
- END ProgMain;
-
- END Obs2.
-