Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / src / OCUtilities.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <OCApi.h>
22
23 #include <OCUtilities.h>
24
25 #include <boost/algorithm/string.hpp>
26
27 #include <sstream>
28 #include <iterator>
29 #include <algorithm>
30
31 OC::Utilities::QueryParamsKeyVal OC::Utilities::getQueryParams(const std::string& uri)
32 {
33     OC::Utilities::QueryParamsKeyVal qp;
34     if(uri.empty())
35     {
36         return qp;
37     }
38
39     std::vector<std::string> queryparams;
40     boost::split(queryparams, uri, boost::is_any_of(OC_QUERY_SEPARATOR), boost::token_compress_on);
41
42     for(std::string& it: queryparams)
43     {
44         auto index = it.find('=');
45
46         if(index == std::string::npos)
47         {
48             qp[it] = "";
49         }
50         else
51         {
52             qp[it.substr(0, index)] = it.substr(index + 1);
53         }
54     }
55         return qp;
56     }
57
58 namespace OC {
59
60 OCStackResult result_guard(const OCStackResult r)
61 {
62  std::ostringstream os;
63
64  switch(r)
65  {
66     default:
67         os << "result_guard(): unhandled exception: " << OCException::reason(r);
68         throw OCException(os.str(), r);
69
70     /* Exceptional conditions: */
71     case OC_STACK_NO_MEMORY:
72     case OC_STACK_COMM_ERROR:
73     case OC_STACK_NOTIMPL:
74     case OC_STACK_INVALID_URI:
75     case OC_STACK_INVALID_QUERY:
76     case OC_STACK_INVALID_IP:
77     case OC_STACK_INVALID_PORT:
78     case OC_STACK_INVALID_CALLBACK:
79     case OC_STACK_INVALID_METHOD:
80     case OC_STACK_INVALID_PARAM:
81     case OC_STACK_INVALID_OBSERVE_PARAM:
82         os << "result_guard(): " << r << ": " << OCException::reason(r);
83         throw OCException(os.str(), r);
84
85     /* Non-exceptional failures or success: */
86     case OC_STACK_OK:
87     case OC_STACK_NO_RESOURCE:
88     case OC_STACK_RESOURCE_ERROR:
89     case OC_STACK_SLOW_RESOURCE:
90     case OC_STACK_NO_OBSERVERS:
91     case OC_STACK_OBSERVER_NOT_FOUND:
92 #ifdef WITH_PRESENCE
93     case OC_STACK_PRESENCE_STOPPED:
94     case OC_STACK_PRESENCE_TIMEOUT:
95     case OC_STACK_PRESENCE_DO_NOT_HANDLE:
96 #endif
97     break;
98  }
99
100  return r;
101 }
102
103 } // namespace OC
104