pdf-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pdf-devel] Error module: first lines


From: gerel
Subject: [pdf-devel] Error module: first lines
Date: Fri, 14 Dec 2007 18:30:00 -0300

Well, I have written a few lines. For now I'm just following the lib/error.h 
API.

Also, We could define a maximum of errors and abort in case it's exceeded.

Also, I'm not sure if "pdf_error_one_per_line" will be of use.

Suggestions are welcome, as always :-)


--- pdf-error.h ---

/* Time-stamp: <2007-12-14 17:41:34 gerel> */

#ifndef PDF_ERROR_H
#define PDF_ERROR_H

#define PDF_ERROR 1
#define PDF_OK 0

enum pdf_error_t
{
     /* foo module errros */
     PDF_EINVCHAR = 1,
     PDF_EINVSYNTAX,
     PDF_EEOF,
     /* memory module errors */
     PDF_EOUTOFMEM,
     /* general errors */
     PDF_EFATAL
};

extern const char * pdf_error_errlist [];

extern enum pdf_error_t pdf_errno;

/* Print a message with `fprintf (stderr, FORMAT, ...)';
   if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
   If STATUS is nonzero, terminate the program with `exit (STATUS)'.  */
extern void pdf_error (int status, int errnum, const char *format, ...);

extern void pdf_perror (const char *str);

 /* 
  * Exits the program with PDF_EFATAL.
  * If 'str' is not empty prints 'str' followed by the error description. 
  */
extern void pdf_abort (const char *str);

/* This variable is incremented each time `error' is called.  */
extern unsigned int pdf_error_message_count;

/* Sometimes we want to have at most one error per line.  This
   variable controls whether this mode is selected or not.  */
extern int pdf_error_one_per_line;


#endif /* PDF_ERROR_H */

--- end of pdf-error.h ---


--- pdf-error.c ---

/* Time-stamp: <2007-12-14 18:22:41 gerel> */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include <pdf-error.h>

const char * pdf_error_errlist [] =
{
        "invalid character",
        "invalid syntax",
        "end of file",
        "out of memory",
        "fatal error"
};

enum pdf_error_t pdf_errno = 0;
unsigned int pdf_error_message_count = 0;
int pdf_error_one_per_line = 0;


void
pdf_perror (const char *str)
{ 
  pdf_error (0, (int) pdf_errno, str);
}


void
pdf_abort (const char *str)
{
  pdf_error (1, (int) PDF_EFATAL, str);
}


void
pdf_error (int status, int errnum, const char *format, ...)
{
  va_list args;

  fprintf (stderr, "gnupdf: ");

  if (format != NULL)
    {
      va_start (args, format);
      vfprintf (stderr, format, args);
      fprintf (stderr, ": ");
    }
  
  if (errnum)
    fprintf (stderr, "%s", pdf_error_errlist[errnum-1]);
  
  fprintf (stderr, ".\n");
  fflush (stderr);

  pdf_error_message_count++;

  if (status)
    exit (status);
  
}

--- end of pdf-error.c ---


cheers

-gerel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]