//===RESTITUISCE LE LINEE MARCATE ;1 NEL FILE===//
void getMarked(int line_num, int value) {
ifstream inputFile;
inputFile.open(PATH);
ofstream outputFile;
outputFile.open("file/output_file.txt");
string linea;
string parola;
vector word;
int n = 1;
if ( outputFile.is_open() ) {
while(getline(inputFile, linea))
{
if (n != line_num)
{
outputFile << linea << endl;
}
else
{
stringstream str(linea);
while (getline(str, parola, ';'))
{
word.push_back(parola);
}
if (value == 1)
{
outputFile << word[0] << ";1" << endl;
}
else
{
outputFile << word[0] << ";0" << endl;
}
}
word.clear();
n ++;
}
}
else
{
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("CAN NOT OPEN FILE"), wxT("ERROR"), wxICON_ERROR);
dial -> ShowModal();
}
inputFile.close();
outputFile.close();
remove("file/file.csv");
rename("file/output_file.txt", "file/file.csv");
}
//metodo che va a leggere il file di testo con i task
//===LEGGE IL FILE E AGGIUNGE ALLA TABELLA LE RIGHE===//
void leggiFile() {
if (grid -> GetNumberRows() > 1) {
grid -> DeleteRows(0, grid->GetNumberRows()-1, true);
}
fstream file;
file.open(PATH, ios::in); //input filestream
string linea = "";
string parola;
vector word;
int conta_elementi = 1;
if ( file.is_open() ) {
while(getline(file, linea)) {
//parsing
stringstream str(linea);
while (getline(str, parola, ';')) {
word.push_back(parola);
}
//Controllo se il file è vuoto
if (len() == 1)
{
grid -> AppendRows(1);
grid -> SetCellValue(0, 0, word[0]);
grid -> AppendRows(1);
grid -> ForceRefresh();
}
else
{
grid -> SetCellValue(conta_elementi -1, 0, word[0]);
grid -> AppendRows(1);
grid -> ForceRefresh();
}
conta_elementi ++;
if (conta_elementi%2 == 0)
{
grid -> SetCellBackgroundColour(conta_elementi-2, 0, wxColor(69, 69, 69));
grid -> SetCellTextColour(conta_elementi-2, 0, wxColor(217, 217, 217));
}
else
{
grid -> SetCellBackgroundColour(conta_elementi-2, 0, wxColor(97, 97, 97));
grid -> SetCellTextColour(conta_elementi-2, 0, wxColor(217, 217, 217));
}
if (word[1] == "1")
{
grid -> SetCellBackgroundColour(conta_elementi-2, 0, wxColor(70, 189, 92));
grid -> SetCellTextColour(conta_elementi-2, 0, wxColor(0, 0, 0));
}
word.clear();
}
} else {
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("CAN NOT OPEN FILE"), wxT("ERROR"), wxICON_ERROR);
dial -> ShowModal();
}
grid->EnableEditing(false);
grid -> DeleteRows(conta_elementi-1,1);
grid -> ForceRefresh();
//transfertToCSV();
}
//===AGGIUNGE UNA LINEA NEL FILE===//
void aggiungi( string arg ) {
ofstream file;
file.open(PATH, ios::app);
if ( file.is_open() ) {
file << arg;
file << ";0\n";
} else {
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("CAN NOT OPEN FILE"), wxT("ERROR"), wxICON_ERROR);
dial -> ShowModal();
}
file.close();
}
//metodo che cerca una linea del file di testo e restituisce -1 se non è presente e il numero di linea se è presente
int cerca( string arg ) {
ifstream file;
file.open(PATH, ios::in);
int num_linea = 1;
string linea = "";
string arg1, arg2;
arg1 = arg+";1";
arg2 = arg+";0";
if ( file.is_open() ) {
while ( file.good() ) {
getline(file, linea);
if ( linea != arg2 && linea != arg1) {
num_linea ++;
} else {
file.close();
return num_linea;
}
}
} else {
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("CAN NOT OPEN FILE"), wxT("ERROR"), wxICON_ERROR);
dial -> ShowModal();
}
file.close();
return -1;
}
//metodo che va a eliminare il file di testo e lo riscrive senza la linea ricavata dalla funzione cerca( string arg )
void elimina( int n_linea ) {
ifstream inputFile;
inputFile.open(PATH);
ofstream outputFile;
outputFile.open("file/output_file.txt");
string linea;
int n = 1;
if ( outputFile.is_open() ) {
while(getline(inputFile, linea)) {
if (n != n_linea) {
outputFile << linea << endl;
}
n ++;
}
} else {
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("CAN NOT OPEN FILE"), wxT("ERROR"), wxICON_ERROR);
dial -> ShowModal();
}
inputFile.close();
outputFile.close();
remove("file/file.csv");
rename("file/output_file.txt", "file/file.csv");
}
//metodo che restituisce il numero di linee del file
int len() {
int len = 0;
string line = "";
ifstream file;
file.open(PATH, ios::in);
if ( file.is_open() ) {
while ( file.good() ) {
getline(file, line);
len ++;
}
} else {
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("CAN NOT OPEN FILE"), wxT("ERROR"), wxICON_ERROR);
dial -> ShowModal();
}
file.close();
return len - 1;
}
//metodo converte il file da .txt a .csv {è contenuto nella libreria esterna TextToCsv.h}
void transfertToCSV()
{
vector vettore;
string linea;
ifstream file_da_leggere;
ofstream file_da_scrivere;
file_da_leggere.open(PATH, ios::in);
if ( file_da_leggere.is_open() ) {
while(getline(file_da_leggere, linea)) {
vettore.push_back(linea);
}
}
else {
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("CAN NOT OPEN FILE"), wxT("ERROR"), wxICON_ERROR);
dial->ShowModal();
}
int dim = vettore.size();
file_da_scrivere.open("file.csv", ios::out);
for (int i = 0; i < dim ; i ++)
{
if (i != dim)
{
file_da_scrivere << vettore[i] << "," << endl;
}
else
{
file_da_scrivere << vettore[i] << endl;
}
}
}
//metodo per tenere traccia dei movimenti tramite la scrittura di un file di log {è contenuto nella libreria esterna log_lib.hpp}
/*
* LIBRERIA DI LOG
* DESC: IN BASE AL PARAMETRO PASSATO LA FUNZIONE PERMETTE DI TENERE TRACCIA DEI CAMBIAMENTI DEL FILE CSV
* LA PATH DEL FILE DI LOG: LOG/LOG_FILE.LOG
* [DATA - ORA] CAMBIAMENTO
**/
static std::string log_path = "log/log_file.log";
void log_write(int arg)
{
std::ofstream write(log_path, std::ios::app);
std::time_t now = time(0);
std::tm *ltm = localtime(&now);
std::string line = "[" + std::to_string(ltm -> tm_mday) + "/" + std::to_string(1 + ltm -> tm_mon) + "/" + std::to_string(1900 + ltm -> tm_year) + "-" + std::to_string(ltm -> tm_hour) + ":" + std::to_string(ltm -> tm_min) + ":" + std::to_string(ltm -> tm_sec) + "]";
switch(arg)
{
case 1:
line += " added";
break;
case 2:
line += " removed";
break;
case 3:
line += " marked";
break;
case 4:
line += " unmarked";
break;
default:
break;
}
write << line << std::endl;
write.close();
}