chiark / gitweb /
Output processing.
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 7 Jun 2009 19:18:38 +0000 (20:18 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 7 Jun 2009 19:18:38 +0000 (20:18 +0100)
pctb/common.h
pctb/convert.c
pctb/convert.h
pctb/ocr.c
pctb/structure.c
pctb/yppsc-commod-processor

index 73e55aea43378e68ea143a08d7d4d0decc1b5029..f942c72a2ffad9e911bbf77500a42b1905884c4c 100644 (file)
@@ -110,6 +110,8 @@ void fatal(const char *fmt, ...)       FMT(1,2) NORET;
 void sysassert_fail(const char *file, int line, const char *what)
    __attribute__((noreturn));
 
 void sysassert_fail(const char *file, int line, const char *what)
    __attribute__((noreturn));
 
+void waitpid_check_exitstatus(pid_t pid, const char *what);
+
 
 void *mmalloc(size_t sz);
 void *mrealloc(void *p, size_t sz);
 
 void *mmalloc(size_t sz);
 void *mrealloc(void *p, size_t sz);
index bac41d90ef37b1e0229d4ad98644318f9c973fe4..291647e79f4c3930fec1d9ae7c9ccb45718f4695 100644 (file)
@@ -49,17 +49,19 @@ static enum {
 
 static char *o_screenshots_fn;
 static int o_single_page, o_quiet;
 
 static char *o_screenshots_fn;
 static int o_single_page, o_quiet;
+static const char *o_outputmode= "upload";
 
 FILE *screenshots_file;
 
 
 
 FILE *screenshots_file;
 
 
-static void badusage(const char *fmt, ...)
-     __attribute__((format(printf,1,2),noreturn));
-static void badusage(const char *fmt, ...) {
-  va_list al;
-  va_start(al,fmt);
+static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
+static void vbadusage(const char *fmt, va_list al) {
+  fputs("bad usage: ",stderr);
+  vfprintf(stderr,fmt,al);
+  fputc('\n',stderr);
   exit(12);
 }
   exit(12);
 }
+DEFINE_VWRAPPERF(static, badusage, NORET);
 
 static void open_screenshots_file(const char *mode) {
   screenshots_file= fopen(o_screenshots_fn, mode);
 
 static void open_screenshots_file(const char *mode) {
   screenshots_file= fopen(o_screenshots_fn, mode);
@@ -68,6 +70,31 @@ static void open_screenshots_file(const char *mode) {
          o_screenshots_fn, strerror(errno));
 }
 
          o_screenshots_fn, strerror(errno));
 }
 
+static void run_analysis(void) {
+  FILE *tf;
+
+  sysassert( tf= tmpfile() );
+  progress("running recognition...");
+  analyse(tf);
+
+  sysassert( fseek(tf,0,SEEK_SET) == 0);
+
+  progress_log("processing results (--%s)...", o_outputmode);
+  pid_t processor;
+  sysassert( (processor= fork()) != -1 );
+
+  if (!processor) {
+    sysassert( dup2(fileno(tf),0) ==0 );
+    execlp("./yppsc-commod-processor", "yppsc-commod-processor",
+          o_outputmode, (char*)0);
+    sysassert(!"execlp commod-processor failed");
+  }
+
+  waitpid_check_exitstatus(processor, "output processor/uploader");
+  fclose(tf);
+  progress_log("all complete.");
+}  
+
 int main(int argc, char **argv) {
   const char *arg;
   int r;
 int main(int argc, char **argv) {
   const char *arg;
   int r;
@@ -80,12 +107,18 @@ int main(int argc, char **argv) {
       o_mode= mode_findwindow;
     else if (!strcmp(arg,"--screenshot-only"))
       o_mode= mode_screenshot;
       o_mode= mode_findwindow;
     else if (!strcmp(arg,"--screenshot-only"))
       o_mode= mode_screenshot;
-    else if (!strcmp(arg,"--analyse-only"))
+    else if (!strcmp(arg,"--analyse-only") ||
+            !strcmp(arg,"--same"))
       o_mode= mode_analyse;
     else if (!strcmp(arg,"--single-page"))
       o_single_page= 1;
     else if (!strcmp(arg,"--quiet"))
       o_quiet= 1;
       o_mode= mode_analyse;
     else if (!strcmp(arg,"--single-page"))
       o_single_page= 1;
     else if (!strcmp(arg,"--quiet"))
       o_quiet= 1;
+    else if (!strcmp(arg,"--upload") ||
+            !strcmp(arg,"--arbitrage") ||
+            !strcmp(arg,"--tsv") ||
+            !strcmp(arg,"--best-prices"))
+      o_outputmode= arg+2;
     else if (!strcmp(arg,"--screenshots-file"))
       o_screenshots_fn= ARGVAL;
 #define DF(f)                                  \
     else if (!strcmp(arg,"--screenshots-file"))
       o_screenshots_fn= ARGVAL;
 #define DF(f)                                  \
@@ -122,8 +155,7 @@ int main(int argc, char **argv) {
     else read_screenshots();
   }
   if (o_mode & mf_analyse) {
     else read_screenshots();
   }
   if (o_mode & mf_analyse) {
-    analyse();
-    //output_tsv();
+    run_analysis();
   }
   return 0;
 }
   }
   return 0;
 }
@@ -224,3 +256,25 @@ void *mrealloc(void *p, size_t sz) {
   sysassert( r= realloc(p,sz) );
   return r;
 }
   sysassert( r= realloc(p,sz) );
   return r;
 }
