string.cpp 3 KB
Newer Older
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1
2
3
4
5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
Raphael Defosseux's avatar
Raphael Defosseux committed
6
7
8
 * the OAI Public License, Version 1.1  (the "License"); you may not use this
 *file except in compliance with the License. You may obtain a copy of the
 *License at
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
#include "string.hpp"
Rohan's avatar
Rohan committed
22
#include <iostream>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
23
24
25
26
27
28

#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <stdarg.h>
Rohan's avatar
Rohan committed
29
#include <regex>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
30
31
32
33
34
35

template<class T>
class Buffer {
 public:
  explicit Buffer(size_t size) {
    msize = size;
Raphael Defosseux's avatar
Raphael Defosseux committed
36
    mbuf  = new T[msize];
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
37
38
  }
  ~Buffer() {
Raphael Defosseux's avatar
Raphael Defosseux committed
39
    if (mbuf) delete[] mbuf;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
40
  }
Raphael Defosseux's avatar
Raphael Defosseux committed
41
42
  T* get() { return mbuf; }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
43
44
45
 private:
  Buffer();
  size_t msize;
Raphael Defosseux's avatar
Raphael Defosseux committed
46
  T* mbuf;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
47
48
};

Raphael Defosseux's avatar
Raphael Defosseux committed
49
std::string util::string_format(const char* format, ...) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
50
51
52
  va_list args;

  va_start(args, format);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
53
  size_t size = vsnprintf(NULL, 0, format, args) + 1;  // Extra space for '\0'
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
54
55
56
57
58
59
60
61
62
63
64
65
  va_end(args);

  Buffer<char> buf(size);

  va_start(args, format);
  vsnprintf(buf.get(), size, format, args);
  va_end(args);

  return std::string(buf.get(), size - 1);  // We don't want the '\0' inside
}

// Licence : https://creativecommons.org/licenses/by-sa/4.0/legalcode
Raphael Defosseux's avatar
Raphael Defosseux committed
66
// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring#217605
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
67
68

// trim from start
Raphael Defosseux's avatar
Raphael Defosseux committed
69
std::string& util::ltrim(std::string& s) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
70
71
  s.erase(
      s.begin(),
Raphael Defosseux's avatar
Raphael Defosseux committed
72
73
      std::find_if(
          s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
74
75
76
77
  return s;
}

// trim from end
Raphael Defosseux's avatar
Raphael Defosseux committed
78
std::string& util::rtrim(std::string& s) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
79
  s.erase(
Raphael Defosseux's avatar
Raphael Defosseux committed
80
81
82
      std::find_if(
          s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace)))
          .base(),
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
83
84
85
86
87
      s.end());
  return s;
}

// trim from both ends
Raphael Defosseux's avatar
Raphael Defosseux committed
88
std::string& util::trim(std::string& s) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
89
90
  return util::ltrim(util::rtrim(s));
}
Rohan's avatar
Rohan committed
91
92

// extract query param from given querystring
Rohan's avatar
Rohan committed
93
94
95
std::string query_param_tmp;
//
std::string util::get_query_param(std::string querystring, std::string param) {
Rohan's avatar
Rohan committed
96
  std::regex reList("([^=]*)=([^&]*)&?");
Rohan's avatar
Rohan committed
97
  query_param_tmp.clear();
Rohan's avatar
Rohan committed
98
99
  std::for_each(
      std::sregex_iterator(querystring.begin(), querystring.end(), reList),
Rohan's avatar
Rohan committed
100
101
102
103
104
      std::sregex_iterator(), [param](std::smatch match) {
        if (match[1] == param) {
          query_param_tmp = match[2].str().c_str();
          return;
        }
Rohan's avatar
Rohan committed
105
      });
Rohan's avatar
Rohan committed
106
  return query_param_tmp;
Rohan's avatar
Rohan committed
107
}