204 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			204 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
#include <applicat.h>
 | 
						|
#include <dongle.h>
 | 
						|
#include <diction.h>
 | 
						|
#include <isam.h>
 | 
						|
#include <prefix.h>
 | 
						|
#include <progind.h>
 | 
						|
#include <sheet.h>
 | 
						|
#include <utility.h>
 | 
						|
 | 
						|
#include "ba1.h"
 | 
						|
 | 
						|
class TPackFiles_application:public TSkeleton_application
 | 
						|
{
 | 
						|
  TArray_sheet   * _selsheet;
 | 
						|
  long           _firm;
 | 
						|
 | 
						|
protected:
 | 
						|
  virtual void main_loop();
 | 
						|
  virtual bool create () ;
 | 
						|
  virtual bool destroy();
 | 
						|
  virtual bool extended_firm() const { return TRUE; }
 | 
						|
  void build_sheet();
 | 
						|
  void search_blanks(TSystemisamfile & f);
 | 
						|
public:
 | 
						|
  TPackFiles_application() : _selsheet(NULL), _firm(0) {}
 | 
						|
  ~TPackFiles_application() {};
 | 
						|
};
 | 
						|
 | 
						|
bool TPackFiles_application::create() // initvar e arrmask
 | 
						|
 | 
						|
{
 | 
						|
  _firm = get_firm();
 | 
						|
  if (!set_firm())
 | 
						|
    return FALSE;
 | 
						|
  const bool is_prassi = user() == ::dongle().administrator();
 | 
						|
  _selsheet  = new TArray_sheet(-1, -1, -4, -4, TR("Selezione files"),
 | 
						|
                             is_prassi ? 
 | 
						|
                             HR("@1|N.@5|Nome@20|EOD@7|EOX@7|Lung.  |Descrizione@43|Flags@7") :
 | 
						|
                             HR("N.@5|Nome@20|EOD@7|EOX@7|Lung.  |Descrizione@43|Flags@7"),
 | 
						|
                             is_prassi ? 0 : 0x10);
 | 
						|
  return TSkeleton_application::create();
 | 
						|
}
 | 
						|
 | 
						|
bool TPackFiles_application::destroy()
 | 
						|
 | 
						|
{
 | 
						|
  if (_firm) set_firm(_firm);
 | 
						|
  if (_selsheet != NULL) delete  _selsheet;
 | 
						|
  return TSkeleton_application::destroy() ;
 | 
						|
}
 | 
						|
 | 
						|
