chiark / gitweb /
WIP island determination; factor out resolve_{start,finish}!
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 14 Jun 2009 21:49:13 +0000 (22:49 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 14 Jun 2009 21:49:13 +0000 (22:49 +0100)
pctb/Makefile
pctb/ocr.c
pctb/ocr.h

index 93e5860..f798e52 100644 (file)
@@ -35,7 +35,7 @@ TARGETS= ypp-commodities
 
 all: $(TARGETS)
 
-CONVERT_OBJS= convert.o ocr.o pages.o structure.o common.o rgbimage.o
+CONVERT_OBJS= convert.o ocr.o pages.o structure.o common.o rgbimage.o resolve.o
 
 ypp-commodities: $(CONVERT_OBJS) -lnetpbm -lXtst -lX11
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
index fafdba8..927b93a 100644 (file)
@@ -75,10 +75,6 @@ struct OcrReader {
   int aresults, nresults;
 };
 
-static FILE *resolver;
-static pid_t resolver_pid;
-static int resolver_done;
-
 DEBUG_DEFINE_DEBUGF(ocr)
 
 #define FGETSLINE (dbfile_getsline(lbuf,sizeof(lbuf),__FILE__,__LINE__))
@@ -189,7 +185,7 @@ static void readdb(OcrReader *rd) {
   free(dbfname);
 }
 
-static void cu_pr_ctxmap(unsigned ctxmap) {
+static void cu_pr_ctxmap(FILE *resolver, unsigned ctxmap) {
   fprintf(resolver,"{");
   const char *spc="";
   int ctxi;
@@ -203,46 +199,25 @@ static void cu_pr_ctxmap(unsigned ctxmap) {
 
 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
                            int unk_l, int unk_r, unsigned unk_ctxmap) {
-  int jobpipe[2],donepipe[2], c,i, x,y;
+  int c,i, x,y;
   const OcrResultGlyph *s;
   const char *p;
-  char cb;
   Pixcol pv;
 
-  if (!o_resolver)
+  FILE *resolver= resolve_start();
+  if (!resolver)
     fatal("OCR failed - unrecognised characters or ligatures.\n"
          "Character set database needs to be updated or augmented.\n"
          "See README.charset.\n");
   
-  if (!resolver) {
-    sysassert(! pipe(jobpipe) );
-    sysassert(! pipe(donepipe) );
-    resolver_pid= fork();
-    sysassert(resolver_pid!=-1);
-    if (!resolver_pid) {
-      sysassert( dup2(jobpipe[0],0) ==0 );
-      sysassert(! close(jobpipe[1]) );
-      sysassert(! close(donepipe[0]) );
-      /* we know donepipe[1] is >= 4 and we have dealt with all the others
-       * so we aren't in any danger of overwriting some other fd 4: */
-      sysassert( dup2(donepipe[1],4) ==4 );
-      execlp(o_resolver, o_resolver,
-            DEBUGP(callout) ? "--debug" : "--noop-arg",
-            "--automatic-1",
-            (char*)0);
-      sysassert(!"execlp ocr-resolver failed");
-    }
-    sysassert(! close(jobpipe[0]) );
-    sysassert(! close(donepipe[1]) );
-    resolver= fdopen(jobpipe[1],"w"); sysassert(resolver);
-    resolver_done= donepipe[0];
-  }
-  fprintf(resolver,"%d %d ",unk_l,unk_r);
-  cu_pr_ctxmap(unk_ctxmap);
+  fprintf(resolver,
+         "char\n"
+         "%d %d ",unk_l,unk_r);
+  cu_pr_ctxmap(resolver,unk_ctxmap);
   for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
     if (!strcmp(s->s," ")) continue;
     fprintf(resolver," %d %d ",s->l,s->r);
-    cu_pr_ctxmap(s->ctxmap);
+    cu_pr_ctxmap(resolver,s->ctxmap);
     fprintf(resolver," ");
     for (p=s->s; (c= *p); p++) {
       if (c=='\\') fprintf(resolver,"\\%c",c);
@@ -267,28 +242,8 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
     fputs("\",\n",resolver);
   }
   fputs("};\n",resolver);
-  sysassert(!ferror(resolver));
-  sysassert(!fflush(resolver));
-
-  sysassert(resolver);
-
-  int r;
-  for (;;) {
-    r= read(resolver_done,&cb,1);
-    if (r==-1) { sysassert(errno==EINTR); continue; }
-    break;
-  }
-
-  if (r==0) {
-    waitpid_check_exitstatus(resolver_pid, "character resolver");
-    fclose(resolver);
-    close(resolver_done);
-    resolver= 0;
-  } else {
-    assert(r==1);
-    sysassert(cb==0);
-  }
 
+  resolve_finish();
   readdb(rd);
 }
 
index 943c210..97a5c66 100644 (file)
@@ -56,7 +56,11 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType, int w, Pixcol cols[]);
    * array is valid until next call to ocr()
    */
 
+
 extern const char *o_resolver;
 
+FILE *resolve_start(void);
+void resolve_finish(void);
+
 
 #endif /*OCR_H*/