Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Estrazione - utilita.cpp

utilita.cpp

Caricato da: AldoBaldo
Scarica il programma completo

  1. #include "utilita.h"
  2.  
  3.  
  4. /*==============================================================================
  5. "Recupera" il puntatore alla struttura contenente i dati globali del programma,
  6. memorizzato nel campo GWL_USERDATA della finestra principale.
  7. ==============================================================================*/
  8.  
  9. GLOBALE *RicavaDatiGlobali( HWND hwnd ) {
  10.     return ((GLOBALE*)GetWindowLong(hwnd,GWL_USERDATA));
  11. }
  12.  
  13.  
  14. /*==============================================================================
  15. "tipoFile" identifica il tipo di file che sara' riconosciuto dalla finestra di
  16. navigazione e che sarà usato come estensione di default, se del caso. Se e' NULL
  17. saranno accettati file di ogni tipo e non sara' attribuita alcuna estensione di
  18. default.
  19. "nomeFileDef" identifica il nome del file usato come default nella finestra del
  20. tipo "Salva". Se "nomeFileDef" e' un puntatore valido, la finestra usata sara'
  21. una finestra del tipo "Salva"; se e' NULL, la finestra usata sarà una finestra
  22. del tipo "Apri".
  23. ==============================================================================*/
  24.  
  25. const char *IdentificaFile( HWND hwnd, const char *tipoFile,
  26.                             const char *nomeFileDef ) {
  27.     OPENFILENAME ofn;
  28.     char filtro[32];
  29.     char *p = filtro;
  30.     char *nomeFile;
  31.     DWORD esito;
  32.  
  33.     ZeroMemory(&ofn, sizeof(ofn));
  34.  
  35.     if( tipoFile != NULL ) {
  36.         p += wsprintf( p, "Tipo file: %s", tipoFile ) + 1;
  37.         p += wsprintf( p, "*.%s", tipoFile ) + 1;
  38.         *p = '\0';
  39.     }
  40.     else {
  41.         p += wsprintf( p, "Tutti i file (*.*)" ) + 1;
  42.         p += wsprintf( p, "*.*" ) + 1;
  43.         *p = '\0';
  44.     }
  45.  
  46.     try {
  47.         nomeFile = new char[4*MAX_PATH];
  48.         *nomeFile = '\0';
  49.     } catch( ... ) {
  50.         return NULL;
  51.     }
  52.  
  53.     if( nomeFileDef != NULL )
  54.         lstrcpy( nomeFile, nomeFileDef );
  55.  
  56.     ofn.lStructSize = sizeof(ofn);
  57.     ofn.hwndOwner = hwnd;
  58.     ofn.lpstrFilter = filtro;
  59.     ofn.lpstrFile = nomeFile;
  60.     ofn.nMaxFile = MAX_PATH;
  61.     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  62.     ofn.lpstrDefExt = tipoFile;
  63.  
  64.     if( nomeFileDef != NULL )
  65.         esito = GetSaveFileName( &ofn );
  66.     else esito = GetOpenFileName( &ofn );
  67.  
  68.     if ( esito == FALSE ) {
  69.         if( CommDlgExtendedError() != 0 )
  70.             Errore( kErrSceltaFileNonValida, hwnd );
  71.         delete[] nomeFile;
  72.         return NULL;
  73.     }
  74.  
  75.     return nomeFile;
  76. }
  77.  
  78.  
  79. char *CaricaFileDiTesto( const char *percFile ) {
  80.     FILE *f = fopen( percFile, "r" );
  81.     int i=0, c=0;
  82.  
  83.     if( f == NULL ) return NULL;
  84.     while( fgetc(f) != EOF ) ++i;
  85.     rewind( f ); // torna all'inizio
  86.  
  87.     try {
  88.         char *dati = new char[i+1];
  89.  
  90.         for( c=fgetc(f), i=0; c!=EOF; c=fgetc(f), ++i )
  91.             dati[i] = c;
  92.         dati[i] = '\0';
  93.  
  94.         fclose( f );
  95.         return dati;
  96.     } catch( ... ) {
  97.         fclose( f );
  98.         return NULL;
  99.     }
  100. }
  101.  
  102.  
  103. /*==============================================================================
  104. NOTA BENE: il puntatore restituito da questa funzione si riferisce a uno spazio
  105. di memoria allocato dinamicamente tramite l'operatore "new". Lo spazio allocato
  106. deve quindi essere liberato per mezzo dell'operatore "delete[]".
  107. ==============================================================================*/
  108.  
  109. const char *RicavaPrimoFileTrascinato( LPSTR lpszArgs ) {
  110.     char *esito, *e, *inizio, *fine;
  111.  
  112.     if( *lpszArgs == '\0' ) {
  113.         try {
  114.             esito = new char[1];
  115.             *esito = '\0';
  116.         } catch( ... ) {
  117.             return NULL;
  118.         }
  119.     }
  120.     else {
  121.         if( *lpszArgs == '\"' ) {
  122.             // trova l'inizio e la fine del virgolettato
  123.             for( inizio = lpszArgs; *inizio == '\"'; ++inizio );
  124.             for( fine = inizio; *fine != '\"'; ++fine );
  125.         }
  126.         else {
  127.             for( inizio=fine=lpszArgs; *fine!=' ' && *fine!='\0'; ++fine );
  128.         }
  129.  
  130.         try {
  131.             esito = new char[(fine-inizio)+1];
  132.         } catch( ... ) {
  133.             return NULL;
  134.         }
  135.  
  136.         // copia il primo parametro della command line
  137.         for( e = esito; fine > inizio; *e++ = *inizio++ );
  138.         *e = '\0';
  139.     }
  140.  
  141.     return esito;
  142. }
  143.  
  144.  
  145. /*==============================================================================
  146. Compone una stringa informativa sul programma in un buffer statico e ne
  147. restituisce il puntatore.
  148. ==============================================================================*/
  149.  
  150. const char *ComponiStrInfo( void ) {
  151.     static char info[64] = "";
  152.     char *p = info;
  153.  
  154.     p += wsprintf( p, "%s\n", kStrNomeProgramma );
  155.     p += wsprintf( p, "v %d", kVersioneProgramma );
  156.     p += wsprintf( p, ".%d", kSottoversioneProgramma );
  157.  
  158.     if( kRevisioneProgramma != 0 )
  159.         p += wsprintf( p, ".%d", kRevisioneProgramma );
  160.  
  161.     p += wsprintf( p, " %s", kStrCopyright );
  162.  
  163.     return info;
  164. }
  165.  
  166.  
  167. /*==============================================================================
  168. Copia la stringa passata tramite il parametro "s" in uno spazio di memoria
  169. dinamico appositamente allocato tramite "new". Se il parametro "tipo" ha valore
  170. 1, la copia della stringa viene convertita in maiuscolo; se il parametro "tipo"
  171. ha valore -1, la copia della stringa viene convertita in minuscolo; se il
  172. parametro "tipo" ha valore 0, la copia della stringa e' identica all'originale.
  173. Il chiamante DEVE liberare tramite "delete[]" la memoria dinamica allocata.
  174. Se la copia non e' possibile, la funzione restituisce NULL.
  175. ==============================================================================*/
  176.  
  177. char *DuplicaStringa( const char *s, int tipo ) {
  178.     if( s == NULL ) return NULL;
  179.  
  180.     try {
  181.         unsigned long l = lstrlen( s );
  182.         char *dup = new char[l+1];
  183.         memcpy( dup, s, l+1 );
  184.  
  185.         switch( tipo ) {
  186.             case -1: CharLowerBuff( dup, l ); break;
  187.             case  1: CharUpperBuff( dup, l ); break;
  188.             default: break;
  189.         }
  190.  
  191.         return dup;
  192.     } catch( ... ) {
  193.         return NULL;
  194.     }
  195. }