+
+void waitpid_check_exitstatus(pid_t pid, const char *what) { 
+  pid_t got;
+  int st;
+  for (;;) {
+    got= waitpid(pid, &st, 0);
+    if (pid==-1) { sysassert(errno==EINTR); continue; }
+    break;
+  }
+  sysassert( got==pid );
+
+  if (WIFEXITED(st)) {
+    if (WEXITSTATUS(st))
+      fatal("%s failed with nonzero exit status %d",
+           what, WEXITSTATUS(st));
+  } else if (WIFSIGNALED(st)) {
+    fatal("%s died due to signal %s%s", what,
+         strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
+  } else {
+    fatal("%s gave strange wait status %d", what, st);
+  }
+}
index 15ec06d1ad7ce4217418e2754ea487bc94fe7955..90e5e243acc737c580c284063a881636d2b8d5a1 100644 (file)
@@ -43,7 +43,7 @@
 void find_structure(CanonImage *im);
 void read_screenshots(void);
 void read_one_screenshot(void);
 void find_structure(CanonImage *im);
 void read_screenshots(void);
 void read_one_screenshot(void);
-void analyse(void);
+void analyse(FILE *tsv_output);
 
 /*----- from convert.c -----*/
 
 
 /*----- from convert.c -----*/
 
index d12218f39fd2953eef1f892c9bb7fbd360e3b64c..6a91935769ecfc7da0e70681ea80db627c49327e 100644 (file)
@@ -245,7 +245,7 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
             DEBUGP(callout) ? "--debug" : "--noop-arg",
             "--automatic-1",
             (char*)0);
             DEBUGP(callout) ? "--debug" : "--noop-arg",
             "--automatic-1",
             (char*)0);
-      sysassert(!"execlp failed");
+      sysassert(!"execlp ocr-resolver failed");
     }
     sysassert(! close(jobpipe[0]) );
     sysassert(! close(donepipe[1]) );
     }
     sysassert(! close(jobpipe[0]) );
     sysassert(! close(donepipe[1]) );
