Task: Strip block comments





Java: import java.io.*;

public class StripBlockComments{
    public static String readFile(String filename) {
	BufferedReader reader = new BufferedReader(new FileReader(filename));
	try {
	    StringBuilder fileContents = new StringBuilder();
	    char[] buffer = new char[4096];
	    while (reader.read(buffer, 0, 4096) > 0) {
		fileContents.append(buffer);
	    }
	    return fileContents.toString();
	} finally {
	    reader.close();
	}
    }

    public static String stripComments(String beginToken, String endToken,
				       String input) {
	StringBuilder output = new StringBuilder();
	while (true) {
	    int begin = input.indexOf(beginToken);
	    int end = input.indexOf(endToken, begin+beginToken.length());
	    if (begin == -1 || end == -1) {
		output.append(input);
		return output.toString();
	    }
	    output.append(input.substring(0, begin));
	    input = input.substring(end + endToken.length());
	}
    }

    public static void main(String[] args) {
	if (args.length < 3) {
	    System.out.println("Usage: BeginToken EndToken FileToProcess");
	    System.exit(1);
	}

	String begin = args[0];
	String end = args[1];
	String input = args[2];

	try {
	    System.out.println(stripComments(begin, end, readFile(input)));
	} catch (Exception e) {
	    e.printStackTrace();
	    System.exit(1);
	}
    }
}






Fortran:       SUBROUTINE UNBLOCK(THIS,THAT)	
Copies from file INF to file OUT, record by record, except skipping null output records.
       CHARACTER*(*) THIS,THAT	
       INTEGER LOTS			
       PARAMETER (LOTS = 6666)		
       CHARACTER*(LOTS) ACARD,ALINE	
       INTEGER LC,LL,L		
       INTEGER L1,L2		
       INTEGER NC,NL		
       LOGICAL BLAH		
       INTEGER MSG,KBD,INF,OUT		
       COMMON /IODEV/MSG,KBD,INF,OUT	
        NC = 0		
        NL = 0		
        BLAH = .FALSE.	
Chug through the input.
   10   READ(INF,11,END = 100) LC,ACARD(1:MIN(LC,LOTS))	
   11   FORMAT (Q,A)		
        NC = NC + 1		
        IF (LC.GT.LOTS) THEN	
          WRITE (MSG,12) NC,LC,LOTS	
   12     FORMAT ("Record ",I0," has length ",I0,"
          LC = LOTS			
        END IF			
Chew through ACARD according to mood.
        LL = 0		
        L2 = 0		
   20   L1 = L2 + 1	
        IF (L1.LE.LC) THEN	
          L2 = L1		
          IF (BLAH) THEN	
   21       IF (L2 + LEN(THAT) - 1 .LE. LC) THEN	
              IF (ACARD(L2:L2 + LEN(THAT) - 1).EQ.THAT) THEN	
                BLAH = .FALSE.		
                L2 = L2 + LEN(THAT) - 1	
                GO TO 20		
              END IF		
              L2 = L2 + 1	
              GO TO 21		
            END IF	
           ELSE		
   22       IF (L2 + LEN(THIS) - 1 .LE. LC) THEN	
              IF (ACARD(L2:L2 + LEN(THIS) - 1).EQ.THIS) THEN	
                BLAH = .TRUE.		
                L = L2 - L1		
                ALINE(LL + 1:LL + L) = ACARD(L1:L2 - 1)	
                LL = LL + L		
                L2 = L2 + LEN(THIS) - 1	
                GO TO 20		
              END IF		
              L2 = L2 + 1	
              GO TO 22		
            END IF	
            L = LC - L1 + 1	
            ALINE(LL + 1:LL + L) = ACARD(L1:LC)	
            LL = LL + L		
          END IF	
        END IF	
Cast forth some output.
        IF (LL.GT.0) THEN	
          WRITE (OUT,23) ALINE(1:LL)	
   23     FORMAT (">",A,"<") 		
          NL = NL + 1			
        END IF        		
        GO TO 10	
Completed.
  100   WRITE (MSG,101) NC,NL	
  101   FORMAT (I0," read, ",I0," written.")
      END       

      PROGRAM TEST
      INTEGER MSG,KBD,INF,OUT
      COMMON /IODEV/MSG,KBD,INF,OUT
      KBD = 5
      MSG = 6
      INF = 10
      OUT = 11
      OPEN (INF,FILE="Source.txt",STATUS="OLD",ACTION="READ")
      OPEN (OUT,FILE="Src.txt",STATUS="REPLACE",ACTION="WRITE")

      CALL UNBLOCK("/*","*/")

      END