Skip to content

Commit eb83f03

Browse files
Merge pull request #398 from chrisemblhh/buff2str
Improved `buff2str` function Looks good too me too Fixes #377
2 parents 48ca05d + 96f754a commit eb83f03

2 files changed

Lines changed: 34 additions & 31 deletions

File tree

applications/nxingest/nxingest_nexus.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
============================================================================= */
3939
#include "nxingest_nexus.h"
4040

41+
#include <string>
42+
#include <stdint.h>
43+
#include <sstream>
44+
#include <vector>
45+
46+
using std::string;
47+
48+
string buff2str(const void* buff, int rank, const int dim[], int data_type, const char* vector);
49+
4150
// Enumeration for the different calculation available for arrays.
4251
// Positives number are used for specific value of the array.
4352
enum Calculation { Direct = 0, Avg = -1, Min = -2, Max = -3, Std = -4, Sum = -5};
@@ -206,7 +215,8 @@ char* NxClass::readTag(char *input, char *value, int user)
206215
status = NXgetdata (nxFp, buff);
207216
if(status != NXING_OK) throw log.set("readTag", "Can't get the data", nexus_path, "",NXING_ERR_BASE_NEXUS-status);
208217
}
209-
value = buff2str(buff, rank, dim, data_type, vector, value);
218+
const string strval = buff2str(buff, rank, dim, data_type, vector);
219+
strcpy(value, strval.c_str());
210220
log.set("readTag", nx_name, "Return", value).printLevel(NXING_LOG_DEBUG);
211221

212222
return value;
@@ -233,7 +243,7 @@ char* NxClass::getLocation(char *value)
233243

234244

