rtop
rtop_utils.h
Go to the documentation of this file.
1 #define _BSD_SOURCE
2 #include <vector>
3 #include <map>
4 #include <unistd.h>
5 #include <algorithm>
6 #include <vector>
7 #include <ncurses.h>
8 #include <stdexcept>
9 
10 #ifndef RTOP_UTILS_H_
11 #define RTOP_UTILS_H_
12 #include "params.h"
13 #include "info.h"
14 #include "rtop_logger.h"
15 
16 extern src::severity_logger<severity_level> lg;
17 extern logSpacer log_spacer;
18 
19 namespace rtop
20 {
22 
26 template <typename T, typename Compare>
27 std::vector<int> sort_permutation(const std::vector<T>& vec,Compare compare)
28 {
29  std::vector<int> p(vec.size());
30  std::iota(p.begin(), p.end(), 0);
31  std::sort(p.begin(), p.end(),[&](unsigned int i, unsigned int j){return compare(vec[i], vec[j]); });
32  return p;
33 }
34 
36 
37 char toChar(std::string str1)
38 {
39  if (str1[0] == '\\' && str1[1] == 'n')
40  return '\n';
41  else if (str1 == " ")
42  return ' ';
43  else if (str1 == ",")
44  return ',';
45  else
46  return ',';
47 }
48 
50 
51 
56 {
57  private:
59  char sep;
61  std::vector<std::string> words;
63  int sz;
64  public:
66  StringParser(char c): sep{c}{sz=0;}
68  void operator<<(std::string);
70  std::vector<std::string> Words() const {return words;}
72  int size() const {return sz;}
74  void clear()
75  {
76  words.clear();
77  sz = 0;
78  }
80  void setSeparator(char c){sep = c;}
81 };
82 
83 void StringParser::operator<<(std::string str1)
84 {
85  int len=0, start=0;
86  for(int i=0; i<str1.size(); i++) // iterate over each character of line
87  {
88  if (str1[i] == sep) // if character is separtor, parse substring from position start, of length len, and push into words vector
89  {
90  words.push_back(str1.substr(start, len));
91  len = 0;
92  start = i+1;
93  } // increment length of current substring
94  else
95  len++;
96  }
97  words.push_back(str1.substr(start, len)); // push the last substring
98  sz = words.size();
99  //remove whitespace
100  //
101  for(auto& word: words)
102  word.erase(remove_if(word.begin(), word.end(), isspace), word.end());
103 }
104 
106 void milliSleep(int msec)
107 {
109  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"--> milliSleep::"<<msec;
110  int r = 0;
111  for(int i=0; i<(msec/20); i++) // divide interval into 20ms parts
112  {
113  r = usleep(20000); // sleep for 20 msec
114  if (r == -1)
115  {
116  BOOST_LOG_SEV(lg, error)<<log_spacer<<"usleep: "<<strerror(errno);
118  throw std::runtime_error("usleep: ");
119  }
120  if (EXIT) // to improve responsivness to F10 press
121  {
122  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"milliSleep::rcvd EXIT, "<<msec-(i+1)*20<<" msec remaining";
123  break;
124  }
125  }
126  r = usleep((msec%20 )*1000); // sleep for remaining usec
127  if (r == -1)
128  {
129  BOOST_LOG_SEV(lg, error)<<log_spacer<<"usleep: "<<strerror(errno);
131  throw std::runtime_error("usleep: ");
132  }
133  BOOST_LOG_SEV(lg, debug)<<log_spacer<<"<-- milliSleep::with "<<0<<" msec remaining";
135 }
136 
138 std::string msecToTimeStr(unsigned long msec)
139 {
140  unsigned long sec = msec/1000; // seconds
141  int msec_remainder = msec%1000; // remaining msec
142  unsigned long minutes = sec/60; // minutes
143  int sec_remainder = sec%60; // remaining seconds
144  std::stringstream ss{""}; // construct time string
145  ss<<minutes<<":"<<sec_remainder<<"."<<msec_remainder;
146  return ss.str();
147 }
148 
150 template<class T>
151 std::ostream& operator<<(std::ostream& ofs, const InfoProc<T>& obj)
152 {
153  ofs<<obj.val;
154  return ofs;
155 }
156 
158 
162 template<class T>
163 std::string vec_to_string(std::vector<T>& vec)
164 {
165  std::stringstream ss;
166  for(auto elem: vec)
167  ss<<elem<<" ";
168  return ss.str();
169 }
170 
172 template<class T>
173 void swap(std::vector<T>& v, int i, int j)
174 {
175  T tmp = v[i];
176  v[i] = v[j];
177  v[j] = tmp;
178 }
179 
181 template<class T>
182 void swap(T&a, T&b)
183 {
184  T tmp = a;
185  a = b;
186  b = tmp;
187 }
188 
190 std::map<std::string, int> color_map{{"BLACK", COLOR_BLACK}, {"RED", COLOR_RED}, {"GREEN", COLOR_GREEN}, {"YELLOW", COLOR_YELLOW}, {"BLUE", COLOR_BLUE}, {"MAGENTA", COLOR_MAGENTA}, {"CYAN", COLOR_CYAN}, {"WHITE", COLOR_WHITE}};
191 
192 } // namespace rtop
193 
194 #endif // RTOP_UTILS_H_
responsible for parsing strings in a line, specfied by some character separator
Definition: rtop_utils.h:55
void setSeparator(char c)
sets separator character
Definition: rtop_utils.h:80
void milliSleep(int msec)
makes calling thread sleep for specified milliseconds
Definition: rtop_utils.h:106
char toChar(std::string str1)
converts string to its equivalent char value.
Definition: rtop_utils.h:37
void operator<<(std::string)
reads in a line and stores parsed string into StringParser::words
Definition: rtop_utils.h:83
logSpacer log_spacer
Definition: rtop_logger.h:186
void clear()
clears StringParser::words vector
Definition: rtop_utils.h:74
std::string msecToTimeStr(unsigned long msec)
converts msec to TimerStr for display as min:sec.secs_fraction
Definition: rtop_utils.h:138
char sep
character separator
Definition: rtop_utils.h:59
StringParser(char c)
StringParser constructor. Instantiates a StringParser object with provided separator character.
Definition: rtop_utils.h:66
void delSpace()
deletes one white-space from white-space string corresponding to invoking thread
Definition: rtop_logger.h:151
std::vector< std::string > Words() const
returns a vector of parsed strings stored in StringParser::words
Definition: rtop_utils.h:70
std::ostream & operator<<(std::ostream &ofs, const InfoProc< T > &obj)
overloaded << operator for printing InfoProc contents
Definition: rtop_utils.h:151
std::vector< int > sort_permutation(const std::vector< T > &vec, Compare compare)
returns permutation vector after sorting the given vector based on the provided compare function
Definition: rtop_utils.h:27
void addSpace()
adds one white-space to white-space string corresponding to invoking thread
Definition: rtop_logger.h:135
src::severity_logger< severity_level > lg
Definition: rtop_logger.h:108
enables indentation of logs for easy viewing
Definition: rtop_logger.h:118
data structure that defines type of entry from which panel menu entries obtain their c-string data
Definition: info.h:71
std::vector< std::string > words
stores the parsed strings
Definition: rtop_utils.h:61
std::string vec_to_string(std::vector< T > &vec)
concatenates a vectors entries into a string
Definition: rtop_utils.h:163
void swap(std::vector< T > &v, int i, int j)
swaps supplied vectors' element corresponding to indices i and j
Definition: rtop_utils.h:173
bool EXIT
Definition: params.h:9
int sz
size of words
Definition: rtop_utils.h:63
std::map< std::string, int > color_map
dictionary containing color_pair number corresponding to each color_name
Definition: rtop_utils.h:190
Definition: action.h:7
int size() const
returns size of StringParser::words vector
Definition: rtop_utils.h:72