"Fossies" - the Fresh Open Source Software Archive

Member "udunits-2.2.28/lib/error.c" (7 Dec 2020, 2491 Bytes) of package /linux/privat/udunits-2.2.28.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "error.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.2.26_vs_2.2.28.

    1 /*
    2  * Copyright 2020 University Corporation for Atmospheric Research
    3  *
    4  * This file is part of the UDUNITS-2 package.  See the file COPYRIGHT
    5  * in the top-level source-directory of the package for copying and
    6  * redistribution conditions.
    7  */
    8 /*
    9  * This module is thread-compatible but not thread-safe.  Multi-threaded
   10  * access must be externally synchronized.
   11  */
   12 
   13 /*LINTLIBRARY*/
   14 
   15 #include "config.h"
   16 
   17 #include "udunits2.h"
   18 
   19 #include <stdarg.h>
   20 #include <stdio.h>
   21 
   22 
   23 /*
   24  * Writes an error-message to the standard-error stream when received and
   25  * appends a newline.  This is the initial error-message handler.
   26  *
   27  * Arguments:
   28  *  fmt The format for the error-message.
   29  *  args    The arguments of "fmt".
   30  * Returns:
   31  *  <0  A output error was encountered.  See "errno".
   32  *  else    The number of bytes of "fmt" and "arg" written excluding any
   33  *      terminating NUL.
   34  */
   35 int
   36 ut_write_to_stderr(
   37     const char* const   fmt,
   38     va_list     args)
   39 {
   40     int nbytes = vfprintf(stderr, fmt, args);
   41 
   42     (void)fputc('\n', stderr);
   43 
   44     return nbytes;
   45 }
   46 
   47 
   48 /*
   49  * Does nothing with an error-message.
   50  *
   51  * Arguments:
   52  *  fmt The format for the error-message.
   53  *  args    The arguments of "fmt".
   54  * Returns:
   55  *  0   Always.
   56  */
   57 int
   58 ut_ignore(
   59     const char* const   fmt,
   60     va_list     args)
   61 {
   62     return 0;
   63 }
   64 
   65 
   66 static ut_error_message_handler errorMessageHandler = ut_write_to_stderr;
   67 
   68 
   69 /*
   70  * Returns the previously-installed error-message handler and optionally
   71  * installs a new handler.  The initial handler is "ut_write_to_stderr()".
   72  *
   73  * Arguments:
   74  *      handler     NULL or pointer to the error-message handler.  If NULL,
   75  *          then the handler is not changed.  The
   76  *          currently-installed handler can be obtained this way.
   77  * Returns:
   78  *  Pointer to the previously-installed error-message handler.
   79  */
   80 ut_error_message_handler
   81 ut_set_error_message_handler(
   82     ut_error_message_handler    handler)
   83 {
   84     ut_error_message_handler    prev = errorMessageHandler;
   85 
   86     if (handler != NULL)
   87     errorMessageHandler = handler;
   88 
   89     return prev;
   90 }
   91 
   92 
   93 /*
   94  * Handles an error-message.
   95  *
   96  * Arguments:
   97  *  fmt The format for the error-message.
   98  *  ... The arguments for "fmt".
   99  * Returns:
  100  *  <0  An output error was encountered.
  101  *  else    The number of bytes of "fmt" and "arg" written excluding any
  102  *      terminating NUL.
  103  */
  104 int
  105 ut_handle_error_message(
  106     const char* const   fmt,
  107     ...)
  108 {
  109     int         nbytes;
  110     va_list     args;
  111 
  112     va_start(args, fmt);
  113 
  114     nbytes = errorMessageHandler(fmt, args);
  115 
  116     va_end(args);
  117 
  118     return nbytes;
  119 }