para_afh: Accept negative values for begin_chunk and end_chunk.
authorAndre Noll <maan@systemlinux.org>
Sat, 5 Apr 2008 21:52:03 +0000 (23:52 +0200)
committerAndre Noll <maan@systemlinux.org>
Sat, 5 Apr 2008 21:52:03 +0000 (23:52 +0200)
Negative values are interpreted as offsets counted from the
end of the audio file.

afh.c
afh.ggo

diff --git a/afh.c b/afh.c
index 86f34ace67e0efcba11fb9ddfe1a2f23ca2416b7..dd69d9e458d73ff9fbd9735f590b54a3b999922f 100644 (file)
--- a/afh.c
+++ b/afh.c
@@ -64,18 +64,26 @@ static int cat_file(void *audio_file_data, struct afh_info *afhi)
        size_t size;
 
 
        size_t size;
 
 
-       if (conf.begin_chunk_arg < 0)
-               return -ERRNO_TO_PARA_ERROR(EINVAL);
-       first_chunk = conf.begin_chunk_arg;
-
-       if (conf.end_chunk_given) {
-               if (conf.end_chunk_arg < 0)
-                       return -ERRNO_TO_PARA_ERROR(EINVAL);
-               if (conf.end_chunk_arg >= afhi->chunks_total)
+       if (conf.begin_chunk_arg < 0) {
+               if (-conf.begin_chunk_arg > afhi->chunks_total)
                        return -ERRNO_TO_PARA_ERROR(EINVAL);
                        return -ERRNO_TO_PARA_ERROR(EINVAL);
-               last_chunk = conf.end_chunk_arg;
+               first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
+       } else
+               first_chunk = conf.begin_chunk_arg;
+       if (conf.end_chunk_given) {
+               if (conf.end_chunk_arg < 0) {
+                       if (-conf.end_chunk_arg > afhi->chunks_total)
+                               return -ERRNO_TO_PARA_ERROR(EINVAL);
+                       last_chunk = afhi->chunks_total + conf.end_chunk_arg;
+               } else {
+                       if (conf.end_chunk_arg >= afhi->chunks_total)
+                               return -ERRNO_TO_PARA_ERROR(EINVAL);
+                       last_chunk = conf.end_chunk_arg;
+               }
        } else
                last_chunk = afhi->chunks_total - 1;
        } else
                last_chunk = afhi->chunks_total - 1;
+       if (first_chunk >= last_chunk)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
        if (!afhi->chunks_total)
                return 1;
        afh_get_header(afhi, audio_file_data, &buf, &size);
        if (!afhi->chunks_total)
                return 1;
        afh_get_header(afhi, audio_file_data, &buf, &size);
diff --git a/afh.ggo b/afh.ggo
index 7fb9d5ac3379010e6eacb59aff359dc5b9ada26b..c7552ef978e96bcdc0b110cb3d07e7c0e338ab3b 100644 (file)
--- a/afh.ggo
+++ b/afh.ggo
@@ -75,8 +75,14 @@ default="0"
 dependon="stream"
 optional
 details="
 dependon="stream"
 optional
 details="
-       The chunk_num argument must be non-negative as chunks start
-       at zero.
+       The chunk_num argument must be between -num_chunks and
+       num_chunks - 1 inclusively where num_chunks is the total number
+       of chunks which is printed when using the --info option. If
+       chunk_num is negative, the given number of chunks are counted
+       backwards from the end of the file. For example --begin_chunk
+       -100 instructs para_afh to start output at chunk num_chunks
+       - 100. This is mainly useful for cutting off the end of an
+       audio file.
 "
 
 option "end_chunk" e
 "
 
 option "end_chunk" e
@@ -86,8 +92,8 @@ int typestr="chunk_num"
 dependon="stream"
 optional
 details="
 dependon="stream"
 optional
 details="
-       The chunk_num argument must be less than the total number of
-       chunks. The default is to write up to the last chunk.
+       For the chunk_num argument the same rules as for --begin_chunk
+       apply.  The default is to write up to the last chunk.
 "
 
 option "just_in_time" j
 "
 
 option "just_in_time" j