⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfg2inp.c

📁 蒙特卡罗模拟光子成像C语言版,代码简洁专业
💻 C
字号:
/****************************************************************************
 * This file is part of tMCimg.
 * 
 * tMCimg is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "tMCimg.h"
#include "mcparse.h"

static int dump_config(const struct Config *, const char *);

#if (HAVE_GETOPT_LONG)
# if (HAVE_GETOPT_H)
# include <getopt.h>
# endif

extern char *optarg;
extern int optind, optopt;

static struct option lopt[3] = {
  { "output", 1, NULL, 'o' },
  { "help",   0, NULL, 'h' },
  { NULL,     0, NULL,  0  }
};
#else
# if (HAVE_GETOPT)
#  include <unistd.h>

extern char *optarg;
extern int optind, optopt;
# endif
#endif

static void usage(const char *s)
{
#if ((HAVE_GETOPT_LONG) || (HAVE_GETOPT))
  printf("SYNTAX: %s [--output file] [--help]\n", s);
#else
  printf("SYNTAX: %s [-o file]\n", s);
#endif

  return;
}

int main(int argc, char *argv[])
{
  char *output_file = "-";
  FILE *fp;  
  int n;

#if ((HAVE_GETOPT_LONG) || (HAVE_GETOPT))
# if (HAVE_GETOPT_LONG)
  while ((n = getopt_long(argc, argv, "o:h", lopt, NULL)) != -1)
#else /* !HAVE_GETOPT_LONG implies HAVE_GETOPT */
  while ((n = getopt(argc, argv, "o:h")) != -1)
#endif
    switch (n)
      {
      case 'o':
#if HAVE_STRDUP
        output_file = strdup(optarg);
#else
# error "What do you mean, no strdup()?"
#endif
        break;
      case 'h':
        usage(argv[0]);
        return 0;
      case ':':
        printf("Error - missing argument\n");
        usage(argv[0]);
        return 1;
      case '?':  /* unrecognized character */
      default:
        printf("Error - unknown flag '%c'\n", optopt);
        usage(argv[0]);
        return 1;
      }

  if (optind == argc)
    {
      usage(argv[0]);
      return 1;
    }
  else
    do
      {
        struct Config cfg;

        if ((fp = fopen(argv[optind++], "rt")) == NULL) 
          {
            perror("Error opening file");
            printf("Can't open file \"%s\"\n", argv[1]);
            return 1;
          } 

        memset(&cfg, 0, sizeof(struct Config));

        if (parseconfig(&cfg, fp))
          return 1;

        fclose(fp);

        if (dump_config(&cfg, output_file))
          return 1;
      }
    while (optind < argc);
#else  /* !HAVE_GETOPT && !HAVE_GETOPT_LONG */

  /* Under windows, we have to do things the hard way */

  if (argc == 1)
    {
      /* No arguments at all, just print a helpful message and exit */

      usage(argv[0]);
      return 0;
    }

  for (n = 1; n < argc; n++)
    if ((argv[n][0] == '-') || (argv[n][0] == '/'))
      {
        switch (argv[n][1])
          {
          case 'o':
#if HAVE_STRDUP
            output_file = strdup(argv[++n]);
#else
# error "What do you mean, no strdup()?"
#endif
            break;
          case 'h':
            usage(argv[0]);
            return 0;
          default:
            fprintf(stderr, "Error: unknown flag '%c'\n", argv[n][1]);
            usage(argv[0]);

            return 1;
          }
      }
    else
      {
        struct Config cfg;

        if ((fp = fopen(argv[n], "rt")) == NULL) 
          {
            perror("Error opening file");
            printf("Can't open file \"%s\"\n", argv[n]);
            return 1;
          } 

        memset(&cfg, 0, sizeof(struct Config));

        if (parseconfig(&cfg, fp))
          return 1;

        fclose(fp);

        if (dump_config(&cfg, output_file))
          return 1;
      }
#endif

  return 0;
}

static int dump_config(const struct Config *config, const char *output)
{
  FILE *fp;
  int i;

  fp = stdout;

  if ((output[0] != '-') || (output[1] != '\0'))
    {
      fp = fopen(output, "w");

      if (fp == NULL)
	{
	  perror("Can't write output file");
	  return 1;
	}
    }

  fprintf(fp, "%d\n",  config->NT);
  fprintf(fp, "%ld\n", config->seed);
  fprintf(fp, "%f\n",  config->frequency);

  fprintf(fp, "%e %e %d\n", config->Tmin, config->stepT, config->nTstep);
  fprintf(fp, "%s\n", config->segFile);

  /* Add 1 for legacy support */
  fprintf(fp, "%f %d %d %d\n",
	  config->xstep, config->nxstep, config->Ixmin + 1,
	  config->Ixmax + 1);
  fprintf(fp, "%f %d %d %d\n", config->ystep, config->nystep,
	  config->Iymin + 1, config->Iymax + 1);
  fprintf(fp, "%f %d %d %d\n", config->zstep, config->nzstep,
	  config->Izmin + 1, config->Izmax + 1);

  fprintf(fp, "%d\n", config->Ntissue);

  for (i = 1; i <= config->Ntissue; i++)
    fprintf(fp, "%f %f %f %f\n", config->tmus[i],
	    config->tg[i], config->tmua[i], config->tn[i]);

  fprintf(fp, "%d %f %f %f %f\n", config->nSrcs, config->srcRad[0],
          config->srcDir[0][0], config->srcDir[0][1], config->srcDir[0][2]);

  for (i = 0; i < config->nSrcs; i++)
    fprintf(fp, "%f %f %f\n", config->srcLoc[i][0], 
            config->srcLoc[i][1], config->srcLoc[i][2]);

  fprintf(fp, "%d %f\n", config->nDets, config->detRad[0]);

  for (i = 0; i < config->nDets; i++)
    fprintf(fp, "%f %f %f\n",
	    config->detLoc[i][0], config->detLoc[i][1], config->detLoc[i][2]);

  if (fp != stdout)
    fclose(fp);

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -