"Fossies" - the Fresh Open Source Software Archive

Member "gfsview-snapshot-121130/batch/gfsview-batch.c" (30 Nov 2012, 6810 Bytes) of package /linux/privat/gfsview-snapshot-121130.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gfsview-batch.c" see the Fossies "Dox" file reference documentation.

    1 /* Gerris - The GNU Flow Solver
    2  * Copyright (C) 2001-2010 National Institute of Water and
    3  * Atmospheric Research
    4  *
    5  * This program is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU General Public License as
    7  * published by the Free Software Foundation; either version 2 of the
    8  * License, or (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   13  * General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program; if not, write to the Free Software
   17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   18  * 02111-1307, USA.  
   19  */
   20 
   21 #include "config.h"
   22 
   23 #include <unistd.h>
   24 #include <sys/types.h>
   25 #include <sys/stat.h>
   26 #include <errno.h>
   27 #include <getopt.h>
   28 #include <gfs.h>
   29 
   30 #include "render.h"
   31 
   32 static void read_commands (GtsFile * fp, 
   33                GList ** list, 
   34                GfsGlViewParams * view, 
   35                GfsSimulation * sim)
   36 {
   37   while (fp->type == GTS_STRING)
   38     if (!strcmp (fp->token->str, "View"))
   39       gfs_gl_view_params_read (view, fp);
   40     else if (!strcmp (fp->token->str, "Clear")) {
   41       g_list_foreach (*list, (GFunc) gts_object_destroy, NULL);
   42       g_list_free (*list);
   43       *list = NULL;
   44       gts_file_next_token (fp);
   45     }
   46     else if (!strcmp (fp->token->str, "Echo")) {
   47       gts_file_next_token (fp);
   48       if (fp->type != '{') {
   49     gts_file_error (fp, "expecting an opening brace");
   50     break;
   51       }
   52       guint scope = fp->scope_max;
   53       gint c = gts_file_getc (fp);
   54       while (c != EOF && fp->scope > scope) {
   55     putchar (c);
   56     c = gts_file_getc (fp);
   57       }
   58       fflush (stdout);
   59       if (fp->scope != scope) {
   60     gts_file_error (fp, "parse error");
   61     break;
   62       }
   63       gts_file_next_token (fp);
   64     }
   65     else if (!strcmp (fp->token->str, "Save") || !strcmp (fp->token->str, "Append")) {
   66       GfsGl2PSParams p;
   67       GfsOutputFile * out = NULL;
   68       FILE * fptr = NULL;
   69 
   70       gts_file_next_token (fp);
   71       if (fp->type != GTS_STRING) {
   72     gts_file_error (fp, "expecting a string (filename)");
   73     break;
   74       }
   75       if (!strcmp (fp->token->str, "Save"))
   76     fptr = (!strcmp (fp->token->str, "stdout") ? stdout :
   77         !strcmp (fp->token->str, "stderr") ? stderr :
   78         fopen (fp->token->str, "w"));
   79       else { /* Append */
   80     if ((out = gfs_output_file_open (fp->token->str, "w")))
   81       fptr = out->fp;
   82       }
   83       if (fptr == NULL) {
   84     gts_file_error (fp, "cannot open file `%s'", fp->token->str);
   85     break;
   86       }
   87       gts_file_next_token (fp);
   88       gfs_gl2ps_params_read (&p, fp);
   89       if (fp->type != GTS_ERROR)
   90     gfs_gl_osmesa_render (&p, sim, view, *list, fptr, FALSE);
   91       if (out) {
   92     /* Append mode, just free memory, do not close file */
   93     out->refcount++;
   94     gfs_output_file_close (out);
   95       }
   96       else if (fptr != stdout && fptr != stderr)
   97     fclose (fptr);
   98     }
   99     else { /* GfsGl objects */
  100       GfsGl * gl;
  101 
  102       if ((gl = gfs_gl_new_from_file (fp))) {
  103     gl->p = view;
  104     if (sim)
  105       gfs_gl_set_simulation (gl, sim);
  106     *list = g_list_append (*list, gl);
  107       }
  108       else if (fp->type != GTS_ERROR) {
  109     gts_file_error (fp, "unknown keyword `%s'", fp->token->str);
  110     break;
  111       }
  112     }
  113 }
  114 
  115 static GfsSimulation * read_simulation (GtsFile * fp, GList * list)
  116 {
  117   GfsSimulation * sim = gfs_simulation_read (fp);
  118   
  119   if (sim == NULL)
  120     return NULL;
  121   
  122   gfs_simulation_init (sim);
  123   g_list_foreach (list, (GFunc) gfs_gl_set_simulation, sim);
  124   return sim;
  125 }
  126 
  127 int main (int argc, char * argv[])
  128 {
  129   GfsGlViewParams view;
  130   GfsSimulation * sim = NULL;
  131   GList * list = NULL;
  132   int c = 0;
  133 
  134   /* initialize gfs */
  135   gfs_init (&argc, &argv);
  136 
  137   /* initialize gfsgl */
  138   gfs_gl_init ();
  139 
  140   /* options */
  141   while (c != EOF) {
  142     static struct option long_options[] = {
  143       {"survive-broken-pipe", no_argument, NULL, 's'},
  144       {"help", no_argument, NULL, 'h'},
  145       {"version", no_argument, NULL, 'V'},
  146       { NULL }
  147     };
  148     int option_index = 0;
  149     switch ((c = getopt_long (argc, argv, "hVs", long_options, &option_index))) {
  150     case 's':
  151       /* These options are ignored, they are here for command-line compatibility with
  152      GfsView interactive version */
  153       break;
  154     case 'h': /* help */
  155       fprintf (stderr,
  156              "Usage: gfsview-batch [OPTION] FILE1 FILE2 ...\n"
  157          "The Gerris flow solver visualisation tool (batch mode).\n"
  158          "\n"
  159          "  -h    --help                display this help and exit\n"
  160          "  -V    --version             output version information and exit\n"
  161          "\n"
  162          "Reports bugs to %s\n",
  163          FTT_MAINTAINER);
  164       return 0; /* success */
  165       break;
  166     case 'V': /* version */
  167       fprintf (stderr,
  168            "gfsview-batch: using %dD libgfs version %s\n",
  169            FTT_DIMENSION, VERSION);
  170       return 0; /* succes */
  171       break;
  172     case '?': /* wrong options */
  173       fprintf (stderr, "Try `gfsview-batch --help' for more information.\n");
  174       return 1; /* failure */
  175     }
  176   }
  177 
  178   gfs_gl_view_params_init (&view);
  179   
  180   /* read files on command line */
  181   for (c = optind; c < argc; c++) {
  182     FILE * fptr = gfs_gl_popen (argv[c]);
  183     GtsFile * fp;
  184 
  185     if (fptr == NULL) {
  186       fprintf (stderr, "gfsview-batch: cannot open file `%s'\n", argv[c]);
  187       return 1;
  188     }
  189     fp = gts_file_new (fptr);
  190     while (fp->type != GTS_ERROR && !feof (fptr)) {
  191       if (fp->type == '\n')
  192     gts_file_next_token (fp);
  193       else if (fp->type == GTS_INT || !strcmp (fp->token->str, "GModule")) {
  194     GfsSimulation * sim1 = read_simulation (fp, list);
  195     if (sim1) {
  196       if (sim)
  197         gts_object_destroy (GTS_OBJECT (sim));
  198       sim = sim1;
  199     }
  200       }
  201       else if (fp->type == GTS_STRING)
  202     read_commands (fp, &list, &view, sim);
  203       else
  204     gts_file_error (fp, "expecting an integer got %d", fp->type);
  205     }
  206     if (fp->type == GTS_ERROR) {
  207       fprintf (stderr, "gfsview-batch: %s:%d:%d: %s\n", argv[c], fp->line, fp->pos, fp->error);
  208       return 1;
  209     }
  210     gts_file_destroy (fp);
  211     pclose (fptr);
  212   }
  213 
  214   /* wait for parameters/simulations on standard input */
  215   while (1) {
  216     GtsFile * fp = gts_file_new (stdin);
  217 
  218     while (fp->type != GTS_ERROR)
  219       if (feof (stdin))
  220     return 0;
  221       else if (fp->type == '\n')
  222     gts_file_next_token (fp);
  223       else if (fp->type == GTS_INT) {
  224     GfsSimulation * sim1 = read_simulation (fp, list);
  225     if (sim1) {
  226       if (sim)
  227         gts_object_destroy (GTS_OBJECT (sim));
  228       sim = sim1;     
  229     }
  230       }
  231       else if (fp->type == GTS_STRING)
  232     read_commands (fp, &list, &view, sim);
  233       else
  234     gts_file_error (fp, "expecting an integer got %d", fp->type);
  235 
  236     fprintf (stderr, "gfsview-batch: <stdin>:%d:%d: %s\n", fp->line, fp->pos, fp->error);
  237     return 1;
  238   }
  239   
  240   return 0;
  241 }