mirror of
https://github.com/Unidata/netcdf-c.git
synced 2024-12-09 08:11:38 +08:00
36102e3c32
re: Issue https://github.com/Unidata/netcdf-c/issues/2190 The primary purpose of this PR is to improve the utf8 support for windows. This is persuant to a change in Windows that supports utf8 natively (almost). The almost means that it is still utf16 internally and the set of characters representable by utf8 is larger than those representable by utf16. This leaves open the question in the Issue about handling the Windows 1252 character set. This required the following changes: 1. Test the Windows build and major version in order to see if native utf8 is supported. 2. If native utf8 is supported, Modify dpathmgr.c to call the 8-bit version of the windows fopen() and open() functions. 3. In support of this, programs that use XGetOpt (Windows versions) need to get the command line as utf8 and then parse to arc+argv as utf8. This requires using a homegrown command line parser named XCommandLineToArgvA. 4. Add a utility program called "acpget" that prints out the current Windows code page and locale. Additionally, some technical debt was cleaned up as follows: 1. Unify all the places which attempt to read all or a part of a file into the dutil.c#NC_readfile code. 2. Similary unify all the code that creates temp files into dutil.c#NC_mktmp code. 3. Convert almost all remaining calls to fopen() and open() to NCfopen() and NCopen3(). This is to ensure that path management is used consistently. This touches a number of files. 4. extern->EXTERNL as needed to get it to work under Windows.
223 lines
5.2 KiB
C
223 lines
5.2 KiB
C
/*
|
|
* Copyright 2018, University Corporation for Atmospheric Research
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
|
|
#include "ncpathmgr.h"
|
|
|
|
/*
|
|
Implement the ncstdio.h interface using unix stdio.h
|
|
*/
|
|
|
|
/* Forward */
|
|
static int NCFile_read(ncstdio*,void*,const size_t,size_t*);
|
|
static int NCFile_write(ncstdio*,const void*,const size_t,size_t*);
|
|
static int NCFile_free(ncstdio*);
|
|
static int NCFile_close(ncstdio*,int);
|
|
static int NCFile_flush(ncstdio*)
|
|
static int NCFile_seek(ncstdio*,off_t);
|
|
static int NCFile_sync(ncstdio*,off_t);
|
|
static int NCFile_uid(ncstdio*,int*);
|
|
|
|
/* Define the stdio.h base operators */
|
|
|
|
|
|
struct NCFile_ops NCFile_ops = {
|
|
NCFile_read,
|
|
NCFile_write,
|
|
NCFile_free,
|
|
NCFile_close,
|
|
NCFile_flush,
|
|
NCFile_seek,
|
|
NCFile_sync,
|
|
NCFile_uid
|
|
};
|
|
|
|
/* In order to implement the close with delete, we
|
|
need the file path.
|
|
*/
|
|
struct ncFileState {
|
|
char* path;
|
|
File* file;
|
|
};
|
|
|
|
static struct ncFileState
|
|
getState(ncstdio* iop)
|
|
{
|
|
if(iop != NULL) {
|
|
if(iop->state != NULL) {
|
|
return (struct ncFileState*)iop->state;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
ncFile_create(const char *path, int ioflags, ncstdio** filepp)
|
|
{
|
|
ncstdio* filep;
|
|
File* f;
|
|
struct ncFileState* state;
|
|
|
|
f = NCfopen(path,"w+");
|
|
if(f == NULL)
|
|
return errno;
|
|
filep = (ncstdio*)calloc(sizeof(ncstdio),1);
|
|
if(filep == NULL) {fclose(f); return NC_ENOMEM;}
|
|
state = (ncstdio*)calloc(sizeof(ncFileState),1);
|
|
if(state == NULL) {fclose(f); free(filep); return NC_ENOMEM;}
|
|
filep->ops = &NCFILE_ops;
|
|
filep->ioflags = ioflags;
|
|
filep->state = (void*)state;
|
|
state->path = strdup(path);
|
|
state->file = f;
|
|
if(filepp) *filepp = filep;
|
|
return NC_NOERR;
|
|
}
|
|
|
|
int
|
|
ncFile_open(const char *path, int ioflags, ncstdio** filepp)
|
|
{
|
|
ncstdio* filep;
|
|
File* f;
|
|
|
|
if(fIsSet(ioflags,NC_NOCLOBBER))
|
|
f = NCfopen(path,"r");
|
|
else
|
|
f = NCfopen(path,"w+");
|
|
if(f == NULL)
|
|
return errno;
|
|
|
|
filep = (ncstdio*)calloc(sizeof(ncstdio),1);
|
|
if(filep == NULL) {fclose(f); return NC_ENOMEM;}
|
|
state = (ncstdio*)calloc(sizeof(ncFileState),1);
|
|
if(state == NULL) {fclose(f); free(filep); return NC_ENOMEM;}
|
|
filep->ops = &NCFILE_ops;
|
|
filep->ioflags = ioflags;
|
|
filep->state = (void*)state;
|
|
state->path = strdup(path);
|
|
state->file = f;
|
|
if(filepp) *filepp = filep;
|
|
return NC_NOERR;
|
|
}
|
|
|
|
static int
|
|
NCFile_close(ncstdio* filep, int delfile)
|
|
{
|
|
struct ncFileState* state;
|
|
if(filep == NULL) return NC_EINVAL;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state == NULL || state->file == NULL) return NC_NOERR;
|
|
fclose(state->file);
|
|
state->file = NULL;
|
|
if(delfile)
|
|
unlink(state->path);
|
|
return NC_NOERR;
|
|
}
|
|
|
|
static int
|
|
NCFile_free(ncstdio* filep)
|
|
{
|
|
struct ncFileState* state;
|
|
if(filep == NULL) return NC_NOERR;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state != NULL) {
|
|
if(state->file != NULL) return NC_EINVAL;
|
|
if(state->path != NULL)
|
|
free(state->path);
|
|
free(state);
|
|
}
|
|
free(filep);
|
|
return NC_NOERR;
|
|
}
|
|
|
|
static int
|
|
NCFile_flush(ncstdio* filep);
|
|
{
|
|
File* state;
|
|
if(filep == NULL) return NC_EINVAL;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state == NULL) return NC_EINVAL;
|
|
if(state->file == NULL) return NC_EINVAL;
|
|
fflush(state->file);
|
|
return NC_NOERR;
|
|
}
|
|
|
|
static int
|
|
NCFile_sync(ncstdio* filep);
|
|
{
|
|
File* state;
|
|
#ifdef USE_FSYNC
|
|
int fd;
|
|
#endif
|
|
if(filep == NULL) return NC_EINVAL;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state == NULL) return NC_EINVAL;
|
|
if(state->file == NULL) return NC_EINVAL;
|
|
#ifdef HAVE_FSYNC
|
|
#ifdef USE_FSYNC
|
|
fd = fileno(state->file);
|
|
#ifndef _WIN32
|
|
fsync(fd);
|
|
#else
|
|
_commit(fd);
|
|
#endif /* _WIN32 */
|
|
#endif
|
|
#endif
|
|
return NC_NOERR;
|
|
}
|
|
|
|
static int
|
|
NCFile_seek(ncstdio* filep, off_t pos);
|
|
{
|
|
struct ncFileState* state;
|
|
if(filep == NULL) return NC_EINVAL;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state == NULL) return NC_EINVAL;
|
|
if(state->file == NULL) return NC_EINVAL;
|
|
if(!fseek(state->file,pos)) return (errno > 0 ?errno : EINVAL);
|
|
return NC_NOERR;
|
|
}
|
|
|
|
static int
|
|
NCFile_read(ncstdio* filep, void* memory, const size_t size, size_t* actualp);
|
|
{
|
|
struct ncFileState* state;
|
|
size_t actual;
|
|
if(filep == NULL) return NC_EINVAL;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state == NULL || state->file == NULL) return NC_EINVAL;
|
|
actual = fread(memory,1,size,state->file);
|
|
if(actualp) *actualp = actual;
|
|
return (actual < size ? NC_EIO : NC_NOERR);
|
|
}
|
|
|
|
static int
|
|
NCFile_write(ncstdio* filep, const void* memory, const size_t size, size_t* actual);
|
|
{
|
|
struct ncFileState* state;
|
|
size_t actual;
|
|
if(filep == NULL) return NC_EINVAL;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state == NULL || state->file == NULL) return NC_EINVAL;
|
|
actual = fwrite(memory,1,size,state->file);
|
|
if(actualp) *actualp = actual;
|
|
return (actual < size ? NC_EIO : NC_NOERR);
|
|
}
|
|
|
|
static int
|
|
NCFile_uid(ncstdio* filep, int* idp)
|
|
{
|
|
struct ncFileState* state;
|
|
if(filep == NULL) return NC_EINVAL;
|
|
state = (struct ncFileState*)filep->state;
|
|
if(state == NULL || state->file == NULL) return NC_EINVAL;
|
|
if(idp) *idp = fileno(state->file);
|
|
return NC_NOERR;
|
|
}
|
|
|