1 /********************************************************************* 2 * Copyright 1993, UCAR/Unidata 3 * See netcdf/COPYRIGHT file for copying and redistribution conditions. 4 * $Header: /upc/share/CVS/netcdf-3/ncgen/genlib.c,v 1.57 2010/04/04 19:39:47 dmh Exp $ 5 *********************************************************************/ 6 7 #include "includes.h" 8 9 /* invoke netcdf calls (or generate C or Fortran code) to create netcdf 10 * from in-memory structure. 11 The output file name is chosen by using the following in priority order: 12 1. -o flag name 13 2. command line input file with .cdl changed to .nc 14 3. dataset name as specified in netcdf <name> {...} 15 */ 16 void 17 define_netcdf(void) 18 { 19 char filename[2048+1]; 20 21 /* Rule for specifying the dataset name: 22 1. use -o name 23 2. use input cdl file name 24 3. use the datasetname 25 It would be better if there was some way 26 to specify the datasetname independently of the 27 file name, but oh well. 28 */ 29 if(netcdf_name) { /* -o flag name */ 30 strncpy(filename,netcdf_name,2048); 31 } else { /* construct a usable output file name */ 32 if (cdlname != NULL && strcmp(cdlname,"-") != 0) {/* cmd line name */ 33 char* p; 34 35 strncpy(filename,cdlname,2048); 36 /* remove any suffix and prefix*/ 37 p = strrchr(filename,'.'); 38 if(p != NULL) {*p= '\0';} 39 p = strrchr(filename,'/'); 40 if(p != NULL) {memmove(filename,(p+1),2048);} 41 42 } else {/* construct name from dataset name */ 43 strncpy(filename,datasetname,2048); /* Reserve space for extension, terminating '\0' */ 44 } 45 /* Append the proper extension */ 46 strncat(filename,binary_ext,2048-(strlen(filename) + strlen(binary_ext))); 47 } 48 49 /* Execute exactly one of these */ 50 #ifdef ENABLE_C 51 if (l_flag == L_C) gen_ncc(filename); else /* create C code to create netcdf */ 52 #endif 53 #ifdef ENABLE_F77 54 if (l_flag == L_F77) gen_ncf77(filename); else /* create Fortran code */ 55 #endif 56 #ifdef ENABLE_JAVA 57 if(l_flag == L_JAVA) { 58 gen_ncjava(filename); 59 } else 60 #endif 61 /* Binary is the default */ 62 #ifdef ENABLE_BINARY 63 gen_netcdf(filename); /* create netcdf */ 64 #else 65 derror("No language specified"); 66 #endif 67 close_netcdf(); 68 cleanup(); 69 } 70 71 void 72 close_netcdf(void) 73 { 74 #ifdef ENABLE_C 75 if (l_flag == L_C) cl_c(); else /* create C code to close netcdf */ 76 #endif 77 #ifdef ENABLE_F77 78 if (l_flag == L_F77) cl_f77(); else 79 #endif 80 #ifdef ENABLE_JAVA 81 if (l_flag == L_JAVA) cl_java(); else 82 #endif 83 #ifdef ENABLE_BINARY 84 if (l_flag == L_BINARY) cl_netcdf(); 85 #endif 86 } 87 88 /** 89 Return a string representing 90 the fully qualified name of the symbol. 91 Symbol must be top level 92 Caller must free. 93 */ 94 void 95 topfqn(Symbol* sym) 96 { 97 char* fqn; 98 char* fqnname; 99 char* parentfqn; 100 Symbol* parent; 101 102 if(sym->fqn != NULL) 103 return; /* already defined */ 104 105 #ifdef USE_NETCDF4 106 if(!usingclassic) { 107 parent = sym->container; 108 /* Recursively compute parent fqn */ 109 if(parent == NULL) { /* implies this is the rootgroup */ 110 assert(sym->grp.is_root); 111 sym->fqn = strdup(""); 112 return; 113 } else if(parent->fqn == NULL) { 114 topfqn(parent); 115 } 116 parentfqn = parent->fqn; 117 118 fqnname = fqnescape(sym->name); 119 fqn = (char*)malloc(strlen(fqnname) + strlen(parentfqn) + 1 + 1); 120 strcpy(fqn,parentfqn); 121 strcat(fqn,"/"); 122 strcat(fqn,fqnname); 123 sym->fqn = fqn; 124 } else 125 #endif /*USE_NETCDF4*/ 126 { 127 sym->fqn = strdup(sym->name); 128 } 129 } 130 131 /** 132 Return a string representing 133 the fully qualified name of a nested symbol 134 (i.e. field or econst). 135 Caller must free. 136 */ 137 void 138 nestedfqn(Symbol* sym) 139 { 140 char* fqn; 141 char* fqnname; 142 Symbol* parent; 143 144 if(sym->fqn != NULL) 145 return; /* already defined */ 146 147 /* Parent must be a type */ 148 parent = sym->container; 149 assert (parent->objectclass == NC_TYPE); 150 151 assert(parent->fqn != NULL); 152 153 fqnname = fqnescape(sym->name); 154 fqn = (char*)malloc(strlen(fqnname) + strlen(parent->fqn) + 1 + 1); 155 strcpy(fqn,parent->fqn); 156 strcat(fqn,"."); 157 strcat(fqn,fqnname); 158 sym->fqn = fqn; 159 } 160 161 /** 162 Return a string representing 163 the fully qualified name of an attribute. 164 Caller must free. 165 */ 166 void 167 attfqn(Symbol* sym) 168 { 169 char* fqn; 170 char* fqnname; 171 char* parentfqn; 172 Symbol* parent; 173 174 if(sym->fqn != NULL) 175 return; /* already defined */ 176 177 assert (sym->objectclass == NC_ATT); 178 179 parent = sym->container; 180 if(parent == NULL) 181 parentfqn = ""; 182 else 183 parentfqn = parent->fqn; 184 185 fqnname = fqnescape(sym->name); 186 fqn = (char*)malloc(strlen(fqnname) + strlen(parentfqn) + 1 + 1); 187 strcpy(fqn,parentfqn); 188 strcat(fqn,"_"); 189 strcat(fqn,fqnname); 190 sym->fqn = fqn; 191 } 192 193 #if 0 194 /* Result is pool alloc'd*/ 195 char* 196 cprefixed(List* prefix, char* suffix, char* separator) 197 { 198 int slen; 199 int plen; 200 int i; 201 char* result; 202 203 ASSERT(suffix != NULL); 204 plen = prefixlen(prefix); 205 if(prefix == NULL || plen == 0) return codify(suffix); 206 /* plen > 0*/ 207 slen = 0; 208 for(i=0;i<plen;i++) { 209 Symbol* sym = (Symbol*)listget(prefix,i); 210 slen += (strlen(sym->name)+strlen(separator)); 211 } 212 slen += strlen(suffix); 213 slen++; /* for null terminator*/ 214 result = poolalloc(slen); 215 result[0] = '\0'; 216 /* Leave off the root*/ 217 i = (rootgroup == (Symbol*)listget(prefix,0))?1:0; 218 for(;i<plen;i++) { 219 Symbol* sym = (Symbol*)listget(prefix,i); 220 strcat(result,sym->name); /* append "<prefix[i]/>"*/ 221 strcat(result,separator); 222 } 223 strcat(result,suffix); /* append "<suffix>"*/ 224 return result; 225 } 226 #endif /*0*/
HyperKWIC - Version 7.20DA executed at 11:37 on 27 Oct 2017 | Polyhedron Solutions - INTERNAL USE | COMMERCIAL (Any O/S) SN 4AKIed