[Oberon] Removing unnecessary and embedded RETURNs.

peter at easthope.ca peter at easthope.ca
Sun Nov 12 14:58:18 CET 2023


This is my alternative Mail.ReadText with no RETURN.  ReportError was 
helpful in debugging; probably can be deleted now.

Apology for the verbose comments.  Added to help a non-expert 
understand the non-obvious activity.  =8~)

I wonder whether this use of CASE is good practise.  Observations?  

Thanks,                   ... P.L.

	PROCEDURE ReportError(c: CHAR);
	BEGIN
		Texts.WriteString(W, "Mail.ReadText(): LF absent at CASE "); Texts.Write(W, c);
		Texts.Write(W, "."); Texts.WriteLn(W);
		Texts.Append(Oberon.Log, W.buf); HALT(6)
	END ReportError;
	
	(* According to Post Office Protocol, the end of a message body is
		marked by character sequence CR LF "." CR LF.
		A message line can begin with a period.  To distinguish from end-of-message, 
		the message sender prepends an additional period which is removed before the 
		message is displayed to the recipient.  ReadText is external to this byte padding.
		https://en.wikipedia.org/wiki/Post_Office_Protocol
		https://tools.ietf.org/html/rfc1939#section-3 *)
	PROCEDURE ReadText(S: NetTools.Session; VAR R: Files.Rider);
		VAR
			buffer: ARRAY BufLen OF CHAR;
			len, readLen, i: SIGNED32;
			ch: CHAR;
			progress: CHAR; (*
				"0"= continuing
				"1"= CR
				"2"= CR LF
				"3"= CR LF "."
				"4"= CR LF "." CR
				"5"= CR LF "." CR LF = end-of-message; or no more characters *)
	BEGIN
		readLen := 0;
		i := readLen;
		progress := "0";
		WHILE progress < "5" DO (* Write byte to the MailMessages file. *)
			IF i = readLen THEN (* More characters required. *)
				len := NetSystem.Available(S.C);
				IF (len = 0) & (NetSystem.State(S.C) # NetSystem.inout) THEN
					progress := "5"
				ELSE
					IF len > (BufLen-2) THEN readLen := BufLen-2 ELSE readLen := len END;
					NetSystem.ReadBytes(S.C, 0, readLen, buffer);
					DEC(len, readLen);
					i := 0
				END
			ELSE (* i < readLen. character available *)
				ch := buffer[i];
				CASE progress OF
					"0": IF ch = Strings.CR THEN progress := "1" END |
					"1": IF ch = Strings.LF THEN progress := "2" ELSE ReportError(progress) END |
					"2": IF ch = Strings.CR THEN progress := "1"
						ELSIF ch = "." THEN progress := "3" ELSE progress := "0" END |
					"3": IF ch = Strings.CR THEN progress := "4" ELSE progress := "0" END |
					"4": IF ch = Strings.LF THEN progress := "5" ELSE ReportError(progress) END
				END;
				Files.Write(R, ch);
				INC(i)
			END
		END
	END ReadText;

- 
VoIP:   +1 604 670 0140
work: https://en.wikibooks.org/wiki/User:PeterEasthope



More information about the Oberon mailing list