void TPackFiles_application::build_sheet()
 | 
						|
{
 | 
						|
  _selsheet->destroy();
 | 
						|
  TDir cdir;
 | 
						|
  cdir.get(LF_DIR,_nolock,_nordir,_sysdirop);
 | 
						|
  const bool is_prassi = user() == ::dongle().administrator();
 | 
						|
  const int nitems = (int)cdir.eod();
 | 
						|
  for (int i = 0; i < nitems; i++) // fill sheet
 | 
						|
  {
 | 
						|
    TToken_string riga(128);
 | 
						|
    cdir.get(i+1,_nolock,_nordir,_sysdirop);
 | 
						|
    if (is_prassi)
 | 
						|
      riga.add("");
 | 
						|
    riga.add(i+1);
 | 
						|
    riga.add(cdir.name());
 | 
						|
    riga.add(cdir.eod());
 | 
						|
    riga.add(cdir.eox());
 | 
						|
    riga.add((int)cdir.len());
 | 
						|
    riga.add(cdir.des());
 | 
						|
    riga.add(cdir.flags());
 | 
						|
    _selsheet->add(riga);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
void TPackFiles_application::search_blanks(TSystemisamfile& f)
 | 
						|
{
 | 
						|
  // Scorre il file corrente per record cancellando eventuali record vuoti/blank
 | 
						|
                
 | 
						|
  f.open(_excllock, TRUE);
 | 
						|
  const long records = f.items();
 | 
						|
  TString k;
 | 
						|
  
 | 
						|
  for (long i = 0; f.status() == NOERR && i < records; i++)
 | 
						|
  {
 | 
						|
    f.readat(i + 1); // Legge per numero di record, evitando falli di indice...
 | 
						|
    k = f.curr().key(); // Chiave 1; non puo' essere vuota (anche perche' no si puo' scrivere un record vuoto)
 | 
						|
    if (f.curr().empty() || k.trim().empty()) // Teoricamente ce ne sarebbe uno solo...
 | 
						|
    {                                         // ma se l'indice e' rovinato possono esserci piu' records vuoti
 | 
						|
      f.curr().discard();                     // che vanno cmq eliminati; le pack() successive completano l'opera.
 | 
						|
      f.rewriteat(i + 1);
 | 
						|
    }
 | 
						|
  }
 | 
						|
  f.close();
 | 
						|
}
 | 
						|
                
 | 
						|
void TPackFiles_application::main_loop()
 | 
						|
{
 | 
						|
  KEY tasto = K_ENTER;
 | 
						|
  while (tasto != K_ESC)
 | 
						|
  {
 | 
						|
    build_sheet();
 | 
						|
    tasto = _selsheet->run();
 | 
						|
    switch(tasto)
 | 
						|
    {
 | 
						|
      case K_ENTER:  
 | 
						|
        if (_selsheet->checked()>0)
 | 
						|
        {
 | 
						|
          int status;
 | 
						|
          const long items = _selsheet->items();
 | 
						|
          bool retry,present;
 | 
						|
          TFilename f_name,d_name;
 | 
						|
          TString cmd;
 | 
						|
          TDir d;
 | 
						|
          TDir ds;
 | 
						|
          TProgind p(items, TR("Compattazione in corso..."), TRUE, TRUE );
 | 
						|
          for (long i = 2; i<=items; i++) // Skip LF_DIR
 | 
						|
          { 
 | 
						|
            p.setstatus(i);
 | 
						|
            if (p.iscancelled())
 | 
						|
              break;
 | 
						|
            if (!_selsheet->checked(i-1))
 | 
						|
              continue;
 | 
						|
            d.get(i);     
 | 
						|
            if (d.len() == 0)
 | 
						|
              continue;    // Skip files with flags over 10000
 | 
						|
            const TString pref(prefix().name());
 | 
						|
            prefix().set("");
 | 
						|
            ds.get(i);
 | 
						|
            prefix().set(pref);
 | 
						|
            const long flags = ds.flags();
 | 
						|
//            const int module = abs((int)flags); non piu usato verificare
 | 
						|
            if (flags >= 10000L)
 | 
						|
              continue;    // Skip files with flags over 10000
 | 
						|
            TSystemisamfile f(i);
 | 
						|
            
 | 
						|
            f_name = d.name();
 | 
						|
            f_name.ext("dbf");
 | 
						|
            retry   = FALSE;
 | 
						|
            present = f_name.exist();
 | 
						|
            do
 | 
						|
            {
 | 
						|
              status = NOERR;
 | 
						|
              if (present && flags < 10000L) // Se il file c'e' prova a compattarlo
 | 
						|
              { 
 | 
						|
                search_blanks(f);
 | 
						|
                if (f.packfile() == NOERR)
 | 
						|
                  f.packindex();
 | 
						|
                status = f.status();
 | 
						|
                if (status == -60) // Se ritorna -60 allora il file e' aperto da qualcuno
 | 
						|
                  retry = yesno_box(FR("Il file %s non puo' essere compattato perche' aperto da altre applicazioni. Riprovare?"),(const char*) f_name);
 | 
						|
              } 
 | 
						|
            } while  (retry); // Ci riprovo !
 | 
						|
            
 | 
						|
            if (!present || status == -60)
 | 
						|
            {
 | 
						|
//              if (!present && d.len() > 0 && has_module(module,CHK_DONGLE))// Se il file non esiste lo costruisce
 | 
						|
//                f.build(0L);
 | 
						|
              
 | 
						|
              continue; // Se il file non esiste e non si ha il modulo abilitato evita inutili controlli
 | 
						|
            }           // Se le pack() hanno ritornato -60 ma il file esiste, allora prosegue con il prossimo
 | 
						|
            if (status != NOERR && 
 | 
						|
              yesno_box(FR("Rilevato l'errore %d cercando di compattare il file %s. Si desidera scaricarlo e ricaricarlo?"),status,(const char*)f_name))
 | 
						|
            {
 | 
						|
              // Dump the file, Zap it and Reload it.
 | 
						|
              d_name = "";
 | 
						|
              d_name.temp("fdump");
 | 
						|
              if (f.dump(d_name,0) == NOERR) // Dump
 | 
						|
              {
 | 
						|
                // Zap
 | 
						|
                d.get(i,_nolock, _nordir,_sysdirop);               
 | 
						|
                bool is_com = d.is_com();
 | 
						|
                d.get(i,_nolock, is_com ? _comdir : _nordir);
 | 
						|
                d.eod() = 0L;
 | 
						|
                d.put(i, is_com ? _comdir : _nordir);
 | 
						|
                if (f.pack() == NOERR)
 | 
						|
                  if (f.load(d_name) == NOERR) // Reload
 | 
						|
                    remove_file(d_name);
 | 
						|
                  else
 | 
						|
                    error_box(FR("Impossibile ricaricare %s. Errore %d"),(const char*) d_name, f.status());
 | 
						|
                else
 | 
						|
                  error_box(FR("Impossibile azzerare %s. Errore %d"),(const char*) f_name, f.status());
 | 
						|
              }
 | 
						|
                else
 | 
						|
                  error_box(FR("Impossibile scaricare %s. Errore %d"),(const char*) d_name, f.status());
 | 
						|
            }
 | 
						|
          }
 | 
						|
        }
 | 
						|
        else
 | 
						|
          warning_box(TR("Nessun file selezionato"));
 | 
						|
        break;
 | 
						|
      default:
 | 
						|
        break;
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
int ba1300(int argc, char** argv)
 | 
						|
{
 | 
						|
  TPackFiles_application a;
 | 
						|
  a.run(argc,argv,TR("Compatta files"));
 | 
						|
  return 0;
 | 
						|
}
 |