📄 chapter17.html
字号:
}
}
<font color=#0000ff>class</font> SourceCodeFile {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> String
startMarker = <font color=#004488>"</font><font color=#004488>/</font><font color=#004488>/:"</font>, <font color=#009900>// Start of source file</font>
endMarker = <font color=#004488>"} </font><font color=#004488>/</font><font color=#004488>/</font><font color=#004488>/:~"</font>, <font color=#009900>// End of source</font>
endMarker2 = <font color=#004488>"}; </font><font color=#004488>/</font><font color=#004488>/</font><font color=#004488>/:~"</font>, <font color=#009900>// C++ file end</font>
beginContinue = <font color=#004488>"} </font><font color=#004488>/</font><font color=#004488>/</font><font color=#004488>/:Continued"</font>,
endContinue = <font color=#004488>"</font><font color=#004488>/</font><font color=#004488>/</font><font color=#004488>/:Continuing"</font>,
packMarker = <font color=#004488>"###"</font>, <font color=#009900>// Packed file header tag</font>
eol = <font color=#009900>// Line separator on current system</font>
System.getProperty(<font color=#004488>"line.separator"</font>),
filesep = <font color=#009900>// System's file path separator</font>
System.getProperty(<font color=#004488>"file.separator"</font>);
<font color=#0000ff>public</font> <font color=#0000ff>static</font> String copyright = <font color=#004488>""</font>;
<font color=#0000ff>static</font> {
<font color=#0000ff>try</font> {
BufferedReader cr =
<font color=#0000ff>new</font> BufferedReader(
<font color=#0000ff>new</font> FileReader(<font color=#004488>"Copyright.txt"</font>));
String crin;
<font color=#0000ff>while</font>((crin = cr.readLine()) != <font color=#0000ff>null</font>)
copyright += crin + <font color=#004488>"\n"</font>;
cr.close();
} <font color=#0000ff>catch</font>(Exception e) {
copyright = <font color=#004488>""</font>;
}
}
<font color=#0000ff>private</font> String filename, dirname,
contents = <font color=#0000ff>new</font> String();
<font color=#0000ff>private</font> <font color=#0000ff>static</font> String chapter = <font color=#004488>"c02"</font>;
<font color=#009900>// The file name separator from the old system:</font>
<font color=#0000ff>public</font> <font color=#0000ff>static</font> String oldsep;
<font color=#0000ff>public</font> String toString() {
<font color=#0000ff>return</font> dirname + filesep + filename;
}
<font color=#009900>// Constructor for parsing from document file:</font>
<font color=#0000ff>public</font> SourceCodeFile(String firstLine,
BufferedReader in) {
dirname = chapter;
<font color=#009900>// Skip past marker:</font>
filename = firstLine.substring(
startMarker.length()).trim();
<font color=#009900>// Find space that terminates file name:</font>
<font color=#0000ff>if</font>(filename.indexOf(' ') != -1)
filename = filename.substring(
0, filename.indexOf(' '));
System.out.println(<font color=#004488>"found: "</font> + filename);
contents = firstLine + eol;
<font color=#0000ff>if</font>(copyright.length() != 0)
contents += copyright + eol;
String s;
<font color=#0000ff>boolean</font> foundEndMarker = <font color=#0000ff>false</font>;
<font color=#0000ff>try</font> {
<font color=#0000ff>while</font>((s = in.readLine()) != <font color=#0000ff>null</font>) {
<font color=#0000ff>if</font>(s.startsWith(startMarker))
Pr.error(<font color=#004488>"No end of file marker for "</font> +
filename);
<font color=#009900>// For this program, no spaces before </font>
<font color=#009900>// the "package" keyword are allowed</font>
<font color=#009900>// in the input source code:</font>
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(s.startsWith(<font color=#004488>"package"</font>)) {
<font color=#009900>// Extract package name:</font>
String pdir = s.substring(
s.indexOf(' ')).trim();
pdir = pdir.substring(
0, pdir.indexOf(';')).trim();
<font color=#009900>// Capture the chapter from the package</font>
<font color=#009900>// ignoring the 'com' subdirectories:</font>
<font color=#0000ff>if</font>(!pdir.startsWith(<font color=#004488>"com"</font>)) {
<font color=#0000ff>int</font> firstDot = pdir.indexOf('.');
<font color=#0000ff>if</font>(firstDot != -1)
chapter =
pdir.substring(0,firstDot);
<font color=#0000ff>else</font>
chapter = pdir;
}
<font color=#009900>// Convert package name to path name:</font>
pdir = pdir.replace(
'.', filesep.charAt(0));
System.out.println(<font color=#004488>"package "</font> + pdir);
dirname = pdir;
}
contents += s + eol;
<font color=#009900>// Move past continuations:</font>
<font color=#0000ff>if</font>(s.startsWith(beginContinue))
<font color=#0000ff>while</font>((s = in.readLine()) != <font color=#0000ff>null</font>)
<font color=#0000ff>if</font>(s.startsWith(endContinue)) {
contents += s + eol;
<font color=#0000ff>break</font>;
}
<font color=#009900>// Watch for end of code listing:</font>
<font color=#0000ff>if</font>(s.startsWith(endMarker) ||
s.startsWith(endMarker2)) {
foundEndMarker = <font color=#0000ff>true</font>;
<font color=#0000ff>break</font>;
}
}
<font color=#0000ff>if</font>(!foundEndMarker)
Pr.error(
<font color=#004488>"End marker not found before EOF"</font>);
System.out.println(<font color=#004488>"Chapter: "</font> + chapter);
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"Error reading line"</font>);
}
}
<font color=#009900>// For recovering from a packed file:</font>
<font color=#0000ff>public</font> SourceCodeFile(BufferedReader pFile) {
<font color=#0000ff>try</font> {
String s = pFile.readLine();
<font color=#0000ff>if</font>(s == <font color=#0000ff>null</font>) <font color=#0000ff>return</font>;
<font color=#0000ff>if</font>(!s.startsWith(packMarker))
Pr.error(<font color=#004488>"Can't find "</font> + packMarker
+ <font color=#004488>" in "</font> + s);
s = s.substring(
packMarker.length()).trim();
dirname = s.substring(0, s.indexOf('#'));
filename = s.substring(s.indexOf('#') + 1);
dirname = dirname.replace(
oldsep.charAt(0), filesep.charAt(0));
filename = filename.replace(
oldsep.charAt(0), filesep.charAt(0));
System.out.println(<font color=#004488>"listing: "</font> + dirname
+ filesep + filename);
<font color=#0000ff>while</font>((s = pFile.readLine()) != <font color=#0000ff>null</font>) {
<font color=#009900>// Watch for end of code listing:</font>
<font color=#0000ff>if</font>(s.startsWith(endMarker) ||
s.startsWith(endMarker2)) {
contents += s;
<font color=#0000ff>break</font>;
}
contents += s + eol;
}
} <font color=#0000ff>catch</font>(IOException e) {
System.err.println(<font color=#004488>"Error reading line"</font>);
}
}
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font> hasFile() {
<font color=#0000ff>return</font> filename != <font color=#0000ff>null</font>;
}
<font color=#0000ff>public</font> String directory() { <font color=#0000ff>return</font> dirname; }
<font color=#0000ff>public</font> String filename() { <font color=#0000ff>return</font> filename; }
<font color=#0000ff>public</font> String contents() { <font color=#0000ff>return</font> contents; }
<font color=#009900>// To write to a packed file:</font>
<font color=#0000ff>public</font> <font color=#0000ff>void</font> writePacked(DataOutputStream out) {
<font color=#0000ff>try</font> {
out.writeBytes(
packMarker + dirname + <font color=#004488>"#"</font>
+ filename + eol);
out.writeBytes(contents);
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"writing "</font> + dirname +
filesep + filename);
}
}
<font color=#009900>// To generate the actual file:</font>
<font color=#0000ff>public</font> <font color=#0000ff>void</font> writeFile(String rootpath) {
File path = <font color=#0000ff>new</font> File(rootpath, dirname);
path.mkdirs();
PrintWriter p =
IO.psOpen(<font color=#0000ff>new</font> File(path, filename));
p.print(contents);
IO.close(p);
}
}
<font color=#0000ff>class</font> DirMap {
<font color=#0000ff>private</font> Hashtable t = <font color=#0000ff>new</font> Hashtable();
<font color=#0000ff>private</font> String rootpath;
DirMap() {
rootpath = System.getProperty(<font color=#004488>"user.dir"</font>);
}
DirMap(String alternateDir) {
rootpath = alternateDir;
}
<font color=#0000ff>public</font> <font color=#0000ff>void</font> add(SourceCodeFile f){
String path = f.directory();
<font color=#0000ff>if</font>(!t.containsKey(path))
t.put(path, <font color=#0000ff>new</font> Vector());
((Vector)t.get(path)).addElement(f);
}
<font color=#0000ff>public</font> <font color=#0000ff>void</font> writePackedFile(String fname) {
DataOutputStream packed = IO.dosOpen(fname);
<font color=#0000ff>try</font> {
packed.writeBytes(<font color=#004488>"###Old Separator:"</font> +
SourceCodeFile.filesep + <font color=#004488>"###\n"</font>);
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"Writing separator to "</font> + fname);
}
Enumeration e = t.keys();
<font color=#0000ff>while</font>(e.hasMoreElements()) {
String dir = (String)e.nextElement();
System.out.println(
<font color=#004488>"Writing directory "</font> + dir);
Vector v = (Vector)t.get(dir);
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < v.size(); i++) {
SourceCodeFile f =
(SourceCodeFile)v.elementAt(i);
f.writePacked(packed);
}
}
IO.close(packed);
}
<font color=#009900>// Write all the files in their directories:</font>
<font color=#0000ff>public</font> <font color=#0000ff>void</font> write() {
Enumeration e = t.keys();
<font color=#0000ff>while</font>(e.hasMoreElements()) {
String dir = (String)e.nextElement();
Vector v = (Vector)t.get(dir);
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < v.size(); i++) {
SourceCodeFile f =
(SourceCodeFile)v.elementAt(i);
f.writeFile(rootpath);
}
<font color=#009900>// Add file indicating file quantity</font>
<font color=#009900>// written to this directory as a check:</font>
IO.close(IO.dosOpen(
<font color=#0000ff>new</font> File(<font color=#0000ff>new</font> File(rootpath, dir),
Integer.toString(v.size())+<font color=#004488>".files"</font>)));
}
}
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> CodePackager {
<font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> String usageString =
<font color=#004488>"usage: java CodePackager packedFileName"</font> +
<font color=#004488>"\nExtracts source code files from packed \n"</font> +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -