otsdaq  3.03.00
CgiDataUtilities.cc
1 #include "otsdaq/CgiDataUtilities/CgiDataUtilities.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 
4 using namespace ots;
5 
6 //==============================================================================
11 std::string CgiDataUtilities::getOrPostData(cgicc::Cgicc& cgi, const std::string& needle)
12 {
13  std::string postData = "";
14  if((postData = CgiDataUtilities::postData(cgi, needle)) == "")
16  cgi, needle); // get command from form, if PreviewEntry
17  return postData;
18 }
19 
20 //==============================================================================
25 std::string CgiDataUtilities::postData(cgicc::Cgicc& cgi, const std::string& needle)
26 {
27  std::string postData = "&" + cgi.getEnvironment().getPostData();
28  //__COUT__ << "PostData: " + postData << std::endl;
29  size_t start_pos = postData.find(
30  "&" + needle +
31  "="); // add & and = to make sure found field and not part of a value
32  if(start_pos == std::string::npos)
33  return ""; // needle not found
34 
35  size_t end_pos = postData.find('=', start_pos); // verify = sign
36  if(end_pos == std::string::npos)
37  return ""; //= not found
38 
39  start_pos += needle.length() + 2; // get past & and field
40  end_pos = postData.find('&', start_pos); // skip needle and = sign
41  if(end_pos == std::string::npos)
42  end_pos = postData.length(); // not found, so take data to end
43 
44  //__COUT__ << "start_pos=" << start_pos
45  // << "end_pos=" << end_pos << std::endl;
46  return postData.substr(start_pos, end_pos - start_pos); // return value
47 }
48 
49 //==============================================================================
53 std::string CgiDataUtilities::getData(cgicc::Cgicc& cgi, const std::string& needle)
54 {
55  std::string getData = "&" + cgi.getEnvironment().getQueryString();
56  //__COUT__ << "getData: " + getData << std::endl;
57 
58  size_t start_pos = getData.find(
59  "&" + needle +
60  "="); // add & and = to make sure found field and not part of a value
61  if(start_pos == std::string::npos)
62  return ""; // needle not found
63 
64  size_t end_pos = getData.find('=', start_pos); // verify = sign
65  if(end_pos == std::string::npos)
66  return ""; //= not found
67 
68  start_pos += needle.length() + 2; // get past & and field
69  end_pos = getData.find('&', start_pos); // skip needle and = sign
70  if(end_pos != std::string::npos)
71  end_pos -= start_pos; // found, so determine sz of field
72 
73  //__COUT__ << "start_pos=" << start_pos << " '" << getData[start_pos] <<
74  // "' end_pos=" << end_pos << " := " << getData.substr(start_pos,end_pos) <<
75  // std::endl;
76 
77  return getData.substr(start_pos, end_pos); // return value
78 }
79 
80 //==============================================================================
81 int CgiDataUtilities::getOrPostDataAsInt(cgicc::Cgicc& cgi, const std::string& needle)
82 {
83  return atoi(getOrPostData(cgi, needle).c_str());
84 }
85 int CgiDataUtilities::postDataAsInt(cgicc::Cgicc& cgi, const std::string& needle)
86 {
87  return atoi(postData(cgi, needle).c_str());
88 }
89 int CgiDataUtilities::getDataAsInt(cgicc::Cgicc& cgi, const std::string& needle)
90 {
91  return atoi(getData(cgi, needle).c_str());
92 }
93 
94 //==============================================================================
95 uint64_t CgiDataUtilities::getOrPostDataAsUint64_t(cgicc::Cgicc& cgi,
96  const std::string& needle)
97 {
98  __COUTS__(20) << "getDataAsUint64_t() " << getData(cgi, needle).c_str() << __E__;
99  return std::strtoull(getOrPostData(cgi, needle).c_str(), nullptr, 10);
100 }
101 uint64_t CgiDataUtilities::postDataAsUint64_t(cgicc::Cgicc& cgi,
102  const std::string& needle)
103 {
104  __COUTS__(20) << "getDataAsUint64_t() " << getData(cgi, needle).c_str() << __E__;
105  return std::strtoull(postData(cgi, needle).c_str(), nullptr, 10);
106 }
107 uint64_t CgiDataUtilities::getDataAsUint64_t(cgicc::Cgicc& cgi, const std::string& needle)
108 {
109  __COUTS__(20) << "getDataAsUint64_t() " << getData(cgi, needle).c_str() << __E__;
110  return std::strtoull(getData(cgi, needle).c_str(), nullptr, 10);
111 }
static std::string postData(cgicc::Cgicc &cgi, const std::string &needle)
static std::string getOrPostData(cgicc::Cgicc &cgi, const std::string &needle)
static std::string getData(cgicc::Cgicc &cgi, const std::string &needle)