fix sonarlint issues, add IDerivedWaveform interface

This commit is contained in:
2020-11-29 10:25:48 +01:00
parent b2e269b67c
commit 6c5032da10
22 changed files with 271 additions and 233 deletions

View File

@ -14,11 +14,11 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.NavigableMap;
import java.util.Stack;
import java.util.TreeMap;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
@ -37,39 +37,30 @@ import com.minres.scviewer.database.RelationType;
*/
public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder {
/** The Constant TIME_RES. */
private static final Long TIME_RES = 1000L; // ps;
private static final Long TIME_RES = 1000L; // ps
/** The db. */
private IWaveformDb db;
/** The module stack. */
private Stack<String> moduleStack;
private ArrayDeque<String> moduleStack;
/** The signals. */
private List<IWaveform> signals;
/** The max time. */
private long maxTime;
/**
* Instantiates a new VCD db.
*/
public VCDDbLoader() {
}
private static boolean isGzipped(File f) {
InputStream is = null;
try {
is = new FileInputStream(f);
try (InputStream is = new FileInputStream(f)) {
byte [] signature = new byte[2];
int nread = is.read( signature ); //read the gzip signature
return nread == 2 && signature[ 0 ] == (byte) 0x1f && signature[ 1 ] == (byte) 0x8b;
} catch (IOException e) {
}
catch (IOException e) {
return false;
} finally {
try { is.close();} catch (IOException e) { }
}
}
@ -79,21 +70,27 @@ public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder {
*/
@SuppressWarnings("unchecked")
@Override
public boolean load(IWaveformDb db, File file) throws Exception {
public boolean load(IWaveformDb db, File file) throws InputFormatException {
if(file.isDirectory() || !file.exists()) return false;
this.db=db;
this.maxTime=0;
String name = file.getCanonicalFile().getName();
if(!(name.endsWith(".vcd") ||
name.endsWith(".vcdz") ||
name.endsWith(".vcdgz") ||
name.endsWith(".vcd.gz")) )
return false;
signals = new Vector<IWaveform>();
moduleStack= new Stack<String>();
FileInputStream fis = new FileInputStream(file);
boolean res = new VCDFileParser(false).load(isGzipped(file)?new GZIPInputStream(fis):fis, this);
moduleStack=null;
boolean res = false;
try {
String name = file.getCanonicalFile().getName();
if(!(name.endsWith(".vcd") ||
name.endsWith(".vcdz") ||
name.endsWith(".vcdgz") ||
name.endsWith(".vcd.gz")) )
return false;
signals = new Vector<>();
moduleStack= new ArrayDeque<>();
FileInputStream fis = new FileInputStream(file);
res = new VCDFileParser(false).load(isGzipped(file)?new GZIPInputStream(fis):fis, this);
moduleStack=null;
} catch(IOException e) {
moduleStack=null;
throw new InputFormatException();
}
if(!res) throw new InputFormatException();
// calculate max time of database
for(IWaveform waveform:signals) {
@ -159,9 +156,8 @@ public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder {
@SuppressWarnings("unchecked")
@Override
public Integer newNet(String name, int i, int width) {
String netName = moduleStack.empty()? name: moduleStack.lastElement()+"."+name;
String netName = moduleStack.isEmpty()? name: moduleStack.peek()+"."+name;
int id = signals.size();
assert(width>=0);
if(width==0) {
signals.add( i<0 ? new VCDSignal<DoubleVal>(db, id, netName, width) :
new VCDSignal<DoubleVal>((VCDSignal<DoubleVal>)signals.get(i), id, netName));
@ -191,7 +187,7 @@ public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder {
Long time = currentTime* TIME_RES;
signal.addSignalChange(time, value);
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.vcd.ITraceBuilder#appendTransition(int, long, com.minres.scviewer.database.vcd.BitVector)
*/
@ -202,7 +198,7 @@ public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder {
Long time = currentTime* TIME_RES;
signal.addSignalChange(time, new DoubleVal(value));
}
/* (non-Javadoc)
* @see com.minres.scviewer.database.IWaveformDbLoader#getAllRelationTypes()
*/

View File

@ -11,6 +11,7 @@
package com.minres.scviewer.database.vcd;
import java.io.*;
import java.text.ParseException;
import java.util.*;
import com.minres.scviewer.database.BitValue;
@ -19,7 +20,7 @@ import com.minres.scviewer.database.BitVector;
class VCDFileParser {
private StreamTokenizer tokenizer;
private IVCDDatabaseBuilder traceBuilder;
private HashMap<String, Integer> nameToNetMap = new HashMap<String, Integer>();
private HashMap<String, Integer> nameToNetMap = new HashMap<>();
private long picoSecondsPerIncrement;
private boolean stripNetWidth;
private boolean replaceColon;
@ -50,19 +51,19 @@ class VCDFileParser {
}
}
private void parseScope() throws Exception {
private void parseScope() throws IOException, ParseException {
nextToken(); // Scope type (ignore)
nextToken();
traceBuilder.enterModule(tokenizer.sval);
match("$end");
}
private void parseUpscope() throws Exception {
private void parseUpscope() throws IOException, ParseException {
match("$end");
traceBuilder.exitModule();
}
private void parseVar() throws Exception {
private void parseVar() throws IOException {
nextToken(); // type
String type = tokenizer.sval;
nextToken(); // size
@ -72,11 +73,12 @@ class VCDFileParser {
nextToken();
String id = tokenizer.sval;
nextToken();
String netName = tokenizer.sval;
StringBuilder sb = new StringBuilder();
sb.append(tokenizer.sval);
while (nextToken() && !tokenizer.sval.equals("$end")) {
netName+=tokenizer.sval;
sb.append(tokenizer.sval);
}
String netName = sb.toString();
Integer net = nameToNetMap.get(id);
if (net == null) { // We've never seen this net before
int openBracket = netName.indexOf('[');
@ -86,9 +88,9 @@ class VCDFileParser {
}
if(replaceColon) {
if (openBracket != -1) {
netName = netName.substring(0, openBracket).replaceAll(":", ".")+netName.substring(openBracket);
netName = netName.substring(0, openBracket).replace(":", ".")+netName.substring(openBracket);
} else
netName=netName.replaceAll(":", ".");
netName=netName.replace(":", ".");
}
nameToNetMap.put(id, traceBuilder.newNet(netName, -1, width));
} else {
@ -97,25 +99,28 @@ class VCDFileParser {
}
}
private void parseComment() throws Exception {
private void parseComment() throws IOException {
nextToken();
String s = tokenizer.sval;
StringBuilder s = new StringBuilder();
s.append(tokenizer.sval);
nextToken();
while(!tokenizer.sval.equals("$end")){
s+=" "+tokenizer.sval;
s.append(" ").append(tokenizer.sval);
nextToken();
}
replaceColon|=s.contains("ARTERIS Architecture");
replaceColon|=s.toString().contains("ARTERIS Architecture");
}
private void parseTimescale() throws Exception {
private void parseTimescale() throws IOException {
nextToken();
String s = tokenizer.sval;
StringBuilder sb = new StringBuilder();
sb.append(tokenizer.sval);
nextToken();
while(!tokenizer.sval.equals("$end")){
s+=" "+tokenizer.sval;
sb.append(" ").append(tokenizer.sval);
nextToken();
}
String s = sb.toString();
switch (s.charAt(s.length() - 2)){
case 'p': // Nano-seconds
picoSecondsPerIncrement = 1;
@ -141,7 +146,7 @@ class VCDFileParser {
picoSecondsPerIncrement *= Long.parseLong(s);
}
private boolean parseDefinition() throws Exception {
private boolean parseDefinition() throws IOException, ParseException {
nextToken();
if (tokenizer.sval.equals("$scope"))
parseScope();
@ -166,7 +171,7 @@ class VCDFileParser {
return true;
}
private boolean parseTransition() throws Exception {
private boolean parseTransition() throws IOException {
if (!nextToken()) return false;
if (tokenizer.sval.charAt(0) == '#') { // If the line begins with a #, this is a timestamp.
currentTime = Long.parseLong(tokenizer.sval.substring(1)) * picoSecondsPerIncrement;
@ -179,19 +184,13 @@ class VCDFileParser {
}
if (tokenizer.sval.equals("$dumpvars") || tokenizer.sval.equals("$end"))
return true;
String value, id;
if (tokenizer.sval.charAt(0) == 'b') {
// Multiple value net. Value appears first, followed by space,
// then identifier
String value;
String id;
if (tokenizer.sval.charAt(0) == 'b' || tokenizer.sval.charAt(0) == 'r') {
// Multiple value net. Value appears first, followed by space, then identifier
value = tokenizer.sval.substring(1);
nextToken();
id = tokenizer.sval;
}else if (tokenizer.sval.charAt(0) == 'r') {
// Multiple value net. Value appears first, followed by space,
// then identifier
value = tokenizer.sval.substring(1);
nextToken();
id = tokenizer.sval;
} else {
// Single value net. identifier first, then value, no space.
value = tokenizer.sval.substring(0, 1);
@ -199,10 +198,8 @@ class VCDFileParser {
}
Integer net = nameToNetMap.get(id);
if (net == null) {
System.out.println("unknown net " + id + " value " + value);
if (net == null)
return true;
}
int netWidth = traceBuilder.getNetWidth(net);
if(netWidth==0) {
@ -253,14 +250,14 @@ class VCDFileParser {
return true;
}
private void match(String value) throws Exception {
private void match(String value) throws ParseException, IOException {
nextToken();
if (!tokenizer.sval.equals(value))
throw new Exception("Line "+tokenizer.lineno()+": parse error, expected "+value+" got "+tokenizer.sval);
throw new ParseException("Line "+tokenizer.lineno()+": parse error, expected "+value+" got "+tokenizer.sval, tokenizer.lineno());
}
private boolean nextToken() throws IOException {
return tokenizer.nextToken() != StreamTokenizer.TT_EOF;
}
};
}

View File

@ -46,18 +46,16 @@ public class VCDSignal<T extends IEvent> extends HierNode implements IWaveform {
fullName=name;
this.id=id;
this.width=width;
this.values=new TreeMap<Long, IEvent[]>();
this.values=new TreeMap<>();
}
public VCDSignal(VCDSignal<T> other, int id, String name) {
public VCDSignal(VCDSignal<T> o, int id, String name) {
super(name);
fullName=name;
this.id=id;
assert(other instanceof VCDSignal<?>);
VCDSignal<T> o = (VCDSignal<T>)other;
this.width=o.width;
this.values=o.values;
this.db=other.getDb();
this.db=o.getDb();
}
@Override
@ -105,13 +103,13 @@ public class VCDSignal<T extends IEvent> extends HierNode implements IWaveform {
public IEvent[] getEventsBeforeTime(Long time) {
Entry<Long, IEvent[]> e = values.floorEntry(time);
if(e==null)
return null;
return new IEvent[] {};
else
return values.floorEntry(time).getValue();
return values.floorEntry(time).getValue();
}
@Override
public Boolean equals(IWaveform other) {
public boolean isSame(IWaveform other) {
return( other instanceof VCDSignal<?> && this.getId().equals(other.getId()));
}