#ifndef UTILITIES_H_ #define UTILITIES_H_ #include "General.h" #include "Version.h" #include /********************************************************************** * COPYRIGHT 2020 MULTI-TECH SYSTEMS, INC. * * ALL RIGHTS RESERVED BY AND FOR THE EXCLUSIVE BENEFIT OF * MULTI-TECH SYSTEMS, INC. * * MULTI-TECH SYSTEMS, INC. - CONFIDENTIAL AND PROPRIETARY * INFORMATION AND/OR TRADE SECRET. * * NOTICE: ALL CODE, PROGRAM, INFORMATION, SCRIPT, INSTRUCTION, * DATA, AND COMMENT HEREIN IS AND SHALL REMAIN THE CONFIDENTIAL * INFORMATION AND PROPERTY OF MULTI-TECH SYSTEMS, INC. * USE AND DISCLOSURE THEREOF, EXCEPT AS STRICTLY AUTHORIZED IN A * WRITTEN AGREEMENT SIGNED BY MULTI-TECH SYSTEMS, INC. IS PROHIBITED. * ***********************************************************************/ inline bool fileExists(std::string file) { struct stat buffer = {}; return (stat(file.c_str(), &buffer) == 0) ? true : false; } inline std::string toCamelCase(const char *d_name) { std::string camelString = strdup(d_name); std::string tempString = ""; for (size_t x = 0; x < camelString.length(); x++) { if (camelString[x] == '-' || camelString[x] == '_') { tempString = camelString.substr(x + 1, 1); transform(tempString.begin(), tempString.end(), tempString.begin(), toupper); camelString.erase(x, 2); camelString.insert(x, tempString); } } return camelString; } inline void exitHandler(int code) { if (code != 0) { std::cout << "exiting with " << std::to_string(code); } exit(code); } inline mode_t fileType(std::string file) { struct stat buf = {}; if (stat(file.c_str(), &buf) == 0) { return buf.st_mode & S_IFMT; } else { return -1; } } /* findFileGlob() function has been added for generic file name search. For example: one device has file /dev/tpm0, another one has /dev/tpm3. And as a search parameter, you can specify the filepath with wildcard, findFileGlob("/dev/tpm*"). The function will find the required file. */ inline mode_t findFileGlob(std::string file) { struct stat buf = {}; glob_t gl; mode_t result = -1; if (0 == glob(file.c_str(), 0, NULL, &gl)) { if (0 < gl.gl_pathc && 0 == stat(gl.gl_pathv[0], &buf)) { result = buf.st_mode & S_IFMT; } globfree(&gl); } return result; } #endif /* UTILITIES_H_ */