Skip to content

Commit

Permalink
Merge pull request #1211 from pnorbert/bpls-xml
Browse files Browse the repository at this point in the history
bpls pretty prints attributes with names "*xml" (case insensitive mat…
  • Loading branch information
pnorbert authored Feb 19, 2019
2 parents acb9e42 + d7b5868 commit bea52fd
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 43 deletions.
29 changes: 29 additions & 0 deletions source/adios2/helper/adiosString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "adiosString.h"

/// \cond EXCLUDE_FROM_DOXYGEN
#include <algorithm> //std::transform
#include <fstream>
#include <ios> //std::ios_base::failure
#include <sstream>
Expand Down Expand Up @@ -101,6 +102,34 @@ std::string AddExtension(const std::string &name,
return result;
}

bool EndsWith(const std::string &str, const std::string &ending,
const bool caseSensitive)
{
if (str.length() >= ending.length())
{
if (caseSensitive)
{
return (!str.compare(str.length() - ending.length(),
ending.length(), ending));
}
else
{
std::string strLC = std::string(str);
std::string endLC = std::string(ending);
std::transform(strLC.begin(), strLC.end(), strLC.begin(),
::tolower);
std::transform(endLC.begin(), endLC.end(), endLC.begin(),
::tolower);
return (!strLC.compare(strLC.length() - endLC.length(),
endLC.length(), endLC));
}
}
else
{
return false;
}
};

std::vector<std::string>
GetParametersValues(const std::string &key,
const std::vector<Params> &parametersVector) noexcept
Expand Down
10 changes: 10 additions & 0 deletions source/adios2/helper/adiosString.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ Params BuildParametersMap(const std::vector<std::string> &parameters,
std::string AddExtension(const std::string &name,
const std::string extension) noexcept;

/**
* Check if a string ends with another substring
* @param str input string
* @param ending input string to compare with
* @param caseSensitive input flag
* @return true if the 'str' string ends with the string 'ending'
*/
bool EndsWith(const std::string &str, const std::string &ending,
const bool caseSensitive = true);

/**
* Get values for each param entry of a certain key in a vector.
* If key not found then string in vector is empty.
Expand Down
2 changes: 1 addition & 1 deletion source/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# BPLS
add_executable(bpls ./bpls/bpls.cpp)
target_link_libraries(bpls adios2 adios2sys_interface)
target_link_libraries(bpls adios2 adios2sys_interface pugixml)
install(TARGETS bpls EXPORT adios2
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
Expand Down
163 changes: 124 additions & 39 deletions source/utils/bpls/bpls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#endif

#include "adios2sys/CommandLineArguments.hxx"
#include <pugixml.hpp>

namespace adios2
{
Expand Down Expand Up @@ -550,10 +551,88 @@ static inline int ndigits(size_t n)
return snprintf(digitstr, 32, "%zu", n);
}

template <class T>
int printAttributeValue(core::Engine *fp, core::IO *io,
core::Attribute<T> *attribute)
{
enum ADIOS_DATATYPES adiosvartype = type_to_enum(attribute->m_Type);
if (attribute->m_IsSingleValue)
{
print_data((void *)&attribute->m_DataSingleValue, 0, adiosvartype,
true);
}
else
{
fprintf(outf, "{");
size_t nelems = attribute->m_DataArray.size();
for (size_t j = 0; j < nelems; j++)
{
print_data((void *)&attribute->m_DataArray[j], 0, adiosvartype,
true);
if (j < nelems - 1)
{
fprintf(outf, ", ");
}
}
fprintf(outf, "}");
}
return 0;
}

template <>
int printAttributeValue(core::Engine *fp, core::IO *io,
core::Attribute<std::string> *attribute)
{
enum ADIOS_DATATYPES adiosvartype = type_to_enum(attribute->m_Type);
bool xmlprint = helper::EndsWith(attribute->m_Name, "xml", false);
bool printDataAnyway = true;

if (attribute->m_IsSingleValue)
{
if (xmlprint)
{
printDataAnyway =
print_data_xml(attribute->m_DataSingleValue.data(),
attribute->m_DataSingleValue.length());
}
if (printDataAnyway)
{
print_data((void *)&attribute->m_DataSingleValue, 0, adiosvartype,
true);
}
}
else
{
fprintf(outf, "{");
size_t nelems = attribute->m_DataArray.size();
for (size_t j = 0; j < nelems; j++)
{
if (xmlprint)
{
printDataAnyway =
print_data_xml(attribute->m_DataArray[j].data(),
attribute->m_DataArray[j].length());
}
if (printDataAnyway)
{
print_data((void *)&attribute->m_DataArray[j], 0, adiosvartype,
true);
}
if (j < nelems - 1)
{
fprintf(outf, ", ");
}
}
fprintf(outf, "}");
}
return 0;
}

int nEntriesMatched = 0;

int doList_vars(core::Engine *fp, core::IO *io)
{

const core::DataMap &variables = io->GetVariablesDataMap();
const core::DataMap &attributes = io->GetAttributesDataMap();

Expand All @@ -578,7 +657,8 @@ int doList_vars(core::Engine *fp, core::IO *io)

// size_t nNames = entries.size();

// calculate max length of variable names and type names in the first round
// calculate max length of variable names and type names in the first
// round
int maxlen = 4; // need int for printf formatting
int maxtypelen = 7;
for (const auto &entrypair : entries)
Expand Down Expand Up @@ -852,35 +932,6 @@ int printVariableInfo(core::Engine *fp, core::IO *io,
return retval;
}

template <class T>
int printAttributeValue(core::Engine *fp, core::IO *io,
core::Attribute<T> *attribute)
{
enum ADIOS_DATATYPES adiosvartype = type_to_enum(attribute->m_Type);

if (attribute->m_IsSingleValue)
{
print_data((void *)&attribute->m_DataSingleValue, 0, adiosvartype,
true);
}
else
{
fprintf(outf, "{");
size_t nelems = attribute->m_DataArray.size();
for (size_t j = 0; j < nelems; j++)
{
print_data((void *)&attribute->m_DataArray[j], 0, adiosvartype,
true);
if (j < nelems - 1)
{
fprintf(outf, ", ");
}
}
fprintf(outf, "}");
}
return 0;
}

#define PRINT_ARRAY(str, ndim, dims, loopvar, format) \
fprintf(outf, "%s", str); \
if (ndim > 0) \
Expand Down Expand Up @@ -922,7 +973,8 @@ void printMeshes(core::Engine *fp)
int mpi_comm_dummy = 0;
if (fp->nmeshes == 0)
{
fprintf(outf, "Mesh info: There are no meshes defined in this file\n");
fprintf(outf, "Mesh info: There are no meshes defined in this
file\n");
return;
}
fprintf(outf, "Mesh info: \n");
Expand All @@ -935,10 +987,12 @@ void printMeshes(core::Engine *fp)
if (meshid != mi->id)
fprintf(
outf,
" bpls warning: meshid (=%d) != inquired mesh id (%d)\n",
" bpls warning: meshid (=%d) != inquired mesh id
(%d)\n",
meshid, mi->id);
if (strcmp(fp->mesh_namelist[meshid], mi->name))
fprintf(outf, " bpls warning: mesh name in list (=\"%s\") != "
fprintf(outf, " bpls warning: mesh name in list (=\"%s\")
!= "
"inquired mesh name (\"%s\")\n",
fp->mesh_namelist[meshid], mi->name);
if (mi->file_name)
Expand All @@ -954,7 +1008,8 @@ void printMeshes(core::Engine *fp)
{
case ADIOS_MESH_UNIFORM:
fprintf(outf, "uniform\n");
PRINT_ARRAY64(" dimensions: ", mi->uniform->num_dimensions,
PRINT_ARRAY64(" dimensions: ",
mi->uniform->num_dimensions,
mi->uniform->dimensions, j)
if (mi->uniform->origins)
{
Expand Down Expand Up @@ -1015,7 +1070,8 @@ void printMeshes(core::Engine *fp)
mi->structured->points[0]);
for (i = 1; i < mi->structured->num_dimensions; i++)
{
fprintf(outf, ", \"%s\"", mi->structured->points[i]);
fprintf(outf, ", \"%s\"",
mi->structured->points[i]);
}
fprintf(outf, "\n");
}
Expand All @@ -1038,7 +1094,8 @@ void printMeshes(core::Engine *fp)
mi->unstructured->points[0]);
for (i = 1; i < mi->unstructured->nvar_points; i++)
{
fprintf(outf, ", \"%s\"", mi->unstructured->points[i]);
fprintf(outf, ", \"%s\"",
mi->unstructured->points[i]);
}
fprintf(outf, "\n");
}
Expand Down Expand Up @@ -1139,7 +1196,8 @@ int doList(const char *path)
{
//, variables, timesteps, and attributes
// all parameters are integers,
// besides the last parameter, which is an array of strings for holding
// besides the last parameter, which is an array of strings for
// holding
// the
// list of group names
// ntsteps = fp->tidx_stop - fp->tidx_start + 1;
Expand Down Expand Up @@ -1478,6 +1536,10 @@ int readVar(core::Engine *fp, core::IO *io, core::Variable<T> *variable)
if (nelems < maxreadn)
maxreadn = nelems;

bool xmlprint = helper::EndsWith(variable->m_Name, ".xml", false);
if (xmlprint && nelems > maxreadn)
maxreadn = nelems;

// special case: string. Need to use different elemsize
/*if (vi->type == adios_string)
{
Expand Down Expand Up @@ -1728,7 +1790,8 @@ int readVarBlock(core::Engine *fp, core::IO *io, core::Variable<T> *variable,
maxreadn = nelems;

// allocate data array
// data = (void *)malloc(maxreadn * elemsize + 8); // +8 for just to be sure
// data = (void *)malloc(maxreadn * elemsize + 8); // +8 for just to be
// sure

// determine strategy how to read in:
// - at once
Expand Down Expand Up @@ -1809,7 +1872,8 @@ int readVarBlock(core::Engine *fp, core::IO *io, core::Variable<T> *variable,

/* In current implementation we read with global selection, so
* we need to adjust start_t for global offsets here.
* TODO: this will change in the future to block reading with relative
* TODO: this will change in the future to block reading with
* relative
* selection
*/
if (variable->m_ShapeID == ShapeID::GlobalArray)
Expand Down Expand Up @@ -2288,6 +2352,27 @@ int print_data_characteristics(void *min, void *max, double *avg,
return 0;
}

/* s is a character array not necessarily null terminated.
* return false on OK print, true if it not XML (not printed)*/
bool print_data_xml(const char *s, const size_t length)
{
pugi::xml_document document;
auto parse_result = document.load_buffer(s, length);
if (!parse_result)
{
/*throw std::invalid_argument(
"ERROR: XML: parse error in XML string, description: " +
std::string(parse_result.description()) +
", check with any XML editor if format is ill-formed\n ");*/
return true;
}
std::cout << std::endl;
document.save(std::cout, PUGIXML_TEXT(" "),
pugi::format_default | pugi::format_no_declaration);
std::cout << std::flush;
return false;
}

int print_data(const void *data, int item, enum ADIOS_DATATYPES adiosvartype,
bool allowformat)
{
Expand Down
8 changes: 5 additions & 3 deletions source/utils/bpls/bpls.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ int printVariableInfo(core::Engine *fp, core::IO *io,
core::Variable<T> *variable);

template <class T>
int printAttributeValue(core::Engine *fp, core::IO *io,
core::Attribute<T> *attribute);
template <class T>
int readVar(core::Engine *fp, core::IO *io, core::Variable<T> *variable);

template <class T>
Expand All @@ -111,6 +108,11 @@ void print_slice_info(core::VariableBase *variable, bool timed, uint64_t *s,
uint64_t *c, Dims count);
int print_data(const void *data, int item, enum ADIOS_DATATYPES adiosvartypes,
bool allowformat);

/* s is a character array not necessarily null terminated.
* return false on OK print, true if it not XML (not printed)*/
bool print_data_xml(const char *s, const size_t length);

int print_dataset(const void *data, const std::string vartype, uint64_t *s,
uint64_t *c, int tdims, int *ndigits);
void print_endline(void);
Expand Down

0 comments on commit bea52fd

Please sign in to comment.