OSSP CVS Repository

ossp - Check-in [1093]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 1093
Date: 2001-Oct-06 18:01:48 (local)
2001-Oct-06 16:01:48 (UTC)
User:rse
Branch:
Comment: Add sa_error() function.
Tickets:
Inspections:
Files:
ossp-pkg/sa/sa.c      1.8 -> 1.9     79 inserted, 0 deleted
ossp-pkg/sa/sa.h      1.9 -> 1.10     3 inserted, 0 deleted

ossp-pkg/sa/sa.c 1.8 -> 1.9

--- sa.c 2001/10/05 20:52:15     1.8
+++ sa.c 2001/10/06 16:01:48     1.9
@@ -71,6 +71,9 @@
     int              nWriteLen;   /* write buffer current length */
     int              nWriteSize;  /* write buffer current size */
     char            *cpWriteBuf;  /* write buffer memory chunk */
+    char             szError[256];
+    char             szErrorInfo[128];
+    sa_rc_t          rvErrorInfo;
 };
 
 /* boolean values */
@@ -210,6 +213,79 @@
     return chars;
 }
 
+/* remember extra error information */
+static sa_rc_t sa_errorinfo(sa_t *sa, sa_rc_t rv, const char *fmt, ...)
+{
+    va_list ap;
+
+    /* argument sanity check */
+    if (sa == NULL)
+        return SA_ERR_ARG;
+
+    if (rv == SA_OK && fmt == NULL) {
+        /* reset error information */
+        sa->szErrorInfo[0] = '\0';
+        sa->rvErrorInfo = SA_OK;
+    }
+    else {
+        /* remember error information */
+        va_start(ap, fmt);
+        sa_mvsnprintf(sa->szErrorInfo, sizeof(sa->szErrorInfo), fmt, ap);
+        sa->rvErrorInfo = rv;
+        va_end(ap);
+    }
+    return SA_OK;
+}
+
+/* return error string */
+sa_rc_t sa_error(sa_t *sa, sa_rc_t rv, char **str)
+{
+    char *sz;
+    char *cpBuf;
+    int nBuf;
+    int n;
+
+    /* argument sanity check */
+    if (sa == NULL || str == NULL)
+        return SA_ERR_ARG;
+
+    /* start at begin of buffer */
+    cpBuf = sa->szError;
+    nBuf  = sizeof(sa->szError);
+
+    /* translate result value into corresponding string */
+    if      (rv == SA_OK)      sz = "everything ok";
+    else if (rv == SA_ERR_ARG) sz = "invalid argument";
+    else if (rv == SA_ERR_USE) sz = "invalid use";
+    else if (rv == SA_ERR_MEM) sz = "no more memory available";
+    else if (rv == SA_ERR_SYS) sz = "operating system error";
+    else if (rv == SA_ERR_EOF) sz = "end of file in socket communication";
+    else if (rv == SA_ERR_INT) sz = "internal error";
+    else                       sz = "unknown error";
+    n = sa_msnprintf(cpBuf, nBuf, "%s", sz);
+    cpBuf += n;
+    nBuf  -= n;
+
+    /* optionally annotate with error information */
+    if (rv == sa->rvErrorInfo && sa->szErrorInfo[0] != '\0') {
+        n = sa_msnprintf(cpBuf, nBuf, "; %s", sa->szErrorInfo);
+        cpBuf += n;
+        nBuf  -= n;
+        sa_errorinfo(sa, SA_OK, NULL);
+    }
+
+    /* optionally annotate with operating system error information */
+    if (rv == SA_ERR_SYS) {
+        n = sa_msnprintf(cpBuf, nBuf, "; %s (%d)", strerror(errno), errno);
+        cpBuf += n;
+        nBuf  -= n;
+    }
+
+    /* return pointer to internal buffer */
+    *str = sa->szError;
+    return SA_OK;
+}
+
 /* create address object */
 sa_rc_t sa_addr_create(sa_addr_t **saap)
 {
@@ -612,6 +688,9 @@
     sa->nWriteLen         = 0;
     sa->nWriteSize        = 0;
     sa->cpWriteBuf        = NULL;
+    sa->szError[0]        = '\0';
+    sa->szErrorInfo[0]    = '\0';
+    sa->rvErrorInfo       = SA_OK;
 
     /* pass object to caller */
     *sap = sa;


ossp-pkg/sa/sa.h 1.9 -> 1.10

--- sa.h 2001/10/06 11:05:43     1.9
+++ sa.h 2001/10/06 16:01:48     1.10
@@ -102,6 +102,9 @@
     SA_ERR_INT  /* internal error */
 } sa_rc_t;
 
+/* error handling operations */
+sa_rc_t sa_error        (sa_t *sa, sa_rc_t rv, char **str);
+
 /* address object operations */
 sa_rc_t sa_addr_create  (sa_addr_t **saa);
 sa_rc_t sa_addr_destroy (sa_addr_t *saa);

CVSTrac 2.0.1