Added string helper for removing spaces and parsing list

Fix

Fix string parser
master
Xavier Arteaga 5 years ago committed by Xavier Arteaga
parent 89b24b54e5
commit de230826b9

@ -0,0 +1,125 @@
/*
* Copyright 2013-2020 Software Radio Systems Limited
*
* This file is part of srsLTE.
*
* srsLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#ifndef SRSLTE_STRING_HELPERS_H
#define SRSLTE_STRING_HELPERS_H
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
namespace srslte {
/**
* Removes all matching characters from a string
* @param input input string
* @param c character to match
* @return It returns the resultant string without the matched characters
*/
static inline std::string string_remove_char(const std::string& input, char c)
{
std::string ret = input;
std::size_t p1 = ret.find(c);
while (p1 != std::string::npos) {
ret.erase(p1);
p1 = ret.find(' ');
}
return ret;
}
/**
* Private casting overloaded functions
*/
namespace {
#define INTEGER_CAST(TYPE) \
inline void string_cast(const std::string& str, TYPE& v) { v = (TYPE)std::strtol(str.c_str(), nullptr, 10); }
INTEGER_CAST(int32_t)
INTEGER_CAST(uint32_t)
INTEGER_CAST(int16_t)
INTEGER_CAST(uint16_t)
INTEGER_CAST(int8_t)
INTEGER_CAST(uint8_t)
#undef INTEGER_CAST
inline void string_cast(const std::string& str, float& f)
{
f = std::strtof(str.c_str(), nullptr);
}
inline void string_cast(const std::string& str, double& d)
{
d = std::strtod(str.c_str(), nullptr);
}
inline void string_cast(const std::string& str, std::string& str2)
{
str2 = str;
}
} // namespace
/**
* This function parses a string into any defined type
* @tparam Target type
* @param str input string
* @return Parsed value of the given type
*/
template <class Type>
static inline Type string_cast(const std::string& str)
{
Type ret;
string_cast(str, ret);
return ret;
}
/**
* It splits a given string into multiple elements given a delimiter. The elements are casted to the specified type.
* @tparam Insertable It is the list data-type. It needs to implement insert(iterator, element)
* @param input It is the input string
* @param delimiter Character used for indicating the end of the strings
* @param list contains the parsed values
*/
template <class Insertable>
static inline void string_parse_list(const std::string& input, char delimiter, Insertable& list)
{
std::stringstream ss(input);
// Removes all possible elements of the list
list.clear();
while (ss.good()) {
std::string substr;
std::getline(ss, substr, delimiter);
if (not substr.empty()) {
list.insert(list.end(), string_cast<typename Insertable::value_type>(substr));
}
}
}
} // namespace srslte
#endif // SRSLTE_STRING_HELPERS_H

