From f6aa7ec43b2d9e53c5e241858475b1886a0b5ad7 Mon Sep 17 00:00:00 2001
From: marco <marco@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Thu, 22 Apr 2010 07:00:05 +0000
Subject: [PATCH] unit test (and fix) for loading pdb files with only 66
 columns

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2100 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/io/src/mol/pdb_reader.cc         | 16 ++++++++++++----
 modules/io/tests/test_io_pdb.cc          |  8 ++++++++
 modules/io/tests/testfiles/pdb/short.pdb |  2 ++
 3 files changed, 22 insertions(+), 4 deletions(-)
 create mode 100644 modules/io/tests/testfiles/pdb/short.pdb

diff --git a/modules/io/src/mol/pdb_reader.cc b/modules/io/src/mol/pdb_reader.cc
index c8e113652..8c8e7cbca 100644
--- a/modules/io/src/mol/pdb_reader.cc
+++ b/modules/io/src/mol/pdb_reader.cc
@@ -382,11 +382,19 @@ void PDBReader::ParseAndAddAtom(const StringRef& line, int line_num,
   if(is_pqr_) {
     occ=std::make_pair(true, 1.0);
     temp=std::make_pair(true, 0.0);
-    charge=line.substr(54,6).ltrim().to_float();
-    radius=line.substr(60, 6).ltrim().to_float();
+    if (line.length()>=60) {
+      charge=line.substr(54,6).ltrim().to_float();      
+    }
+    if (line.length()>=66) {
+      radius=line.substr(60, 6).ltrim().to_float();      
+    }
   } else {
-    occ=line.substr(54,6).ltrim().to_float();
-    temp=line.substr(60, 6).ltrim().to_float();
+    if (line.length()>=60) {
+      occ=line.substr(54,6).ltrim().to_float();      
+    }
+    if (line.length()>=66) {
+      temp=line.substr(60, 6).ltrim().to_float();      
+    }
   }
   LOGN_TRACE( "line: [" << line << "]" );
   String s_ele;
diff --git a/modules/io/tests/test_io_pdb.cc b/modules/io/tests/test_io_pdb.cc
index fe5c51f3a..f8a5d70ad 100644
--- a/modules/io/tests/test_io_pdb.cc
+++ b/modules/io/tests/test_io_pdb.cc
@@ -171,4 +171,12 @@ BOOST_AUTO_TEST_CASE(anisou_record)
 }
 
 
+BOOST_AUTO_TEST_CASE(only_66_cols)
+{
+  String fname("testfiles/pdb/short.pdb");
+  PDBReader reader(fname);
+  mol::EntityHandle ent=mol::CreateEntity();
+  reader.Import(ent);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/modules/io/tests/testfiles/pdb/short.pdb b/modules/io/tests/testfiles/pdb/short.pdb
new file mode 100644
index 000000000..316a527e8
--- /dev/null
+++ b/modules/io/tests/testfiles/pdb/short.pdb
@@ -0,0 +1,2 @@
+ATOM      1  N   SER     1     -35.012 -34.611  -8.045  1.0   0.0
+ATOM      2  H1  SER     1     -34.315 -35.342  -8.037  1.0   0.0
\ No newline at end of file
-- 
GitLab