From ff87e7251050e0b9a914744a88cbdfe4c3065799 Mon Sep 17 00:00:00 2001 From: Eyck Jentzsch Date: Mon, 15 Nov 2021 10:52:59 +0100 Subject: [PATCH] allow multiple loading of same format --- .../.classpath | 2 +- .../OSGI-INF/component.xml | 6 +- .../database/text/TextDbLoaderFactory.java | 77 +++++++++++++++++++ .../OSGI-INF/component.xml | 6 +- .../scviewer/database/vcd/VCDDbLoader.java | 3 +- .../database/vcd/VCDDbLoaderFactory.java | 77 +++++++++++++++++++ .../OSGI-INF/component.xml | 2 +- .../database/IWaveformDbLoaderFactory.java | 33 ++++++++ .../database/internal/WaveformDb.java | 20 ++--- 9 files changed, 208 insertions(+), 18 deletions(-) create mode 100644 plugins/com.minres.scviewer.database.text/src/com/minres/scviewer/database/text/TextDbLoaderFactory.java create mode 100644 plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoaderFactory.java create mode 100644 plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/IWaveformDbLoaderFactory.java diff --git a/plugins/com.minres.scviewer.database.text/.classpath b/plugins/com.minres.scviewer.database.text/.classpath index 3f4be5c..047d7de 100644 --- a/plugins/com.minres.scviewer.database.text/.classpath +++ b/plugins/com.minres.scviewer.database.text/.classpath @@ -2,12 +2,12 @@ + - diff --git a/plugins/com.minres.scviewer.database.text/OSGI-INF/component.xml b/plugins/com.minres.scviewer.database.text/OSGI-INF/component.xml index 2d260e4..12f186e 100644 --- a/plugins/com.minres.scviewer.database.text/OSGI-INF/component.xml +++ b/plugins/com.minres.scviewer.database.text/OSGI-INF/component.xml @@ -1,7 +1,7 @@ - - + + - + diff --git a/plugins/com.minres.scviewer.database.text/src/com/minres/scviewer/database/text/TextDbLoaderFactory.java b/plugins/com.minres.scviewer.database.text/src/com/minres/scviewer/database/text/TextDbLoaderFactory.java new file mode 100644 index 0000000..2324ff4 --- /dev/null +++ b/plugins/com.minres.scviewer.database.text/src/com/minres/scviewer/database/text/TextDbLoaderFactory.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2012 IT Just working. + * Copyright (c) 2020 MINRES Technologies GmbH + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IT Just working - initial API and implementation + *******************************************************************************/ +package com.minres.scviewer.database.text; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.GZIPInputStream; + +import com.minres.scviewer.database.IWaveformDbLoader; +import com.minres.scviewer.database.IWaveformDbLoaderFactory; + +/** + * The Class TextDbLoader. + */ +public class TextDbLoaderFactory implements IWaveformDbLoaderFactory { + + /** The Constant x. */ + static final byte[] x = "scv_tr_stream".getBytes(); + + /** + * Checks if f is gzipped. + * + * @param f the f + * @return true, if is gzipped + */ + private static boolean isGzipped(File 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) { + return false; + } + } + + /** + * Can load. + * + * @param inputFile the input file + * @return true, if successful + */ + @Override + public boolean canLoad(File inputFile) { + if (!inputFile.isDirectory() && inputFile.exists()) { + boolean gzipped = isGzipped(inputFile); + try(InputStream stream = gzipped ? new GZIPInputStream(new FileInputStream(inputFile)) : new FileInputStream(inputFile)){ + byte[] buffer = new byte[x.length]; + int readCnt = stream.read(buffer, 0, x.length); + if (readCnt == x.length) { + for (int i = 0; i < x.length; i++) + if (buffer[i] != x[i]) + return false; + } + return true; + } catch (Exception e) { + return false; + } + } + return false; + } + + @Override + public IWaveformDbLoader getLoader() { + return new TextDbLoader(); + } +} diff --git a/plugins/com.minres.scviewer.database.vcd/OSGI-INF/component.xml b/plugins/com.minres.scviewer.database.vcd/OSGI-INF/component.xml index a7d284e..d998cde 100644 --- a/plugins/com.minres.scviewer.database.vcd/OSGI-INF/component.xml +++ b/plugins/com.minres.scviewer.database.vcd/OSGI-INF/component.xml @@ -1,7 +1,7 @@ - - + + - + diff --git a/plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoader.java b/plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoader.java index 1bf27e9..57f19f1 100644 --- a/plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoader.java +++ b/plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoader.java @@ -164,7 +164,8 @@ public class VCDDbLoader implements IWaveformDbLoader, IVCDDatabaseBuilder { @Override public void enterModule(String tokenString) { if(moduleStack.isEmpty()) { - if("SystemC".compareTo(tokenString)!=0) moduleStack.push(tokenString); + if("SystemC".compareTo(tokenString)!=0) + moduleStack.push(tokenString); } else moduleStack.push(moduleStack.peek()+"."+tokenString); diff --git a/plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoaderFactory.java b/plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoaderFactory.java new file mode 100644 index 0000000..5bab7ec --- /dev/null +++ b/plugins/com.minres.scviewer.database.vcd/src/com/minres/scviewer/database/vcd/VCDDbLoaderFactory.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2015-2021 MINRES Technologies GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * MINRES Technologies GmbH - initial API and implementation + *******************************************************************************/ +package com.minres.scviewer.database.vcd; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.GZIPInputStream; + +import com.minres.scviewer.database.IWaveformDbLoader; +import com.minres.scviewer.database.IWaveformDbLoaderFactory; + +/** + * The Class VCDDb. + */ +public class VCDDbLoaderFactory implements IWaveformDbLoaderFactory { + /** + * Checks if is gzipped. + * + * @param f the f + * @return true, if is gzipped + */ + private static boolean isGzipped(File 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) { + return false; + } + } + + + /** + * Can load. + * + * @param inputFile the input file + * @return true, if successful + */ + @Override + public boolean canLoad(File inputFile) { + if(!inputFile.isDirectory() || inputFile.exists()) { + String name = inputFile.getName(); + if(!(name.endsWith(".vcd") || + name.endsWith(".vcdz") || + name.endsWith(".vcdgz") || + name.endsWith(".vcd.gz")) ) + return false; + boolean gzipped = isGzipped(inputFile); + try(InputStream stream = gzipped ? new GZIPInputStream(new FileInputStream(inputFile)) : new FileInputStream(inputFile)){ + byte[] buffer = new byte[8]; + if (stream.read(buffer, 0, buffer.length) == buffer.length) { + return buffer[0]=='$'; + } + } catch (Exception e) { + return false; + } + } + return false; + } + + + @Override + public IWaveformDbLoader getLoader() { + return new VCDDbLoader(); + } +} diff --git a/plugins/com.minres.scviewer.database/OSGI-INF/component.xml b/plugins/com.minres.scviewer.database/OSGI-INF/component.xml index e313c27..d3f7df9 100644 --- a/plugins/com.minres.scviewer.database/OSGI-INF/component.xml +++ b/plugins/com.minres.scviewer.database/OSGI-INF/component.xml @@ -1,5 +1,5 @@ - + diff --git a/plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/IWaveformDbLoaderFactory.java b/plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/IWaveformDbLoaderFactory.java new file mode 100644 index 0000000..c983e67 --- /dev/null +++ b/plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/IWaveformDbLoaderFactory.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2015-2021 MINRES Technologies GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * MINRES Technologies GmbH - initial API and implementation + *******************************************************************************/ +package com.minres.scviewer.database; + +import java.io.File; + +/** + * A factory for creating IWaveformDb objects. + */ +public interface IWaveformDbLoaderFactory { + + /** + * Check if the loader produced by this factory can load the given file. + * + * @param inputFile the input file + * @return true, if successful + */ + public boolean canLoad(File inputFile); + /** + * Gets the database. + * + * @return the database + */ + IWaveformDbLoader getLoader(); +} diff --git a/plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/internal/WaveformDb.java b/plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/internal/WaveformDb.java index 3c5a976..093f9c2 100644 --- a/plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/internal/WaveformDb.java +++ b/plugins/com.minres.scviewer.database/src/com/minres/scviewer/database/internal/WaveformDb.java @@ -25,6 +25,7 @@ import com.minres.scviewer.database.IHierNode; import com.minres.scviewer.database.IWaveform; import com.minres.scviewer.database.IWaveformDb; import com.minres.scviewer.database.IWaveformDbLoader; +import com.minres.scviewer.database.IWaveformDbLoaderFactory; import com.minres.scviewer.database.RelationType; /** @@ -33,7 +34,7 @@ import com.minres.scviewer.database.RelationType; public class WaveformDb extends HierNode implements IWaveformDb, PropertyChangeListener { /** The loaders. */ - private static List loaders = new LinkedList<>(); + private static List loaderFactories = new LinkedList<>(); /** The loaded. */ private boolean loaded; @@ -52,8 +53,8 @@ public class WaveformDb extends HierNode implements IWaveformDb, PropertyChangeL * * @param loader the loader */ - public synchronized void bind(IWaveformDbLoader loader) { - loaders.add(loader); + public synchronized void bind(IWaveformDbLoaderFactory loader) { + loaderFactories.add(loader); } /** @@ -61,8 +62,8 @@ public class WaveformDb extends HierNode implements IWaveformDb, PropertyChangeL * * @param loader the loader */ - public synchronized void unbind(IWaveformDbLoader loader) { - loaders.remove(loader); + public synchronized void unbind(IWaveformDbLoaderFactory loader) { + loaderFactories.remove(loader); } /** @@ -70,8 +71,8 @@ public class WaveformDb extends HierNode implements IWaveformDb, PropertyChangeL * * @return the loaders */ - public static List getLoaders() { - return Collections.unmodifiableList(loaders); + public static List getLoaders() { + return Collections.unmodifiableList(loaderFactories); } /** @@ -124,8 +125,9 @@ public class WaveformDb extends HierNode implements IWaveformDb, PropertyChangeL @Override public boolean load(File inp) { boolean retval = true; - for (IWaveformDbLoader loader : loaders) { - if (loader.canLoad(inp)) { + for (IWaveformDbLoaderFactory loaderFactory : loaderFactories) { + if (loaderFactory.canLoad(inp)) { + IWaveformDbLoader loader = loaderFactory.getLoader(); loader.addPropertyChangeListener(this); try { loader.load(this, inp);