Files correlati : Ricompilazione Demo : [ ] Commento :correzioni dovute al passaggio al nuovo compilatore git-svn-id: svn://10.65.10.50/trunk@14698 c028cbd2-c16b-5b4b-a496-9718f37d4682
		
			
				
	
	
		
			611 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			611 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
| #include <xvt.h>
 | |
| #include <statbar.h>
 | |
| 
 | |
| #include <diction.h>
 | |
| #include <utility.h>
 | |
| 
 | |
| ///////////////////////////////////////////////////////////
 | |
| // TPerformance_profiler
 | |
| ///////////////////////////////////////////////////////////
 | |
| 
 | |
| #ifdef DBG
 | |
| static int _depth = 0;
 | |
| #endif
 | |
| 
 | |
| TPerformance_profiler::TPerformance_profiler(const char* desc, bool trc)
 | |
|                      : _desc(desc), _trc(trc)
 | |
| {
 | |
| #ifdef DBG
 | |
|   _depth++;
 | |
|   _start = clock();
 | |
| 
 | |
|   if (!_trc)
 | |
|   {
 | |
|     TString80 msg;
 | |
|     msg << "Profiling " << desc << "...";
 | |
|     statbar_set_title(TASK_WIN, msg);
 | |
|   }
 | |
| 
 | |
|   while (true)
 | |
|   {
 | |
|     const clock_t clk = clock();
 | |
|     if (clk != _start)
 | |
|     {
 | |
|       _start = clk;
 | |
|       break;
 | |
|     }
 | |
|   }
 | |
| #endif
 | |
| }
 | |
| 
 | |
| void TPerformance_profiler::show() const
 | |
| {
 | |
| #ifdef DBG
 | |
|   const double s = double(clock() - _start) / CLOCKS_PER_SEC;
 | |
| 
 | |
|   int hour = 0, min = 0;
 | |
|   int sec = int(s);
 | |
|   const int cent = int((s - sec)*100);
 | |
| 
 | |
|   if (sec >= 3600)
 | |
|   {
 | |
|     hour = sec / 3600;
 | |
|     sec -= hour * 3600;
 | |
|   }
 | |
|   if (sec >= 60)
 | |
|   {
 | |
|     min = sec / 60;
 | |
|     sec -= min * 60;
 | |
|   }
 | |
| 
 | |
|   TString256 msg = _desc;
 | |
|   msg.format("%s %02d:%02d:%02d.%02d", (const char*)_desc, hour, min, sec, cent);
 | |
| 
 | |
|   if (_trc)
 | |
|     __trace(msg);
 | |
|   else
 | |
|   {
 | |
|     if (_depth <= 1)
 | |
|     {
 | |
|       statbar_set_title(TASK_WIN, msg);
 | |
|       if (s > 0)
 | |
|         xvt_sys_sleep(500);
 | |
|     }
 | |
|   }
 | |
| #endif
 | |
| }
 | |
| 
 | |
| TPerformance_profiler::~TPerformance_profiler()
 | |
| {
 | |
|   show();
 | |
| #ifdef DBG
 | |
|   _depth--;
 | |
| #endif
 | |
| }
 | |
| 
 | |
| // @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 (xvt_str_compare_ignoring_case(orig, dest) == 0)
 | |
|     return TRUE;  // Or FALSE?
 | |
| 
 | |
|   FILE* i = fopen(orig, rflag);
 | |
|   if (i == NULL) 
 | |
|     return error_box(FR("Impossibile leggere il file %s\nda copiare in %s"), orig, dest);
 | |
| 
 | |
|   if (!append)
 | |
|     ::remove_file(dest);
 | |
|   FILE* o = fopen(dest, wflag);
 | |
|   if (o == NULL) 
 | |
|   {
 | |
|     fclose(i);
 | |
|     return error_box(FR("Impossibile creare il file '%s' (%s)\nper copiare il file '%s'\nErrore %d"), dest, wflag, orig, errno);
 | |
|   }  
 | |
| 
 | |
