------------------------------------------------------------------------------ This method also has to store the resulting string internally.
| 251 | //------------------------------------------------------------------------------ |
| 252 | // This method also has to store the resulting string internally. |
| 253 | const char* vtkMINCImageAttributes::ConvertDataArrayToString(vtkDataArray* array) |
| 254 | { |
| 255 | const char* resultStr = ""; |
| 256 | vtkIdType n = array->GetNumberOfTuples(); |
| 257 | if (n == 0) |
| 258 | { |
| 259 | return resultStr; |
| 260 | } |
| 261 | |
| 262 | int dataType = array->GetDataType(); |
| 263 | if (dataType == VTK_CHAR) |
| 264 | { |
| 265 | vtkCharArray* charArray = vtkArrayDownCast<vtkCharArray>(array); |
| 266 | if (charArray) |
| 267 | { |
| 268 | resultStr = charArray->GetPointer(0); |
| 269 | // Check to see if string has a terminal null (the null might be |
| 270 | // part of the attribute, or stored in the following byte) |
| 271 | if ((n > 0 && resultStr[n - 1] == '\0') || (charArray->GetSize() > n && resultStr[n] == '\0')) |
| 272 | { |
| 273 | return resultStr; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | std::ostringstream os; |
| 279 | |
| 280 | for (vtkIdType i = 0; i < n; i++) |
| 281 | { |
| 282 | double val = array->GetComponent(i, 0); |
| 283 | if (dataType == VTK_DOUBLE || dataType == VTK_FLOAT) |
| 284 | { |
| 285 | // Use NetCDF's usual precision for printing the values |
| 286 | char storage[128]; |
| 287 | if (dataType == VTK_DOUBLE) |
| 288 | { |
| 289 | auto result = vtk::format_to_n(storage, 128, "{:.15g}", val); |
| 290 | *result.out = '\0'; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | auto result = vtk::format_to_n(storage, 128, "{:.7g}", val); |
| 295 | *result.out = '\0'; |
| 296 | } |
| 297 | // Add a decimal if there isn't one, to distinguish from int |
| 298 | for (char* cp = storage; *cp != '.'; cp++) |
| 299 | { |
| 300 | if (*cp == '\0') |
| 301 | { |
| 302 | *cp++ = '.'; |
| 303 | *cp = '\0'; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | os << storage; |
| 308 | } |
| 309 | else if (dataType == VTK_CHAR) |
| 310 | { |
no test coverage detected