00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <time.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #ifndef _WIN32
00008 # include <unistd.h>
00009 #endif
00010
00011 #include "cgns_io.h"
00012 #include "getargs.h"
00013
00014 static char options[] = "ahxl";
00015 static char *usgmsg[] = {
00016 "usage : cgnsconvert [options] InputFile [OutputFile]",
00017 "options:",
00018 " -a : write ADF file",
00019 " -h : write HDF5 file",
00020 " -x : write XML file",
00021 " -l : expand links in ouput file",
00022 NULL
00023 };
00024
00025 int main (int argc, char **argv)
00026 {
00027 char *inpfile, *outfile;
00028 char tempfile[1024];
00029 int n, inptype, outtype = CGIO_FILE_NONE;
00030 int inpcg, outcg, links = 0;
00031 struct stat inpst, outst;
00032 time_t ts, te;
00033 static char *FileType[] = {"NONE", "ADF", "HDF5", "XML"};
00034
00035 if (argc < 2)
00036 print_usage (usgmsg, NULL);
00037 while ((n = getargs (argc, argv, options)) > 0) {
00038 switch (n) {
00039 case 'a':
00040 outtype = CGIO_FILE_ADF;
00041 break;
00042 case 'h':
00043 outtype = CGIO_FILE_HDF5;
00044 break;
00045 case 'x':
00046 outtype = CGIO_FILE_XML;
00047 break;
00048 case 'l':
00049 links = 1;
00050 break;
00051 }
00052 }
00053
00054 if (argind == argc)
00055 print_usage (usgmsg, "InputFile not given");
00056 if (outtype != CGIO_FILE_NONE && cgio_is_supported(outtype)) {
00057 fprintf(stderr, "output type %s not supported\n",
00058 FileType[outtype]);
00059 exit(1);
00060 }
00061
00062 inpfile = argv[argind++];
00063 if (argind < argc)
00064 outfile = argv[argind];
00065 else
00066 outfile = inpfile;
00067 sprintf(tempfile, "%s.temp", outfile);
00068 unlink(tempfile);
00069
00070 if (stat (inpfile, &inpst)) {
00071 fprintf (stderr, "can't stat %s\n", inpfile);
00072 exit (1);
00073 }
00074
00075 if (cgio_open_file (inpfile, 'r', CGIO_FILE_NONE, &inpcg))
00076 cgio_error_exit("cgio_open_file");
00077 if (cgio_get_file_type (inpcg, &inptype))
00078 cgio_error_exit("cgio_get_file_type");
00079
00080 if (outtype == CGIO_FILE_NONE) outtype = inptype;
00081
00082 printf("converting %s file %s to %s file %s\n",
00083 FileType[inptype], inpfile, FileType[outtype], outfile);
00084 if (links)
00085 printf ("links will be included in output file\n");
00086 fflush(stdout);
00087
00088 ts = time (NULL);
00089 if (cgio_open_file (tempfile, 'w', outtype, &outcg))
00090 cgio_error_exit("cgio_open_file");
00091 if (cgio_copy_file (inpcg, outcg, links))
00092 cgio_error_exit("cgio_copy_file");
00093 if (cgio_close_file (inpcg) || cgio_close_file (outcg))
00094 cgio_error_exit("cgio_close_file");
00095 te = time (NULL);
00096
00097 unlink (outfile);
00098 if (rename (tempfile, outfile)) {
00099 fprintf (stderr, "rename %s -> %s failed", tempfile, outfile);
00100 exit (1);
00101 }
00102
00103 if (stat (outfile, &outst)) {
00104 fprintf (stderr, "can't stat %s\n", outfile);
00105 exit (1);
00106 }
00107
00108 printf ("%-4s input file size = %ld bytes\n",
00109 FileType[inptype], (long)inpst.st_size);
00110 printf ("%-4s output file size = %ld bytes\n",
00111 FileType[outtype], (long)outst.st_size);
00112 printf ("conversion time = %d secs\n", (int)(te - ts));
00113 return 0;
00114 }
00115