summaryrefslogtreecommitdiffstats
path: root/rc.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:40:35 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:40:35 +0000
commitf7deb94049481143ef12eab590840d39aecedc14 (patch)
tree9ff4090b78d8343fd073ba1ea32068bb0d07caa6 /rc.cc
parentAdding debian version 1.3-4. (diff)
downloadzutils-f7deb94049481143ef12eab590840d39aecedc14.tar.xz
zutils-f7deb94049481143ef12eab590840d39aecedc14.zip
Merging upstream version 1.4~pre2.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'rc.cc')
-rw-r--r--rc.cc153
1 files changed, 114 insertions, 39 deletions
diff --git a/rc.cc b/rc.cc
index 54c65fc..1376655 100644
--- a/rc.cc
+++ b/rc.cc
@@ -1,5 +1,5 @@
/* Zutils - Utilities dealing with compressed files
- Copyright (C) 2009-2014 Antonio Diaz Diaz.
+ Copyright (C) 2009-2015 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -37,7 +37,7 @@ int verbosity = 0;
namespace {
const char * const config_file_name = "zutilsrc";
-const char * const program_year = "2014";
+const char * const program_year = "2015";
std::string compressor_names[num_formats] =
{ "bzip2", "gzip", "lzip", "xz" }; // default compressor names
@@ -45,6 +45,23 @@ std::string compressor_names[num_formats] =
// args to compressors, maybe empty
std::vector< std::string > compressor_args[num_formats];
+// vector of enabled formats plus [num_formats] for uncompressed.
+// empty means all enabled.
+std::vector< bool > enabled_formats;
+
+struct { const char * from; const char * to; int format_index; } const
+ known_extensions[] = {
+ { ".bz2", "", fmt_bz2 },
+ { ".tbz", ".tar", fmt_bz2 },
+ { ".tbz2", ".tar", fmt_bz2 },
+ { ".gz", "", fmt_gz },
+ { ".tgz", ".tar", fmt_gz },
+ { ".lz", "", fmt_lz },
+ { ".tlz", ".tar", fmt_lz },
+ { ".xz", "", fmt_xz },
+ { ".txz", ".tar", fmt_xz },
+ { 0, 0, -1 } };
+
int my_fgetc( FILE * const f )
{
@@ -95,12 +112,12 @@ const std::string & my_fgets( FILE * const f, int & linenum )
while( std::isspace( ch ) )
{ if( ch == '\n' ) { ++linenum; } ch = my_fgetc( f ); }
}
- if( ch == EOF ) { if( s.size() > 0 ) { ++linenum; } break; }
+ if( ch == EOF ) { if( s.size() ) { ++linenum; } break; }
else if( ch == '\n' )
{
++linenum; strip = true;
if( trailing_escape( s ) ) s.erase( s.size() - 1 );
- else if( s.size() > 0 ) break;
+ else if( s.size() ) break;
}
else s += ch;
}
@@ -108,6 +125,29 @@ const std::string & my_fgets( FILE * const f, int & linenum )
}
+bool parse_compressor_command( const std::string & s, int i,
+ const int format_index )
+ {
+ const int len = s.size();
+ while( i < len && std::isspace( s[i] ) ) ++i; // strip spaces
+ int l = i;
+ while( i < len && !std::isspace( s[i] ) ) ++i;
+ if( l >= i || s[l] == '-' ) return false;
+ compressor_names[format_index].assign( s, l, i - l );
+
+ compressor_args[format_index].clear();
+ while( i < len )
+ {
+ while( i < len && std::isspace( s[i] ) ) ++i; // strip spaces
+ l = i;
+ while( i < len && !std::isspace( s[i] ) ) ++i;
+ if( l < i )
+ compressor_args[format_index].push_back( std::string( s, l, i - l ) );
+ }
+ return true;
+ }
+
+
bool parse_rc_line( const std::string & line,
const char * const filename, const int linenum )
{
@@ -136,23 +176,11 @@ bool parse_rc_line( const std::string & line,
std::fprintf( stderr, "%s %d: missing '='.\n", filename, linenum );
return false; }
++i; // skip the '='
- while( i < len && std::isspace( line[i] ) ) ++i; // strip spaces
- l = i;
- while( i < len && !std::isspace( line[i] ) ) ++i;
- if( l >= i )
- { if( verbosity >= 0 )
- std::fprintf( stderr, "%s %d: missing compressor name.\n", filename, linenum );
- return false; }
- compressor_names[format_index].assign( line, l, i - l );
-
- compressor_args[format_index].clear();
- while( i < len )
+ if( !parse_compressor_command( line, i, format_index ) )
{
- while( i < len && std::isspace( line[i] ) ) ++i; // strip spaces
- l = i;
- while( i < len && !std::isspace( line[i] ) ) ++i;
- if( l < i )
- compressor_args[format_index].push_back( std::string( line, l, i - l ) );
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s %d: missing compressor name.\n", filename, linenum );
+ return false;
}
return true;
}
@@ -181,6 +209,70 @@ int process_rcfile( const std::string & name )
} // end namespace
+bool enabled_format( const int format_index )
+ {
+ if( enabled_formats.size() <= num_formats ) return true;
+ if( format_index < 0 ) return enabled_formats[num_formats];
+ return enabled_formats[format_index];
+ }
+
+
+void parse_format_list( const std::string & arg )
+ {
+ const std::string un( "uncompressed" );
+ bool error = arg.empty();
+ enabled_formats.assign( num_formats + 1, false );
+
+ for( unsigned l = 0, r; l < arg.size(); l = r + 1 )
+ {
+ r = std::min( arg.find( ',', l ), arg.size() );
+ if( l >= r ) { error = true; break; } // empty format
+ int format_index = num_formats;
+ const std::string s( arg, l, r - l );
+ for( int i = 0; i < num_formats; ++i )
+ if( s == format_names[i] )
+ { format_index = i; break; }
+ if( format_index == num_formats && un.find( s ) != 0 )
+ { error = true; break; }
+ enabled_formats[format_index] = true;
+ }
+ if( error )
+ { show_error( "Bad argument for '--format' option." ); std::exit( 1 ); }
+ }
+
+
+int parse_format_type( const std::string & arg )
+ {
+ for( int i = 0; i < num_formats; ++i )
+ if( arg == format_names[i] )
+ return i;
+ show_error( "Bad argument for '--force-format' option." );
+ std::exit( 1 );
+ }
+
+
+int extension_index( const std::string & name )
+ {
+ for( int i = 0; known_extensions[i].from; ++i )
+ {
+ const std::string ext( known_extensions[i].from );
+ if( name.size() > ext.size() &&
+ name.compare( name.size() - ext.size(), ext.size(), ext ) == 0 )
+ return i;
+ }
+ return -1;
+ }
+
+int extension_format( const int eindex )
+ { return ( eindex >= 0 ) ? known_extensions[eindex].format_index : -1; }
+
+const char * extension_from( const int eindex )
+ { return known_extensions[eindex].from; }
+
+const char * extension_to( const int eindex )
+ { return known_extensions[eindex].to; }
+
+
void maybe_process_config_file( const Arg_parser & parser )
{
for( int i = 0; i < parser.arguments(); ++i )
@@ -203,32 +295,15 @@ void maybe_process_config_file( const Arg_parser & parser )
void parse_compressor( const std::string & arg, const int format_index,
const int eretval )
{
- const int len = arg.size();
- int i = 0;
- while( i < len && std::isspace( arg[i] ) ) ++i; // strip spaces
- int l = i;
- while( i < len && !std::isspace( arg[i] ) ) ++i;
- if( l >= i )
+ if( !parse_compressor_command( arg, 0, format_index ) )
{ show_error( "Missing compressor name." ); std::exit( eretval ); }
- compressor_names[format_index].assign( arg, l, i - l );
-
- compressor_args[format_index].clear();
- while( i < len )
- {
- while( i < len && std::isspace( arg[i] ) ) ++i; // strip spaces
- l = i;
- while( i < len && !std::isspace( arg[i] ) ) ++i;
- if( l < i )
- compressor_args[format_index].push_back( std::string( arg, l, i - l ) );
- }
}
const char * get_compressor_name( const int format_index )
{
if( format_index >= 0 && format_index < num_formats &&
- compressor_names[format_index].size() &&
- compressor_names[format_index][0] != '-' )
+ compressor_names[format_index].size() )
return compressor_names[format_index].c_str();
return 0;
}