Created
February 23, 2024 14:57
-
-
Save KnightsWhoSayNi0/68dcdb65e182c9674b6dc7309ee837bf to your computer and use it in GitHub Desktop.
Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
/** | |
* @author John Godman | |
* Simple file token parser. | |
*/ | |
public class Main | |
{ | |
public static final int BUF_SIZE = 80, | |
EOF = -1, // end of file see javadoc | |
NO_VALUE = -1, | |
TOKENS_PER_LINE = 3; // how many tokens to print on each line | |
public static final String PATH = "/home/john/Documents/projects/test/input.txt"; | |
public static void main(String[] args) throws FileNotFoundException, | |
IOException | |
{ | |
// input file | |
File filein = new File(PATH); | |
FileReader fileDS = new FileReader(filein); | |
BufferedReader rdr = new BufferedReader(fileDS); | |
char[] buffer = new char[BUF_SIZE]; | |
int offset = 0, tokenStart = NO_VALUE, tokenEnd = NO_VALUE, // offset + token ptrs | |
tokenCount = 0; | |
while (rdr.read(buffer, offset, (BUF_SIZE - offset)) != EOF) | |
{ | |
offset = 0; // reset our offset | |
for (int i = 0; i < BUF_SIZE; i++) | |
{ | |
if (buffer[i] != ' ') // delimiter | |
{ | |
if (tokenStart == NO_VALUE) | |
tokenStart = i; | |
if (buffer[i] == 0) | |
tokenEnd = i; | |
} else | |
tokenEnd = i; | |
if (tokenEnd != NO_VALUE) | |
{ | |
for (int j = tokenStart; j < tokenEnd; j++) | |
System.out.print(buffer[j]); | |
System.out.print(" "); | |
if (++tokenCount == TOKENS_PER_LINE) | |
{ | |
System.out.println(); | |
tokenCount = 0; | |
} | |
tokenStart = tokenEnd = NO_VALUE; // reset token ptrs | |
} | |
} | |
char[] tempbuf = new char[BUF_SIZE]; // create a temporary buffer for swapping | |
if (tokenStart != NO_VALUE && tokenEnd == NO_VALUE) | |
{ // place tail token into head of buffer & offset the next read | |
int k; | |
for (k = tokenStart; k < BUF_SIZE; k++) | |
tempbuf[k - tokenStart] = buffer[k]; | |
offset = k - tokenStart; | |
} | |
buffer = tempbuf; | |
tokenStart = tokenEnd = NO_VALUE; // reset token ptrs | |
} | |
rdr.close(); | |
fileDS.close(); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment