[go: up one dir, main page]

File: OgUtil.cpp

package info (click to toggle)
field3d 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,500 kB
  • ctags: 3,213
  • sloc: cpp: 25,268; ansic: 2,577; python: 222; sh: 72; makefile: 25
file content (107 lines) | stat: -rw-r--r-- 3,277 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//----------------------------------------------------------------------------//
// Includes
//----------------------------------------------------------------------------//

#include "OgUtil.h"

//----------------------------------------------------------------------------//

FIELD3D_NAMESPACE_OPEN

//----------------------------------------------------------------------------//
// Helper functions
//----------------------------------------------------------------------------//

const char* ogGroupTypeToString(OgGroupType type)
{
  switch(type)
  {
  case F3DGroupType:
    return "group";
  case F3DAttributeType:
    return "attribute";
  case F3DDatasetType:
    return "dataset";
  default:
    return "";
  }
}

//----------------------------------------------------------------------------//

bool readString(Alembic::Ogawa::IGroupPtr group, const size_t idx, 
                std::string &s)
{
  // Grab data
  Alembic::Ogawa::IDataPtr data = group->getData(idx, OGAWA_THREAD);
  // Check that we got something
  if (!data) {
    std::cout << "OgUtil::readString() got null data for index " 
              << idx << std::endl;
    std::cout << "  numChildren(): " << group->getNumChildren() << std::endl;
    return false;
  }
  // Check data length
  const size_t length = data->getSize();
  if (length % sizeof(std::string::value_type) != 0) {
    return false;
  }
  // String length
  const size_t stringLength = length / sizeof(std::string::value_type);
  // Read into temp buffer. Reading straight into std::string is Bad.
  std::vector<std::string::value_type> temp(stringLength + 1);
  // Add null terminator
  temp[stringLength] = 0;
  // Read the data
  data->read(length, &temp[0], 0, OGAWA_THREAD);
  // Construct string. The string see the temp buffer as a const char *.
  s = std::string(&temp[0]);
  // Done
  return true;
}

//----------------------------------------------------------------------------//

OgDataType readDataType(Alembic::Ogawa::IGroupPtr group, const size_t idx) 
{
  // Data type
  OgDataType dataType;;
  // Grab data
  Alembic::Ogawa::IDataPtr data = group->getData(idx, OGAWA_THREAD);
  // Check data length
  const size_t sizeLength = sizeof(OgDataType);
  const size_t length = data->getSize();
  if (length != sizeLength) {
    std::cout << "readDataType() " << sizeLength << " != " << length << std::endl;
    return F3DInvalidDataType;
  }
  // Read the data directly to the input param
  data->read(length, &dataType, 0, OGAWA_THREAD);
  // Done
  return dataType;
}

//----------------------------------------------------------------------------//

bool writeString(Alembic::Ogawa::OGroupPtr group, const std::string &s)
{
  // Strings are written without zero terminator
  Alembic::Ogawa::ODataPtr data = 
    group->addData(s.size() * sizeof(std::string::value_type), s.c_str());
  return data != NULL;
}

//----------------------------------------------------------------------------//

bool getGroupName(Alembic::Ogawa::IGroupPtr group, 
                  std::string &name)
{
  return readString(group, 0, name);
}

//----------------------------------------------------------------------------//

FIELD3D_NAMESPACE_SOURCE_CLOSE

//----------------------------------------------------------------------------//