Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / OCLib / OCReflect.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 #include <string>
7 #include <cstring>
8
9 #include "OCReflect.h"
10
11 OC::OCReflect::entity OC::OCReflect::remote_resource::operator()(const std::string& name)
12 {
13  return OC::OCReflect::entity();
14 }
15
16 namespace OC { namespace OCReflect { namespace to_OCStack {
17
18 // Free a C array of char *:
19 void release(char **in)
20 {
21  if(nullptr == in)
22   return;
23
24  for(char **cursor = in; nullptr != *cursor; ++cursor)
25   {
26     if(nullptr != *cursor)
27      free(*cursor);
28   }
29
30  free(in);
31 }
32
33 char *strdup(const char *s)
34 {
35  if(nullptr == s)
36   return nullptr;
37
38  char *ret = static_cast<char *>(malloc(1 + strlen(s)));
39
40  if(nullptr == ret)
41   return nullptr;
42
43  return strcpy(ret, s); 
44 }
45
46 char *strdup(const std::string& s)
47 {
48  char *ret = static_cast<char *>(malloc(1 + s.length()));
49
50  if(nullptr == ret)
51   return nullptr;
52
53  return strcpy(ret, s.c_str()); 
54 }
55
56 // Count the number of elements in a NULL-terminated C array of char*:
57 size_t length(char **in)
58 {
59  if(nullptr == in)
60   return 0;
61
62  size_t ret = 0;
63
64  for(char **cursor = in; nullptr != *cursor; ++cursor)
65   ++ret; 
66
67  return ret; 
68 }
69
70 // Note: caller is responsible for the returned memory:
71 char **convert(const std::vector<std::string>& vs)
72 {
73  char **out;
74
75  try
76   {
77         out = (char **)malloc(vs.size()*sizeof(char *));
78
79         if(nullptr == out)
80          throw;
81
82         size_t i = 0;
83         for(; vs.size() != i; ++i)
84          {
85                 out[i] = to_OCStack::strdup(vs[i]);
86
87                 if(nullptr == out[i])
88                  throw;
89          }
90
91         out[i] = nullptr;
92
93         return out;
94   }
95  catch(...)
96   {
97         if(nullptr != out)
98          release(out);
99   }
100
101  return nullptr;
102 }
103
104 std::string convert(const OC::OCReflect::named_property_binding& npb)
105 {
106  const std::string& name                                        = std::get<0>(npb);
107  const OC::OCReflect::property_signature ps     = std::get<1>(npb);
108
109  std::ostringstream os;
110
111  os << name << ':' << "oc.";
112
113  using OC::OCReflect::property_type;
114
115  switch(ps.type)
116   {
117         case property_type::nil:
118         case property_type::rational:
119         case property_type::string:             
120         case property_type::list:       
121         case property_type::INVALID:    
122                                                                         throw std::runtime_error("not implemented"); 
123                                                                         break;
124
125         case property_type::boolean:    os << "bt." << 'b';     
126                                                                         break;
127
128         case property_type::integer:    os << "bt." << 'i';     
129                                                                         break;
130   }
131
132  return os.str();
133 }
134
135 std::vector<std::string> convert(const OC::OCReflect::named_property_binding_vector& psv)
136 {
137  std::vector<std::string> out;
138
139  for(const auto& ps : psv)
140   out.emplace_back(convert(ps));
141
142  return out;
143 }
144
145 char *flatten(const std::vector<std::string>& input, const std::string& delim)
146 {
147  std::string out;
148
149  for(size_t i = 0; input.size() != i; ++i)
150   {
151     out += input[i];
152
153     if(i < input.size() - 1)
154      out += ";";
155   }
156
157  return OC::OCReflect::to_OCStack::strdup(out);
158 }
159
160 }}} // namespace OC::OCReflect::to_OCStack