"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/xml.c" between
udunits-2.2.26.tar.gz and udunits-2.2.28.tar.gz

About: UDUNITS supports units of physical quantities. Its C library provides for arithmetic manipulation of units and for conversion of numeric values between compatible units.

xml.c  (udunits-2.2.26):xml.c  (udunits-2.2.28)
/* /*
* Copyright 2013 University Corporation for Atmospheric Research * Copyright 2020 University Corporation for Atmospheric Research
* *
* This file is part of the UDUNITS-2 package. See the file COPYRIGHT * This file is part of the UDUNITS-2 package. See the file COPYRIGHT
* in the top-level source-directory of the package for copying and * in the top-level source-directory of the package for copying and
* redistribution conditions. * redistribution conditions.
*/ */
/* /*
* This module is thread-compatible but not thread-safe. Multi-threaded * This module is thread-compatible but not thread-safe. Multi-threaded
* access must be externally synchronized. * access must be externally synchronized.
*/ */
/*LINTLIBRARY*/ /*LINTLIBRARY*/
#include <config.h> #include <config.h>
#ifndef _XOPEN_SOURCE #include "udunits2.h"
# define _XOPEN_SOURCE 500
#if defined(__linux__)
# ifndef _GNU_SOURCE
# define _GNU_SOURCE
# endif
#endif #endif
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#ifndef _MSC_VER #ifndef _MSC_VER
#include <libgen.h> #include <libgen.h>
#endif #endif
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef _MSC_VER #ifndef _MSC_VER
#include <strings.h> #include <strings.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#if defined(__linux__)
#include <dlfcn.h>
#elif defined(__APPLE__)
#define _DARWIN_C_SOURCE
#include <dlfcn.h>
#elif defined _WIN32
#include <windows.h>
#endif
#include <expat.h>
#ifndef DLL_UDUNITS2 #ifndef DLL_UDUNITS2
#define XML_STATIC # define XML_STATIC
#endif #endif
#include "expat.h"
#include "udunits2.h"
#ifndef _XOPEN_PATH_MAX #ifndef PATH_MAX
# define _XOPEN_PATH_MAX 1024 // Includes terminating NUL # define PATH_MAX 4096 // Includes terminating NUL
#endif #endif
#define NAME_SIZE 128 #define NAME_SIZE 128
#define ACCUMULATE_TEXT \ #define ACCUMULATE_TEXT \
XML_SetCharacterDataHandler(currFile->parser, accumulateText) XML_SetCharacterDataHandler(currFile->parser, accumulateText)
#define IGNORE_TEXT \ #define IGNORE_TEXT \
XML_SetCharacterDataHandler(currFile->parser, NULL) XML_SetCharacterDataHandler(currFile->parser, NULL)
typedef enum { typedef enum {
START, START,
skipping to change at line 559 skipping to change at line 570
} }
return success; return success;
} }
/* /*
* Maps a unit to identifiers. * Maps a unit to identifiers.
* *
* Arguments: * Arguments:
* unit Pointer to the unit. * unit Pointer to the unit.
* id Pointer to the identifier upon wich to base all * id Pointer to the identifier upon which to base all
* derived identifiers. * derived identifiers.
* encoding The encoding of "id". * encoding The encoding of "id".
* isName Whether or not "id" is a name. * isName Whether or not "id" is a name.
* Returns: * Returns:
* 0 Failure. * 0 Failure.
* else Success. * else Success.
*/ */
static int static int
mapUnitToIds( mapUnitToIds(
ut_unit* const unit, ut_unit* const unit,
skipping to change at line 601 skipping to change at line 612
} }
return success; return success;
} }
/* /*
* Maps a unit to a name and all derivatives of the name. * Maps a unit to a name and all derivatives of the name.
* *
* Arguments: * Arguments:
* unit Pointer to the unit. * unit Pointer to the unit.
* name Pointer to the name upon wich to base all derived names. * name Pointer to the name upon which to base all derived names .
* encoding The encoding of "name". * encoding The encoding of "name".
* Returns: * Returns:
* 0 Failure. * 0 Failure.
* else Success. * else Success.
*/ */
static int static int
mapUnitToNames( mapUnitToNames(
ut_unit* const unit, ut_unit* const unit,
const char* const name, const char* const name,
ut_encoding encoding) ut_encoding encoding)
{ {
return mapUnitToIds(unit, name, encoding, 1); return mapUnitToIds(unit, name, encoding, 1);
} }
/* /*
* Maps a unit to a symbol and all derivatives of the symbol. * Maps a unit to a symbol and all derivatives of the symbol.
* *
* Arguments: * Arguments:
* unit Pointer to the unit. * unit Pointer to the unit.
* symbol Pointer to the symbol upon wich to base all derived * symbol Pointer to the symbol upon which to base all derived
* symbols. * symbols.
* encoding The encoding of "symbol". * encoding The encoding of "symbol".
* Returns: * Returns:
* 0 Failure. * 0 Failure.
* else Success. * else Success.
*/ */
static int static int
mapUnitToSymbols( mapUnitToSymbols(
ut_unit* const unit, ut_unit* const unit,
const char* const symbol, const char* const symbol,
skipping to change at line 1743 skipping to change at line 1754
} }
} }
/* /*
* Handles the end of an <import> element. * Handles the end of an <import> element.
*/ */
static void static void
endImport( endImport(
void* data) void* data)
{ {
char buf[_XOPEN_PATH_MAX]; char buf[PATH_MAX];
const char* path; const char* path;
if (text[0] == '/') { if (text[0] == '/') {
path = text; path = text;
} }
else { else {
(void)snprintf(buf, sizeof(buf), (void)snprintf(buf, sizeof(buf),
#ifdef _MSC_VER #ifdef _MSC_VER
// The directory pathname has a trailing backslash on Windows // The directory pathname has a trailing backslash on Windows
"%s%s", "%s%s",
skipping to change at line 2021 skipping to change at line 2032
ut_status status; ut_status status;
XML_Parser parser = XML_ParserCreate(NULL); XML_Parser parser = XML_ParserCreate(NULL);
if (parser == NULL) { if (parser == NULL) {
status = UT_OS; status = UT_OS;
ut_set_status(status); ut_set_status(status);
ut_handle_error_message(strerror(errno)); ut_handle_error_message(strerror(errno));
ut_handle_error_message("Couldn't create XML parser"); ut_handle_error_message("Couldn't create XML parser");
} }
else { else {
char base[_XOPEN_PATH_MAX]; char base[PATH_MAX];
#ifdef _MSC_VER #ifdef _MSC_VER
{ {
char drive[_MAX_DRIVE+1]; // Will have trailing colon char drive[_MAX_DRIVE+1]; // Will have trailing colon
char directory[_MAX_DIR+1]; // Will have trailing backslash char directory[_MAX_DIR+1]; // Will have trailing backslash
_splitpath(path, drive, directory, NULL, NULL); _splitpath(path, drive, directory, NULL, NULL);
(void)snprintf(base, sizeof(base), "%s%s", drive, directory); (void)snprintf(base, sizeof(base), "%s%s", drive, directory);
base[sizeof(base)-1] = 0; base[sizeof(base)-1] = 0;
} }
#else #else
{ {
skipping to change at line 2060 skipping to change at line 2071
status = readXmlWithParser(parser, path); status = readXmlWithParser(parser, path);
} /* parser "base" set */ } /* parser "base" set */
XML_ParserFree(parser); XML_ParserFree(parser);
} /* parser != NULL */ } /* parser != NULL */
return status; return status;
} }
/* A bit hacky but much better than modifying binaries. */
static const char*
default_udunits2_xml_path()
{
// Returned absolute pathname of XML database
static char absXmlPathname[PATH_MAX];
if (absXmlPathname[0] == 0) {
const char* prefix = NULL; // Installation directory
# if defined(__APPLE__) || defined(__linux__)
Dl_info info;
const char sep = '/'; // Pathname component separator
char buf[PATH_MAX];
const char relXmlPathname[] = "share/udunits/udunits2.xml";
// The following should get pathname of shared-library
if (!dladdr(default_udunits2_xml_path, &info)) {
prefix = NULL;
}
else {
strncpy(buf, info.dli_fname, sizeof(buf))[sizeof(buf)-1] = 0;
memmove(buf, dirname(buf), sizeof(buf)); // "lib"
memmove(buf, dirname(buf), sizeof(buf)); // "lib/.."
prefix = buf;
}
# elif defined(_WIN32)
const char sep = '\\'; // Pathname component separator
char buf[MAX_PATH * 4];
const char relXmlPathname[] = "share\\udunits\\udunits2.xml";
HMODULE hModule = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCTSTR)default_udunits2_xml_path, &hModule);
GetModuleFileName(hModule, buf, sizeof(buf)); // Library pathname
*strrchr(buf, sep) = 0; // "lib"
*strrchr(buf, sep) = 0; // "lib/.."
prefix = buf;
# endif
if (prefix == NULL) {
strncpy(absXmlPathname, DEFAULT_UDUNITS2_XML_PATH,
sizeof(absXmlPathname));
} // Use default pathname
else {
int prefixLen = strlen(prefix);
if (prefix[prefixLen-1] == sep) {
--prefixLen;
if (prefix[prefixLen-1] == sep)
--prefixLen;
}
snprintf(absXmlPathname, sizeof(absXmlPathname), "%.*s%c%s",
prefixLen, prefix, sep, relXmlPathname);
} // Use computed pathname
absXmlPathname[sizeof(absXmlPathname)-1] = 0; // Ensure NUL terminated
} // `absXmlPathname` not set
return absXmlPathname;
}
/** /**
* Returns the pathname of the XML database. * Returns the pathname of the XML database.
* *
* @param path The pathname of the XML file or NULL. * @param path The pathname of the XML file or NULL.
* @param status Status. One of UT_OPEN_ARG, UT_OPEN_ENV, or UT_OPEN_DEFAULT. * @param status Status. One of UT_OPEN_ARG, UT_OPEN_ENV, or UT_OPEN_DEFAULT.
* @return If "path" is not NULL, then it is returned; otherwise, the * @return If "path" is not NULL, then it is returned; otherwise, the
* pathname specified by the environment variable * pathname specified by the environment variable
* UDUNITS2_XML_PATH is returned if set; otherwise, the * UDUNITS2_XML_PATH is returned if set; otherwise, the
* compile-time pathname of the installed, default, unit * compile-time pathname of the installed, default, unit
* database is returned. * database is returned. Caller must not free.
*/ */
const char* const char*
ut_get_path_xml( ut_get_path_xml(
const char* path, const char* path,
ut_status* status) ut_status* status)
{ {
if (path != NULL) { if (path != NULL) {
*status = UT_OPEN_ARG; *status = UT_OPEN_ARG;
} }
else { else {
path = getenv("UDUNITS2_XML_PATH"); path = getenv("UDUNITS2_XML_PATH");
if (path != NULL) { if (path != NULL) {
*status = UT_OPEN_ENV; *status = UT_OPEN_ENV;
} }
else { else {
path = DEFAULT_UDUNITS2_XML_PATH; path = default_udunits2_xml_path();
*status = UT_OPEN_DEFAULT; *status = UT_OPEN_DEFAULT;
} }
} }
return path; return path;
} }
/** /**
* Returns the unit-system corresponding to an XML file. This is the usual way * Returns the unit-system corresponding to an XML file. This is the usual way
* that a client will obtain a unit-system. * that a client will obtain a unit-system.
* *
 End of changes. 15 change blocks. 
16 lines changed or deleted 91 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)