File: helper.h

package info (click to toggle)
intel-compute-runtime 20.44.18297-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,780 kB
  • sloc: cpp: 379,729; lisp: 4,931; python: 299; sh: 196; makefile: 8
file content (68 lines) | stat: -rw-r--r-- 1,821 bytes parent folder | download
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
/*
 * Copyright (C) 2018-2020 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once
#include "shared/source/os_interface/os_library.h"

#include "igfxfmid.h"

#include <exception>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

void addSlash(std::string &path);

std::vector<char> readBinaryFile(const std::string &fileName);

void readFileToVectorOfStrings(std::vector<std::string> &lines, const std::string &fileName, bool replaceTabs = false);

size_t findPos(const std::vector<std::string> &lines, const std::string &whatToFind);

PRODUCT_FAMILY getProductFamilyFromDeviceName(const std::string &deviceName);

class MessagePrinter {
  public:
    MessagePrinter() = default;
    MessagePrinter(bool suppressMessages) : suppressMessages(suppressMessages) {}

    void printf(const char *message) {
        if (!suppressMessages) {
            ::printf("%s", message);
        }
        ss << std::string(message);
    }

    template <typename... Args>
    void printf(const char *format, Args... args) {
        if (!suppressMessages) {
            ::printf(format, std::forward<Args>(args)...);
        }
        ss << stringFormat(format, std::forward<Args>(args)...);
    }

    const std::stringstream &getLog() {
        return ss;
    }

  private:
    template <typename... Args>
    std::string stringFormat(const std::string &format, Args... args) {
        std::string outputString;
        size_t size = static_cast<size_t>(snprintf(nullptr, 0, format.c_str(), args...) + 1);
        if (size <= 0) {
            return outputString;
        }
        outputString.resize(size);
        snprintf(&*outputString.begin(), size, format.c_str(), args...);
        return outputString.c_str();
    }

    std::stringstream ss;
    bool suppressMessages = false;
};