teditor  1.8.0@@fee5e94
Terminal based editor written in C++
utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <string>
5 #include <stdexcept>
6 #include <vector>
7 #include <stdarg.h>
8 #include <unordered_map>
9 
10 namespace teditor {
11 
13 std::string format(const char* fmt, ...);
14 std::string format(const char* fmt, va_list& vl);
15 
16 
22 #define THROW(fmt, ...) \
23  do { \
24  std::string msg = format("Exception occured! file=%s line=%d: " \
25  fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
26  throw std::runtime_error(msg); \
27  } while(0)
28 
35 #define ASSERT(check, fmt, ...) \
36  do { \
37  if(!(check)) THROW(fmt, ##__VA_ARGS__); \
38  } while(0)
39 
40 
42 typedef std::vector<std::string> Strings;
43 
45 inline bool allStringsEmpty(const Strings& strs) {
46  return std::all_of(strs.begin(), strs.end(),
47  [](const std::string& s) { return s.empty(); });
48 }
49 
51 template <typename T>
52 bool same(const std::vector<T>& s1, const std::vector<T>& s2) {
53  if(s1.size() != s2.size()) return false;
54  return std::equal(s1.begin(), s1.end(), s2.begin());
55 }
56 
57 
58 std::string env(const std::string& s);
59 std::string expandEnvVars(const std::string& str);
60 
61 bool startsWith(const char* s1, int len, const char* s2);
62 Strings split(const std::string &s, char delim);
63 std::string join(const Strings& s, char delim, int start=0, int end=-1);
64 
65 std::string num2str(int num);
66 int str2num(const std::string& str);
67 float str2real(const std::string& str);
68 double str2double(const std::string& str);
69 
71 bool isUnderGit(const std::string& dir);
73 std::string gitBranchName(const std::string& dir);
74 
75 
76 struct CmdStatus {
77  std::string output, error;
78  int status;
79 };
80 
81 CmdStatus check_output(const std::string& cmd);
83 CmdStatus check_output(const std::string& cmd, const std::string& host);
84 
86 std::string urlHexify(const std::string& url);
87 
89 void downloadUrl(const std::string& url, const std::string& file,
90  const std::string& dnldProg = "curl",
91  const std::string& dnldProgOpts = "-s");
92 
94 std::string downloadUrlToString(const std::string& url,
95  const std::string& dnldProg = "curl",
96  const std::string& dnldProgOpts = "-s");
97 
98 bool isOpenParen(char c);
99 bool isCloseParen(char c);
100 bool isParen(char);
101 char getMatchingParen(char c);
102 
104 bool strFind(const std::string& line, const std::string& str);
106 bool iStrFind(const std::string& line, const std::string& str);
107 
108 void dos2unix(std::string& in);
109 std::string dos2unix(const std::string& in);
110 
111 
115 class History : public Strings {
116  public:
122  History(const std::string& f, int _max);
123 
124  int max() const { return maxLen; }
125  const std::string& getFile() const { return file; }
126 
128  void prune();
129 
131  void store() const;
132 
134  void add(const std::string& elem);
135 
136  const Strings& get() const { return *this; }
137 
138  protected:
139  virtual bool isDuplicate(const std::string& a, const std::string& b) const;
140 
141  private:
142  std::string file;
143  int maxLen;
144 }; // class History
145 
146 unsigned numThreads();
147 
148 template <typename K, typename V>
149 std::vector<K> sortedKeys(
150  const std::unordered_map<K, V>& map) {
151  std::vector<K> keys;
152  for (auto itr : map) keys.push_back(itr.first);
153  std::sort(keys.begin(), keys.end());
154  return keys;
155 }
156 
157 } // end namespace teditor
teditor::History::getFile
const std::string & getFile() const
Definition: utils.h:125
teditor::History::store
void store() const
Definition: utils.cpp:276
teditor::split
Strings split(const std::string &str, char delim)
Definition: utils.cpp:48
teditor::allStringsEmpty
bool allStringsEmpty(const Strings &strs)
Definition: utils.h:45
teditor::str2num
int str2num(const std::string &str)
Definition: utils.cpp:73
teditor::strFind
bool strFind(const std::string &line, const std::string &str)
Definition: utils.cpp:237
teditor::History::prune
void prune()
Definition: utils.cpp:272
teditor::getMatchingParen
char getMatchingParen(char c)
Definition: utils.cpp:205
teditor::History::get
const Strings & get() const
Definition: utils.h:136
teditor::findFirstUpwards
std::string findFirstUpwards(const std::string &dir, const std::string &file)
Definition: file_utils.cpp:158
teditor::check_output
CmdStatus check_output(const std::string &cmd)
Definition: utils.cpp:110
teditor::str2double
double str2double(const std::string &str)
Definition: utils.cpp:87
teditor::urlHexify
std::string urlHexify(const std::string &url)
Hexify the input url.
Definition: utils.cpp:169
teditor::numThreads
unsigned numThreads()
Definition: utils.cpp:301
teditor::Strings
std::vector< std::string > Strings
Definition: utils.h:42
teditor::join
std::string join(const Strings &s, char delim, int start, int end)
Definition: utils.cpp:56
utils.h
teditor::downloadUrl
void downloadUrl(const std::string &url, const std::string &file, const std::string &dnldProg, const std::string &dnldProgOpts)
Download the content of input url into the given file.
Definition: utils.cpp:179
teditor::same
bool same(const std::vector< T > &s1, const std::vector< T > &s2)
Definition: utils.h:52
teditor::isCloseParen
bool isCloseParen(char c)
Definition: utils.cpp:199
teditor::History::History
History(const std::string &f, int _max)
Ctor.
Definition: utils.cpp:265
teditor::dos2unix
void dos2unix(std::string &in)
Definition: utils.cpp:249
teditor::isParen
bool isParen(char c)
Definition: utils.cpp:203
teditor::CmdStatus
Definition: utils.h:76
teditor::CmdStatus::error
std::string error
Definition: utils.h:77
teditor::CmdStatus::status
int status
Definition: utils.h:78
teditor::expandEnvVars
std::string expandEnvVars(const std::string &str)
Definition: utils.cpp:219
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::History::add
void add(const std::string &elem)
Definition: utils.cpp:285
teditor::str2real
float str2real(const std::string &str)
Definition: utils.cpp:80
teditor::num2str
std::string num2str(int num)
Definition: utils.cpp:66
file_utils.h
utf8.h
teditor::History
Container to store history (files-visited or command-history, etc)
Definition: utils.h:115
teditor::CmdStatus::output
std::string output
Definition: utils.h:77
teditor::History::isDuplicate
virtual bool isDuplicate(const std::string &a, const std::string &b) const
Definition: utils.cpp:297
teditor::gitBranchName
std::string gitBranchName(const std::string &dir)
Definition: utils.cpp:99
teditor::iStrFind
bool iStrFind(const std::string &line, const std::string &str)
Definition: utils.cpp:241
teditor::startsWith
bool startsWith(const char *s1, int len1, const char *s2)
Definition: utils.cpp:41
teditor::History::max
int max() const
Definition: utils.h:124
teditor::slurpToArr
Strings slurpToArr(const std::string &file)
Definition: file_utils.cpp:97
teditor::format
std::string format(const char *fmt, va_list &vl)
Definition: utils.cpp:22
teditor::isOpenParen
bool isOpenParen(char c)
Definition: utils.cpp:195
teditor::sortedKeys
std::vector< K > sortedKeys(const std::unordered_map< K, V > &map)
Definition: utils.h:149
teditor::downloadUrlToString
std::string downloadUrlToString(const std::string &url, const std::string &dnldProg, const std::string &dnldProgOpts)
Download the content of input url into a string.
Definition: utils.cpp:187
teditor::isFile
bool isFile(const std::string &f)
Definition: file_utils.cpp:43
teditor::env
std::string env(const std::string &s)
Definition: utils.cpp:36
teditor
Definition: any.hpp:10
teditor::isUnderGit
bool isUnderGit(const std::string &dir)
Definition: utils.cpp:94