ApraLinuxUtils 1.0.0
C++ utility library for embedded Linux systems
 
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1/*
2 * Utils.cpp
3 *
4 * Copyright (c) 2024 Apra Labs
5 *
6 * This file is part of ApraUtils.
7 *
8 * Licensed under the MIT License.
9 * See LICENSE file in the project root for full license information.
10 */
11
12#include <stdio.h>
13#include <memory>
14#include <sys/stat.h>
15#include <algorithm>
16#include <dirent.h>
17#include <cstring>
18#include <fstream>
19#include "models/Range.h"
20#include "utils/Utils.h"
21
22using namespace apra;
23Utils::Utils()
24{
25
26}
27
28Utils::~Utils()
29{
30}
31
32bool Utils::saveRawFile(string fileName, uint8_t *data, size_t size)
33{
34 FILE *fp = fopen(fileName.c_str(), "wb");
35 if (!fp)
36 {
37 return false;
38 }
39 size_t ret = fwrite(data, 1, size, fp);
40 fclose(fp);
41 return ret > 0;
42}
43
44string Utils::makeDir(string path)
45{
46 string cmd = "mkdir -p \"";
47 cmd += path + "\"";
48 string errStr;
49 try
50 {
51 errStr = exec(cmd);
52 } catch (std::runtime_error &e)
53 {
54 printf("mkdir error: %s\n", e.what());
55 }
56 if (errStr.length())
57 {
58 printf("Utils::makeDir %s\n", errStr.c_str());
59 return "";
60 }
61 return path;
62}
63
64std::string Utils::exec(const std::string &cmd, bool debug)
65{
66 if (debug)
67 {
68 printf("CMD To be Run %s \n", cmd.c_str());
69 }
70 std::array<char, 128> buffer;
71 std::string result;
72 std::shared_ptr<FILE> pipe(popen((cmd + " 2>&1").c_str(), "r"), pclose);
73 if (!pipe)
74 {
75 throw std::runtime_error(string("popen() failed for") + cmd);
76 }
77 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
78 {
79 result += buffer.data();
80 }
81 return result;
82}
83
84vector<uint8_t> Utils::extractBytes(uint64_t hexData, uint8_t numberOfBytes)
85{
86 vector<uint8_t> extractedBytes;
87 numberOfBytes = numberOfBytes > 8 ? 8 : numberOfBytes;
88 for (uint8_t count = 0; count < numberOfBytes; count++)
89 {
90 uint8_t extractByte = (hexData >> ((numberOfBytes - 1 - count) * 8))
91 & 0xFF;
92 extractedBytes.push_back(extractByte);
93 }
94 return extractedBytes;
95}
96
97uint64_t Utils::combineBytes(vector<uint8_t> byteArray)
98{
99 uint64_t combinedResult = 0;
100 for (uint8_t count = 0; count < byteArray.size(); count++)
101 {
102 combinedResult = ((combinedResult << 8) & 0xFFFFFFFFFFFFFF00)
103 | byteArray[count];
104 }
105 return combinedResult;
106}
107
108bool Utils::inRange(int64_t value, Range range)
109{
110 return (value >= range.m_min && value <= range.m_max);
111}
112
113bool Utils::directoryExists(const std::string &path)
114{
115 bool returnValue;
116 struct stat info;
117 if (stat(path.c_str(), &info) != 0)
118 {
119 returnValue = false;
120 }
121 else
122 {
123 returnValue = S_ISDIR(info.st_mode) != 0;
124 }
125 return returnValue;
126}
127
128bool Utils::fileExists(const std::string &path)
129{
130 bool returnValue;
131 struct stat info;
132 if (stat(path.c_str(), &info) != 0)
133 {
134 returnValue = false;
135 }
136 else
137 {
138 returnValue = S_ISREG(info.st_mode) != 0;
139 }
140 return returnValue;
141}
142
143bool Utils::caseInsensitiveSearch(std::string const str,
144 std::string const pattern)
145{
146 if (pattern.empty()) {
147 return true;
148 }
149
150 std::string::const_iterator it = std::search(str.begin(), str.end(),
151 pattern.begin(), pattern.end(),
152 [](unsigned char ch1, unsigned char ch2)
153 { return std::toupper(ch1) == std::toupper(ch2);});
154 return (it != str.end());
155}
156
157string Utils::trim(string str)
158{
159 string WHITESPACE = " \n\r\t\f\v";
160 string returnValue;
161 size_t first = str.find_first_not_of(WHITESPACE);
162 if (first == std::string::npos)
163 {
164 returnValue = "";
165 }
166 else
167 {
168 size_t last = str.find_last_not_of(WHITESPACE);
169 returnValue = str.substr(first, (last - first + 1));
170 }
171 return returnValue;
172}
173
174void Utils::getFilesInDirectoryRecursive(const std::string &directoryPath,
175 std::vector<std::string> &fileList)
176{
177 DIR *dir = opendir(directoryPath.c_str());
178 if (dir == nullptr)
179 {
180 printf("Error opening directory: %s\n", directoryPath.c_str());
181 return;
182 }
183
184 dirent *entry;
185 while ((entry = readdir(dir)) != nullptr)
186 {
187 if (std::string(entry->d_name) != "."
188 && std::string(entry->d_name) != "..")
189 {
190 std::string fullPath = directoryPath + "/" + entry->d_name;
191
192 if (entry->d_type == DT_DIR)
193 {
194 getFilesInDirectoryRecursive(fullPath, fileList); // Recursively call for subdirectory
195 }
196 else
197 {
198 fileList.push_back(fullPath); // Add file path to the list
199 }
200 }
201 }
202
203 closedir(dir);
204}
205
206void Utils::getFilesInDirectory(string path, vector<string> &files,
207 bool recursive)
208{
209 files.clear();
210 if (recursive)
211 {
212 getFilesInDirectoryRecursive(path, files);
213 return;
214 }
215 DIR *dir = opendir(path.c_str());
216 if (dir == nullptr)
217 {
218 printf("Error opening directory: %s\n", path.c_str());
219 return;
220 }
221 dirent *entry;
222 while ((entry = readdir(dir)) != nullptr)
223 {
224 if (entry->d_type == DT_REG)
225 {
226 files.push_back(entry->d_name); // Add file name to the list
227 }
228 }
229 closedir(dir);
230}
231
232string Utils::readTextFile(string filePath)
233{
234 string response;
235 bool fileExistCheck = fileExists(filePath);
236 if (!fileExistCheck)
237 {
238 return response;
239 }
240 ifstream inFile;
241 inFile.open(filePath.c_str());
242 if (!inFile)
243 {
244 return response;
245 }
246 char x;
247 string data;
248 while (inFile >> x)
249 {
250 data = data + x;
251 }
252 inFile.close();
253
254 if (data.length() > 0)
255 {
256 response = data;
257 }
258 return response;
259}
260uint16_t Utils::convertToU12p4(double value)
261{
262 uint16_t decVal = value;
263 uint16_t intVal = value;
264 value = value - intVal;
265
266 for (int16_t idx = 0; idx < 4; idx++)
267 {
268 value *= 2;
269 intVal = value;
270 if (intVal == 1)
271 {
272 value = value - intVal;
273 decVal = (decVal << 1) + 1;
274 }
275 else
276 {
277 decVal = decVal << 1;
278 }
279 }
280 return decVal;
281}
282double Utils::convertFrom12p4(uint16_t value)
283{
284 double returnValue;
285 double decVal = 0;
286 uint16_t fractionValue = 0;
287 decVal = value >> 4;
288 fractionValue = value & 0x000F;
289 double temp = 0.00;
290 double twos = 2.00;
291 for (int32_t idx = 3; idx >= 0; idx--)
292 {
293 temp += (fractionValue >> idx & 0x1) / twos;
294 twos = twos * 2;
295 }
296 returnValue = (decVal + temp);
297 return returnValue;
298}
299uint16_t Utils::convertTo10p6(double value)
300{
301 uint16_t decVal = value;
302 uint16_t intVal = value;
303 value = value - intVal;
304
305 for (int16_t idx = 0; idx < 6; idx++)
306 {
307 value *= 2;
308 intVal = value;
309 if (intVal == 1)
310 {
311 value = value - intVal;
312 decVal = (decVal << 1) + 1;
313 }
314 else
315 {
316 decVal = decVal << 1;
317 }
318 }
319 return decVal;
320
321}
322double Utils::convertFrom10p6(uint16_t value)
323{
324 double returnValue;
325 double decVal = 0;
326 uint16_t fractionValue = 0;
327 decVal = value >> 6;
328 fractionValue = value & 0x0003F;
329 double temp = 0.00;
330 double twos = 2.00;
331 for (int32_t idx = 5; idx >= 0; idx--)
332 {
333 temp += (fractionValue >> idx & 0x1) / twos;
334 twos = twos * 2;
335 }
336 returnValue = (decVal + temp);
337 return returnValue;
338
339}
340uint16_t Utils::convertToUFormat(double value, uint8_t format)
341{
342 uint16_t decVal = value;
343 uint16_t intVal = value;
344 value = value - intVal;
345
346 for (int16_t idx = 0; idx < format; idx++)
347 {
348 value *= 2;
349 intVal = value;
350 if (intVal == 1)
351 {
352 value = value - intVal;
353 decVal = (decVal << 1) + 1;
354 }
355 else
356 {
357 decVal = decVal << 1;
358 }
359 }
360 return decVal;
361
362}
363double Utils::convertFromUFormat(uint16_t value, uint8_t format)
364{
365 double returnValue;
366 double decVal = 0;
367 uint16_t fractionValue = 0;
368 decVal = value >> format;
369 fractionValue = value & ((1 << format) - 1);
370 double temp = 0.00;
371 double twos = 2.00;
372 for (int32_t idx = (format - 1); idx >= 0; idx--)
373 {
374 temp += (fractionValue >> idx & 0x1) / twos;
375 twos = twos * 2;
376 }
377 returnValue = (decVal + temp);
378 return returnValue;
379}
380uint64_t Utils::mergefrom8Bytes(uint8_t * bytes)
381{
382 uint64_t response = 0;
383 if (bytes)
384 {
385 for (uint8_t cnt = 0; cnt < 8; cnt++)
386 {
387 uint64_t pushValue = bytes[cnt];
388 response |= pushValue << (8 * cnt);
389 }
390 }
391 return response;
392}
393
394void Utils::extractTo8Bytes(uint64_t timeInSec, uint8_t *bytes)
395{
396 if (bytes)
397 {
398 for (uint8_t cnt = 0; cnt < 8; cnt++)
399 {
400 uint64_t extractedBytes = (timeInSec >> (8 * cnt));
401 bytes[cnt] = extractedBytes & 0xFF;
402 }
403 }
404}
int64_t m_max
Definition Range.h:27
int64_t m_min
Definition Range.h:26
static uint16_t convertToUFormat(double value, uint8_t format)
Definition Utils.cpp:340
static bool fileExists(const std::string &path)
Definition Utils.cpp:128
static bool saveRawFile(string fileName, uint8_t *data, size_t size)
Definition Utils.cpp:32
static string makeDir(string path)
Definition Utils.cpp:44
static uint64_t mergefrom8Bytes(uint8_t *bytes)
Definition Utils.cpp:380
static bool inRange(int64_t value, Range range)
Definition Utils.cpp:108
static double convertFrom12p4(uint16_t value)
Definition Utils.cpp:282
static void getFilesInDirectory(string path, vector< string > &files, bool recursive=true)
Definition Utils.cpp:206
static double convertFromUFormat(uint16_t value, uint8_t format)
Definition Utils.cpp:363
static uint16_t convertTo10p6(double value)
Definition Utils.cpp:299
static string trim(string str)
Definition Utils.cpp:157
static vector< uint8_t > extractBytes(uint64_t hexData, uint8_t numberOfBytes)
Definition Utils.cpp:84
static uint64_t combineBytes(vector< uint8_t > byteArray)
Definition Utils.cpp:97
static string exec(const string &cmd, bool debug=false)
Definition Utils.cpp:64
static bool directoryExists(const std::string &path)
Definition Utils.cpp:113
static uint16_t convertToU12p4(double value)
Definition Utils.cpp:260
static string readTextFile(string filePath)
Definition Utils.cpp:232
static bool caseInsensitiveSearch(std::string const str, std::string const pattern)
Definition Utils.cpp:143
static double convertFrom10p6(uint16_t value)
Definition Utils.cpp:322
static void extractTo8Bytes(uint64_t timeInSec, uint8_t *bytes)
Definition Utils.cpp:394