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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <ctime>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <windows.h>
using namespace std;
string mystr; // stringa temporanea da usare a piacimento
fstream logfile; // istanzia una classe fstream con identificatore logfile
//filebuf *lfbuf = logfile.rdbuf(); // puntatore con l'oggetto filebuf associato con lo stream di logfile
time_t rawtime;
struct tm * timeinfo;
struct info_base {
string materia,
testo,
autori,
editore,
capitolo,
paginestudiate,
paginetralasciate;
int limiteduratasessioni, // durata di una singola sessione (in millisecondi)
pausatrasessioni; // pausa tra una sessione e l'altra
} a;
void outputest (); // eliminare a completamento programma
void input (); // ottiene i primi dati dall'utente
void sessioni (); // gestisce lo studio scandito in sessioni flessibili
bool fileExists (string); // controlla che il file esista
bool checkheader (const char []); // controlla se è già presente un header per un dato giorno
void writeheader (bool); // scrive l'header nel file di log
int main () {
input(); // ottiene i primi dati dall'utente
// test
outputest();
sessioni();
// fine del programma
cout << "\n\nEND OF PROGRAM\n" << endl;
system("pause");
}
void input () { // ottiene i primi dati dall'utente
cout << "Materia (es. Citologia): ";
getline(cin, a.materia);
cout << "Testo (es. ISTOLOGIA): ";
getline(cin, a.testo);
cout << "Autori (es. P. Rosati, C. Colombo, N. Maraldi): ";
getline(cin, a.autori);
cout << "Editore (es. edi-ermes): ";
getline(cin, a.editore);
cout << "Capitolo (es. 01): ";
getline(cin, a.capitolo);
do {
cout << "Durata sessione in minuti (es. 40): ";
getline(cin, mystr);
stringstream(mystr) >> a.limiteduratasessioni;
} while (a.limiteduratasessioni < 0); // verifica che il valore inserito sia positivo
a.pausatrasessioni = (a.limiteduratasessioni / 10);
while (a.pausatrasessioni % 5 != 0) // fa sì che le pause non siano inferiori ai 5 minuti (es. 3 minuti diventano 5)
a.pausatrasessioni += 1;
a.limiteduratasessioni *= 60000; // trasformazione minuti => millisecondi
a.pausatrasessioni *= 60000; // trasformazione minuti => millisecondi
}
void sessioni () { // manages the studying sessions
time_t start, end;
double diff; // time elapsed
char buffer[100]; // date and time buffer
bool headerpresente; // true if logfile.txt has a matching header, false otherwise
int n = 1;
cout << "\nLet's get started with session n." << (n < 10 ? "0" : "") << n
<< "!\nPress ENTER to terminate current session: ";
time(&start); // beginning of timed process (I'll use this to know how long I've studied each session
{ // beggining of main writing operations on the logfile.txt
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 100, "%a %d %b %Y", timeinfo);
headerpresente = checkheader(buffer); // controlla se in logfile.txt è già presente una sezione riportante la data odierna
if (headerpresente == true) {
cout << "Found match for header " << buffer << endl;
if (logfile.is_open()) {
cout << "file is open";
logfile.seekg(0, ios::end);
logfile << "\n\n\n" << a.materia << '\n'
<< "\n Title: " << a.testo // these are all string type elements of structure a
<< "\n Authors: " << a.autori
<< "\n Editor: " << a.editore << "\n\n"
<< "\n Chapter: " << a.capitolo;
}
}
else {
cout << "Matching header not found\n";
if (logfile.is_open()) {
cout << "file is open";
logfile.seekg(0, ios::end);
logfile << "__________________\n"
<< " /\n" << buffer << " /\n"
<< "_______________/\n" << endl;
}
}
logfile.close();
cin.ignore(); // ENTER per terminare
} // beggining of main writing operations on the logfile.txt
time(&end); // end of timed process
diff = difftime(end, start); // returns time elapsed between time(&start) and time(&end)
cout << "\nmain writing operations on for " << diff << " seconds\n\n";
}
bool fileExists (string filename) {
logfile.open(filename.c_str(), ios::in);
if (logfile.is_open() == true) {
logfile.close();
return true;
}
else {
logfile.close();
return false;
}
}
bool checkheader(const char intestazione []) {
existancecheck:
if (fileExists("logfile.txt") == true) { // check that logfile.txt has already been created
cout << "\nlogfile.txt presente sul disco\n";
logfile.open("logfile.txt", ios::in | ios::out);
if (logfile.is_open()) {
cout << "\nLooking for the following header: " << intestazione << '\n' << endl;
cout << logfile.tellg() << endl;
char c;
char linea [100];
fill_n(linea, 100, '\0');
int i = 0;
while (logfile.good()) {
while (logfile.get(c)) {
linea[i] = c;
++i;
if (c == '\n') {
i = 0;
break;
}
if (strncmp (linea, intestazione, 100 ) == 0) {
return true;
}
}
fill_n(linea, 100, '\0');
}
return false;
}
else {
cout << "\nImpossible to open logfile.txt\n";
}
}
else {
cout << "\nlogfile.txt non esiste...ora lo creo io!\n";
logfile.open("logfile.txt", ios::out); // creates logfile.txt if it doesn't exist
goto existancecheck; // go back to checking if the file now exists
}
}
void writeheader (bool headerpresente) {
if (headerpresente == true)
cout << "\nl'header c'è!\n" << flush;
else
cout << "\nnessun header!\n" << flush;
}
void outputest () { // eliminare a completamento programma
cout << '\n' << a.materia << ", " << a.testo << ", " << a.autori << ", "
<< a.editore << ", " << a.capitolo << ", "
<< a.limiteduratasessioni / 60000 << ", " << a.pausatrasessioni / 60000
<< ".\n" << endl;
}
|