Цитата:
Сообщение от Gromov
В конце концов, в том коде, что вы прикладываете в первом сообщении вообще всё плохо с объявлением класса разделяемой памяти и его использованием.
|
Код:
#include "shm.hpp"
#include <sys/vfs.h>
#include <sys/mount.h>
#include <string>
#include <stdio.h>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#define MY_FLASH_FILENAME "/media/u1/"
#define FLASH_MOUNT_DIR "/media/u1"
#define TYPE_VFAT 0x4d44
using std::string;
struct tm *mytime; // временная структура
string GetTime() {
char timebuf[10];
time_t now;
time(&now);
mytime = localtime(&now);
strftime(timebuf, 10, "%H:%M", mytime);
string logtime = string(timebuf);
return logtime;
}
string GetFilename() { //Определение имени для файла с учетом текущей даты
time_t date;
time(&date);
mytime = localtime(&date);
char buffer[20];
string data;
string ext = " log.txt";
strftime(buffer, 20, "%d.%m.%Y", mytime); // dd.mm.yy
data = MY_FLASH_FILENAME + string(buffer) + ext; // dd.mm.yy + log.txt
return data;
}
void WriteFirst(vector<string> &buffer) {
int num = 0;
ofstream out(GetFilename().c_str(), ios::app);
out << "Время;" << setw(15);
while (num < buffer.size()) {
if (num < buffer.size() - 1) {
out << buffer[num] << ";" << setw(15);
num++;
}
else {
out << buffer[num] << endl;
num++;
}
}
out.close();
}
vector<string> GetValues(vector<string> &buffer, vector<string> &types) { // Cчитывание и запись нужных sm-переменных из текст. файла в матрицу
struct statfs flash_fs;
statfs(FLASH_MOUNT_DIR, &flash_fs);
if (flash_fs.f_type != TYPE_VFAT)
exit(1);
string buf;
int num = 0;
ifstream rec("/media/u1/vals.txt");
while (!rec.eof()) {
rec >> buf;
num++;
}
buffer.resize(num);
types.resize(num);
rec.seekg(0, ios::beg); //возврат потока в начало файла
rec.clear();
for (int i = 0; i < num; i++) {
getline(rec, types[i], '.');
getline(rec, buffer[i]);
cout << buffer[i] << " " << types[i] << endl; //для отладки(удалить)
}
rec.close();
WriteFirst(buffer);
return buffer, types; //
}
void InitFBD(vector<string> &buffer, vector<string> &types, Shm mymem) {
int num;
for (num = 0; num < buffer.size(); num++) {
if (types[num] == "bool" && mymem.getType(buffer[num].c_str()) == BOOL)
continue;
else if (types[num] == "float" && mymem.getType(buffer[num].c_str()) == FLOAT)
continue;
else if (types[num] == "short" && mymem.getType(buffer[num].c_str()) == SHORT)
continue;
else if (types[num] == "long" && mymem.getType(buffer[num].c_str()) == LONG)
continue;
else exit(1);
}
if (mymem.getType("Eject") != BOOL)
exit(1);
if (mymem.getType("FreeDiskSpace") != FLOAT)
exit(1);
}
bool CompareFBD(vector<string> &buffer, vector<string> &types, vector<string> &values, Shm mymem) { //Постоянная проверка и экстримальная запись по изм.
int num;
values.resize(buffer.size());
bool change = false;
while (!change) {
for (num = 0; num < values.size(); num++)
if (types[num] == "bool" && mymem.GetBool(buffer[num].c_str()) != values[num])
return true;
}
return change;
}
vector<string> GetFBD(vector<string> &buffer, vector<string> &types, vector<string> &values, Shm mymem) {
int num;
for (num = 0; num < values.size(); num++)
if (types[num] == "bool") {
values[num] = string(mymem.GetBool(buffer[num].c_str()));
continue;
}
else if (types[num] == "float") {
values[num] = string(mymem.GetFloat(buffer[num].c_str()));
continue;
}
else if (types[num] == "int") {
values[num] = string(mymem.GetShort(buffer[num].c_str()));
continue;
}
else if (types[num] == "long")
values[num] = string(mymem.GetLong(buffer[num].c_str()));
return values;
}
void WriteVals(vector<string> &values) { //Запись самого лога в текстовый файл.
struct statfs flash_fs;
statfs(FLASH_MOUNT_DIR, &flash_fs);
if (flash_fs.f_type != TYPE_VFAT)
return;
int num = 0;
string buf;
ofstream out(GetFilename().c_str() , ios::app);
out << GetTime() << ";" << setw(15);
while (num < values.size()) {
if (num < values.size() - 1) {
out << values[num] << ";" << setw(15);
num++;
}
else {
out << values[num] << endl << setw(15);
num++;
}
}
out.close();
}
int main() {
Shm mymem("./load_files.srv");
vector<string> buffer;
vector<string> types;
vector<string> values;
GetValues(buffer, types);
InitFBD(buffer, types, mymem);
GetFBD(buffer, types, values, mymem);
struct timespec tv;
tv.tv_sec = 0;
tv.tv_nsec = 1000000;
while (1) {
time_t timeto;
timeto = time(NULL) + 600;
do {
nanosleep(&tv, NULL);
if (CompareFBD(buffer, types, values, mymem))
break;
} while (timeto > time(NULL));
// Функция сохранения значения в файл
GetFBD(buffer, types, values, mymem);
WriteVals(values);
struct statfs flash_fs;
statfs(FLASH_MOUNT_DIR, &flash_fs);
if (flash_fs.f_type == TYPE_VFAT) {
float FreeDiskSpace = (float)flash_fs.f_bfree / (float)flash_fs.f_blocks * 100.0;
mymem.setFloat("FreeDiskSpace", FreeDiskSpace);
}
else
mymem.setFloat("FreeDiskSpace", 0.0);
if (mymem.getBool("Eject")) {
umount(FLASH_MOUNT_DIR);
}
}
}
Последний вариант