@ -20,6 +20,7 @@
*/
#include "srslte/radio/radio.h"
#include "srslte/common/string_helpers.h"
#include "srslte/config.h"
#include <list>
#include <string>
@ -47,18 +48,6 @@ radio::~radio()
}
}
static inline void split_string(const std::string& input, char delimiter, std::vector<std::string>& list)
{
std::stringstream ss(input);
while (ss.good()) {
std::string substr;
getline(ss, substr, delimiter);
if (not substr.empty()) {
list.push_back(substr);
}
}
}
int radio::init(const rf_args_t& args, phy_interface_radio* phy_)
{
phy = phy_;
@ -99,7 +88,7 @@ int radio::init(const rf_args_t& args, phy_interface_radio* phy_)
// Split multiple RF channels using `;` delimiter
std::vector<std::string> device_args_list;
split_string(args.device_args, ';', device_args_list);
string_parse_list(args.device_args, ',', device_args_list);
// Add auto if list is empty
if (device_args_list.empty()) {

@ -24,6 +24,7 @@
#include <iostream>
#include <mutex>
#include <srsenb/hdr/phy/phy.h>
#include <srslte/common/string_helpers.h>
#include <srslte/common/test_common.h>
#include <srslte/common/threads.h>
#include <srslte/interfaces/enb_interfaces.h>
@ -1395,14 +1396,7 @@ int parse_args(int argc, char** argv, phy_test_bench::args_t& args)
// populate UE Active cell list
if (not args.ue_cell_list_str.empty()) {
args.ue_cell_list.clear();
std::stringstream ss(args.ue_cell_list_str);
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
auto pci = (uint32_t)strtoul(substr.c_str(), nullptr, 10);
args.ue_cell_list.push_back(pci);
}
srslte::string_parse_list(args.ue_cell_list_str, ',', args.ue_cell_list);
} else {
return SRSLTE_ERROR;
}

@ -41,17 +41,17 @@
#define SRSLTE_RRC_N_BANDS 43
typedef struct {
std::string ue_category_str;
uint32_t ue_category;
int ue_category_ul;
int ue_category_dl;
uint32_t release;
uint32_t feature_group;
uint8_t supported_bands[SRSLTE_RRC_N_BANDS];
uint32_t nof_supported_bands;
bool support_ca;
int mbms_service_id;
uint32_t mbms_service_port;
std::string ue_category_str;
uint32_t ue_category;
int ue_category_ul;
int ue_category_dl;
uint32_t release;
uint32_t feature_group;
std::array<uint8_t, SRSLTE_RRC_N_BANDS> supported_bands;
uint32_t nof_supported_bands;
bool support_ca;
int mbms_service_id;
uint32_t mbms_service_port;
} rrc_args_t;
#define SRSLTE_UE_CATEGORY_DEFAULT "4"

@ -265,20 +265,6 @@ private:
return true;
}
std::vector<uint8_t> split_string(const std::string input)
{
std::vector<uint8_t> list;
std::stringstream ss(input);
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
if (not substr.empty()) {
list.push_back(strtol(substr.c_str(), nullptr, 10));
}
}
return list;
}
class rrc_connect_proc
{
public:

@ -22,6 +22,7 @@
#include "srsue/hdr/stack/upper/nas.h"
#include "srslte/common/bcd_helpers.h"
#include "srslte/common/security.h"
#include "srslte/common/string_helpers.h"
#include <fstream>
#include <iomanip>
#include <iostream>
@ -261,7 +262,8 @@ void nas::init(usim_interface_nas* usim_, rrc_interface_nas* rrc_, gw_interface_
}
// parse and sanity check EIA list
std::vector<uint8_t> cap_list = split_string(cfg_.eia);
std::vector<uint8_t> cap_list;
srslte::string_parse_list(cfg_.eia, ',', cap_list);
if (cap_list.empty()) {
nas_log->error("Empty EIA list. Select at least one EIA algorithm.\n");
}
@ -274,7 +276,7 @@ void nas::init(usim_interface_nas* usim_, rrc_interface_nas* rrc_, gw_interface_
}
// parse and sanity check EEA list
cap_list = split_string(cfg_.eea);
srslte::string_parse_list(cfg_.eea, ',', cap_list);
if (cap_list.empty()) {
nas_log->error("Empty EEA list. Select at least one EEA algorithm.\n");
}

@ -21,15 +21,13 @@
#include "srsue/hdr/ue.h"
#include "srslte/build_info.h"
#include "srslte/common/string_helpers.h"
#include "srslte/radio/radio.h"
#include "srslte/srslte.h"
#include "srsue/hdr/phy/phy.h"
#include "srsue/hdr/stack/ue_stack_lte.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <pthread.h>
#include <sstream>
#include <string>
using namespace srslte;
@ -53,7 +51,7 @@ ue::~ue()
int ue::init(const all_args_t& args_, srslte::logger* logger_)
{
int ret = SRSLTE_SUCCESS;
logger = logger_;
logger = logger_;
// Init UE log
log.init("UE ", logger);
@ -174,17 +172,19 @@ int ue::parse_args(const all_args_t& args_)
args.phy.agc_enable = args.rf.rx_gain < 0.0f;
// populate DL EARFCN list
if (!args.phy.dl_earfcn.empty()) {
args.phy.dl_earfcn_list.clear();
std::stringstream ss(args.phy.dl_earfcn);
uint32_t idx = 0;
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
uint32_t earfcn = (uint32_t)strtoul(substr.c_str(), nullptr, 10);
args.stack.rrc.supported_bands[idx] = srslte_band_get_band(earfcn);
args.stack.rrc.nof_supported_bands = ++idx;
args.phy.dl_earfcn_list.push_back(earfcn);
if (not args.phy.dl_earfcn.empty()) {
// Parse DL-EARFCN list
srslte::string_parse_list(args.phy.dl_earfcn, ',', args.phy.dl_earfcn_list);
// Populates supported bands
args.stack.rrc.nof_supported_bands = 0;
for (uint32_t& earfcn : args.phy.dl_earfcn_list) {
uint8_t band = srslte_band_get_band(earfcn);
// Try to find band, if not appends it
if (std::find(args.stack.rrc.supported_bands.begin(), args.stack.rrc.supported_bands.end(), band) ==
args.stack.rrc.supported_bands.end()) {
args.stack.rrc.supported_bands[args.stack.rrc.nof_supported_bands++] = band;
}
}
} else {
log.error("Error: dl_earfcn list is empty\n");
@ -193,21 +193,14 @@ int ue::parse_args(const all_args_t& args_)
}
// populate UL EARFCN list
if (!args.phy.ul_earfcn.empty()) {
if (not args.phy.ul_earfcn.empty()) {
std::vector<uint32_t> ul_earfcn_list;
srslte::string_parse_list(args.phy.ul_earfcn, ',', ul_earfcn_list);
// For each parsed UL-EARFCN links it to the corresponding DL-EARFCN
args.phy.ul_earfcn_map.clear();
std::stringstream ss(args.phy.ul_earfcn);
uint32_t idx = 0;
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
uint32_t ul_earfcn = (uint32_t)strtoul(substr.c_str(), nullptr, 10);
if (idx < args.phy.dl_earfcn_list.size()) {
// If it can be matched with a DL EARFCN, otherwise ignore entry
uint32_t dl_earfcn = args.phy.dl_earfcn_list[idx];
args.phy.ul_earfcn_map[dl_earfcn] = ul_earfcn;
idx++;
}
for (size_t i = 0; i < SRSLTE_MIN(ul_earfcn_list.size(), args.phy.dl_earfcn_list.size()); i++) {
args.phy.ul_earfcn_map[args.phy.dl_earfcn_list[i]] = ul_earfcn_list[i];
}
}

@ -24,6 +24,7 @@
#include <iostream>
#include <map>
#include <memory>
#include <srslte/common/string_helpers.h>
#include <srslte/phy/channel/channel.h>
#include <srslte/phy/utils/random.h>
#include <srslte/srslte.h>
@ -388,21 +389,13 @@ static void pci_list_parse_helper(std::string& list_str, std::set<uint32_t>& lis
}
} else if (list_str == "none") {
// Do nothing
} else if (!list_str.empty()) {
} else if (not list_str.empty()) {
// Remove spaces from neightbour cell list
std::size_t p1 = list_str.find(' ');
while (p1 != std::string::npos) {
list_str.erase(p1);
p1 = list_str.find(' ');
}
list_str = srslte::string_remove_char(list_str, ' ');
// Add cell to known cells
std::stringstream ss(list_str);
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
list.insert((uint32_t)strtoul(substr.c_str(), nullptr, 10));
}
srslte::string_parse_list(list_str, ',', list);
}
}

Loading…
Cancel
Save