|
6 | 6 |
|
7 | 7 | #include <assert.h> |
8 | 8 |
|
| 9 | +#include <cstring> |
| 10 | + |
9 | 11 | using namespace iRICLib; |
10 | 12 |
|
11 | 13 | namespace { |
12 | 14 |
|
13 | 15 | static const std::string ECNODE = "ErrorCode"; |
14 | 16 |
|
| 17 | +void setupStringBuffer(const std::vector<std::string>& vals, cgsize_t* dims, std::vector<char>* buffer) |
| 18 | +{ |
| 19 | + cgsize_t maxLen = 0; |
| 20 | + for (int i = 0; i < vals.size(); ++i) { |
| 21 | + const std::string& s = vals.at(i); |
| 22 | + if (i == 0 || s.length() > maxLen) { |
| 23 | + maxLen = s.length(); |
| 24 | + } |
| 25 | + } |
| 26 | + *(dims + 0) = maxLen; |
| 27 | + *(dims + 1) = static_cast<cgsize_t> (vals.size()); |
| 28 | + buffer->assign(maxLen * vals.size(), ' '); |
| 29 | + for (int i = 0; i < vals.size(); ++i) { |
| 30 | + const std::string& s = vals.at(i); |
| 31 | + memcpy(buffer->data() + maxLen * i, s.c_str(), s.length()); |
| 32 | + } |
| 33 | +} |
| 34 | + |
15 | 35 | } // namespace |
16 | 36 |
|
17 | 37 | int CgnsFile::Sol_Read_Count(int* count) |
@@ -78,6 +98,36 @@ int CgnsFile::Sol_Read_BaseIterative_Real(int step, const char *name, double* va |
78 | 98 | return 2; |
79 | 99 | } |
80 | 100 |
|
| 101 | +int CgnsFile::Sol_Read_BaseIterative_StringLen(int step, const char* name, int* length) |
| 102 | +{ |
| 103 | + if (step > impl->m_solId) { |
| 104 | + return 1; |
| 105 | + } |
| 106 | + for (const BaseIterativeT<std::string>& bit : impl->m_solBaseIterStrings) { |
| 107 | + if (bit.name() == name) { |
| 108 | + const std::string& v = bit.values().at(step - 1); |
| 109 | + *length = v.length(); |
| 110 | + return 0; |
| 111 | + } |
| 112 | + } |
| 113 | + return 2; |
| 114 | +} |
| 115 | + |
| 116 | +int CgnsFile::Sol_Read_BaseIterative_String(int step, const char* name, char* strvalue) |
| 117 | +{ |
| 118 | + if (step > impl->m_solId) { |
| 119 | + return 1; |
| 120 | + } |
| 121 | + for (const BaseIterativeT<std::string>& bit : impl->m_solBaseIterStrings) { |
| 122 | + if (bit.name() == name) { |
| 123 | + const std::string& v = bit.values().at(step - 1); |
| 124 | + std::strcpy(strvalue, v.c_str()); |
| 125 | + return 0; |
| 126 | + } |
| 127 | + } |
| 128 | + return 2; |
| 129 | +} |
| 130 | + |
81 | 131 | int CgnsFile::Sol_Read_GridCoord2d(int step, double* x, double* y) |
82 | 132 | { |
83 | 133 | int ier = cg_goto(impl->m_fileId, impl->m_baseId, "Zone_t", impl->m_zoneId, |
@@ -242,6 +292,31 @@ int CgnsFile::Sol_Write_BaseIterative_Real(const char *name, double value) |
242 | 292 | return cg_array_write(data.name().c_str(), RealDouble, 1, &dimVec, data.values().data()); |
243 | 293 | } |
244 | 294 |
|
| 295 | +int CgnsFile::Sol_Write_BaseIterative_String(const char* name, const char* value) |
| 296 | +{ |
| 297 | + bool found = false; |
| 298 | + BaseIterativeT<std::string> data(name); |
| 299 | + for (BaseIterativeT<std::string>& bit : impl->m_solBaseIterStrings) { |
| 300 | + if (bit.name() == name) { |
| 301 | + bit.addValue(std::string(value)); |
| 302 | + data = bit; |
| 303 | + found = true; |
| 304 | + } |
| 305 | + } |
| 306 | + if (! found) { |
| 307 | + BaseIterativeT<std::string> newData(name); |
| 308 | + newData.addValue(std::string(value)); |
| 309 | + impl->m_solBaseIterStrings.push_back(newData); |
| 310 | + data = newData; |
| 311 | + } |
| 312 | + // write the value. |
| 313 | + cgsize_t dimVec[2]; |
| 314 | + std::vector<char> buffer; |
| 315 | + setupStringBuffer(data.values(), dimVec, &buffer); |
| 316 | + impl->gotoBaseIter(); |
| 317 | + return cg_array_write(data.name().c_str(), Character, 2, dimVec, buffer.data()); |
| 318 | +} |
| 319 | + |
245 | 320 | int CgnsFile::Sol_Write_GridCoord2d(double *x, double *y) |
246 | 321 | { |
247 | 322 | return impl->m_solutionWriter->Sol_Write_GridCoord2d(x, y); |
|
0 commit comments