Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove more old utf8 code #430

Merged
merged 4 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cf
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ MALLOC_CHECK=""
CPPFLAGS=""
LDFLAGS=""

#CFLAGS="-g -O0 $CFLAGS"
CFLAGS="-g $CFLAGS"
CFLAGS="-g -O0 $CFLAGS -Wno-undefined"

MAKE=make
IGNORE="test 0 = 1"
Expand Down
2 changes: 1 addition & 1 deletion cf.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Visual Studio
#VS=1
VS=1

# Is netcdf-4 and/or DAP enabled?
NC4=1
Expand Down
20 changes: 17 additions & 3 deletions include/ncutf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef NCUTF8_H
#define NCUTF8_H 1

#include "ncexternl.h"

/* Provide a wrapper around whatever utf8 library we use. */

/*
Expand All @@ -15,7 +17,7 @@
* NC_ENOMEM -- out of memory
* NC_EBADNAME-- not valid utf8
*/
extern int nc_utf8_validate(const unsigned char * name);
EXTERNL int nc_utf8_validate(const unsigned char * name);

/*
* Apply NFC normalization to a string.
Expand All @@ -28,7 +30,19 @@ extern int nc_utf8_validate(const unsigned char * name);
* NC_ENOMEM -- out of memory
* NC_EBADNAME -- other failure
*/
extern int nc_utf8_normalize(const unsigned char* str, unsigned char** normalp);
EXTERNL int nc_utf8_normalize(const unsigned char* str, unsigned char** normalp);

#endif /*NCUTF8_H*/
/*
* Convert a normalized utf8 string to utf16. This is approximate
* because it just does the truncation version of conversion for
* each 32-bit codepoint to get the corresponding utf16.
* Return codes:
* NC_NOERR -- success
* NC_ENOMEM -- out of memory
* NC_EINVAL -- invalid argument or internal error
* NC_EBADNAME-- not valid utf16
*/

EXTERNL int nc_utf8_to_utf16(const unsigned char* s8, unsigned short** utf16p, size_t* lenp);

#endif /*NCUTF8_H*/
80 changes: 78 additions & 2 deletions libdispatch/dutf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
*/