|   const int 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(FR("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
 | |
| { 
 | |
|   return xvt_fsys_access(file, 0x00) == 0;
 | |
| }    
 | |
| 
 | |
| long fsize(const char* name)
 | |
| {
 | |
|   return xvt_fsys_file_attr(name, XVT_FILE_ATTR_SIZE);
 | |
| } 
 | |
| 
 | |
| // @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
 | |
| {
 | |
|   return xvt_fsys_mkdir(dir) != 0;
 | |
| }
 | |
| 
 | |
| // @doc EXTERNAL
 | |
| 
 | |
| // @func Cancella un file
 | |
| //
 | |
| // @rdesc Ritorna il risultato dell'operazione
 | |
| //
 | |
| // @flag TRUE | Se l'operazione e' avvenuta con successo
 | |
| // @flag FALSE | Se l'operazione non e' riuscita
 | |
| bool remove_file(const char* file) // @parm Nome della directory da creare
 | |
| {
 | |
|   return xvt_fsys_removefile(file) != 0;
 | |
| }
 | |
| 
 | |
| // @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);
 | |
|   int i;
 | |
|   
 | |
|   for (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, TR("Selezionare il file")) == FL_OK;
 | |
|   xvt_fsys_set_dir(&dir);
 | |
|                  
 | |
|   if (good)
 | |
|   {
 | |
|     char buf[_MAX_PATH];
 | |
|     xvt_fsys_convert_dir_to_str(&fs.dir, buf, sizeof(buf));
 | |
|     file = buf;
 | |
|     file.add(fs.name);
 | |
|   }
 | |
|   return good;  
 | |
| }  
 | |
| 
 | |
| 
 | |
| // 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;
 | |
| 
 | |
|   char buf[512];
 | |
|   va_start(pars, fmt);
 | |
|   const int tot = _vsnprintf(buf, sizeof(buf), fmt, pars);
 | |
|   va_end(pars);
 | |
| 
 | |
|   CHECK(tot < 512, "Ue'! Ma quanto scrivi?");
 | |
|   TString& tmp = get_tmp_string();
 | |
|   tmp = buf;
 | |
| 
 | |
|   return tmp.get_buffer();
 | |
| }
 | |
| 
 | |
| // @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";
 | |
| 
 | |
|   TString& tmp = get_tmp_string();
 | |
|   tmp = app;
 | |
|   return tmp;
 | |
| }
 | |
| 
 | |
| 
 | |
| ///////////////////////////////////////////////////////////
 | |
| // 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>
 | |
| {
 | |
|   char* tmp = get_tmp_string(50).get_buffer();
 | |
|   int i;
 | |
|   for (i = 0; data[i]; i++)
 | |
|     tmp[i] = data[i] + (i < 8 ? encryption_key[i] : data[i - 8]);
 | |
|   tmp[i] = '\0';
 | |
|   return tmp; 
 | |
| }
 | |
| 
 | |
| // @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>
 | |
| {
 | |
|   char* tmp = get_tmp_string(50).get_buffer();
 | |
|   int i;
 | |
|   
 | |
|   for (i = 0; data[i]; i++)
 | |
|     tmp[i] = data[i] - (i < 8 ? encryption_key[i] : tmp[i - 8]);
 | |
|   tmp[i] = '\0';
 | |
|   return tmp; 
 | |
| }
 | |
| 
 | |
| //salta gli spazi bianchi ad inizio file
 | |
| istream & eatwhite(istream & i)
 | |
| {
 | |
| #if defined(WIN32) && (_MSC_VER <= 1300)
 | |
|   i.eatwhite();
 | |
| #else
 | |
| 	char c;
 | |
| 	while (i.get(c))
 | |
| 	{
 | |
| 		if (!isspace(c))
 | |
| 		{
 | |
| 			i.putback(c);
 | |
| 			break;
 | |
| 		}
 | |
| 	}
 | |
| #endif
 | |
|   return i;
 | |
| }
 | |
| 
 | |
| const TString & get_hostname()
 | |
| {
 | |
|   TString & tmp = get_tmp_string(256);
 | |
|   char* buff = tmp.get_buffer();
 | |
| 
 | |
|   if (xvt_sys_get_host_name(buff, tmp.size()))
 | |
|     return tmp;
 | |
|   return EMPTY_STRING;
 | |
| }
 | |
| 
 | |
| long daytime()
 | |
| {
 | |
| 	long t = 0L;
 | |
| 	time_t lt;
 | |
| 	if (time(<) != -1) 
 | |
|   {
 | |
| 		struct tm * timeloc = localtime(<);
 | |
|   
 | |
| 		if (timeloc != NULL)
 | |
| 					t = timeloc->tm_sec + timeloc->tm_min * 100L  + timeloc->tm_hour * 10000L;
 | |
| 	}
 | |
| 	return t;
 | |
| }
 | |
| 
 | |
| bool is_power_station()
 | |
| {
 | |
|   const char* ranger[] = { "ANTARES", "ARCHIMEDE", "BATMOBILE", "KIRK", "SPOCK", "UHURA", NULL };
 | |
|   char hostname[256]; xvt_sys_get_host_name(hostname, sizeof(hostname));
 | |
|   for (int i = 0; ranger[i]; i++)
 | |
|   {
 | |
|     if (xvt_str_compare_ignoring_case(hostname, ranger[i]) == 0)
 | |
|       return true;
 | |
|   }
 | |
|   return false;
 | |
| }
 |