From 3ae63a749570d5bd54037ff59cb8c7466fec7d85 Mon Sep 17 00:00:00 2001
From: kramm <kramm>
Date: Tue, 8 May 2007 16:45:46 +0000
Subject: [PATCH] new filename2template() function

---
 lib/args.h |   47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/lib/args.h b/lib/args.h
index f5f0351..98e986d 100644
--- a/lib/args.h
+++ b/lib/args.h
@@ -86,7 +86,12 @@ static void processargs(int argn2,char**argv2)
         }
         else
         {
-            t+=args_callback_command(argv2[t],next);
+	    int num = args_callback_command(argv2[t],next);
+	    if(num>2) {
+		fprintf("internal error in command line parsing\n");
+		exit(1);
+	    }
+            t+=num;
         }
     }
 }
@@ -202,4 +207,44 @@ static char is_in_range(int t, char*irange)
     return 0;
 }
 
+char* filename2template(char*filename, int*startindex)
+{
+    int l = strlen(filename);
+    char*newname = (char*)malloc(l+5);
+    /* first look whether the file is already numbered */
+    while(1) {
+        l--;
+        if(l<0 || strchr("0123456789", filename[l]))
+            break;
+    };
+    if(l>=0) {
+        int lastdigit=l;
+        int firstdigit=l;
+        while(firstdigit && strchr("0123456789", filename[firstdigit-1]))
+            firstdigit--;
+        *startindex = atoi(filename+firstdigit);
+        memcpy(newname, filename, firstdigit);
+        sprintf(newname+firstdigit, "%%%dd", lastdigit+1-firstdigit);
+        strcat(newname+firstdigit, filename+lastdigit+1);
+        return newname;
+    }
+    /* if it isn't, try to paste a %d between filename and extension */
+    char*dot = strrchr(filename, '.');
+    if(dot) {
+        int pos = dot-filename;
+        memcpy(newname, filename, pos);
+        newname[pos++] = '.';
+        newname[pos++] = '%';
+        newname[pos++] = 'd';
+        strcpy(newname+pos, dot);
+        *startindex = 1;
+        return newname;
+    }
+    /* if that didn't work either, just append the number at the end */
+    strcpy(newname, filename);
+    strcat(newname, ".%d");
+    *startindex = 1;
+    return newname;
+}
+
 #endif //__args_h__
-- 
1.7.10.4