summaryrefslogtreecommitdiff
path: root/include/Utility/Utility.h
blob: 8d68baa39e8e12d94cb14d73b121792cf6d76e91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef UTILITIES_H_
#define UTILITIES_H_

#include "General.h"
#include "Version.h"
#include <glob.h>

/**********************************************************************
* 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_ */