Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy and paste to tabs in the CDI display #231

Merged
merged 16 commits into from
Sep 8, 2023
25 changes: 23 additions & 2 deletions src/org/openlcb/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* <p>
* NodeID objects are immutable once created.
*
* @author Bob Jacobsen Copyright 2009, 2010, 2011 2012
* @version $Revision$
* @see org.openlcb.cdi.cmd.Util
*
* @author Bob Jacobsen Copyright 2009, 2010, 2011, 2012, 2023
*/
@Immutable
@ThreadSafe
Expand Down Expand Up @@ -218,4 +219,24 @@ static public void HostToNetworkUint48(byte[] arr, int offset, long value) {
arr[offset+5] = (byte) ((value) & 0xff);
}

/**
* Find the longest starting substring of a List of Strings.
* This is useful for finding e.g. the common prefix of a replication dump
*/
@CheckReturnValue
@NonNull
static public String longestLeadingSubstring(@NonNull java.util.List<String> list) {
String result = list.get(0);
bobjacobsen marked this conversation as resolved.
Show resolved Hide resolved
for (int entry = 1; entry < list.size(); entry++) {
for (int index = Math.min(result.length(), list.get(entry).length()); index >= 0; index--) {
if (list.get(entry).startsWith(result.substring(0,index))) {
// found longest match
result = result.substring(0, index);
break;
}
}
}
return result;
}

}
3 changes: 1 addition & 2 deletions src/org/openlcb/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
* two places.
* <P>
* You have to manually keep this synchronized with the manifest file.
* <P>
*
* @author Bob Jacobsen Copyright 2011 - 2012
* @version $Revision: 17977 $
*/

public class Version {
Expand Down
1 change: 0 additions & 1 deletion src/org/openlcb/cdi/CdiRep.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* object implementing this interface.
*
* @author Bob Jacobsen Copyright 2011
* @version $Revision: -1 $
*/
public interface CdiRep {

Expand Down
30 changes: 21 additions & 9 deletions src/org/openlcb/cdi/cmd/BackupConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -17,12 +18,12 @@
public class BackupConfig {


static public void writeEntry(BufferedWriter outFile, String key, String value) {
static public void writeEntry(BufferedWriter writer, String key, String value) {
try {
outFile.write(Util.escapeString(key));
outFile.write('=');
outFile.write(Util.escapeString(value));
outFile.write('\n');
writer.write(Util.escapeString(key));
writer.write('=');
writer.write(Util.escapeString(value));
writer.write('\n');
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
Expand All @@ -35,25 +36,36 @@ public static void writeConfigToFile(String fileName, ConfigRepresentation repr)

outFile = Files.newBufferedWriter(Paths.get(fileName), Charset.forName("UTF-8"));
final BufferedWriter finalOutFile = outFile;

writeConfigToWriter(finalOutFile, repr);
bobjacobsen marked this conversation as resolved.
Show resolved Hide resolved

outFile.close();
}

public static void writeConfigToWriter(Writer writer, ConfigRepresentation repr) throws
IOException {

final BufferedWriter finalWriter = new BufferedWriter(writer);;
bobjacobsen marked this conversation as resolved.
Show resolved Hide resolved
bobjacobsen marked this conversation as resolved.
Show resolved Hide resolved
repr.visit(new ConfigRepresentation.Visitor() {
@Override
public void visitString(ConfigRepresentation.StringEntry e) {
writeEntry(finalOutFile, e.key, e.getValue());
writeEntry(finalWriter, e.key, e.getValue());
}

@Override
public void visitInt(ConfigRepresentation.IntegerEntry e) {
writeEntry(finalOutFile, e.key, Long.toString(e.getValue()));
writeEntry(finalWriter, e.key, Long.toString(e.getValue()));
}

@Override
public void visitEvent(ConfigRepresentation.EventEntry e) {
writeEntry(finalOutFile, e.key, Utilities.toHexDotsString(e.getValue
writeEntry(finalWriter, e.key, Utilities.toHexDotsString(e.getValue
().getContents()));
}
}
);
outFile.close();

finalWriter.close();
bobjacobsen marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down
10 changes: 7 additions & 3 deletions src/org/openlcb/cdi/cmd/RestoreConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ public static void parseConfigFromFile(@NonNull String filePath, @NonNull Config
callback.onError("Failed to open input file: " + e.toString());
return;
}
parseConfigFromReader(inFile, callback);
}

public static void parseConfigFromReader(@NonNull BufferedReader reader, @NonNull ConfigCallback callback) {
String line = null;
try {
while ((line = inFile.readLine()) != null) {
while ((line = reader.readLine()) != null) {
if (line.charAt(0) == '#') continue;
int pos = line.indexOf('=');
if (pos < 0) {
Expand All @@ -46,9 +50,9 @@ public static void parseConfigFromFile(@NonNull String filePath, @NonNull Config
callback.onConfigEntry(key, value);
}

inFile.close();
reader.close();
} catch (IOException x) {
callback.onError("Error reading input file: " + x.toString());
callback.onError("Error reading for restore: " + x.toString());
return;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/org/openlcb/cdi/cmd/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Created by bracz on 4/9/16.
*
* @see org.openlcb.Utilities
*/
public class Util {
private final static Logger logger = Logger.getLogger(Util.class.getName());
Expand Down
1 change: 0 additions & 1 deletion src/org/openlcb/cdi/jdom/CdiMemConfigReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* by call back.
*
* @author Bob Jacobsen Copyright (C) 2012
* @version $Revision$
*/
public class CdiMemConfigReader {
private final static Logger logger = getLogger(CdiMemConfigReader.class.getName());
Expand Down
1 change: 0 additions & 1 deletion src/org/openlcb/cdi/jdom/JdomCdiReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* JDOM-based OpenLCB loader
*
* @author Bob Jacobsen Copyright 2011
* @version $Revision: -1 $
*/
public class JdomCdiReader {

Expand Down
1 change: 0 additions & 1 deletion src/org/openlcb/cdi/jdom/JdomCdiRep.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* JDOM for reading the underlying XML.
*
* @author Bob Jacobsen Copyright 2011
* @version $Revision: -1 $
*/
public class JdomCdiRep implements CdiRep {

Expand Down
Loading