MetroCollect  2.3.4
SourceTools.h
Go to the documentation of this file.
1 //
2 // SourceTools.h
3 //
4 // Created on August 27th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <array>
24 #include <cctype>
25 #include <fstream>
26 #include <string>
27 #include <string_view>
28 #include <vector>
29 
30 using namespace std::literals;
31 
32 
40  inline uint64_t parseUint(const char*& buffer) noexcept {
41  uint64_t result = 0;
42  while (!std::isdigit(*buffer))
43  buffer++;
44  while (std::isdigit(*buffer)) {
45  result = (result << 1) + (result << 3) + *buffer - '0';
46  buffer++;
47  }
48  return result;
49  }
50 
60  bool resetFile(std::ifstream& file, std::vector<char>& buffer, const std::string_view& path);
61 
71  bool readFile(std::ifstream& file, std::vector<char>& buffer, const std::string_view& path);
72 
73 
78  struct KeyUnit {
79  std::string_view key; //<! Key to find in field name for the unit to be applicable
80  std::string_view unit; //<! The unit
81 
82  constexpr KeyUnit() : key(""sv), unit(""sv) { } //<! Default constructor
83  constexpr KeyUnit(std::string_view aKey, std::string_view aUnit) : key(aKey), unit(aUnit) { } //<! Constructor with values
84  };
85 
95  template<size_t N>
96  std::string findUnit(const std::string& name, const std::array<KeyUnit, N>& keyUnitAssociation, const std::string_view& defaultUnit) {
97  for (const auto& val : keyUnitAssociation) {
98  if (name.find(val.key) != std::string::npos)
99  return std::string(val.unit);
100  }
101  return std::string(defaultUnit);
102  }
103 
104 
140  int alphanumCompare(const char* l, const char* r);
141 }
Stuct to associate a field name containing a certain key with a unit.
Definition: SourceTools.h:78
bool readFile(std::ifstream &file, std::vector< char > &buffer, const std::string_view &path)
Attempts to read file into buffer.
Definition: SourceTools.cc:55
Namespace for sources of metrics objects and operations.
uint64_t parseUint(const char *&buffer) noexcept
Fast and unsafe unsigned integer parser.
Definition: SourceTools.h:40
constexpr KeyUnit(std::string_view aKey, std::string_view aUnit)
Definition: SourceTools.h:83
int alphanumCompare(const char *l, const char *r)
Alphanumeric string comparison.
Definition: SourceTools.cc:70
bool resetFile(std::ifstream &file, std::vector< char > &buffer, const std::string_view &path)
Attempts to open a file and set buffer size accordingly.
Definition: SourceTools.cc:25
std::string findUnit(const std::string &name, const std::array< KeyUnit, N > &keyUnitAssociation, const std::string_view &defaultUnit)
Tries to associate a field name with a unit, falling back on the default unit if none matches...
Definition: SourceTools.h:96