QtProtobuf  0.6
Protobuf plugin to generate Qt classes
utils.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once
27 
28 #include <string>
29 #include <vector>
30 #include <sstream>
31 #include <unordered_map>
32 #include <list>
33 #include <algorithm>
34 
35 #ifdef QT_PROTOBUF_DEBUG_GENERATOR
36 #define QT_PROTOBUF_DEBUG(X) std::cout << X << std::endl
37 #else
38 #define QT_PROTOBUF_DEBUG(X)
39 #endif
40 
41 namespace google { namespace protobuf {
42 class FileDescriptor;
43 }}
44 
45 namespace QtProtobuf {
46 namespace generator {
47 
53 class utils {
54 public:
55 static std::vector<std::string> split(const std::string &str, char delim)
56 {
57  std::vector<std::string> container;
58  if (str.size() <= 0) {
59  return container;
60  }
61  std::stringstream stream(str);
62  std::string token;
63  while (std::getline(stream, token, delim)) {
64  container.push_back(token);
65  }
66 
67  return container;
68 }
69 
70 static void replace(std::string &data, const std::string &from, const std::string &to) {
71  size_t pos = data.find(from);
72  while (pos != std::string::npos) {
73  data.replace(pos, from.size(), to);
74  pos = data.find(from, pos + to.size());
75  }
76 }
77 
78 static void tolower(std::string &str) {
79  std::transform(std::begin(str), std::end(str), std::begin(str), ::tolower);
80 }
81 
82 static std::string removeFileSuffix(std::string fileName) {
83  size_t index = fileName.rfind(".");
84  fileName.resize(index);
85  return fileName;
86 }
87 
88 static std::string extractFileBasename(std::string fileName) {
89  size_t index = fileName.rfind(".");
90  fileName.resize(index);
91  index = fileName.rfind("/");
92  if (index != std::string::npos) {
93  return fileName.substr(index + 1, fileName.size() - 1);
94  }
95  return fileName;
96 }
97 
98 
99 static std::string upperCaseName(const std::string &name)
100 {
101  std::string upperCaseName(name);
102  upperCaseName[0] = static_cast<char>(::toupper(upperCaseName[0]));
103  return upperCaseName;
104 }
105 
106 static std::string lowerCaseName(const std::string &name)
107 {
108  std::string lowerCaseName(name);
109  lowerCaseName[0] = static_cast<char>(::tolower(lowerCaseName[0]));
110  return lowerCaseName;
111 }
112 
113 inline static std::string& rtrim(std::string& s)
114 {
115  s.erase(s.find_last_not_of(" \t\n\r\f\v") + 1);
116  return s;
117 }
118 
119 // trim from beginning of string (left)
120 inline static std::string& ltrim(std::string& s)
121 {
122  s.erase(0, s.find_first_not_of(" \t\n\r\f\v"));
123  return s;
124 }
125 
126 // trim from both ends of string (right then left)
127 inline static std::string& trim(std::string& s)
128 {
129  return ltrim(rtrim(s));
130 }
131 
132 };
133 
134 #define UNUSED(expr) do { (void)(expr); } while (0)
135 
136 using PackagesList = std::unordered_map<std::string/*package*/, std::list<const ::google::protobuf::FileDescriptor *>>;
137 
138 } //namespace generator
139 } //namespace QtProtobuf