#ifndef _fileman_cpp #define _fileman_cpp //CONTROLLATO PER MEMORY LEAK - 21 NOV 2001 - MP #include "fileman.h" Fileman::Fileman() { buffer = NULL; path = NULL; opened = 0; exists = 0; //esiste? current = 0; //posizione corrente (linee) byte = 0; //posizione corrente (byte) } Fileman::Fileman(char *what) { //INIZIALIZZAZIONE VARIABILI path = new char[strlen(what)+1]; if(MAXBUFFERLENGTH<=0) MAXBUFFERLENGTH = 1000; buffer = new char[MAXBUFFERLENGTH]; strcpy(path, what); opened=0; current=0; byte=0; //CONTROLLO ESISTENZA FILE f.open(path, ios::in); if(!f) exists=0; else { exists=1; f.close(); } } Fileman::~Fileman() { delete [] path; delete [] buffer; if(opened) Close(); } void Fileman::Open() { if(exists) f.open(path, ios::in, ios::out); else { return; } if(!f) { opened = 0; } else opened = 1; } void Fileman::Close() { if(opened) { f.close(); opened=0; } } int Fileman::ReadLine(int where) { char c[2]; if(!exists) return 0; Open(); if(!opened) return 0; //POSIZIONAMENTO ALLA LINEA DESIDERATA if(where >= 0) { byte=0; for(int i=0; i < where; i++) { f.get(buffer, MAXBUFFERLENGTH); //prende righe a go-go f.get(c, 1); byte += strlen(buffer)+1; //incrementa il puntatore a byte if(f.eof()) break; } current = where; } //SE NON E' SPECIFICATA UNA LINEA SI USA LA LINEA CORRENTE else { f.seekg(byte); } if(!f.eof()) { //QUESTO E' IL FULCRO DELLA QUESTIONE ;) f.get(buffer, MAXBUFFERLENGTH); f.get(c, 1); // <---qui va a capo riga (DING) if(!strlen(buffer)) { if(f.eof()) { current = 0; byte = 0; Close(); return 0; } } byte += strlen(buffer)+1; current++; //anche questo e' importante! setta la riga corrente (nel file) Close(); return 1; } else { current = 0; Close(); return 0; } } //QUESTO SERVE PER AVANZARE NEL FILE FINO ALLA LINEA IN CUI SI TROVA what //OVVIAMENTE SE NON SI TROVA [!ReadLine()] //LA FUNZIONE RITORNA "FALSE" [ossia zero :] int Fileman::WaitFor(char *what) { do { if(!ReadLine()) return 0; }while(strcmp(what, buffer)); return 1; } int Fileman::Write(char* what, int backstep, int begin) { char c; //SE NON SI SPECIFICA DOVE SCRIVERE LUI AGGIUNGE IN FONDO AL FILE if(!begin) { f.open(path, ios::app); if(!f) { opened=0; } else { opened=1; } } else //ALTRIMENTI SOVRASCRIVE/CREA { f.open(path, ios::out); if(!f) { opened=0; } else { opened=1; } } if(!opened) return 0; //IN SCRITTURA NON MI INTERESSA LA POSIZIONE CORRENTE //ANCHE PERCHE what PUO CONTENERE PIU DI UNA LINEA //e poi fa tutto automaticamente quando gli faccio fare un append ;p f << what; if(backstep) f << endl; Close(); return 1; } #endif