235245
// *****************************************************************************
236-
// Function : NxClass::buff2str
246+
// Function : buff2str
237247
//
238248
// The function will parse the data buffer according to the content of
239249
// vector and return a string.
@@ -244,7 +254,7 @@ char* NxClass::getLocation(char *value)
244254
// [MIN]/[MAX] Minimum/Maximum value.
245255
//
246256
// *****************************************************************************
247-
char* NxClass::buff2str(void* buff, int rank, int dim[], int data_type, char* vector, char* value)
257+
string buff2str(const void* buff, int rank, const int dim[], int data_type, const char* vector)
248258
{
249259
Log log;
250260
try
@@ -253,10 +263,10 @@ char* NxClass::buff2str(void* buff, int rank, int dim[], int data_type, char* ve
253263
// **********************************
254264
if(data_type == NX_CHAR)
255265
{
256-
strcpy( value, (char*) buff);
257-
return value;
266+
return string(static_cast<const char*>(buff));
258267
}
259268
int vec = 0;
269+
std::stringstream stream;
260270
// Parse the vector to check what has to be done with the buffer
261271
// **************************************************************
262272
if(vector == 0 || strlen(vector) == 0) vec = Direct;
@@ -267,7 +277,6 @@ char* NxClass::buff2str(void* buff, int rank, int dim[], int data_type, char* ve
267277
else if ( strcmp(vector, "[SUM]") == 0) vec = Sum; // Sum of all values
268278
else
269279
{
270-
vector[strlen(vector)-1] = 0;
271280
vec = atoi(&vector[1]);
272281
}
273282
// Single Value
@@ -277,30 +286,29 @@ char* NxClass::buff2str(void* buff, int rank, int dim[], int data_type, char* ve
277286
switch(data_type)
278287
{
279288
case NX_INT8 :
280-
sprintf( value, "%d", (int)((char*) buff)[vec]);
289+
stream << static_cast<int>(static_cast<const char*>(buff)[vec]);
281290
break;
282291
case NX_INT16 :
283-
sprintf( value, "%d", ((short*) buff)[vec]);
292+
stream << static_cast<const int16_t*>(buff)[vec];
284293
break;
285294
case NX_INT32 :
286-
sprintf( value, "%d", ((long*) buff)[vec]);
295+
stream << static_cast<const int32_t*>(buff)[vec];
287296
break;
288297
case NX_UINT16 :
289-
sprintf( value, "%d", ((unsigned short*) buff)[vec]);
298+
stream << static_cast<const uint16_t*>(buff)[vec];
290299
break;
291300
case NX_UINT32 :
292-
sprintf( value, "%d", ((unsigned int*) buff)[vec]);
301+
stream << static_cast<const uint32_t*>(buff)[vec];
293302
break;
294303
case NX_FLOAT32 :
295-
sprintf( value, "%f", ((float*) buff)[vec]);
304+
stream << static_cast<const float*>(buff)[vec];
296305
break;
297306
case NX_FLOAT64 :
298-
sprintf( value, "%f", ((double*) buff)[vec]);
307+
stream << static_cast<const double*>(buff)[vec];
299308
break;
300309
default :
301310
break;
302311
}
303-
return(value);
304312
}
305313
// Array from which a value need to be extracted
306314
// *********************************************
@@ -314,39 +322,39 @@ char* NxClass::buff2str(void* buff, int rank, int dim[], int data_type, char* ve
314322
if(num_val <= 0) {
315323
throw log.set("buff2str", "The number of value of the array is null or negative.", "", "" ,NXING_ERR_NEGATIVE_NUMVAL);
316324
} // Big mistake
317-
double *dblBuff = new double[num_val];
325+
std::vector<double> dblBuff = std::vector<double>(num_val, 0.0);
318326
// Transform the array of unknown type into an array of double
319327
// ***********************************************************
320328
int i = 0;
321329
switch(data_type)
322330
{
323331
case NX_INT8 :
324332
for( i = 0; i< num_val; i++)
325-
dblBuff[i] = (double) ((char*) buff)[i];
333+
dblBuff[i] = static_cast<const char*>(buff)[i];
326334
break;
327335
case NX_INT16 :
328336
for( i = 0; i< num_val; i++)
329-
dblBuff[i] = (double) ((short*) buff)[i];
337+
dblBuff[i] = static_cast<const int16_t*>(buff)[i];
330338
break;
331339
case NX_INT32 :
332340
for( i = 0; i< num_val; i++)
333-
dblBuff[i] = (double) ((long*) buff)[i];
341+
dblBuff[i] = static_cast<const int32_t*>(buff)[i];
334342
break;
335343
case NX_UINT16 :
336344
for( i = 0; i< num_val; i++)
337-
dblBuff[i] = (double) ((unsigned short*) buff)[i];
345+
dblBuff[i] = static_cast<const uint16_t*>(buff)[i];
338346
break;
339347
case NX_UINT32 :
340348
for( i = 0; i< num_val; i++)
341-
dblBuff[i] = (double) ((unsigned int*) buff)[i];
349+
dblBuff[i] = static_cast<const uint32_t*>(buff)[i];
342350
break;
343351
case NX_FLOAT32 :
344352
for( i = 0; i< num_val; i++)
345-
dblBuff[i] = (double) ((float*) buff)[i];
353+
dblBuff[i] = static_cast<const float*>(buff)[i];
346354
break;
347355
case NX_FLOAT64 :
348356
for( i = 0; i< num_val; i++)
349-
dblBuff[i] = (double) ((double*) buff)[i];
357+
dblBuff[i] = static_cast<const double*>(buff)[i];
350358
break;
351359
default :
352360
break;
@@ -391,18 +399,16 @@ char* NxClass::buff2str(void* buff, int rank, int dim[], int data_type, char* ve
391399
break;
392400
}
393401
if((vec == Min || vec == Max || vec == Sum ) && (data_type == NX_INT8 || data_type == NX_INT16 || data_type == NX_INT32 || data_type == NX_UINT16 || data_type == NX_UINT32))
394-
sprintf( value, "%ld", (long) num_value);
402+
stream << static_cast<long long int>(num_value);
395403
else
396-
sprintf( value, "%g", num_value);
397-
return(value);
404+
stream << num_value;
398405
}
399-
return(value);
406+
return(stream.str());
400407
}
401408
catch(Log log)
402409
{
403410
log.printLevel(NXING_LOG_ERROR);
404-
return 0;
411+
throw;
405412
}
406413

407414
}
408-

applications/nxingest/nxingest_nexus.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ class NxClass
6161
bool isOK(){ if(status == NXING_OK) return true; else return false; }
6262
bool isNotOK(){ if(status != NXING_OK) return true; else return false; }
6363

64-
private:
65-
char* buff2str(void* buff, int rank, int dim[], int data_type, char* vector, char* value);
66-
6764
};
6865

6966
#endif

0 commit comments

Comments
 (0)