|
Can someone please help me convert the following code to C? I am just learning C and this is too overwhelming...
the java code basically reads in a file (specifically for this program, a text file or whatnot for assembly language code), and the program is a pretty printer which creates uniform columns between the assembly code, for example:
[code]
SENT: .WORD 0 ; Initializes the...
pArray: .BLOCK 20 ; Reserves space...
nArray: .BLOCK 20 ; Reserves space...
indexP: .WORD 0 ; Index Register...
[/code]
Here is the java code (when running arguments, you define the file to be brought in and and integer representing the number of spaces for each column.
[code]
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class PrettyPrinter {
final static String CommentDelimiter = ";";
final static String LabelDelimiter = ":";
final static String BLANK = " ";
final static String Delimiters = " \t" + CommentDelimiter;
static int spacing = 2;
public static int MaxOf(int NewLength, int MaxLength) {
if(NewLength > MaxLength) {
return NewLength;
}
else {
return MaxLength;
}
}
public static int MinOf(int NewI, int MinI) {
if(NewI < MinI) {
return NewI;
}
else {
return MinI;
}
}
public static int indexOf(String Source, String Delimiters) {
int result = Integer.MAX_VALUE;
int loc;
for (int i=0; i<Delimiters.length(); i++) {
loc = Source.indexOf(Delimiters.charAt(i));
result = MinOf(loc,result);
}
if(result == Integer.MAX_VALUE) result = -1;
return result;
}
public static int lengthOf(String line, String delimiters, int max) {
int result = max;
int loc;
loc = indexOf(line,delimiters);
if(loc > 0) {
max = MaxOf(loc,max);
line = line.substring(loc+1);
}
return result;
}
public static String pad(String S, int width) {
while(S.length() < width) {
S = S + BLANK;
}
return S;
}
public static String rtrim(String S) {
while((S.length() > 0) && (S.lastIndexOf(BLANK) == (S.length()-1))) {
S = S.substring(0,(S.length()-1));
}
return S;
}
public static void main(String[] args)throws IOException {
File StandardInput = new File(args[0]);
Scanner Input = new Scanner(StandardInput);
String line, comment = "", field = "";
int LabelLength = 0;
int OperatorLength = 0;
int OperandsLength = 0;
int CommentLength = 0;
int LineLength = 0;
int loc;
if(args.length>1) {
spacing = Integer.parseInt(args[1]);
}
while (Input.hasNext( )) {
line=Input.nextLine();
line=line.replace('\t',' ');
line=line.trim() + BLANK;
LineLength = MaxOf(line.length(),LineLength);
loc = line.indexOf(CommentDelimiter);
if(loc >= 0) {
CommentLength = MaxOf((line.length()-loc),CommentLength);
line = line.substring(0,loc) + BLANK;
}
loc = line.indexOf(LabelDelimiter);
if(loc >= 0) {
LabelLength = MaxOf(loc,LabelLength);
line = (line.substring(loc+LabelDelimiter.length())).trim() + BLANK;
}
loc = line.indexOf(BLANK);
if(loc > 0) {
OperatorLength = MaxOf(loc,OperatorLength);
line = (line.substring(loc+1)).trim() + BLANK;
}
loc = line.indexOf(BLANK);
if(loc > 0) {
OperandsLength = MaxOf(loc,OperandsLength);
line = (line.substring(loc+1)).trim() + BLANK;
}
}
LabelLength = LabelLength + 1 + spacing;
OperatorLength = OperatorLength + spacing;
OperandsLength = OperandsLength + spacing;
// System.out.println("End of pass one: "+LabelLength+", "+OperatorLength+", "+OperandsLength+", "+CommentLength+", "+LineLength);
Input = new Scanner(StandardInput);
String output;
while (Input.hasNext( )) {
output = "";
line=Input.nextLine();
line=line.replace('\t',' ');
line=line.trim() + BLANK;
loc = line.indexOf(CommentDelimiter);
if(loc >= 0) {
comment = line.substring(loc);
line = (line.substring(0,loc)).trim() + BLANK;
}
else {
comment = "";
}
if(line.length() > 1) {
loc = line.indexOf(LabelDelimiter);
if(loc >= 0) {
field = line.substring(0,(loc+LabelDelimiter.length()));
line = (line.substring(loc+LabelDelimiter.length())).trim() + BLANK;
}
else {
field = "";
}
output = output + pad(field,LabelLength);
loc = line.indexOf(BLANK);
if(loc > 0) {
field = line.substring(0,loc);
line = (line.substring(loc+1)).trim() + BLANK;
}
else {
field = "";
}
output = output + pad(field,OperatorLength);
loc = line.indexOf(BLANK);
if(loc > 0) {
field = line.substring(0,loc);
line = (line.substring(loc+1)).trim() + BLANK;
}
else {
field = "";
}
output = output + pad(field,OperandsLength);
}
output = output + pad(comment,(LineLength-(LabelLength+OperatorLength+OperandsLength)));
System.out.println(rtrim(output));
}
}
}
[/code]
Please help!!!
|
|
|
|
|
|
|
// |