123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- #include "comm.h"
- #include <cstdarg>
- #ifndef _WIN32
- #include <sys/ioctl.h>
- #include <sys/stat.h>
- #include <linux/hdreg.h>
- #include <sys/fcntl.h>
- #endif //NOT _WIN32
- #define MAX_PATH_SIZE 256
- void GetNewForm(const char* form, char* newForm) {
- int indexNum = 0;
- int acount = 0;
- newForm[0] = '{';
- for (int i = 0; i < strlen(form); i++)
- {
- //if((i-1 >= 0 && form[i]=='\\') || (i-1 < 0))
- if (form[i] == '%') {
- if (acount != 0)
- {
- newForm[++indexNum] = '"';
- if (acount % 2 != 0) {
- newForm[++indexNum] = ':';
- }
- else {
- newForm[++indexNum] = ',';
- }
- }
- newForm[++indexNum] = '"';
- acount++;
- }
- if (form[i] == ' ') continue;
- newForm[++indexNum] = form[i];
- }
- newForm[++indexNum] = '"';
- newForm[++indexNum] = '}';
- }
- string GetOutPutStr(const char* form, ...) {
- char* newForm = new char[strlen(form) * 3 + 5];
- memset(newForm, 0, strlen(form) * 3 + 5);
- if (strlen(form) < 2) {
- strcpy(newForm, "{\"\"}");
- }
- else {
- GetNewForm(form, newForm);
- }
- va_list vaList;
- va_start(vaList, form);
- #ifdef RVC_OS_WIN
- int acount = _vscprintf(newForm, vaList);
- #else
- int acount = vsnprintf(0, 0, newForm, vaList);
- va_end(vaList);
- va_start(vaList, form);
- #endif
- char* buf = new char[acount + 1];
- memset(buf, 0, acount + 1);
- vsprintf(buf, newForm, vaList);
- va_end(vaList);
- string ret;
- ret.assign(buf);
- delete buf;
- delete newForm;
- return ret;
- }
- int str2int(const string str, int& ret) {
- if (str.size() == 0) return 1;
- ret = 0;
- int symbol = 0;
- for (int i = 0; i < str.size(); i++) {
- if (i == 0 && str[i] == '-') {
- symbol = 1;
- continue;
- }
- if (i == 0 && str[i] == '+') continue;
- if (i == 0) {
- while (str[i] == '0' && i < str.size()) {
- ++i;
- }
- if (i == str.size()) return 0;
- }
- if (str[i] < '0' || str[i] >'9') return 2;
- ret += (str[i] - '0') * pow(10, str.size() - i - 1);
- }
- if (symbol) ret -= 2 * ret;
- return 0;
- }
- int str2int(const string str) {
- int ret;
- str2int(str, ret);
- return ret;
- }
- #ifdef __linux__
-
- void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id)
- {
- cpu_id.c_str();
- std::ifstream ifs(file_name, std::ios::binary);
- if (!ifs.is_open())
- {
- return;
- }
- char line[4096] = { 0 };
- while (!ifs.eof())
- {
- ifs.getline(line, sizeof(line));
- if (!ifs.good())
- {
- break;
- }
- const char* cpu = strstr(line, match_words);
- if (NULL == cpu)
- {
- continue;
- }
- cpu += strlen(match_words);
- while ('\0' != cpu[0])
- {
- if (' ' != cpu[0])
- {
- cpu_id.push_back(cpu[0]);
- }
- ++cpu;
- }
- if (!cpu_id.empty())
- {
- break;
- }
- }
- ifs.close();
- }
- bool get_cpu_id_by_system(std::string& cpu_id, const string save_path)
- {
- //cpu_id.c_str();
- if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
- char dmidecode_result[MAX_PATH_SIZE] = { 0 };
- strcpy(dmidecode_result, save_path.c_str());
- strcat(dmidecode_result, "/.dmidecode_result.txt");
- char command[512] = { 0 };
- snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
- if (0 == system(command))
- {
- parse_cpu_id(dmidecode_result, "ID:", cpu_id);
- }
- unlink(dmidecode_result);
- return(!cpu_id.empty());
- }
- void parse_board_serial(const char* file_name, const char* match_words, std::string& board_serial)
- {
- board_serial.c_str();
- std::ifstream ifs(file_name, std::ios::binary);
- if (!ifs.is_open())
- {
- return;
- }
- char line[4096] = { 0 };
- while (!ifs.eof())
- {
- ifs.getline(line, sizeof(line));
- if (!ifs.good())
- {
- break;
- }
- const char* board = strstr(line, match_words);
- if (NULL == board)
- {
- continue;
- }
- board += strlen(match_words);
- while ('\0' != board[0])
- {
- if (' ' != board[0])
- {
- board_serial.push_back(board[0]);
- }
- ++board;
- }
- if ("None" == board_serial)
- {
- board_serial.clear();
- continue;
- }
- if (!board_serial.empty())
- {
- break;
- }
- }
- ifs.close();
- }
- bool get_board_serial_by_system(std::string& board_serial,const string save_path)
- {
- //board_serial.c_str();
- if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
- char dmidecode_result[MAX_PATH_SIZE] = { 0 };
- strcpy(dmidecode_result, save_path.c_str());
- strcat(dmidecode_result, "/.dmidecode_result.txt");
- char command[512] = { 0 };
- snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
- if (0 == system(command))
- {
- parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
- }
- else {
- return false;
- }
- unlink(dmidecode_result);
- return true;
- }
- bool parse_disk_serial(const char* line, int line_size, const char* match_words, std::string& serial_no)
- {
- const char* serial_s = strstr(line, match_words);
- if (NULL == serial_s)
- {
- return(false);
- }
- serial_s += strlen(match_words);
- while (isspace(serial_s[0]))
- {
- ++serial_s;
- }
- const char* serial_e = line + line_size;
- const char* comma = strchr(serial_s, ',');
- if (NULL != comma)
- {
- serial_e = comma;
- }
- while (serial_e > serial_s && isspace(serial_e[-1]))
- {
- --serial_e;
- }
- if (serial_e <= serial_s)
- {
- return(false);
- }
- std::string(serial_s, serial_e).swap(serial_no);
- return(true);
- }
- void get_disk_serial(const char* file_name, const char* match_words, std::vector<string>& serial_no)
- {
- std::ifstream ifs(file_name, std::ios::binary);
- if (!ifs.is_open())
- {
- return;
- }
- char line[4096] = { 0 };
- while (!ifs.eof())
- {
- ifs.getline(line, sizeof(line));
- if (!ifs.good())
- {
- break;
- }
- if (0 == ifs.gcount())
- {
- continue;
- }
- string disk_serial;
- if (parse_disk_serial(line, ifs.gcount() - 1, match_words, disk_serial))
- {
- //break;
- serial_no.push_back(disk_serial);
- }
- }
- ifs.close();
- }
- bool isSpace(char x) { return x == ' '; }
- bool get_disk_serial_by_system(std::vector<string>& serial_no, int& errCode, const string save_path)
- {
- //if (save_path.size() + strlen("/.lshw_result.txt") > MAX_PATH_SIZE) return false;
- //char lshw_result[MAX_PATH_SIZE] = { 0 };
- //strcpy(lshw_result, save_path.c_str());
- //strcat(lshw_result, "/.lshw_result.txt");
- //char command[512] = { 0 };
- //snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
- //if (0 == system(command))
- //{
- // get_disk_serial(lshw_result, "serial:", serial_no);
- //}
- //else {
- // return false;
- //}
- errCode = 0;
- struct hd_driveid id;
- int fd = open("/dev/hda", O_RDONLY | O_NONBLOCK);
- if (fd < 0 && errno == 2) {
- fd = open("/dev/sda", O_RDONLY | O_NONBLOCK);
- if (fd < 0 && errno ==2 ) {
- fd = open("/dev/nvme0n1", O_RDONLY | O_NONBLOCK);
- if (fd < 0) {
- perror("read failed:");
- errCode = errno;
- return false;
- }
- }
- }
- if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
- string xx((const char*)(id.serial_no));
- xx.erase(remove_if(xx.begin(), xx.end(), isSpace), xx.end());
- serial_no.push_back(xx);
- }
- close(fd);
- //unlink(lshw_result);
- return(true);
- }
- #include <unistd.h>
- bool dir_is_exist(string dirPath) {
- return (access(dirPath.c_str(), F_OK) == 0);
- }
- #include <sys/stat.h>
- //创建成功返回0
- int dir_create(string dirPath) {
- if (!dir_is_exist(dirPath)) {
- return mkdir(dirPath.c_str(), S_IRWXU);
- }
- return 0;
- }
- #include <dirent.h>
- bool set_system_time_by_sec(int sec)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- tv.tv_sec += sec;
- //cout << tv.tv_sec << endl;
- if (settimeofday(&tv, NULL) < 0)
- {
- return false;
- }
- return true;
- }
- #endif // __linux__
|