chiark
/
gitweb
/
~yarrgweb
/
ypp-sc-tools.db-test.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Much better error handling.
[ypp-sc-tools.db-test.git]
/
pctb
/
ocr.c
diff --git
a/pctb/ocr.c
b/pctb/ocr.c
index 712f90df77c21e239b8f47c256564aae512acaf4..314a8294bea8b9c7db56bda9d6d5fa259159a7bc 100644
(file)
--- a/
pctb/ocr.c
+++ b/
pctb/ocr.c
@@
-40,11
+40,19
@@
static int resolver_done;
DEBUG_DEFINE_DEBUGF(ocr)
DEBUG_DEFINE_DEBUGF(ocr)
+#define dbassert(x) \
+ ((x) ? (void)0 : \
+ fatal("Error in character set database.\n" \
+ " Requirement not met: %s:%d: %s", __FILE__,__LINE__, #x))
+
static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
+ errno=0;
char *s= fgets(lbuf,lbufsz,f);
char *s= fgets(lbuf,lbufsz,f);
- eassert(s);
+ sysassert(!ferror(f));
+ dbassert(!feof(f));
+ assert(s);
int l= strlen(lbuf);
int l= strlen(lbuf);
-
eassert(l>0); e
assert(lbuf[--l]='\n');
+
dbassert(l>0); db
assert(lbuf[--l]='\n');
lbuf[l]= 0;
}
#define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf)))
lbuf[l]= 0;
}
#define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf)))
@@
-71,21
+79,21
@@
static void readdb(OcrReader *rd) {
char *dbfname=0;
asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
char *dbfname=0;
asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
-
e
assert(dbfname);
+
sys
assert(dbfname);
db= fopen(dbfname,"r");
free(dbfname);
if (!db) {
db= fopen(dbfname,"r");
free(dbfname);
if (!db) {
-
e
assert(errno==ENOENT);
+
sys
assert(errno==ENOENT);
return;
}
FGETSLINE(db,lbuf);
return;
}
FGETSLINE(db,lbuf);
-
e
assert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
+
db
assert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
r= fscanf(db, "%d", &h);
r= fscanf(db, "%d", &h);
-
e
assert(r==1);
-
e
assert(h==rd->h);
+
db
assert(r==1);
+
db
assert(h==rd->h);
for (;;) {
FGETSLINE(db,lbuf);
for (;;) {
FGETSLINE(db,lbuf);
@@
-101,13
+109,13
@@
static void readdb(OcrReader *rd) {
found_ctx:
for (nchrs=0;;) {
found_ctx:
for (nchrs=0;;) {
- int c= fgetc(db);
e
assert(c!=EOF);
- if (c=='\n') {
e
assert(nchrs); break; }
-
e
assert(nchrs<MAXGLYPHCHRS);
+ int c= fgetc(db);
sysassert(!ferror(db)); db
assert(c!=EOF);
+ if (c=='\n') {
db
assert(nchrs); break; }
+
db
assert(nchrs<MAXGLYPHCHRS);
if (c=='\\') {
unsigned cr;
if (c=='\\') {
unsigned cr;
- c= fgetc(db);
e
assert(c=='x');
- r= fscanf(db, "%2x", &cr);
e
assert(r==1);
+ c= fgetc(db);
sysassert(!ferror(db)); db
assert(c=='x');
+ r= fscanf(db, "%2x", &cr);
sysassert(!ferror(db)); db
assert(r==1);
assert(cr>0 && cr<=255);
c= cr;
}
assert(cr>0 && cr<=255);
c= cr;
}
@@
-123,10
+131,10
@@
static void readdb(OcrReader *rd) {
current= &rd->contexts[ctxi];
for (;;) {
FGETSLINE(db,lbuf);
current= &rd->contexts[ctxi];
for (;;) {
FGETSLINE(db,lbuf);
- if (!lbuf[0]) {
e
assert(current != &rd->contexts[ctxi]); break; }
+ if (!lbuf[0]) {
db
assert(current != &rd->contexts[ctxi]); break; }
char *ep;
char *ep;
- cv= strtoul(lbuf,&ep,16);
e
assert(!*ep);
-
e
assert(!(cv & ~((1UL << rd->h)-1)));
+ cv= strtoul(lbuf,&ep,16);
db
assert(!*ep);
+
db
assert(!(cv & ~((1UL << rd->h)-1)));
for (j=0; j<current->nlinks; j++)
if (current->links[j].col == cv) {
for (j=0; j<current->nlinks; j++)
if (current->links[j].col == cv) {
@@
-134,16
+142,15
@@
static void readdb(OcrReader *rd) {
goto found_link;
}
goto found_link;
}
- additional= m
alloc(sizeof(*additional)); eassert(additional
);
+ additional= m
malloc(sizeof(*additional)
);
additional->s[0]= 0;
additional->nlinks= additional->alinks= 0;
additional->links= 0;
if (current->nlinks==current->alinks) {
current->alinks++;
current->alinks<<=1;
additional->s[0]= 0;
additional->nlinks= additional->alinks= 0;
additional->links= 0;
if (current->nlinks==current->alinks) {
current->alinks++;
current->alinks<<=1;
- current->links= realloc(current->links,
- sizeof(*current->links) * current->alinks);
- eassert(current->links);
+ current->links= mrealloc(current->links,
+ sizeof(*current->links) * current->alinks);
}
current->links[current->nlinks].col= cv;
current->links[current->nlinks].then= additional;
}
current->links[current->nlinks].col= cv;
current->links[current->nlinks].then= additional;
@@
-153,12
+160,12
@@
static void readdb(OcrReader *rd) {
found_link:;
}
found_link:;
}
-
e
assert(!current->s[0]);
+
db
assert(!current->s[0]);
strcpy(current->s, chrs);
current->endsword= endsword;
}
strcpy(current->s, chrs);
current->endsword= endsword;
}
-
e
assert(!ferror(db));
-
e
assert(!fclose(db));
+
sys
assert(!ferror(db));
+
sys
assert(!fclose(db));
}
static void cu_pr_ctxmap(unsigned ctxmap) {
}
static void cu_pr_ctxmap(unsigned ctxmap) {
@@
-175,33
+182,33
@@
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) {
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,
r,
i, x,y;
+ int jobpipe[2],donepipe[2], c,i, x,y;
const OcrResultGlyph *s;
const char *p;
char cb;
Pixcol pv;
if (!resolver) {
const OcrResultGlyph *s;
const char *p;
char cb;
Pixcol pv;
if (!resolver) {
-
r= pipe(jobpipe); eassert(!r
);
-
r= pipe(donepipe); eassert(!r
);
+
sysassert(! pipe(jobpipe)
);
+
sysassert(! pipe(donepipe)
);
resolver_pid= fork();
resolver_pid= fork();
-
e
assert(resolver_pid!=-1);
+
sys
assert(resolver_pid!=-1);
if (!resolver_pid) {
if (!resolver_pid) {
-
r= dup2(jobpipe[0],0); eassert(r==0
);
-
r= close(jobpipe[1]); eassert(!r
);
-
r= close(donepipe[0]); eassert(!r
);
+
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: */
/* 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: */
-
r= dup2(donepipe[1],4); eassert(r==4
);
+
sysassert( dup2(donepipe[1],4) ==4
);
execlp("./show-thing.tcl", "./show-thing.tcl",
DEBUGP(callout) ? "--debug" : "--noop-arg",
"--automatic-1",
(char*)0);
execlp("./show-thing.tcl", "./show-thing.tcl",
DEBUGP(callout) ? "--debug" : "--noop-arg",
"--automatic-1",
(char*)0);
-
e
assert(!"execlp failed");
+
sys
assert(!"execlp failed");
}
}
-
r= close(jobpipe[0]); eassert(!r
);
-
r= close(donepipe[1]); eassert(!r
);
- resolver= fdopen(jobpipe[1],"w");
e
assert(resolver);
+
sysassert(! close(jobpipe[0])
);
+
sysassert(! close(donepipe[1])
);
+ resolver= fdopen(jobpipe[1],"w");
sys
assert(resolver);
resolver_done= donepipe[0];
}
fprintf(resolver,"%d %d ",unk_l,unk_r);
resolver_done= donepipe[0];
}
fprintf(resolver,"%d %d ",unk_l,unk_r);
@@
-234,38
+241,43
@@
static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
fputs("\",\n",resolver);
}
fputs("};\n",resolver);
fputs("\",\n",resolver);
}
fputs("};\n",resolver);
-
e
assert(!ferror(resolver));
-
e
assert(!fflush(resolver));
+
sys
assert(!ferror(resolver));
+
sys
assert(!fflush(resolver));
-
e
assert(resolver);
+
sys
assert(resolver);
+ int r;
for (;;) {
r= read(resolver_done,&cb,1);
for (;;) {
r= read(resolver_done,&cb,1);
- if (r==-1) {
e
assert(errno==EINTR); continue; }
+ if (r==-1) {
sys
assert(errno==EINTR); continue; }
break;
}
if (r==0) {
pid_t pid;
break;
}
if (r==0) {
pid_t pid;
+ int st;
for (;;) {
for (;;) {
- pid= waitpid(resolver_pid, &
r
, 0);
- if (pid==-1) {
e
assert(errno==EINTR); continue; }
+ pid= waitpid(resolver_pid, &
st
, 0);
+ if (pid==-1) {
sys
assert(errno==EINTR); continue; }
break;
}
break;
}
- eassert(pid==resolver_pid);
- if (WIFEXITED(r)) {
- eassert(!WEXITSTATUS(r));
+ 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;
fclose(resolver);
close(resolver_done);
resolver= 0;
- } else if (WIFSIGNALED(r)) {
- eassert(!"resolver child died due to signal");
+ } else if (WIFSIGNALED(st)) {
+ fatal("character resolver died due to signal %s%s",
+ strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
} else {
} else {
-
eassert(!"weird wait status"
);
+
fatal("character resolver gave strange wait status %d",st
);
}
} else {
}
} else {
-
e
assert(r==1);
-
e
assert(cb==0);
+ assert(r==1);
+
sys
assert(cb==0);
}
readdb(rd);
}
readdb(rd);
@@
-275,8
+287,7
@@
static void add_result(OcrReader *rd, const char *s, int l, int r,
unsigned ctxmap) {
if (rd->nresults >= rd->aresults) {
rd->aresults++; rd->aresults<<=1;
unsigned ctxmap) {
if (rd->nresults >= rd->aresults) {
rd->aresults++; rd->aresults<<=1;
- rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
- eassert(rd->results);
+ rd->results= mrealloc(rd->results, sizeof(*rd->results)*rd->aresults);
}
rd->results[rd->nresults].s= s;
rd->results[rd->nresults].l= l;
}
rd->results[rd->nresults].s= s;
rd->results[rd->nresults].l= l;
@@
-287,16
+298,21
@@
static void add_result(OcrReader *rd, const char *s, int l, int r,
struct OcrCellTypeInfo {
unsigned initial, nextword, midword;
struct OcrCellTypeInfo {
unsigned initial, nextword, midword;
+ const char *name;
};
const struct OcrCellTypeInfo ocr_celltype_number= {
};
const struct OcrCellTypeInfo ocr_celltype_number= {
- 4,4,4
+ 4,4,4,
+ .name= "number"
};
const struct OcrCellTypeInfo ocr_celltype_text= {
};
const struct OcrCellTypeInfo ocr_celltype_text= {
- .initial=2 /* Uppercase */,
- .nextword=3 /* Either */,
- .midword=1 /* Lower only */
+ .initial=2, /* Uppercase */
+ .nextword=3, /* Either */
+ .midword=1, /* Lower only */
+ .name= "text"
};
};
+const char *ocr_celltype_name(OcrCellType ct) { return ct->name; }
+
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
int nspaces;
unsigned ctxmap;
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
int nspaces;
unsigned ctxmap;
@@
-419,7
+435,7
@@
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
OcrReader *ocr_init(int h) {
OcrReader *rd;
OcrReader *ocr_init(int h) {
OcrReader *rd;
- rd= m
alloc(sizeof(*rd)); eassert(rd
);
+ rd= m
malloc(sizeof(*rd)
);
memset(rd,0,sizeof(*rd));
rd->h= h;
readdb(rd);
memset(rd,0,sizeof(*rd));
rd->h= h;
readdb(rd);