Skip to content

Commit d4091b3

Browse files
Glur, MarcoGlur, Marco
authored andcommitted
Inchi does not officially support fractions of seconds timeout in options. For safety, input which is formatted accordingly will be reformatted to pass next higher integer value.
1 parent 1bb9a64 commit d4091b3

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

storage/inchi/src/main/java/org/openscience/cdk/inchi/JniInChIInputAdapter.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import net.sf.jniinchi.JniInchiException;
2828
import net.sf.jniinchi.JniInchiInput;
2929

30-
import java.text.DecimalFormat;
3130
import java.util.List;
3231
import java.util.StringTokenizer;
3332

@@ -55,23 +54,11 @@ public JniInChIInputAdapter(List<INCHI_OPTION> options) throws JniInchiException
5554

5655
private static boolean isTimeoutOptions(String op) {
5756
if (op == null || op.length() < 2) return false;
58-
int pos = 0;
59-
int len = op.length();
60-
if (op.charAt(pos) == 'W')
61-
pos++;
62-
while (pos < len && Character.isDigit(op.charAt(pos)))
63-
pos++;
64-
if (pos < len && (op.charAt(pos) == '.' || op.charAt(pos) == ','))
65-
pos++;
66-
while (pos < len && Character.isDigit(op.charAt(pos)))
67-
pos++;
68-
return pos == len;
69-
}
57+
return op.charAt(0) == 'W';
7058

71-
private static boolean isSubSecondTimeout(String op) {
72-
return op.indexOf('.') >= 0 || op.indexOf(',') >= 0;
7359
}
7460

61+
7562
private static String checkOptions(final String ops) throws JniInchiException {
7663
if (ops == null) {
7764
throw new IllegalArgumentException("Null options");
@@ -96,18 +83,14 @@ private static String checkOptions(final String ops) throws JniInchiException {
9683
sbOptions.append(" ");
9784
}
9885
} else if (isTimeoutOptions(op)) {
99-
// only reformat if we actually have a decimal
100-
if (isSubSecondTimeout(op)) {
101-
// because the JNI-InChI library is expecting an platform number, format it as such
102-
Double time = Double.parseDouble(op.substring(1));
103-
DecimalFormat format = new DecimalFormat("#.##");
104-
sbOptions.append(FLAG_CHAR).append('W').append(format.format(time));
105-
} else {
106-
sbOptions.append(FLAG_CHAR).append(op);
107-
}
108-
hasUserSpecifiedTimeout = true;
109-
if (tok.hasMoreTokens()) {
110-
sbOptions.append(" ");
86+
final double time = Math.ceil(Double.parseDouble(op.substring(1)));
87+
// fix #653: safer to use whole seconds, rounded to next bigger integer
88+
if (time >= 0.0) {
89+
sbOptions.append(FLAG_CHAR).append(String.format("W%.0f", time));
90+
hasUserSpecifiedTimeout = true;
91+
if (tok.hasMoreTokens()) {
92+
sbOptions.append(" ");
93+
}
11194
}
11295
}
11396
// 1,5 tautomer option

storage/inchi/src/test/java/org/openscience/cdk/inchi/InChIGeneratorTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,7 @@ public void s_penta_2_3_diene_expl_h() throws Exception {
896896
}
897897
}
898898

899-
// if this test hits the timeout it's likely the users Locale is mixed, the
900-
// InChI library was loaded in one mode and java is in another, the issue
901-
// is InChI takes timeout in seconds and fractional seconds will be either
902-
// 0.1 or 0,1 depending on locale.
903-
@Test(timeout = 500)
899+
@Test(timeout = 1500)
904900
public void timeout() throws Exception {
905901
IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
906902
SmilesParser smipar = new SmilesParser(bldr);

0 commit comments

Comments
 (0)