00001 #include <stdio.h>
00002 #include <ctype.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007
00008 #include "cgns_io.h"
00009 #include "getargs.h"
00010
00011 #define MAX_LEADER 1024
00012 #define INDENT 2
00013
00014 static char leader[MAX_LEADER+1];
00015 static int leader_len;
00016 static int indent = INDENT;
00017 static int follow_links = 0;
00018 static int out_flags = 0;
00019
00020 static char options[] = "bi:faltds";
00021 static char *usgmsg[] = {
00022 "usage : cgnslist [options] CGNSfile [node]",
00023 "options:",
00024 " -b = brief - file summary only",
00025 " -i<cnt> = set indent level (default 2)",
00026 " -f = follow links",
00027 " -l = print node label",
00028 " -t = print node data type",
00029 " -d = print node dimensions",
00030 " -s = print node size in bytes",
00031 " -a = print all -ltds",
00032 NULL
00033 };
00034
00035 static void print_node (int cgio, double node_id)
00036 {
00037 int n, bytes;
00038 char label[CGIO_MAX_NAME_LENGTH+1];
00039 char type[CGIO_MAX_NAME_LENGTH+1];
00040 int ndim, dims[CGIO_MAX_DIMENSIONS];
00041
00042 if ((out_flags & 1) != 0) {
00043 if (cgio_get_label (cgio, node_id, label))
00044 cgio_error_exit ("cgio_get_label");
00045 printf (" %s", label);
00046 }
00047 if ((out_flags & 10) != 0) {
00048 if (cgio_get_data_type (cgio, node_id, type))
00049 cgio_error_exit ("cgio_get_data_type");
00050 if ((out_flags & 2) != 0)
00051 printf (" %s", type);
00052 }
00053 if ((out_flags & 12) != 0) {
00054 if (cgio_get_dimensions (cgio, node_id, &ndim, dims))
00055 cgio_error_exit ("cgio_get_data_type");
00056 if ((out_flags & 4) != 0) {
00057 printf (" (");
00058 if (ndim > 0) {
00059 printf ("%d", dims[0]);
00060 for (n = 1; n < ndim; n++)
00061 printf (",%d", dims[n]);
00062 }
00063 putchar (')');
00064 }
00065 if ((out_flags & 8) != 0) {
00066 if (ndim < 1 || NULL != strchr ("LlMm", type[0]))
00067 bytes = 0;
00068 else if (NULL != strchr ("CcBb", type[0]))
00069 bytes = 1;
00070 else if (type[0] == 'X' || type[0] == 'x')
00071 bytes = type[1] == '8' ? 16 : 8;
00072 else
00073 bytes = type[1] == '8' ? 8 : 4;
00074 for (n = 0; n < ndim; n++)
00075 bytes *= dims[n];
00076 printf (" %d", bytes);
00077 }
00078 }
00079 }
00080
00081 static void print_children (int cgio, double parent_id)
00082 {
00083 int nc, nchildren, len_ret;
00084 char *p = leader + leader_len;
00085 char name[CGIO_MAX_NAME_LENGTH+1];
00086 char name_in_file[CGIO_MAX_LINK_LENGTH+1];
00087 char file_name[CGIO_MAX_FILE_LENGTH+1];
00088 double child_id;
00089
00090 if (cgio_number_children (cgio, parent_id, &nchildren))
00091 cgio_error_exit ("cgio_number_children");
00092 if (!nchildren) return;
00093
00094 if (leader_len + indent > MAX_LEADER) {
00095 fprintf (stderr, "nesting is too deep\n");
00096 exit (1);
00097 }
00098 leader_len += indent;
00099 for (nc = 0; nc < indent; nc++)
00100 p[nc] = ' ';
00101 p[indent] = 0;
00102
00103 for (nc = 1; nc <= nchildren; nc++) {
00104 if (cgio_children_ids (cgio, parent_id, nc, 1, &len_ret,
00105 &child_id))
00106 cgio_error_exit ("cgio_children_ids");
00107 if (cgio_get_name (cgio, child_id, name))
00108 cgio_error_exit ("cgio_get_name");
00109 if (cgio_is_link (cgio, child_id, &len_ret))
00110 cgio_error_exit ("cgio_is_link");
00111
00112 *p = 0;
00113 if (len_ret > 0) {
00114 if (cgio_get_link (cgio, child_id, file_name, name_in_file))
00115 cgio_error_exit ("cgio_get_link");
00116 if (*file_name)
00117 printf ("%s+-%s -> %s @ %s\n", leader, name,
00118 name_in_file, file_name);
00119 else
00120 printf ("%s+-%s -> %s\n", leader, name, name_in_file);
00121 }
00122 else if (out_flags) {
00123 printf ("%s+-%s --", leader, name);
00124 print_node (cgio, child_id);
00125 putchar ('\n');
00126 }
00127 else
00128 printf ("%s+-%s\n", leader, name);
00129
00130 if (follow_links || len_ret <= 0) {
00131 *p = (char)(nc < nchildren ? '|' : ' ');
00132 print_children (cgio, child_id);
00133 }
00134 }
00135 *p = 0;
00136 leader_len -= indent;
00137 }
00138
00139 int main (int argc, char *argv[])
00140 {
00141 double root_id, node_id;
00142 float cgns_version;
00143 int n = 1, cgio, file_type, brief = 0;
00144 char *name, rootname[CGIO_MAX_NAME_LENGTH+1];
00145 struct stat st;
00146 char version[CGIO_MAX_NAME_LENGTH+1];
00147 char created[CGIO_MAX_NAME_LENGTH+1];
00148 char modified[CGIO_MAX_NAME_LENGTH+1];
00149 static char *FileType[] = {"NONE", "ADF", "HDF5", "XML"};
00150
00151 if (argc < 2)
00152 print_usage (usgmsg, NULL);
00153 while ((n = getargs (argc, argv, options)) > 0) {
00154 switch (n) {
00155 case 'b':
00156 brief = 1;
00157 break;
00158 case 'i':
00159 indent = atoi (argarg);
00160 if (indent < 1) {
00161 fprintf (stderr, "indent must be > 0\n");
00162 exit (1);
00163 }
00164 break;
00165 case 'f':
00166 follow_links = 1;
00167 break;
00168 case 'l':
00169 out_flags |= 1;
00170 break;
00171 case 't':
00172 out_flags |= 2;
00173 break;
00174 case 'd':
00175 out_flags |= 4;
00176 break;
00177 case 's':
00178 out_flags |= 8;
00179 break;
00180 case 'a':
00181 out_flags |= 15;
00182 break;
00183 }
00184 }
00185
00186 if (argind == argc)
00187 print_usage (usgmsg, "CGNSfile not given");
00188
00189 if (stat (argv[argind], &st)) {
00190 fprintf (stderr, "can't stat %s\n", argv[argind]);
00191 exit (1);
00192 }
00193
00194 if (cgio_open_file (argv[argind], 'r', CGIO_FILE_NONE, &cgio))
00195 cgio_error_exit ("cgio_open_file");
00196 if (cgio_get_root_id (cgio, &root_id))
00197 cgio_error_exit ("cgio_get_root_id");
00198
00199 if (brief) {
00200 if (cgio_get_file_type (cgio, &file_type))
00201 cgio_error_exit ("cgio_get_file_type");
00202 if (cgio_file_version (cgio, version, created, modified))
00203 cgio_error_exit ("cgio_file_version");
00204 if (0 == cgio_get_node_id (cgio, root_id,
00205 "CGNSLibraryVersion",&node_id) &&
00206 0 == cgio_read_all_data (cgio, node_id, &cgns_version))
00207 printf ("CGNS version : %4.2f\n", cgns_version);
00208 else
00209 printf ("CGNS version : not defined\n");
00210 printf ("file type : %s\n", FileType[file_type]);
00211 printf ("file version : %s\n", version);
00212 printf ("file size : %ld bytes\n", (long)st.st_size);
00213 printf ("creation date : %s\n", created);
00214 printf ("modified date : %s\n", modified);
00215 if (cgio_close_file (cgio))
00216 cgio_error_exit ("cgio_close_file");
00217 return 0;
00218 }
00219
00220 if (++argind < argc) {
00221 name = argv[argind];
00222 if (cgio_get_node_id (cgio, root_id, name, &node_id))
00223 cgio_error_exit ("cgio_get_root_id");
00224 }
00225 else {
00226 if (cgio_get_name (cgio, root_id, rootname))
00227 cgio_error_exit ("cgio_get_name");
00228 node_id = root_id;
00229 name = rootname;
00230 }
00231
00232 for (n = 0; n < indent; n++)
00233 leader[n] = ' ';
00234 leader[indent] = 0;
00235 leader_len = indent;
00236
00237 if (out_flags) {
00238 printf ("%s --", name);
00239 print_node (cgio, node_id);
00240 putchar ('\n');
00241 }
00242 else
00243 printf ("%s\n", name);
00244 print_children (cgio, node_id);
00245
00246 if (cgio_close_file (cgio))
00247 cgio_error_exit ("cgio_close_file");
00248 return 0;
00249 }
00250