File: tst_vlenstr.c

package info (click to toggle)
netcdf-parallel 1%3A4.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 116,192 kB
  • sloc: ansic: 279,265; sh: 14,143; cpp: 5,971; yacc: 2,612; makefile: 2,075; lex: 1,218; javascript: 280; xml: 173; awk: 2
file content (161 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download | duplicates (2)
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
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "netcdf.h"

#undef DEBUG

#ifdef DEBUG
static char* buf = NULL;
#endif

#define totalNumStrings 13

static const char* charPointers[totalNumStrings] = 
	{"banana", "kiwi", "apple", "grape", "apple", "cherry", "lemon", "melon", "watermelon", "strawberry", "peach", "raspberry", "pineapple"};
    
#define first_size 2
#define second_size 1
#define third_size 5
#define fourth_size 3
#define fifth_size 0
#define sixth_size 2

#define attlength 6

void
checkErrorCode(int status, const char* message)
{
    if (status != NC_NOERR){
	fprintf(stderr,"Error code: %d from %s\n",status,message);
        fprintf(stderr,"\t%s\n\n",nc_strerror(status));
    }
}


void
writeVariable(size_t dimlength, int ncid, nc_type vlen_typeID)
{
    int retval = NC_NOERR;
    int dimid;
    int varid;
    
    /* Setup data */
    nc_vlen_t* data = calloc(sizeof(nc_vlen_t),dimlength);

    /* create six variable length arrays of strings */
    int stringIndex = 0;

    data[0].len = first_size;
    data[0].p = charPointers+stringIndex;
    stringIndex += first_size;
    
    data[1].len = second_size;
    data[1].p = charPointers+stringIndex;
    stringIndex += second_size;
    
    data[2].len = third_size;
    data[2].p = charPointers+stringIndex;
    stringIndex += third_size;
    
    data[3].len = fourth_size;
    data[3].p = charPointers+stringIndex;
    stringIndex += fourth_size;
    
    data[4].len = fifth_size;
    data[4].p = charPointers+stringIndex;
    stringIndex += fifth_size;
    
    data[5].len = sixth_size;
    data[5].p = charPointers+stringIndex;
    stringIndex += sixth_size;
    
    /* Write vlen variable named Fruits */
    
    /* Define dimension */
    retval = nc_def_dim(ncid, "D1", dimlength, &dimid);
    checkErrorCode(retval, "nc_def_dim");
    
    /* Define variable */
    retval = nc_def_var(ncid, "Fruits", vlen_typeID, 1, &dimid, &varid);
    checkErrorCode(retval, "nc_def_var");
    
    /* Write variable */
    retval = nc_put_var(ncid, varid, data);
    checkErrorCode(retval, "nc_put_var");
    
    free(data);
}

static void
createFruitsData(int elemno, size_t vlensize, size_t* stringIndexp, nc_vlen_t* data)
{
    data[elemno].len = vlensize;
    data[elemno].p = NULL;
    if(vlensize > 0) {
        data[elemno].p = charPointers+(*stringIndexp);
        (*stringIndexp) += vlensize;
    }
}

void
writeAttribute(size_t len, int ncid, nc_type vlen_typeID)
{
    int retval = NC_NOERR;
    
    /* Setup data */
    nc_vlen_t* data = calloc(sizeof(nc_vlen_t), len);

    /* create six variable length arrays of strings */
    size_t stringIndex = 0;

    createFruitsData(0,first_size,&stringIndex,data);
    createFruitsData(1,second_size,&stringIndex,data);
    createFruitsData(2,third_size,&stringIndex,data);
    createFruitsData(3,fourth_size,&stringIndex,data);
    createFruitsData(4,fifth_size,&stringIndex,data);
    createFruitsData(5,sixth_size,&stringIndex,data);
        
#ifdef DEBUG
    if(buf) {free(buf); buf = NULL;}
    nc_dump_data(ncid,vlen_typeID,data,len,&buf);
    fprintf(stderr,">>> attribute = %s\n",buf);
#endif

    /* Write vlen attribute named Fruits */
    retval = nc_put_att(ncid, NC_GLOBAL, "Fruit", vlen_typeID, len, data);
    checkErrorCode(retval, "nc_put_att");
    
    free(data);
}

int
main(int argc, const char * argv[])
{
    /* Open file */
    int ncid;
    int retval;
    nc_type vlen_typeID;
    
    retval = nc_create("tst_vlenstr.nc", NC_NETCDF4, &ncid);
    checkErrorCode(retval, "nc_create");
    
    /* Define vlen type named MY_VLEN_STRING */
    retval = nc_def_vlen(ncid, "MY_VLEN_STRING", NC_STRING, &vlen_typeID);
    checkErrorCode(retval, "nc_def_vlen");
    
    /* write variable - will be able to read it back fine */
    writeVariable(attlength, ncid, vlen_typeID);
    
    /* write attribute - will read back garbled values */
    writeAttribute(attlength, ncid, vlen_typeID);
    
    retval = nc_close(ncid);
    checkErrorCode(retval, "nc_close");
    
#ifdef DEBUG
    if(buf) free(buf);
#endif
    return retval;
}