Files correlati : Ricompilazione Demo : [ ] Commento : Riportata la versione P@rtners 2.0 patch 349 git-svn-id: svn://10.65.10.50/trunk@10709 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			499 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			499 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
#include <direct.h>
 | 
						|
 | 
						|
#include <os_dep.h>
 | 
						|
#include <strings.h>
 | 
						|
 | 
						|
#define __UTILITY_CPP
 | 
						|
#include <utility.h>
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Permette di copiare un file
 | 
						|
//
 | 
						|
// @rdesc Ritorna il risultato dell'operazione:
 | 
						|
//
 | 
						|
// @flag TRUE | Se l'operazione e' stata effettuata con successo
 | 
						|
// @flag FALSE | Se l'operazione non e' stata effettuata con successo
 | 
						|
bool fcopy(
 | 
						|
  const char* orig,  // @parm Nome del file di origine
 | 
						|
  const char* dest,  // @parm Nome del file di destinazione
 | 
						|
  bool append)       // @parm Controllo per aggiungere il contenuto del
 | 
						|
  //       file <p dest> in coda al file <p orig> (default FALSE)
 | 
						|
 | 
						|
  // @comm Nel caso vengano ravvisati degli errori durante l'operazione vengono
 | 
						|
  //       creati dei box di comunicazione che indicano la causa del problema
 | 
						|
{                               
 | 
						|
  CHECK(orig && *orig && dest && *dest, "fcopy: Invalid file name");
 | 
						|
 | 
						|
  const char* const rflag = "rb";
 | 
						|
  const char* wflag = append ? "ab" : "wb";
 | 
						|
  
 | 
						|
  // Copia il file su se stesso?
 | 
						|
  if (stricmp(orig, dest) == 0)
 | 
						|
    return TRUE;  // Or FALSE?
 | 
						|
 | 
						|
  FILE* i = fopen(orig, rflag);
 | 
						|
  if (i == NULL) 
 | 
						|
    return error_box("Impossibile leggere il file %s\nda copiare in ", orig, dest);
 | 
						|
 | 
						|
  FILE* o = fopen(dest, wflag);
 | 
						|
  if (o == NULL) 
 | 
						|
  {
 | 
						|
    fclose(i);
 | 
						|
    return error_box("Impossibile creare il file %s\nper copiare il file %s", dest, orig);
 | 
						|
  }  
 | 
						|
 | 
						|
  const word size = 16*1024;
 | 
						|
  TString buffer(size);
 | 
						|
  
 | 
						|
  bool ok = TRUE;          
 | 
						|
  while (ok)
 | 
						|
  {
 | 
						|
    const word letti = fread(buffer.get_buffer(), 1, size, i);
 | 
						|
    ok = fwrite(buffer.get_buffer(), 1, letti, o) == letti;
 | 
						|
    if (letti < size) break;
 | 
						|
  }    
 | 
						|
  
 | 
						|
  if (!ok) 
 | 
						|
    error_box("Errore di scrittura: controllare lo spazio libero sul disco!");
 | 
						|
 | 
						|
  fclose(o);
 | 
						|
  fclose(i);
 | 
						|
 | 
						|
  return ok;
 | 
						|
}
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Controlla l'esistenza di un file
 | 
						|
//
 | 
						|
// @rdesc Ritrona i seguenti valori:
 | 
						|
//
 | 
						|
// @flag TRUE | Se il file esiste
 | 
						|
// @flag FALSE | Se il file non esiste
 | 
						|
bool fexist(
 | 
						|
  const char* file)  // @parm Nome del file di cui contrallare l'esistenza
 | 
						|
{ 
 | 
						|
  int err = access(file, 0x00);
 | 
						|
  if (err) // Se la access fallisce provo con una funzione dipendente dal sistema
 | 
						|
  {                
 | 
						|
    if (os_file_exist(file))
 | 
						|
      err = 0;
 | 
						|
  }
 | 
						|
  return err == 0;
 | 
						|
}    
 | 
						|
 | 
						|
