Cerata
A library to generate structural hardware designs
utils.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cerata/utils.h"
16 
17 #include <unordered_map>
18 #include <string>
19 
20 #include "cerata/logging.h"
21 #include "cerata_config/config.h"
22 
23 namespace cerata {
24 
25 std::string ToUpper(std::string str) {
26  for (auto &ch : str) ch = std::toupper(ch);
27  return str;
28 }
29 
30 std::string ToLower(std::string str) {
31  for (auto &ch : str) ch = std::tolower(ch);
32  return str;
33 }
34 
35 std::string ToString(const std::unordered_map<std::string, std::string> &meta) {
36  std::string result;
37  if (!meta.empty()) {
38  result += "{";
39  size_t i = 0;
40  for (const auto &kv : meta) {
41  result += kv.first + "=" + kv.second;
42  if (i != meta.size() - 1) {
43  result += ",";
44  }
45  i++;
46  }
47  result += "}";
48  }
49  return result;
50 }
51 
52 void CreateDir(const std::string &dir_name) {
53  // TODO(johanpel): Create directories in a portable manner, or just wait for <filesystem>
54  int ret = system(("mkdir -p " + dir_name).c_str());
55  if (ret == -1) {
56  CERATA_LOG(ERROR, "Could not create directory.");
57  }
58 }
59 
60 bool FileExists(const std::string &name) {
61  std::ifstream f(name.c_str());
62  return f.good();
63 }
64 
65 std::string version() {
66  return "cerata " + std::to_string(CERATA_VERSION_MAJOR)
67  + "." + std::to_string(CERATA_VERSION_MINOR)
68  + "." + std::to_string(CERATA_VERSION_PATCH);
69 }
70 
71 } // namespace cerata
cerata::ToLower
std::string ToLower(std::string str)
Convert string to lower-case.
Definition: utils.cc:30
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::ToString
std::string ToString(Expression::Op operation)
Human-readable expression operator.
Definition: expression.cc:149
cerata::ToUpper
std::string ToUpper(std::string str)
Convert string to upper-case.
Definition: utils.cc:25
cerata::CreateDir
void CreateDir(const std::string &dir_name)
Create a directory.
Definition: utils.cc:52
cerata::version
std::string version()
Return library version.
Definition: utils.cc:65
cerata::FileExists
bool FileExists(const std::string &name)
Check if file exists.
Definition: utils.cc:60