1 /* This is part of the netCDF package. Copyright 2005 University
2    Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
3    conditions of use. See www.unidata.ucar.edu for more info.
4 
5    Create a test file with a string data for ncdump to read.
6 
7    Russ Rew
8 */
9 
10 #include <nc_tests.h>
11 #include "err_macros.h"
12 #include <netcdf.h>
13 
14 #define FILE4_NAME "tst_string_data.nc"
15 #define DIM4_NAME "line"
16 #define DIM4_LEN 5
17 #define VAR4_NAME "description"
18 #define VAR4_RANK 1
19 #define ATT4_NAME "_FillValue"
20 #define ATT4_LEN  1
21 
22 int
main(int argc,char ** argv)23 main(int argc, char **argv)
24 {
25    int ncid;
26    int dimid, varid;
27    nc_type att_type;
28    size_t att_len;
29 
30    int i;
31 
32    int var_dims[VAR4_RANK];
33    const char *desc_data[DIM4_LEN] = {
34        "first string", "second string", "third string", "", "last \n\"string\""
35    };
36    const char *missing_val[ATT4_LEN] = {""};
37    char *strings_in[DIM4_LEN];
38 
39    printf("\n*** Testing strings.\n");
40    printf("*** creating strings test file %s...", FILE4_NAME);
41    if (nc_create(FILE4_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR;
42 
43    /* Declare a line dimension */
44    if (nc_def_dim(ncid, DIM4_NAME, DIM4_LEN, &dimid)) ERR;
45 
46    /* Declare a string variable */
47    var_dims[0] = dimid;
48    if (nc_def_var(ncid, VAR4_NAME, NC_STRING, VAR4_RANK, var_dims, &varid)) ERR;
49 
50    /* Create and write a variable attribute of string type */
51    if (nc_put_att_string(ncid, varid, ATT4_NAME, ATT4_LEN, missing_val)) ERR;
52    if (nc_enddef(ncid)) ERR;
53 
54    /* Store some data of string type */
55    if(nc_put_var(ncid, varid, desc_data)) ERR;
56 
57    /* Write the file. */
58    if (nc_close(ncid)) ERR;
59 
60    /* Check it out. */
61    if (nc_open(FILE4_NAME, NC_NOWRITE, &ncid)) ERR;
62    if (nc_inq_varid(ncid, VAR4_NAME, &varid)) ERR;
63    if (nc_inq_att(ncid, varid, ATT4_NAME, &att_type, &att_len)) ERR;
64    if (att_type != NC_STRING || att_len != ATT4_LEN) ERR;
65    if (nc_get_att_string(ncid, varid, ATT4_NAME, strings_in)) ERR;
66 
67    if (strcmp(strings_in[0], *missing_val) != 0) ERR;
68    /* string atts should be explicitly freed when done with them */
69    nc_free_string(ATT4_LEN, strings_in);
70 
71    if(nc_get_var_string(ncid, varid, strings_in)) ERR;
72    for (i = 0; i < DIM4_LEN; i++) {
73        if (strcmp(strings_in[i], desc_data[i]) != 0) ERR;
74    }
75    nc_free_string(DIM4_LEN, strings_in);
76 
77    /* Try reading strings in with typeless generic interface also */
78    if(nc_get_var(ncid, varid, strings_in)) ERR;
79 
80    for (i = 0; i < DIM4_LEN; i++) {
81        if (strcmp(strings_in[i], desc_data[i]) != 0) ERR;
82    }
83    nc_free_string(DIM4_LEN, strings_in);
84 
85 
86    /* Try reading strings in with typeless generic array interface also */
87    {
88        size_t cor[VAR4_RANK], edg[VAR4_RANK];
89        cor[0] = 0;
90        edg[0] = DIM4_LEN;
91        if(nc_get_vara(ncid, varid, cor, edg, strings_in)) ERR;
92 
93        for (i = 0; i < DIM4_LEN; i++) {
94 	   if (strcmp(strings_in[i], desc_data[i]) != 0) ERR;
95        }
96        nc_free_string(DIM4_LEN, strings_in);
97    }
98 
99    if (nc_close(ncid)) ERR;
100 
101    SUMMARIZE_ERR;
102    FINAL_RESULTS;
103 }
104