Commit e8d7517f15fde0bdc024dded70dddf60ec2fe1a4
1 parent
ac370c3a
automatically initialise nul value to correctly handle empty string columns
Showing
2 changed files
with
34 additions
and
16 deletions
include/GFitsTableStringCol.hpp
| ... | ... | @@ -53,8 +53,8 @@ public: |
| 53 | 53 | double real(const int& row, const int& col = 0); |
| 54 | 54 | int integer(const int& row, const int& col = 0); |
| 55 | 55 | std::string* data(void) { return m_data; } |
| 56 | - void nullval(const std::string& value); | |
| 57 | - char* nullval(void) { return m_nulval; } | |
| 56 | + void nulval(const std::string& value); | |
| 57 | + char* nulval(void) { return m_nulval; } | |
| 58 | 58 | |
| 59 | 59 | private: |
| 60 | 60 | // Private methods | ... | ... |
src/fits/GFitsTableStringCol.cpp
| ... | ... | @@ -242,14 +242,21 @@ int GFitsTableStringCol::integer(const int& row, const int& inx) |
| 242 | 242 | |
| 243 | 243 | |
| 244 | 244 | /***********************************************************************//** |
| 245 | - * @brief Set NULL string | |
| 245 | + * @brief Set nul value | |
| 246 | 246 | * |
| 247 | - * @param[in] value Pointer on NULL string | |
| 247 | + * @param[in] value Nul value string | |
| 248 | 248 | * |
| 249 | - * Allows the specification of the FITS table NULL string. If the string | |
| 250 | - * is empty the data will not be screened for NULL values. | |
| 249 | + * Allows the specification of the FITS table nul string. The default nul | |
| 250 | + * strings is an empty string which results in NULL entries in case that | |
| 251 | + * no data has been written. | |
| 252 | + * | |
| 253 | + * @todo To correctly reflect the nul value in the data, the column should | |
| 254 | + * be reloaded. However, the column may have been changed, so in principle | |
| 255 | + * saving is needed. However, we may not want to store the data, hence saving | |
| 256 | + * is also not desired. We thus have to develop a method to update the | |
| 257 | + * column information for a new nul value in place ... | |
| 251 | 258 | ***************************************************************************/ |
| 252 | -void GFitsTableStringCol::nullval(const std::string& value) | |
| 259 | +void GFitsTableStringCol::nulval(const std::string& value) | |
| 253 | 260 | { |
| 254 | 261 | // Allocate nul value |
| 255 | 262 | alloc_nulval(value); |
| ... | ... | @@ -282,6 +289,9 @@ void GFitsTableStringCol::init_members(void) |
| 282 | 289 | m_buffer = NULL; |
| 283 | 290 | m_nulval = NULL; |
| 284 | 291 | |
| 292 | + // Initialise nul value | |
| 293 | + alloc_nulval(""); | |
| 294 | + | |
| 285 | 295 | // Return |
| 286 | 296 | return; |
| 287 | 297 | } |
| ... | ... | @@ -358,6 +368,9 @@ void GFitsTableStringCol::free_members(void) |
| 358 | 368 | /***********************************************************************//** |
| 359 | 369 | * @brief Save table column into FITS file |
| 360 | 370 | * |
| 371 | + * The data is transferred from the internal string vector into a character | |
| 372 | + * transfer buffer that can be handled by the cfitsio routines. | |
| 373 | + * | |
| 361 | 374 | * The table column is only saved if it is linked to a FITS file and if the |
| 362 | 375 | * data are indeed present in the class instance. This avoids saving of data |
| 363 | 376 | * that have not been modified. |
| ... | ... | @@ -369,12 +382,15 @@ void GFitsTableStringCol::save(void) |
| 369 | 382 | // Free buffer |
| 370 | 383 | free_buffer(); |
| 371 | 384 | |
| 372 | - // Allocate buffer | |
| 385 | + // Allocate buffer. This also fills the entire buffer with '\0' | |
| 386 | + // characters. | |
| 373 | 387 | alloc_buffer(); |
| 374 | 388 | |
| 375 | 389 | // Transfer string into buffer |
| 376 | - for (int i = 0; i < m_size; ++i) | |
| 377 | - strncpy(m_buffer[i], m_data[i].c_str(), m_width); | |
| 390 | + for (int i = 0; i < m_size; ++i) { | |
| 391 | + if (m_data[i].length() > 0) | |
| 392 | + strncpy(m_buffer[i], m_data[i].c_str(), m_width); | |
| 393 | + } | |
| 378 | 394 | |
| 379 | 395 | // Save column |
| 380 | 396 | save_column(); |
| ... | ... | @@ -476,7 +492,7 @@ void GFitsTableStringCol::release_data(void) |
| 476 | 492 | |
| 477 | 493 | |
| 478 | 494 | /***********************************************************************//** |
| 479 | - * @brief Allocate null value | |
| 495 | + * @brief Allocate nul value | |
| 480 | 496 | ***************************************************************************/ |
| 481 | 497 | void GFitsTableStringCol::alloc_nulval(const std::string& value) |
| 482 | 498 | { |
| ... | ... | @@ -486,11 +502,13 @@ void GFitsTableStringCol::alloc_nulval(const std::string& value) |
| 486 | 502 | // Mark pointer as free |
| 487 | 503 | m_nulval = NULL; |
| 488 | 504 | |
| 489 | - // If we have valid value, allocate and set nul value | |
| 490 | - if (!value.empty()) { | |
| 491 | - m_nulval = new char[m_width+1]; | |
| 492 | - strncpy(m_nulval, value.c_str(), m_width); | |
| 493 | - } | |
| 505 | + // Allocate and initialise nul value | |
| 506 | + m_nulval = new char[m_width+1]; | |
| 507 | + for (int j = 0; j <= m_width; ++j) | |
| 508 | + m_nulval[j] = '\0'; | |
| 509 | + | |
| 510 | + // Set nul value | |
| 511 | + strncpy(m_nulval, value.c_str(), m_width); | |
| 494 | 512 | |
| 495 | 513 | // Return |
| 496 | 514 | return; | ... | ... |