Exemple #1
0
int main(int argc, char* argv[]) {
    int result = parse_cmd_options(argc, argv, &opts);
    int retval;

    if (result != -1)
        return result;

    retval = 0;
    if (argc - optind < 2) {
        /* not enough arguments */
        usage(argv);
        retval = 2;
    } else {
        if (!sscanf(argv[optind], "%ld", &opts.num_samples)) {
            fprintf(stderr, "Format of num_samples parameter is invalid.\n");
            retval = 3;
        } else if (!sscanf(argv[optind+1], "%lg", &opts.gamma)) {
            fprintf(stderr, "Format of gamma parameter is invalid.\n");
            retval = 4;
        } else {
            srand(opts.use_seed ? opts.seed : time(0));
            mt_init(&rng);
            retval = opts.continuous ? sample_continuous() : sample_discrete();
        }
    }

    return retval;
}
Exemple #2
0
int main(int argc, char * argv[]) {
    struct cmd_options opts = parse_cmd_options(argc, argv);

    FILE * fin = fopen(opts.infname, "r");
    handle_fopen_error(fin, argv[0], "error opening input file");
    FILE * fout = fopen(opts.outfname, "w");
    handle_fopen_error(fout, argv[0], "error opening output file");

    int64_t orig_width = 0, orig_height = 0;
    size_t entriesn = get_orig_dimensions(&orig_width, &orig_height, fin);

    fseek(fin, 0, SEEK_SET);

    struct point_set set = {};

    set.ratio.x = get_ratio(opts.width, orig_width);
    set.ratio.y = get_ratio(opts.height, orig_height);

    size_t alloc_size = MAX(set.ratio.x, set.ratio.y) * entriesn;

    set.vec = calloc(alloc_size, sizeof *set.vec);
    if (set.vec == NULL) {
        fprintf(stderr, "%s: not enough memory\n", argv[0]);
        return EXIT_FAILURE;
    }

    fill_set(&set, fin);
    fclose(fin);

    for (size_t i = 0; i < set.size; i++) {
        fprintf(fout, "%.100Lg %.100Lg\n", set.vec[i].x, set.vec[i].y);
    }

    // kernel will take a better care of this:
    // free(set.vec);
    // free(opts.infname);
    // free(opts.outfname);
    // fclose(fout);
    // so don't mind the valgrind "leaks"

    return EXIT_SUCCESS;
}
Exemple #3
0
int main(int argc, char* argv[]) {
    int result = parse_cmd_options(argc, argv, &opts);
    int i, retval;

    if (result != -1)
        return result;

    srand(opts.use_seed ? opts.seed : ((unsigned int)time(0)));
    mt_init(&rng);

    retval = 0;
    if (optind == argc) {
        /* no more arguments, process stdin */
        process_file(stdin, "-");
    } else {
        for (i = optind; i < argc; i++) {
            FILE* f;
            
            if (argv[i][0] == '-')
                f = stdin;
            else
                f = fopen(argv[i], "r");

            if (!f) {
                perror(argv[i]);
                retval = 2;
                continue;
            }

            process_file(f, argv[i]);
            fclose(f);
        }
    }

    return retval;
}
Exemple #4
0
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  qDebug("QT_VERSION=0x%08x", QT_VERSION);

  std::string mission_file;

  // обработать опции командной строки (UNIX-way)
#ifndef QPLOT_WIN32
  mission_file = parse_cmd_options(argc, argv);
#else
  ini_file = QPLOT_INI_FILE;
  if (argc == 2)
     mission_file = argv[1];
