home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1995 November
/
PCWK1195.iso
/
inne
/
win95
/
sieciowe
/
hotja32.lzh
/
hotjava
/
classsrc
/
java
/
tools
/
mem4
/
macroguy.java
< prev
next >
Wrap
Text File
|
1995-08-11
|
3KB
|
116 lines
/*
* @(#)Macroguy.java 1.1 95/05/11 Mary Campione
*
* Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package java.tools.mem4;
import java.io.*;
class Macroguy {
static final int LEN = 1024;
static final String DEFINE = "#define ";
static final String INCLUDE = "#include ";
static Dict definitions;
Macroguy() {
if (definitions == null)
definitions = new Dict();
}
void processFile(InputStream is) {
int c;
StringBuffer buf = new StringBuffer(LEN);
while ((c = is.read()) != -1) {
buf.appendChar(c);
if (c == '\n') {
System.out.print(processLine(buf.toString()));
buf = new StringBuffer(LEN);
}
}
}
String processLine(String str) {
String returnValue = "";
if (str.startsWith(DEFINE)) {
processDefine(str.substring(DEFINE.length()));
} else if (str.startsWith(INCLUDE)) {
processInclude(str.substring(INCLUDE.length()));
} else
returnValue = parseForVars(str);
return returnValue;
}
void processDefine(String str) {
String var, value;
var = str.substring(0, StringUtils.findNextNonAlphaFrom(str, 0));
value = str.substring(var.length()).trim();
value = parseForVars(value);
definitions.addPair(var, value);
}
void processInclude(String str) {
include(str.trim());
}
String parseForVars(String str)
{
String returnValue = "";
String varname;
int i = 0, c;
while (i < str.length()) {
// we have a variable
if ((c = str.charAt(i)) == '$') {
int j;
if (i < (str.length() - 1)) // skip $
i++;
if (str.charAt(i) == '\\') { // false alarm
returnValue += "$";
i++;
} else {
j = StringUtils.findNextNonAlphaFrom(str, i);
varname = str.substring(i, j);
returnValue += definitions.valueOf(varname);
i = j;
}
} else {
returnValue += str.substring(i, i+1);
i++;
}
}
return returnValue;
}
void include(String filename) {
File f = new File(filename);
FileInputStream in;
int c;
if (!f.exists()) return;
in = new FileInputStream(f);
new Macroguy().processFile(in);
in.close();
}
}