MetroCollect  2.3.4
SourceTools.cc
Go to the documentation of this file.
1 //
2 // SourceTools.cc
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 #include "SourceTools.h"
22 
23 
25  bool resetFile(std::ifstream& file, std::vector<char>& buffer, const std::string_view& path) {
26  errno = 0;
27  buffer.clear();
28  buffer.resize(1024, '\0');
29  file = std::ifstream(path.data(), std::ios::binary);
30  if (errno) {
31  perror(std::string(path).append(": Error opening file").c_str());
32  errno = 0;
33  return false;
34  }
35 
36  file.clear();
37  file.seekg(0);
38  file.read(buffer.data(), buffer.size());
39  if (errno) {
40  perror(std::string(path).append(": Error reading file").c_str());
41  errno = 0;
42  return false;
43  }
44 
45  while (!file.eof()) {
46  buffer.resize(buffer.size() * 2, '\0');
47  file.clear();
48  file.seekg(0);
49  file.read(buffer.data(), buffer.size());
50  }
51  buffer.resize(buffer.size() * 2, '\0');
52  return true;
53  }
54 
55  bool readFile(std::ifstream& file, std::vector<char>& buffer, const std::string_view& path) {
56  errno = 0;
57  file.clear();
58  file.seekg(0);
59  file.read(buffer.data(), buffer.size());
60  if (errno) {
61  perror(std::string(path).append(": Error reading file").c_str());
62  errno = 0;
63  return resetFile(file, buffer, path);
64  }
65  buffer[file.gcount()] = '\0';
66  return true;
67  }
68 
69 
70  int alphanumCompare(const char* l, const char* r) {
71  enum mode_t { STRING, NUMBER } mode = STRING;
72  while (*l && *r) {
73  switch (mode) {
74  case STRING:
75  char l_char, r_char;
76  while ((l_char = *l) && (r_char = *r)) {
77  const bool l_digit = std::isdigit(l_char);
78  const bool r_digit = std::isdigit(r_char);
79  if (l_digit && r_digit) {
80  mode = NUMBER;
81  break;
82  }
83  if (l_digit)
84  return -1;
85  if (r_digit)
86  return +1;
87  const int diff = l_char - r_char;
88  if(diff != 0)
89  return diff;
90  ++l;
91  ++r;
92  }
93  break;
94  case NUMBER:
95  char *end;
96  unsigned long l_int = std::strtoul(l, &end, 0);
97  l = end;
98  unsigned long r_int = std::strtoul(r, &end, 0);
99  r = end;
100  const long diff = l_int - r_int;
101  if(diff != 0)
102  return diff;
103  mode = STRING;
104  break;
105  }
106  }
107  if (*r)
108  return -1;
109  if (*l)
110  return +1;
111  return 0;
112  }
113 }
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.
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