#endif

  // Qt приложение
  QApplication app(argc, argv);

  // главное окно приложения (единственное)
  PlotWin pw;
 
  // открыть INI-файл
  setlocale(LC_NUMERIC, "C"); // десятичная точка = '.'
  aclass::aini f(ini_file);
  
  // прочитать название главного окна приложения
  std::string title = f.read_str("", "title", "");
  if (title.size()) pw.setWindowTitle(_QS(title));
  
  // прочитать положение и размеры главного окна приложения
  int w = pw.size().width();
  int h = pw.size().height();
  int x = f.read_long("", "x", -1);
  int y = f.read_long("", "y", -1);
  w = f.read_long("", "width",  w);
  h = f.read_long("", "height", h);
  if (x >= 0 && y >= 0) pw.move(x, y);
  if (w >  0 && h >  0) pw.resize(w, h);

  if (f.read_bool("", "maximized", false))
    pw.setWindowState(Qt::WindowMaximized);
  if (f.read_bool("", "fullscreen", false))
    pw.setWindowState(Qt::WindowFullScreen);

  // прочитать секцию [area] и настроить PlotAreaConf
  PlotAreaConf conf = pw.pa()->getConf();
  qplot_read_conf(&f, "area", &conf);
  pw.pa()->setConf(conf);

  // показать главное окно
  pw.show();

  // построить графики по заданию или включить демо
  if (mission_file != "")
  { // файл задания указан в командной строке
    if (qplot_run(mission_file.c_str(), pw.pa(), &pw))
      pw.showInfo("Success"); // FIXME
    else
      qplot_demo(pw.pa());
  }
  else
    qplot_demo(pw.pa());
  
  int retv = app.exec();

  // сохранить положение и размер главного окна приложения
  x = pw.pos().x();
  y = pw.pos().y();
  w = pw.size().width();
  h = pw.size().height();
  if (pw.windowState() & Qt::WindowMaximized)
    f.write_bool("", "maximized", true);
  else if (pw.windowState() & Qt::WindowFullScreen)
    f.write_bool("", "fullscreen", true);
  else
  {
    f.write_long("", "x",      x);
    f.write_long("", "y",      y);
    f.write_long("", "width",  w);
    f.write_long("", "height", h);
    if (f.has_ident("", "maximized"))  f.write_bool("", "maximized",  false);
    if (f.has_ident("", "fullscreen")) f.write_bool("", "fullscreen", false);
  }

  return retv;
}
Exemple #5
0
int main (int argc, char **argv)
{
	enum ParserState state = 0;
	gunichar in;
	gboolean init = FALSE;
	/* used to parse attribute */
	gint attributes = 0;
	/* Input/output */
	FILE *input = stdin;
	FILE  *output = stdout;
	/* Error */
	GError *error = NULL;

	/* init the glib system  */
	g_type_init();
	/* needed to get the right charset from g_get_charset */
	setlocale(LC_ALL, "");
	/* Get charset */
	g_get_charset(&input_charset);

	/* parse options */
	parse_cmd_options(&argc, &argv);

	/* Handle input file */
	if(input_file && strcmp(input_file, "-")) {
		input = g_fopen(input_file, "r");
		EXCEPTION(input == NULL, "Failed to open: %s: %s\n", input_file, strerror(errno));
	}
	/* Handle output file */
	if(output_file) {
		output = g_fopen(output_file, "w");
		EXCEPTION(output == NULL, "Failed to open: %s: %s\n", output_file, strerror(errno));
	}
	/* Create channel for input */
	GIOChannel *chan = g_io_channel_unix_new(fileno(input));
	/* Set channel encoding */
	g_io_channel_set_encoding(chan, input_charset,&error);
	EXCEPTION(error != NULL, "Failed to set input encoding: %s\n", error->message);

	/* Read input */
	while(g_io_channel_read_unichar(chan, &in, &error) == G_IO_STATUS_NORMAL && error == NULL)
	{
		if(!init) {
			/* Output html header */
			print_page_header(output);
			/* Convert and print body */
			fprintf(output, "<pre style='font-family:monospace'>\n");
			init = TRUE;
		}
		/* If we hit the escape character, go into 'attribute parsing' mode */
		if(in == ESCAPE_CHAR) {
			state = PARSE_ATTRIBUTE;
			/* reset */
			attributes = 0;
		}
		/* if we are in attribute parsing mode, parse attribute */
		else if(state == PARSE_ATTRIBUTE) {
			if(in == '[') {
				/* Begin of attributes */
				state = PARSE_COLOR_ATTRIBUTE;
			}else {
				WARNING("Unknown Escape sequence found: %i\n", in);
				state = PARSE_NORMAL;
			}
		} else if(state == PARSE_COLOR_ATTRIBUTE) {
			if (in == ';') { /* End of element */
				process_attribute(output, attributes);
				attributes = 0;
			} else if(in == 'm') { /* end of attribute */
				process_attribute(output, attributes);
				state = PARSE_NORMAL;
			} else if(in >= '0' && in <= '9') {
				attributes *= 10;
				attributes += in-'0';
			}else if (in == 'h' || in == 'l' ) {
				WARNING("Unsupported attribute found: %i\n", in);
				state = PARSE_NORMAL;
			}
			continue;
		} else if (state == PARSE_NORMAL) {
			/* special chars (htmlspecialchars php doc) */
			if(in == '"') fputs("&quot;", output);
			else if(in == '\'') fputs("&#039;", output);
			else if(in == '&') fputs("&amp;", output);
			else if(in == '<') fputs("&lt;", output);
			else if(in == '>') fputs("&gt;", output);
			/* ascii values stay ascii*/
			else if(in >= 0 && in <= 177)
				fprintf(output, "%c", (char)in);
			/* Rest we encode in utf8 */
			else fprintf(output, "&#%i;", in);
		}
	}
	EXCEPTION(error != NULL, "Failed to read input character: %s\n", error->message);
	if(init) {
		/* Close open tags */
		process_attribute(output, 0);
		fprintf(output,"\n  </pre>\n");
		print_page_footer(output);
	}

	/* free input channel */
	g_io_channel_unref(chan);

	/* close i/o */
	if(input != stdin) fclose(input);
	if(output != stdout) fclose(output);

	return EXIT_SUCCESS;
}