* Added '-i' option to include text in output.
authorUrban Wallasch <urban.wallasch@freenet.de>
Mon, 7 Jun 2021 14:29:34 +0000 (16:29 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Mon, 7 Jun 2021 14:29:34 +0000 (16:29 +0200)
* Improved error reporting for '-I' and '-m' options.

README.md
main.c

index 4b5315d471e033adb8853bdb8657c3c4b2678dbf..079b17488946ec448a14311415eabd3ac386fa5b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -28,7 +28,8 @@ OPTIONS:
   -t n      similarity threshold in bits (0..256) or percent; default: 256
   -b float  blur factor; add -r when changed between runs; default: 2.5
   -r        re-scan files already in database
-  -I file   use file contents as lead-in for output
+  -i text   add text as lead-in to output
+  -I file   add file contents as lead-in to output
   -T n      number of scan threads (default: 4)
   -h        display this help text and exit
 ```
@@ -64,11 +65,13 @@ OPTIONS:
   * `-r` initiates a full re-scan, even for files already present in the
     database.
 
-  * `-I` allows to include a custom lead-in in the output, which is in
+  * `-i` include text as a custom lead-in in the output, which is in
     particular useful to replace the default dummy versions of the `VIEW()`
     and `END()` functions which are used by `imgdupe` to structure its
     output.
 
+  * `-I` same as `-i`, but add the contents of the specified file.
+
   * `-T` allows to set the number of hashing threads to use; for optimal
     performance this should be equal to the number of (physical) processor
     cores.
diff --git a/main.c b/main.c
index 426a253a86c67bc139268cdb90cef6bddfc16cb4..e2f8eaf277a3935837a99b4d96012e13181e7286 100644 (file)
--- a/main.c
+++ b/main.c
@@ -148,7 +148,8 @@ static void usage(char *pname, int ec) {
            "  -t n      similarity threshold in bits (0..256) or percent; default: 256\n"
            "  -b float  blur factor; add -r when changed between runs; default: 2.5\n"
            "  -r        re-scan files already in database\n"
-           "  -I file   use file contents as lead-in for output\n"
+           "  -i text   add text as lead-in to output\n"
+           "  -I file   add file contents as lead-in to output\n"
            "  -T n      number of scan threads (default: 4)\n"
            "  -h        display this help text and exit\n"
     );
@@ -167,7 +168,7 @@ int main(int argc, char *argv[]) {
         "END(){ echo 'Done'; }\n\n";
 
     db = db_init();
-    while ( ( c = getopt( argc, argv, "+:b:d:hI:f:m:prt:T:" ) ) != -1 ) {
+    while ( ( c = getopt( argc, argv, "+:b:d:hi:I:f:m:prt:T:x" ) ) != -1 ) {
         switch (c) {
         case 'b':
             n = atoi(optarg);
@@ -180,18 +181,37 @@ int main(int argc, char *argv[]) {
         case 'h':
             usage(argv[0], EXIT_SUCCESS);
             break;
+        case 'i':
+            n = printf("%s\n", optarg);
+            dprintf("written %d bytes lead-in (-i)\n", n);
+            lead_in = "";
+            break;
         case 'I':
             n = cat_file(optarg);
-            lead_in = "";
-            dprintf("written %d bytes lead-in (%s)\n", n, optarg);
+            if ( n >= 0 ) {
+                lead_in = "";
+                dprintf("written %d bytes lead-in (%s)\n", n, optarg);
+            }
+            else {
+                eprintf("ERROR: '%s': %s\n", optarg, strerror(errno));
+                ++err;
+            }
             break;
         case 'f':
             cfg.db_outfile = optarg;
-            /* fall through */
+            dprintf("loading '%s'\n", optarg);
+            n = db_read(db, optarg);
+            dprintf("read %d entries\n", n > 0 ? n : 0);
+            break;
         case 'm':
             dprintf("loading '%s'\n", optarg);
             n = db_read(db, optarg);
-            dprintf("loaded %d entries\n", n>0?n:0);
+            if ( n >= 0 )
+                dprintf("read %d entries\n", n);
+            else {
+                eprintf("ERROR: '%s': %s\n", optarg, strerror(errno));
+                ++err;
+            }
             break;
         case 'p':
             cfg.db_prune = 1;