summaryrefslogtreecommitdiff
path: root/src/MTS_System.cpp
blob: abed817a182370250c2d77bd8b4a06f32490c9dd (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2015 by Multi-Tech Systems
 *
 * This file is part of libmts.
 *
 * libmts is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libmts is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libmts.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <mts/MTS_System.h>
#include <fstream>
#include <sstream>
#include <cassert>

#ifdef WIN32
#include <windows.h>

//WIN32: FILETIME structure has a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.

static int64_t getEpochTimeMicros() {
    const SYSTEMTIME EPOCH = {1970, 1, 4, 1, 0, 0, 0, 0};
    FILETIME ft;
    BOOL ok = SystemTimeToFileTime(&EPOCH, &ft);
    assert(ok);
    int64_t epochTimeMicros = ((static_cast<uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) / 10;
    return epochTimeMicros;
}

static int64_t getSystemTimeMicros() {
    SYSTEMTIME st;
    GetSystemTime(&st);
    FILETIME ft;
    BOOL ok = SystemTimeToFileTime(&st, &ft);
    assert(ok);
    int64_t systemTimeMicros = ((static_cast<uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) / 10;
    return systemTimeMicros;
}

static int64_t getClockFrequency() {
    LARGE_INTEGER freq;
    BOOL ok = QueryPerformanceFrequency(&freq);
    assert(ok);
    return freq.QuadPart;
}

static int64_t getClockValue() {
    LARGE_INTEGER value;
    BOOL ok = QueryPerformanceCounter(&value);
    assert(ok);
    return value.QuadPart;
}

#else
#include <time.h>
#endif

using namespace MTS;

uint64_t System::timeMicros() {
    int64_t micros = 0;
#ifdef WIN32
    static const int64_t EPOCH_TIME_MICROS = getEpochTimeMicros();
    micros = getSystemTimeMicros() - EPOCH_TIME_MICROS;
#else
    timespec ts;
    int result = clock_gettime(CLOCK_REALTIME, &ts);
    if (result == 0) {
        micros = (static_cast<int64_t>(ts.tv_sec) * 1000000)
        + (ts.tv_nsec / 1000);
    }
#endif
    return micros;
}

uint64_t System::precisionTimeMicros() {
    int64_t micros = 0;
#ifdef WIN32
    static const double TO_MICROS = 1000000.0 / getClockFrequency();
    int64_t value = getClockValue();
    micros = static_cast<int64_t>(value * TO_MICROS);
#else
    micros = timeMicros();
#endif
    return micros;
}

uint64_t System::monoTimeMicros() {
    uint64_t micros = 0;
#ifdef WIN32
    micros = static_cast<uint64_t>(GetTickCount64()) * 1000;
#else
    timespec ts;
    int result = clock_gettime(CLOCK_MONOTONIC, &ts);
    if (result == 0) {
        micros = (static_cast<uint64_t>(ts.tv_sec) * 1000000)
            + (ts.tv_nsec / 1000);
    }
#endif
    return micros;
}

bool System::isBigEndian() {
    static union {
            uint32_t i;
            char c[4];
    } endian = { 0x01020304 };

    return endian.c[0] == 1;
}

void System::swapBytes(uint8_t* const pBuffer, const uint32_t iSize) {
    if (iSize > 1 && pBuffer != 0) {
        uint8_t cByte = 0;
        uint32_t i;
        uint32_t j;
        for (i = 0, j = iSize - 1; i < j; i++, j--) {
            cByte = pBuffer[i];
            pBuffer[i] = pBuffer[j];
            pBuffer[j] = cByte;
        }
    }
}

int32_t System::cmd(const std::string& cmd, std::string& result) {
    std::string output;
    FILE * stream;
    const int max_buffer = 256;
    char buffer[max_buffer];
    int32_t code = -1;

    stream = popen(cmd.c_str(), "r");
    if (stream) {
        while (!feof(stream))
            if (fgets(buffer, max_buffer, stream) != NULL)
                output.append(buffer);
        code = pclose(stream);
    }

    result = output;

    return code;
}

int32_t System::readFile(const std::string& path, std::string& result) {
    std::ifstream infile(path.c_str());
    std::stringstream ss;

    if (!infile.is_open()) {
        return -1;
    }

    ss << infile.rdbuf();

    infile.close();

    result = ss.str();

    return 0;
}