#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <locale.h>
const char kEst[] = ".txt";
const char kStrDeZ[] = " (dezedded).txt";
const size_t kLStrDeZ = 15;
const char kSostituto = '…';
int de_zeta( const char *nome_file );
void presenta( void );
int main( int argc, char *argv[] ) {
setlocale( LC_ALL, "Italian_Italy.1252" );
if( argc>1 ) {
int i;
for( i=1; i<argc; ++i )
de_zeta( argv[i] );
}
else {
presenta();
while( '\n'!=getchar() );
}
return 0;
}
void presenta( void ) {
puts( "\n D E Z E D D E R" );
puts( " ===============================================================" );
puts( " Questo programma può aiutare ad essere certi che i propri file" );
puts( " di testo siano correttamente allineati alle inclinazioni del" );
puts( " Parlamento corrente, così come \"ispirato\" in sede UE.\n" );
puts( " Trascina sull'icona dell'eseguibile i file di tipo .txt da" );
puts( " epurare." );
puts( " ===============================================================" );
}
char *carica_file( const char *nome_file ) {
char *t = NULL; /* il testo da dezetificare */
FILE *f = NULL; /* il file con il testo da dezetificare */
int ok = 0;
if( nome_file ) {
if( (f=fopen(nome_file,"r")) ) {
long lt;
fseek( f, 0, SEEK_END );
lt = ftell( f );
fseek( f, 0, SEEK_SET );
if( (ok=lt>=0) ) {
if( (t=calloc(lt+1,sizeof(*t))) ) {
long i = 0;
int c = fgetc( f );
while( EOF!=c ) {
t[i++] = c;
c = fgetc( f );
}
ok = 1;
}
}
}
}
if( f ) { fclose(f); f = NULL; }
if( !ok && t ) { free(t); t = NULL; }
return t;
}
int sostituisci_carattere( char *t, char ex_c, char nuovo_c ) {
int ok = 0;
if( t && ex_c && nuovo_c ) {
size_t i, lt = strlen( t );
char ex_c_upper = toupper( ex_c );
char ex_c_lower = tolower( ex_c );
for( i=0; i<lt; ++i )
if( t[i] == ex_c_upper || t[i] == ex_c_lower )
t[i] = nuovo_c;
ok = 1;
}
return ok;
}
char *nome_file_dez( const char *nome_file ) {
char *nfDez = NULL; /* il nome del file dezetificato */
char *est = NULL; /* il puntatore all'estensione in nome_file */
if( nome_file ) {
if( (est=strrchr(nome_file,'.')) && 0==strcmp(est,kEst) ) {
size_t l_est = strlen( est );
size_t l_nome_file = strlen( nome_file ) - l_est;
if( (nfDez=calloc(l_nome_file+kLStrDeZ+1,sizeof(*nfDez))) ) {
memcpy( nfDez, nome_file, l_nome_file );
memcpy( nfDez+l_nome_file, kStrDeZ, kLStrDeZ+1 );
}
}
}
return nfDez;
}
int de_zeta( const char *nome_file ) {
char *nfDez = NULL; /* il nome del file dezetificato */
FILE *fDez = NULL; /* il puntatore allo stream del file dezetificato */
char *t = NULL; /* il testo da dezetificare */
int ok = 0;
if( (t=carica_file(nome_file)) ) {
sostituisci_carattere( t, 'z', kSostituto );
if( (nfDez=nome_file_dez(nome_file)) ) {
if( (fDez=fopen(nfDez,"w+")) ) {
fprintf( fDez, t );
fclose(fDez); fDez = NULL;
}
free(nfDez); nfDez = NULL;
}
free(t); t = NULL;
}
return ok;
}