@@ -295,27 +295,10 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
   }
 
   if (r==0) {
   }
 
   if (r==0) {
-    pid_t pid;
-    int st;
-    for (;;) {
-      pid= waitpid(resolver_pid, &st, 0);
-      if (pid==-1) { sysassert(errno==EINTR); continue; }
-      break;
-    }
-    sysassert(pid==resolver_pid);
-    if (WIFEXITED(st)) {
-      if (WEXITSTATUS(st))
-       fatal("character resolver failed with nonzero exit status %d",
-             WEXITSTATUS(st));
-      fclose(resolver);
-      close(resolver_done);
-      resolver= 0;
-    } else if (WIFSIGNALED(st)) {
-      fatal("character resolver died due to signal %s%s",
-           strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
-    } else {
-      fatal("character resolver gave strange wait status %d",st);
-    }
+    waitpid_check_exitstatus(resolver_pid, "character resolver");
+    fclose(resolver);
+    close(resolver_done);
+    resolver= 0;
   } else {
     assert(r==1);
     sysassert(cb==0);
   } else {
     assert(r==1);
     sysassert(cb==0);
index 8919e0b07e9f69a909819d381c6faa0537f579e0..8141feb3db461c60099841db25d3a4954d018dec 100644 (file)
@@ -339,7 +339,7 @@ static void find_table_entry(Rect commod, int colno, Rect *cellr) {
   require_rectangle_r(*cellr, " o");
 }
 
   require_rectangle_r(*cellr, " o");
 }
 
-static void ocr_rectangle(Rect r, const OcrCellType ct) {
+static void ocr_rectangle(Rect r, const OcrCellType ct, FILE *tsv_output) {
   OcrResultGlyph *results, *res;
 
   int w= r.br.x - r.tl.x + 1;
   OcrResultGlyph *results, *res;
 
   int w= r.br.x - r.tl.x + 1;
@@ -364,10 +364,10 @@ static void ocr_rectangle(Rect r, const OcrCellType ct) {
 
   results= ocr(rd,ct,w,cols);
   for (res=results; res->s; res++)
 
   results= ocr(rd,ct,w,cols);
   for (res=results; res->s; res++)
-    printf("%s",res->s);
+    fputs(res->s,tsv_output);
 }
 
 }
 
-void analyse(void) {
+void analyse(FILE *tsv_output) {
   Rect thisr, entryr;
   int page, tryrect, colno;
 
   Rect thisr, entryr;
   int page, tryrect, colno;
 
@@ -386,16 +386,17 @@ void analyse(void) {
       const char *tab= "";
       for (colno=0; colno<columns; colno++) {
        find_table_entry(thisr,colno,&entryr);
       const char *tab= "";
       for (colno=0; colno<columns; colno++) {
        find_table_entry(thisr,colno,&entryr);
-       fputs(tab, stdout);
+       fputs(tab, tsv_output);
        ocr_rectangle(entryr,
                      colno<TEXT_COLUMNS
                      ? &ocr_celltype_text
        ocr_rectangle(entryr,
                      colno<TEXT_COLUMNS
                      ? &ocr_celltype_text
-                     : &ocr_celltype_number);
+                     : &ocr_celltype_number,
+                     tsv_output);
        tab= "\t";
       }
        tab= "\t";
       }
-      fputs("\n", stdout);
-      sysassert(!ferror(stdout));
-      sysassert(!fflush(stdout));
+      fputs("\n", tsv_output);
+      sysassert(!ferror(tsv_output));
+      sysassert(!fflush(tsv_output));
     }
   }
 }
     }
   }
 }
index 981ae92d986a6426d92d250440cef1186b64a8ac..9bb0d71fb1229972fdacfa2c172d2056784e95ac 100755 (executable)
@@ -95,7 +95,7 @@ sub main__arbitrage () {
     main__bestprice();
 }
 
     main__bestprice();
 }
 
-sub main__bestprice () {
+sub main__bestprices () {
     foreach $commod (sort keys %commod) {
        $current= $commod{$commod};
        my $buys=  bs_p($commod,Buy, -1);
     foreach $commod (sort keys %commod) {
        $current= $commod{$commod};
        my $buys=  bs_p($commod,Buy, -1);
@@ -137,8 +137,9 @@ sub main__tsv () {
 }
 
 sub main__upload () {
 }
 
 sub main__upload () {
-    die "Uploading not yet implemented, sorry\n.";
+    die "\nUploading not yet implemented, sorry.\n";
 }
 
 }
 
+$mode =~ s/\-//;
 &{"main__$mode"};
 close(STDOUT) or die $!;
 &{"main__$mode"};
 close(STDOUT) or die $!;