#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "netcdf.h"
#include "ncutf8.h"
#include "utf8proc.h"
Expand All @@ -23,13 +29,13 @@ int nc_utf8_validate(const unsigned char* name)
{
int ncstat = NC_NOERR;
const nc_utf8proc_uint8_t *str;
nc_utf8proc_ssize_t strlen = -1;
nc_utf8proc_ssize_t nchars = -1;
nc_utf8proc_int32_t codepoint;
nc_utf8proc_ssize_t count;

str = (const nc_utf8proc_uint8_t*)name;
while(*str) {
count = nc_utf8proc_iterate(str,strlen,&codepoint);
count = nc_utf8proc_iterate(str,nchars,&codepoint);
if(count < 0) {
switch (count) {
case UTF8PROC_ERROR_NOMEM:
Expand Down Expand Up @@ -94,3 +100,73 @@ nc_utf8_normalize(const unsigned char* utf8, unsigned char** normalp)
done:
return ncstat;
}

/*
* Convert a normalized utf8 string to utf16. This is approximate
* because it just does the truncation version of conversion for
* each 32-bit codepoint to get the corresponding utf16.
* Return codes:
* NC_NOERR -- success
* NC_ENOMEM -- out of memory
* NC_EINVAL -- invalid argument or internal error
* NC_EBADNAME-- not valid utf16
*/

int nc_utf8_to_utf16(const unsigned char* s8, unsigned short** utf16p, size_t* len16p)
{
int ncstat = NC_NOERR;
const nc_utf8proc_uint8_t *str;
nc_utf8proc_ssize_t nchars = -1;
nc_utf8proc_int32_t codepoint;
nc_utf8proc_ssize_t count;
size_t len8, len16;
unsigned short* utf16;
unsigned short* p16;

len8 = strlen((char*)s8);
utf16 = (unsigned short*)malloc(sizeof(unsigned short)*(len8+1));
if(utf16 == NULL) {
ncstat = NC_ENOMEM;
goto done;
}
str = (const nc_utf8proc_uint8_t*)s8;
/* Walk the string and convert each codepoint */
p16 = utf16;
len16 = 0;
while(*str) {
count = nc_utf8proc_iterate(str,nchars,&codepoint);
if(count < 0) {
switch (count) {
case UTF8PROC_ERROR_NOMEM:
case UTF8PROC_ERROR_OVERFLOW:
ncstat = NC_ENOMEM;
break;
case UTF8PROC_ERROR_INVALIDOPTS:
ncstat = NC_EINVAL;
break;
case UTF8PROC_ERROR_INVALIDUTF8:
case UTF8PROC_ERROR_NOTASSIGNED:
default:
ncstat = NC_EBADNAME;
break;
}
goto done;
} else { /* move to next char */
/* Complain if top 16 bits not zero */
if((codepoint & 0x0000FFFF) != 0) {
ncstat = NC_EBADNAME;
goto done;
}
/* Truncate codepoint to 16 bits and store */
*p16++ = (unsigned short)(codepoint & 0x0000FFFF);
str += count;
len16++;
}
}
*p16++ = (unsigned short)0;
if(utf16p) *utf16p = utf16;
if(len16p) *len16p = len16;
done:
if(ncstat) free(utf16);
return ncstat;
}
25 changes: 6 additions & 19 deletions libdispatch/utf8proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
/** @} */

#include <stdlib.h>
#include "ncexternl.h"

#if defined(_MSC_VER) && _MSC_VER < 1800
// MSVC prior to 2013 lacked stdbool.h and inttypes.h
Expand Down Expand Up @@ -120,20 +121,6 @@ typedef bool nc_utf8proc_bool;
#endif
#include <limits.h>

#ifndef UTF8PROC_EXPORTS
#ifdef _WIN32
# ifdef UTF8PROC_EXPORTS
# define UTF8PROC_DLLEXPORT __declspec(dllexport)
# else
# define UTF8PROC_DLLEXPORT __declspec(dllimport)
# endif
#elif __GNUC__ >= 4
# define UTF8PROC_DLLEXPORT __attribute__ ((visibility("default")))
#else
# define UTF8PROC_DLLEXPORT
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -418,7 +405,7 @@ const char *nc_utf8proc_errmsg(nc_utf8proc_ssize_t errcode);
* In case of success, the number of bytes read is returned; otherwise, a
* negative error code is returned.
*/
UTF8PROC_DLLEXPORT nc_utf8proc_ssize_t nc_utf8proc_iterate(const nc_utf8proc_uint8_t *str, nc_utf8proc_ssize_t strlen, nc_utf8proc_int32_t *codepoint_ref);
EXTERNL nc_utf8proc_ssize_t nc_utf8proc_iterate(const nc_utf8proc_uint8_t *str, nc_utf8proc_ssize_t strlen, nc_utf8proc_int32_t *codepoint_ref);

/**
* Check if a codepoint is valid (regardless of whether it has been
Expand Down Expand Up @@ -685,13 +672,13 @@ nc_utf8proc_ssize_t nc_utf8proc_map_custom(
*/
/** @{ */
/** NFD normalization (@ref UTF8PROC_DECOMPOSE). */
UTF8PROC_DLLEXPORT nc_utf8proc_uint8_t *nc_utf8proc_NFD(const nc_utf8proc_uint8_t *str);
EXTERNL nc_utf8proc_uint8_t *nc_utf8proc_NFD(const nc_utf8proc_uint8_t *str);
/** NFC normalization (@ref UTF8PROC_COMPOSE). */
UTF8PROC_DLLEXPORT nc_utf8proc_uint8_t *nc_utf8proc_NFC(const nc_utf8proc_uint8_t *str);
EXTERNL nc_utf8proc_uint8_t *nc_utf8proc_NFC(const nc_utf8proc_uint8_t *str);
/** NFKD normalization (@ref UTF8PROC_DECOMPOSE and @ref UTF8PROC_COMPAT). */
UTF8PROC_DLLEXPORT nc_utf8proc_uint8_t *nc_utf8proc_NFKD(const nc_utf8proc_uint8_t *str);
EXTERNL nc_utf8proc_uint8_t *nc_utf8proc_NFKD(const nc_utf8proc_uint8_t *str);
/** NFKC normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT). */
UTF8PROC_DLLEXPORT nc_utf8proc_uint8_t *nc_utf8proc_NFKC(const nc_utf8proc_uint8_t *str);
EXTERNL nc_utf8proc_uint8_t *nc_utf8proc_NFKC(const nc_utf8proc_uint8_t *str);
/** @} */

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion ncgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ IF(BUILD_SHARED_LIBS AND WIN32)
remove_definitions(-DDLL_NETCDF)
ENDIF()

SET(ncgen_FILES generate.c main.c cdata.c bindata.c genchar.c cvt.c data.c debug.c escapes.c genc.c genbin.c generr.c genlib.c getfill.c odom.c semantics.c ncgeny.c dump.c util.c bytebuffer.c list.c genf77.c f77data.c genj.c jdata.c nc_iter.c ConvertUTF.c ncgen.h)
SET(ncgen_FILES generate.c main.c cdata.c bindata.c genchar.c cvt.c data.c debug.c escapes.c genc.c genbin.c generr.c genlib.c getfill.c odom.c semantics.c ncgeny.c dump.c util.c bytebuffer.c list.c genf77.c f77data.c genj.c jdata.c nc_iter.c ncgen.h)

# don't add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
Expand Down
Loading