RINASim  October 2016
Documentation of framework for OMNeT++
Utils.cc
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2014-2016 Brno University of Technology, PRISTINE project
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 /*
24  * Utils.cpp
25  *
26  * Created on: Mar 5, 2015
27  * Author: gaixas1
28  */
29 
30 #include <Utils.h>
31 #include <algorithm>
32 
33 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
34  std::stringstream ss(s);
35  std::string item;
36  while (std::getline(ss, item, delim)) {
37  elems.push_back(item);
38  }
39  return elems;
40 }
41 
42 std::vector<std::string> split(const std::string &s, char delim) {
43  std::vector<std::string> elems;
44  split(s, delim, elems);
45  return elems;
46 }
47 
48 std::string join(const std::vector<std::string> &elems, const unsigned int n, const char delim) {
49  std::string s = "";
50  char d[2] = {delim, 0};
51 
52  for(unsigned int i = 0; i < elems.size() && i<n; i++){
53  if(i>0){
54  s.append(d);
55  }
56  s.append(elems.at(i));
57  }
58 
59  return s;
60 }
61 
62 bool isPrefix(std::string prefix, std::string s) {
63  if(prefix.size() > s.size()) {
64  return false;
65  }
66  return std::mismatch(prefix.begin(), prefix.end(), s.begin()).first == prefix.end();
67 }
68 
69 void setPolicyDisplayString(cModule* mod, const char* str)
70 {
71  if (getEnvir()->isGUI())
72  {
73  cDisplayString& disp = mod->getDisplayString();
74  disp.setTagArg("t", 1, "t");
75  disp.setTagArg("t", 0, (str == nullptr ? mod->getClassName() : str));
76  }
77 }
78 
79 void interconnectModules(cModule* m1, cModule* m2, std::string n1, std::string n2)
80 {
81  if (!m1->hasGate(n1.c_str()))
82  {
83  m1->addGate(n1.c_str(), cGate::INOUT, false);
84  }
85  cGate* m1In = m1->gateHalf(n1.c_str(), cGate::INPUT);
86  cGate* m1Out = m1->gateHalf(n1.c_str(), cGate::OUTPUT);
87 
88  if (!m2->hasGate(n2.c_str()))
89  {
90  m2->addGate(n2.c_str(), cGate::INOUT, false);
91  }
92  cGate* m2In = m2->gateHalf(n2.c_str(), cGate::INPUT);
93  cGate* m2Out = m2->gateHalf(n2.c_str(), cGate::OUTPUT);
94 
95  if (m2->getParentModule() == m1)
96  {
97  m1In->connectTo(m2In);
98  m2Out->connectTo(m1Out);
99  }
100  else
101  {
102  m1Out->connectTo(m2In);
103  m2Out->connectTo(m1In);
104  }
105 }
bool isPrefix(std::string prefix, std::string s)
Definition: Utils.cc:62
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Definition: Utils.cc:33
std::string join(const std::vector< std::string > &elems, const unsigned int n, const char delim)
Definition: Utils.cc:48
void interconnectModules(cModule *m1, cModule *m2, std::string n1, std::string n2)
Definition: Utils.cc:79
void setPolicyDisplayString(cModule *mod, const char *str)
Definition: Utils.cc:69