long fsize(const char* name)
 | 
						|
{                           
 | 
						|
  long s = 0;
 | 
						|
  if (name && *name)
 | 
						|
  {
 | 
						|
    FILE* f = fopen(name, "r");
 | 
						|
    if (f != NULL)
 | 
						|
    {
 | 
						|
      fseek(f, 0, SEEK_END);
 | 
						|
      s = ftell(f);
 | 
						|
      fclose(f);
 | 
						|
    }
 | 
						|
  }    
 | 
						|
  return s;
 | 
						|
} 
 | 
						|
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Permette di creare una directory
 | 
						|
//
 | 
						|
// @rdesc Ritorna il risultato dell'operazione
 | 
						|
//
 | 
						|
// @flag TRUE | Se l'operazione e' avvenuta con successo
 | 
						|
// @flag FALSE | Se l'operazione non e' riuscita
 | 
						|
bool make_dir(
 | 
						|
  const char* dir) // @parm Nome della directory da creare
 | 
						|
{
 | 
						|
  int res = mkdir(dir);
 | 
						|
  return res == 0;
 | 
						|
}
 | 
						|
 | 
						|
#ifndef FOXPRO
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Ritorna la lista dei file il cui nome corrisponde alla stringa (con caratteri
 | 
						|
//   jolly) passata.
 | 
						|
//
 | 
						|
// @rdesc Ritorna il numero di file che soddisfano la condizione passata (numero di elementi
 | 
						|
//                              di result)
 | 
						|
int list_files(
 | 
						|
  const char* filelist,      // @parm Stringa contenente la maschera di estrazione 
 | 
						|
  TString_array& result)     // @parm Array da riempire con la lista dei file
 | 
						|
  
 | 
						|
{             
 | 
						|
  TFilename dir(filelist);
 | 
						|
  for (int i = dir.len()-1; i >= 0; i--)
 | 
						|
    if (dir[i] == '/' || dir[i] == '\\' || dir[i] == ':') break;
 | 
						|
  
 | 
						|
  TFilename mask(dir.mid(i+1));
 | 
						|
  dir.cut(i > 0 ? i+1 : 0);   
 | 
						|
  
 | 
						|
  xvt_fsys_save_dir();                  
 | 
						|
  
 | 
						|
  if (dir.not_empty())                                             
 | 
						|
  {
 | 
						|
    DIRECTORY directory; xvt_fsys_convert_str_to_dir(dir.get_buffer(), &directory);
 | 
						|
    BOOLEAN ok = xvt_fsys_set_dir(&directory);
 | 
						|
    if (!ok) 
 | 
						|
    {
 | 
						|
      error_box("Impossibile entrare in %s", (const char*)dir);
 | 
						|
      return 0;
 | 
						|
    }  
 | 
						|
  }  
 | 
						|
 | 
						|
  SLIST files = xvt_fsys_list_files("", mask.get_buffer(), FALSE);
 | 
						|
  const int count = xvt_slist_count(files);
 | 
						|
  
 | 
						|
  for (SLIST_ELT e = xvt_slist_get_first(files); e; e = xvt_slist_get_next(files, e))
 | 
						|
  { 
 | 
						|
    char* f = xvt_slist_get(files, e, NULL);
 | 
						|
    if (dir.not_empty()) 
 | 
						|
    {
 | 
						|
      mask = dir; 
 | 
						|
      mask.add(f);
 | 
						|
      result.add(mask);
 | 
						|
    }
 | 
						|
    else 
 | 
						|
      result.add(f);  
 | 
						|
  }
 | 
						|
  
 | 
						|
  xvt_slist_destroy(files);
 | 
						|
  xvt_fsys_restore_dir();
 | 
						|
  
 | 
						|
  return count;
 | 
						|
}
 | 
						|
 | 
						|
bool input_filename(TFilename& file)
 | 
						|
{
 | 
						|
  DIRECTORY dir; 
 | 
						|
  FILE_SPEC fs;
 | 
						|
 | 
						|
  xvt_fsys_get_dir(&dir);
 | 
						|
  xvt_fsys_get_dir(&fs.dir);
 | 
						|
  strcpy(fs.type, file.ext());
 | 
						|
  strcpy(fs.name, file);
 | 
						|
  strcpy(fs.creator, "AGA");
 | 
						|
    
 | 
						|
  const bool good = xvt_dm_post_file_open(&fs, "Selezionare il file ...") == FL_OK;
 | 
						|
  xvt_fsys_set_dir(&dir);
 | 
						|
                 
 | 
						|
  if (good)                 
 | 
						|
    file = fs.name;
 | 
						|
  return good;  
 | 
						|
}  
 | 
						|
 | 
						|
#endif
 | 
						|
 | 
						|
// Certified 99%
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Permette di ritornare una stringa formattata
 | 
						|
//
 | 
						|
// @rdesc Ritorna la stringa desiderata
 | 
						|
char* format(
 | 
						|
  const char* fmt, // @parm Formato che deve essere dato alla stringa
 | 
						|
  ...)                     // @parmvar Uno o piu' parametri corrispondenti ai codici in <p fmt>
 | 
						|
 | 
						|
  // @comm Il funzionamento e' come la <f sprintf> del C, solo che non e' necessario passare la
 | 
						|
  //                     stringa di destinazione alla funzione.
 | 
						|
{
 | 
						|
  va_list pars;
 | 
						|
 | 
						|
  va_start(pars, fmt);
 | 
						|
  const int tot = vsprintf(__tmp_string, fmt, pars);
 | 
						|
  va_end(pars);
 | 
						|
 | 
						|
  CHECK(tot >= 0 && tot < sizeof(__tmp_string)-1, "Ue'! Ma quanto scrivi?");
 | 
						|
  return(__tmp_string);
 | 
						|
}
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Converte la coppia nome-parametro in una stringa che identifica il programma
 | 
						|
//
 | 
						|
// @rdesc Ritorna la stringa identificante il programma
 | 
						|
const char* cmd2name(
 | 
						|
  const char* argv0,  // @parm Nome del programma
 | 
						|
  const char* argv1)  // @parm Nome del parametro (default "")
 | 
						|
{
 | 
						|
  TFilename app(argv0);
 | 
						|
  app = app.name(); 
 | 
						|
  if (argv1 && *argv1) 
 | 
						|
    app << ' ' << argv1;
 | 
						|
  else
 | 
						|
  {
 | 
						|
    const char* space = strchr(argv0, ' ');
 | 
						|
    if (space != NULL)
 | 
						|
      app << space;
 | 
						|
  }
 | 
						|
  
 | 
						|
  app.lower();
 | 
						|
 | 
						|
  const int par = app.find(" -");
 | 
						|
  const int num = par > 0 ? atoi(app.mid(par+2))+1 : 1; 
 | 
						|
 | 
						|
  const char c = (num > 9) ? ('a'+num-10) : ('0'+num);
 | 
						|
  app.cut(3);
 | 
						|
  app << c << "00";
 | 
						|
 | 
						|
  return strcpy(__tmp_string, app);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
///////////////////////////////////////////////////////////
 | 
						|
// Conversione in cifre romane
 | 
						|
///////////////////////////////////////////////////////////
 | 
						|
 | 
						|
HIDDEN const char * cifre_romane = "IVXLCDM@";
 | 
						|
HIDDEN const int valori_cifre [] = { 1, 5, 10, 50, 100, 500, 1000, -1 };
 | 
						|
 | 
						|
HIDDEN int ctoi(char c)
 | 
						|
{
 | 
						|
  if (c == '\0') return 0;
 | 
						|
 | 
						|
  c = toupper(c);
 | 
						|
  for (int i = 0; cifre_romane[i]; i++)
 | 
						|
    if (cifre_romane[i] == c) return valori_cifre[i];
 | 
						|
 | 
						|
  return -1;
 | 
						|
}
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Converte una cifra romana in intero normale
 | 
						|
//
 | 
						|
// @rdesc Ritorna l'equivalente in numeri della cifra romane
 | 
						|
int rtoi(
 | 
						|
  const char * val) // @parm Stringa contenente la cifra scritta in numeri romani
 | 
						|
{
 | 
						|
  if (val == NULL) return 0;
 | 
						|
 | 
						|
  int tot = 0;
 | 
						|
  int value = ctoi (val[0]);
 | 
						|
  for (int i = 1; value > 0; i++)
 | 
						|
  {
 | 
						|
    const int next_val = ctoi(val[i]);
 | 
						|
    if (value < next_val) tot -= value;
 | 
						|
    else                  tot += value;
 | 
						|
    value = next_val;
 | 
						|
  }
 | 
						|
 | 
						|
  return (value == 0) ? tot : -1;
 | 
						|
}
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Converte un numero intero nell'equivalente cifra romana
 | 
						|
//
 | 
						|
// @rdesc Ritorna una stringa contenente la cifra romana
 | 
						|
const char* itor(
 | 
						|
  int num) // @parm Intero da convertire in cifra romana
 | 
						|
{
 | 
						|
  HIDDEN char roman_string[16];
 | 
						|
  int cifra = 0;
 | 
						|
 | 
						|
  for (int pos = 7; pos--;)
 | 
						|
  {
 | 
						|
    int val = valori_cifre[pos];
 | 
						|
    int quanti = num / val;
 | 
						|
    if (quanti < 4)
 | 
						|
    {
 | 
						|
      if ((pos & 1) && quanti == 1 && (num/valori_cifre[pos-1]) == 9)
 | 
						|
      {
 | 
						|
        roman_string[cifra++] = cifre_romane[pos-1];
 | 
						|
        roman_string[cifra++] = cifre_romane[pos+1];
 | 
						|
        val = valori_cifre[pos-1];
 | 
						|
        quanti = 9;
 | 
						|
      }
 | 
						|
      else for (int i = 0; i < quanti; i++)
 | 
						|
        roman_string[cifra++] = cifre_romane[pos];
 | 
						|
    }
 | 
						|
    else
 | 
						|
    {
 | 
						|
      roman_string[cifra++] = cifre_romane[pos];
 | 
						|
      roman_string[cifra++] = cifre_romane[pos+1];
 | 
						|
    }
 | 
						|
 | 
						|
    num -= quanti * val;
 | 
						|
  }
 | 
						|
 | 
						|
  roman_string[cifra] = '\0';
 | 
						|
  return roman_string;
 | 
						|
}
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Permette di codificare i caratteri di escape
 | 
						|
//
 | 
						|
// @rdesc Ritorna il carattere codificato
 | 
						|
const char* esc(
 | 
						|
  const char* s) // @parm Stringa da codificare
 | 
						|
{
 | 
						|
  const char *s1 = s == NULL ? "" : s;
 | 
						|
  char* encoded = get_tmp_string().get_buffer(strlen(s));
 | 
						|
  char* s2 = encoded;       
 | 
						|
  while (*s1)
 | 
						|
  {
 | 
						|
      if (*s1 == '\\')
 | 
						|
      {
 | 
						|
        s1++;
 | 
						|
        switch (tolower(*s1))
 | 
						|
        {
 | 
						|
        case 'b' : *s2++ = '\b'; break;
 | 
						|
        case 'e' : *s2++ = '\033'; break;
 | 
						|
        case 'f' : *s2++ = '\f'; break;
 | 
						|
        case 'n' : *s2++ = '\n'; break;
 | 
						|
        case 'r' : *s2++ = '\r'; break;
 | 
						|
        case 't' : *s2++ = '\t'; break;
 | 
						|
        default  :
 | 
						|
        {
 | 
						|
          if (isdigit(*s1))
 | 
						|
          {
 | 
						|
						int base = 10;
 | 
						|
            if (*s1 == '0')
 | 
						|
            {
 | 
						|
							s1++;
 | 
						|
							if (tolower(*s1) == 'x') 
 | 
						|
							{
 | 
						|
							  s1++;
 | 
						|
							  base = 16;
 | 
						|
							}
 | 
						|
							else 
 | 
						|
								base = 8;
 | 
						|
            }
 | 
						|
						*s2 = 0;
 | 
						|
						char c = tolower(*s1);
 | 
						|
						while (isdigit(c) || (base == 16 && c >= 'a' && c <= 'f'))
 | 
						|
						{
 | 
						|
						  *s2 *= base;
 | 
						|
						  if (isdigit(*s1)) *s2 += (*s1 - 48);
 | 
						|
						  else *s2 += (*s1 - 'a' + 10) & 0x0F;
 | 
						|
						  s1++;
 | 
						|
						  c = tolower(*s1);
 | 
						|
						}
 | 
						|
						s2++; s1--;
 | 
						|
          }
 | 
						|
          else *s2++ = *s1;
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
    else
 | 
						|
      if (*s1 == '^')
 | 
						|
      {
 | 
						|
        s1++;
 | 
						|
        *s2++ = (tolower(*s1) - 'a' + 1);
 | 
						|
      }
 | 
						|
      else *s2++ = *s1 ;
 | 
						|
    s1++;
 | 
						|
  }
 | 
						|
  *s2 = '\0';
 | 
						|
  return encoded;
 | 
						|
}
 | 
						|
 | 
						|
const char* unesc(
 | 
						|
  const char* s) // @parm Stringa da decodificare
 | 
						|
{
 | 
						|
  char *decoded = get_tmp_string().get_buffer(strlen(s)*4);
 | 
						|
	char* s2 = decoded;
 | 
						|
  for (const char *s1 = s; *s1; s1++)
 | 
						|
	{
 | 
						|
		const char& c = *s1;
 | 
						|
		if (c >= '\0' && c < ' ')
 | 
						|
		{
 | 
						|
			switch(c)
 | 
						|
			{
 | 
						|
			case '\n':
 | 
						|
				*s2++ = '\\'; *s2++ = 'n';
 | 
						|
				break;
 | 
						|
			case '\r':
 | 
						|
				*s2++ = '\\'; *s2++ = 'r';
 | 
						|
				break;
 | 
						|
			case '\t':
 | 
						|
				*s2++ = '\\'; *s2++ = 't';
 | 
						|
				break;
 | 
						|
			default: 
 | 
						|
				*s2++ = '\\'; *s2++ = '0';	*s2++ = 'x';
 | 
						|
				sprintf(s2, "%02x", int(c)); s2 += 2;
 | 
						|
				break;
 | 
						|
			}
 | 
						|
		}
 | 
						|
		else
 | 
						|
		  *s2++ = c;
 | 
						|
  }
 | 
						|
	*s2 = '\0';
 | 
						|
  return decoded;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
HIDDEN const char * const encryption_key = "QSECOFR-";
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Permette di criptare una parola
 | 
						|
//
 | 
						|
// @rdesc Ritorna la stringa criptata
 | 
						|
const char * encode(
 | 
						|
  const char * data) // @parm Stringa da criptare
 | 
						|
 | 
						|
  // @xref <f decode>
 | 
						|
{
 | 
						|
  for (int i = 0; data[i]; i++)
 | 
						|
    __tmp_string[i] = data[i] + (i < 8 ? encryption_key[i] : data[i - 8]);
 | 
						|
  __tmp_string[i] = '\0';
 | 
						|
  return __tmp_string; 
 | 
						|
}
 | 
						|
 | 
						|
// @doc EXTERNAL
 | 
						|
 | 
						|
// @func Permette di decodificare una stringa criptata
 | 
						|
//
 | 
						|
// @rdesc Ritorna la stringa in chiaro
 | 
						|
const char * decode(
 | 
						|
  const char * data) // @parm Stringa criptata da tradurre
 | 
						|
 | 
						|
  // @xref <f encode>
 | 
						|
{
 | 
						|
  for (int i = 0; data[i]; i++)
 | 
						|
    __tmp_string[i] = data[i] - (i < 8 ? encryption_key[i] : __tmp_string[i - 8]);
 | 
						|
  __tmp_string[i] = '\0';
 | 
						|
  return __tmp_string; 
 | 
						|
}
 | 
						|
 | 
						|
__int64 mcd(__int64 a, __int64 b)
 | 
						|
 | 
						|
{
 | 
						|
	if (a < 0)
 | 
						|
		a = -a;
 | 
						|
	if (b < 0)
 | 
						|
		b = -b;
 | 
						|
 | 
						|
	__int64 r;
 | 
						|
	
 | 
						|
	while (b > 0)
 | 
						|
	{
 | 
						|
    r = a % b;
 | 
						|
		a = b;
 | 
						|
		b = r;
 | 
						|
	}
 | 
						|
	return a;
 | 
						|
}
 | 
						|
 | 
						|
__int64 mcm(__int64 a, __int64 b)
 | 
						|
 | 
						|
{
 | 
						|
	if (a < 0)
 | 
						|
		a = -a;
 | 
						|
	if (b < 0)
 | 
						|
		b = -b;
 | 
						|
 | 
						|
	return ((a * b) / mcd(a, b));
 | 
						|
}
 |