Field3D
OgUtil.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 // Includes
3 //----------------------------------------------------------------------------//
4 
5 #include "OgUtil.h"
6 
7 //----------------------------------------------------------------------------//
8 
10 
11 //----------------------------------------------------------------------------//
12 // Helper functions
13 //----------------------------------------------------------------------------//
14 
15 const char* ogGroupTypeToString(OgGroupType type)
16 {
17  switch(type)
18  {
19  case F3DGroupType:
20  return "group";
21  case F3DAttributeType:
22  return "attribute";
23  case F3DDatasetType:
24  return "dataset";
25  default:
26  return "";
27  }
28 }
29 
30 //----------------------------------------------------------------------------//
31 
32 bool readString(Alembic::Ogawa::IGroupPtr group, const size_t idx,
33  std::string &s)
34 {
35  // Grab data
36  Alembic::Ogawa::IDataPtr data = group->getData(idx, OGAWA_THREAD);
37  // Check that we got something
38  if (!data) {
39  std::cout << "OgUtil::readString() got null data for index "
40  << idx << std::endl;
41  std::cout << " numChildren(): " << group->getNumChildren() << std::endl;
42  return false;
43  }
44  // Check data length
45  const size_t length = data->getSize();
46  if (length % sizeof(std::string::value_type) != 0) {
47  return false;
48  }
49  // String length
50  const size_t stringLength = length / sizeof(std::string::value_type);
51  // Read into temp buffer. Reading straight into std::string is Bad.
52  std::vector<std::string::value_type> temp(stringLength + 1);
53  // Add null terminator
54  temp[stringLength] = 0;
55  // Read the data
56  data->read(length, &temp[0], 0, OGAWA_THREAD);
57  // Construct string. The string see the temp buffer as a const char *.
58  s = std::string(&temp[0]);
59  // Done
60  return true;
61 }
62 
63 //----------------------------------------------------------------------------//
64 
65 OgDataType readDataType(Alembic::Ogawa::IGroupPtr group, const size_t idx)
66 {
67  // Data type
68  OgDataType dataType;;
69  // Grab data
70  Alembic::Ogawa::IDataPtr data = group->getData(idx, OGAWA_THREAD);
71  // Check data length
72  const size_t sizeLength = sizeof(OgDataType);
73  const size_t length = data->getSize();
74  if (length != sizeLength) {
75  std::cout << "readDataType() " << sizeLength << " != " << length << std::endl;
76  return F3DInvalidDataType;
77  }
78  // Read the data directly to the input param
79  data->read(length, &dataType, 0, OGAWA_THREAD);
80  // Done
81  return dataType;
82 }
83 
84 //----------------------------------------------------------------------------//
85 
86 bool writeString(Alembic::Ogawa::OGroupPtr group, const std::string &s)
87 {
88  // Strings are written without zero terminator
89  Alembic::Ogawa::ODataPtr data =
90  group->addData(s.size() * sizeof(std::string::value_type), s.c_str());
91  return data != NULL;
92 }
93 
94 //----------------------------------------------------------------------------//
95 
96 bool getGroupName(Alembic::Ogawa::IGroupPtr group,
97  std::string &name)
98 {
99  return readString(group, 0, name);
100 }
101 
102 //----------------------------------------------------------------------------//
103 
105 
106 //----------------------------------------------------------------------------//
107 
readDataType
OgDataType readDataType(Alembic::Ogawa::IGroupPtr group, const size_t idx)
Definition: OgUtil.cpp:65
FIELD3D_NAMESPACE_SOURCE_CLOSE
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
ogGroupTypeToString
FIELD3D_NAMESPACE_OPEN const char * ogGroupTypeToString(OgGroupType type)
Definition: OgUtil.cpp:15
writeString
bool writeString(Alembic::Ogawa::OGroupPtr group, const std::string &s)
Definition: OgUtil.cpp:86
F3DInvalidDataType
@ F3DInvalidDataType
Definition: Traits.h:160
getGroupName
bool getGroupName(Alembic::Ogawa::IGroupPtr group, std::string &name)
Definition: OgUtil.cpp:96
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
readString
bool readString(Alembic::Ogawa::IGroupPtr group, const size_t idx, std::string &s)
Definition: OgUtil.cpp:32
OgDataType
OgDataType
Enumerates the various uses for Ogawa-level groups.
Definition: Traits.h:125