Rev 265 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* Copyright (C) 2011.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
package test.display.check;
import test.display.Section;
import uk.me.parabola.imgfmt.app.ImgFileReader;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Read and cache all the strings in the mdr15 section.
*
* Has to be read with regard to the code page that the file is written in.
*/
public class MdrStrings
{
private final ImgFileReader reader
;
private final Section mdr15
;
private final Charset charset
;
private boolean compressedStrings
;
private Map<Integer,
String> stringTable
;
public MdrStrings
(ImgFileReader reader, Section mdr15,
Charset charset
) {
this.
reader = reader
;
this.
mdr15 = mdr15
;
this.
charset = charset
;
}
/**
* Get a string from the string section.
*
* @param off The offset in the text section.
* @return The resultant string.
*/
public String getTextString
(int off
) {
if (compressedStrings
)
return "";
if (stringTable ==
null)
readAllStrings
();
return stringTable.
get(off
);
}
public void readAllStrings
() {
if (compressedStrings
)
return; // we don't know how to do this yet.
stringTable =
new LinkedHashMap<Integer,
String>();
// Remember where we are
long rem = reader.
position();
long start = mdr15.
getStart();
long end = mdr15.
getEnd();
int magic = mdr15.
getMagic();
if ((magic
& 0x1
) !=
0) {
// We can't read these, so remember the fact and return
compressedStrings =
true;
return;
}
reader.
position(start
);
byte[] buffer =
new byte[1024];
int len =
0;
int offset =
0;
while (reader.
position() < end
) {
byte b = reader.
get();
if (b ==
0) {
String str =
new String(buffer,
0, len, charset
);
stringTable.
put(offset, str
);
offset =
(int) (reader.
position() - start
);
len =
0;
} else {
buffer
[len++
] = b
;
}
}
// Return to our callers position
reader.
position(rem
);
}
}