Changeset 1722 for trunk/bindings


Ignore:
Timestamp:
28/10/11 14:49:56 (7 months ago)
Author:
Janik Zikovsky
Message:

Refs #294: writeData(string,string) handles empty strings without throwing. Allowed getType(char). Fixed getStrAttr() to return the right length of the string, no 0-char padding.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/cpp/NeXusFile.cpp

    r1719 r1722  
    6363  template<> 
    6464  NXDLL_EXPORT NXnumtype getType(char number) { 
     65    return CHAR; 
     66    /* This causes runtime errors for getAttr(string, string). 
    6567    stringstream msg; 
    6668    msg << "NeXus::getType() does not know type of \"char\" " << number; 
    6769    throw Exception(msg.str()); 
    68   } 
    69 #endif /* _MSC_VER */ 
     70     */ 
     71  } 
     72#endif 
     73 /* _MSC_VER */ 
    7074 
    7175  // template specialisations for types we know  
     
    340344} 
    341345 
    342 void File::writeData(const string& name, const string& value) { 
    343   if (value.empty()) { 
    344     throw Exception("Supplied empty value to makeData"); 
    345   } 
     346void File::writeData(const string& name, const string& value) 
     347{ 
     348  string my_value(value); 
     349  // Allow empty strings by defaulting to a space 
     350  if (my_value.empty()) 
     351    my_value = " "; 
    346352  vector<int> dims; 
    347   dims.push_back(value.size()); 
     353  dims.push_back(static_cast<int>(my_value.size())); 
    348354  this->makeData(name, CHAR, dims, true); 
    349355 
    350   string my_value(value); 
    351356  this->putData(&(my_value[0])); 
    352357 
    353358  this->closeData(); 
    354359} 
     360 
    355361 
    356362 
     
    11111117  } 
    11121118  char* value = new char[info.length + 1]; 
    1113   this->getAttr(info, value, info.length+1); 
    1114   res = string(value, info.length); 
    1115   delete[] value; 
     1119  try 
     1120  { 
     1121    this->getAttr(info, value, info.length+1); 
     1122  } 
     1123  catch (Exception& e) 
     1124  { 
     1125    //Avoid memory leak 
     1126    delete [] value; 
     1127    throw e; //re-throw 
     1128  } 
     1129 
     1130  //res = string(value, info.length); 
     1131  //allow the constructor to find the ending point of the string. Janik Zikovsky, sep 22, 2010 
     1132  res = string(value); 
     1133  delete [] value; 
     1134 
    11161135  return res; 
    11171136} 
Note: See TracChangeset for help on using the changeset viewer.