File: ncFile.c

package info (click to toggle)
netcdf 1%3A4.4.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 96,828 kB
  • ctags: 15,369
  • sloc: ansic: 163,650; sh: 9,294; yacc: 2,457; makefile: 1,208; lex: 1,161; xml: 173; f90: 7; fortran: 6; awk: 2
file content (220 lines) | stat: -rw-r--r-- 5,340 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 *	Copyright 2012, University Corporation for Atmospheric Research
 *	See netcdf/COPYRIGHT file for copying and redistribution conditions.
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.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 = fopen(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 = fopen(path,"r");
    else
        f = fopen(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;
}