Fixed error for MARC field, which has fixed lenght.
[idzebra-moved-to-github.git] / recctrl / perlread.c
index 152beeb..f21d0c3 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: perlread.c,v 1.2 2002-11-15 22:01:42 adam Exp $
+/* $Id: perlread.c,v 1.7 2003-02-28 16:20:10 pop Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
    Index Data Aps
 
@@ -21,7 +21,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 */
 
 #if HAVE_PERL
-#define PERL_IMPLICIT_CONTEXT     
 #include "perlread.h"
 #include "EXTERN.h"
 #include "perl.h"
@@ -71,7 +70,7 @@ void Filter_create (struct perl_context *context)
   sv_setref_pv(msv, "_p_perl_context", (void*)context);
   XPUSHs(msv) ;
   PUTBACK ;
-  call_method("new", 0);
+  call_method("new", G_EVAL);
 
   SPAGAIN ;
   context->filterRef = POPs;
@@ -93,6 +92,9 @@ int Filter_process (struct perl_context *context)
 
   dSP;
 
+  ENTER;
+  SAVETMPS;
+
   PUSHMARK(SP) ;
   XPUSHs(context->filterRef);
   PUTBACK ;
@@ -100,6 +102,10 @@ int Filter_process (struct perl_context *context)
   SPAGAIN ;
   res = POPi;
   PUTBACK ;
+
+  FREETMPS;
+  LEAVE;
+
   return (res);
 }
 
@@ -139,6 +145,10 @@ int Filter_process (struct perl_context *context)
 */
 void Filter_store_buff (struct perl_context *context, char *buff, size_t len) {
   dSP;
+
+  ENTER;
+  SAVETMPS;
+
   PUSHMARK(SP) ;
   XPUSHs(context->filterRef);
   XPUSHs(sv_2mortal(newSVpv(buff, len)));  
@@ -146,6 +156,9 @@ void Filter_store_buff (struct perl_context *context, char *buff, size_t len) {
   call_method("_store_buff", 0);
   SPAGAIN ;
   PUTBACK ;
+
+  FREETMPS;
+  LEAVE;
 }
 /*  The "file" manipulation function wrappers */
 int grs_perl_readf(struct perl_context *context, size_t len) {
@@ -157,6 +170,33 @@ int grs_perl_readf(struct perl_context *context, size_t len) {
   return (r);
 }
 
+int grs_perl_readline(struct perl_context *context) {
+  int r;
+  char *buf = (char *) xmalloc (4096);
+  char *p = buf;
+  
+  while ((r = (*context->readf)(context->fh,p,1)) && (p-buf < 4095)) {
+    p++;
+    if (*(p-1) == 10) break;
+  }
+
+  *p = 0;
+
+  if (p != buf) Filter_store_buff (context, buf, p - buf);
+  xfree (buf);
+  return (p - buf);
+}
+
+char grs_perl_getc(struct perl_context *context) {
+  int r;
+  char *p;
+  if (r = (*context->readf)(context->fh,p,1)) {
+    return (*p);
+  } else {
+    return (0);
+  }
+}
+
 off_t grs_perl_seekf(struct perl_context *context, off_t offset) {
   return ((*context->seekf)(context->fh, offset));
 }
@@ -170,12 +210,12 @@ void grs_perl_endf(struct perl_context *context, off_t offset) {
 }
 
 /* Get pointers from the context. Easyer to wrap this by SWIG */
-data1_handle grs_perl_get_dh(struct perl_context *context) {
-  return(context->dh);
+data1_handle *grs_perl_get_dh(struct perl_context *context) {
+  return(&context->dh);
 }
 
-NMEM grs_perl_get_mem(struct perl_context *context) {
-  return(context->mem);
+NMEM *grs_perl_get_mem(struct perl_context *context) {
+  return(&context->mem);
 }
 
 /* Set the result in the context */
@@ -210,8 +250,10 @@ void grs_destroy_perl(void *clientData)
 
   logf (LOG_LOG, "Destroying perl interpreter context");
   if (context->perli_ready) {
+    /*
     FREETMPS;
     LEAVE;
+    */
     if (context->origi == NULL)  perl_destruct(context->perli);
    }
   if (context->origi == NULL) perl_free(context->perli);
@@ -243,13 +285,18 @@ static data1_node *grs_read_perl (struct grs_read_info *p)
     char *arglist[6] = { "", "-I", "", "-M", "-e", "" };
 
     if (context->perli_ready) {
+      /*
       FREETMPS;
       LEAVE;
+      */
       if (context->origi == NULL) perl_destruct(context->perli);
     }
     if (context->origi == NULL) perl_construct(context->perli);
+    
+    /*
     ENTER;
     SAVETMPS;
+    */
     context->perli_ready = 1;
 
     /* parse, and run the init call */
@@ -274,8 +321,15 @@ static data1_node *grs_read_perl (struct grs_read_info *p)
 
   /* Wow... if calling with individual update_record calls from perl,
      the filter object reference may go out of scope... */
-  if (!SvOK(context->filterRef)) Filter_create(context);
+  if (!sv_isa(context->filterRef, context->filterClass)) {
+    Filter_create(context);
+    logf (LOG_DEBUG,"Filter recreated");
+  }
 
+  if (!SvTRUE(context->filterRef)) {
+    logf (LOG_WARN,"Failed to initialize perl filter %s",context->filterClass);
+    return (0);
+  }
 
   /* call the process method */
   Filter_process(context);