Esempio n. 1
0
int			main(int ac, char **av)
{
	t_opt	opt;
	int		index;
	t_list	*lst_f;
	t_list	*lst_d;
	t_sort	pt_arg;

	lst_f = NULL;
	lst_d = NULL;
	index = check_opt(ac, av, &opt);
	if (index == ac)
		ft_ls(".", opt);
	else
	{
		split_arg(&lst_f, &lst_d, av + index, ac - index);
		pt_arg = select_sort(opt);
		sort(&lst_f, pt_arg, opt);
		sort(&lst_d, pt_arg, opt);
		print(lst_f, opt);
		if (lst_f && lst_d)
			ft_putchar('\n');
		opt.lf_dir = 1;
		deal_dir(lst_d, opt, ac, index);
		free_arg(&lst_f, &lst_d);
	}
	return (0);
}
Esempio n. 2
0
void	cd_opt(int *i, int *p, char **ar)
{
	int j;

	j = 0;
	while (ar[*i] != NULL && ar[*i][0] == '-')
	{
		j = 0;
		if (check_opt(ar[*i]) == 1)
		{
			while (ar[*i][j] != '\0')
			{
				if (j >= 1 && ar[*i][j] == 'P')
					*p = 1;
				else if (j >= 1 && ar[*i][j] == 'L')
					*p = 0;
				j++;
			}
			*i = *i + 1;
		}
		else
		{
			p = 0;
			return ;
		}
	}
	return ;
}
Esempio n. 3
0
void _opt_register(const char *names, enum opt_type type,
		   char *(*cb)(void *arg),
		   char *(*cb_arg)(const char *optarg, void *arg),
		   void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
		   void *arg, const char *desc)
{
	struct opt_table opt;
	opt.names = names;
	opt.type = type;
	opt.cb = cb;
	opt.cb_arg = cb_arg;
	opt.show = show;
	opt.arg = arg;
	opt.desc = desc;
	check_opt(&opt);
	add_opt(&opt);
}
Esempio n. 4
0
void		check_arg(int argc, char **argv) {
  t_option	option;
  int		i;
  
  init_option(&option);
  i = 0;
  while (i < argc) {
    if (argv[i][0] == '-') {
      if (!check_opt(argv[i], &option)) {
	my_putstr("Usage: my_ls [-lR1rdtaAL] [dir1 [...]]\n");
	return ;
      }
    }
    i++;
  }
  check_files(argc, argv, &option);
}
Esempio n. 5
0
File: main.c Progetto: w4kfu/Stunts
int main(int argc, char **argv)
{
        int fd;
        struct stat st;
        unsigned char *buf = NULL;
	struct conf_c conf = {0};
	struct s_comp comp = {0};

	parse_opt(argc, argv, &conf);
	check_opt(&conf, argv[0]);
        fd = open(conf.in, O_RDONLY);
        if (fd == -1)
        {
                perror("open()");
                exit(EXIT_FAILURE);
        }
        if (fstat(fd, &st) == -1)
        {
                perror("fstat()");
                exit(EXIT_FAILURE);
        }
        if ((buf = malloc(sizeof (char) * st.st_size)) == NULL)
        {
                perror("malloc()");
                exit(EXIT_FAILURE);
        }
        if (read(fd, buf, st.st_size) != st.st_size)
        {
                perror("read()");
                goto clean;
        }
	huff(buf, st.st_size, &comp);
	if (comp.tree)
	{
		if (conf.dot)
			dotty(comp.tree, conf.dot);
		uncomp(&comp, buf + st.st_size);
		if (comp.buf_out)
			dump_to_file(conf.out, comp.buf_out, comp.size);
	}
clean:
        free(buf);
        close(fd);
        return 0;
}
Esempio n. 6
0
int			check_param(char **param, t_opt *opt)
{
	int		i;
	int		j;

	i = 1;
	j = 1;
	while (param[i])
	{
		if (param[i][0] == '-')
		{
			check_opt(param[i], opt);
			j++;
		}
		i++;
	}
	return (j);
}
Esempio n. 7
0
int			parser(int ac, char **av)
{
	int	f;
	int	i;

	g_rec = 0;
	f = 0;
	i = 1;
	while (ac > i && av[i][0] == '-')
	{
		if (av[i][0] == '-' && av[i][1])
		{
			check_opt(av[i]);
			f++;
		}
		i++;
	}
	return (f);
}
Esempio n. 8
0
int					bi_echo(char **arg, t_duo **env)
{
	int i;

	i = 1;
	(void)env;
	check_opt(arg, &i);
	if (tbl_len(arg) > 1)
	{
		while (arg && arg[i])
		{
			ft_putstr_visible(arg[i]);
			if (arg[i + 1])
				ft_putchar(' ');
			i++;
		}
	}
	ft_putchar('\n');
	return (TRUE);
}
Esempio n. 9
0
void opt_register_table(const struct opt_table entry[], const char *desc)
{
	unsigned int i, start = opt_count;

	if (desc) {
		struct opt_table heading = OPT_SUBTABLE(NULL, desc);
		add_opt(&heading);
	}
	for (i = 0; entry[i].type != OPT_END; i++) {
		if (entry[i].type == OPT_SUBTABLE)
			opt_register_table(subtable_of(&entry[i]),
					   entry[i].desc);
		else {
			check_opt(&entry[i]);
			add_opt(&entry[i]);
		}
	}
	/* We store the table length in arg ptr. */
	if (desc)
		opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
}
Esempio n. 10
0
static int			handle_cd_arg(int *i, int *ret, char **arg)
{
	char				*tmp;

	tmp = get_env("OLDPWD");
	if (check_opt(arg, i) == ERROR)
	{
		ft_strdel(&tmp);
		return (FALSE);
	}
	if (!arg[*i])
		*ret = cd_home();
	else if (arg[*i] && arg[*i][0] == '-' && !arg[*i][1])
	{
		if (tmp)
			*ret = change_dir(tmp);
		else
			return (sh_error(TRUE, 11, NULL, NULL));
	}
	else
		*ret = change_dir(arg[*i]);
	ft_strdel(&tmp);
	return (TRUE);
}
Esempio n. 11
0
int main(int argc, const char **argv) {

	const char *disk = "pure64.img";

	const char *config = "pure64-config.txt";

	int i = 1;

	while (i < argc) {
		if (check_opt(argv[i], "help", 'h')) {
			print_help(argv[0]);
			return EXIT_FAILURE;
		} else if (check_opt(argv[i], "disk", 'd')) {
			disk = argv[i + 1];
			i += 2;
		} else if (check_opt(argv[i], "config", 'c')) {
			config = argv[i + 1];
			i += 2;
		} else if (is_opt(argv[i])) {
			fprintf(stderr, "Unknown option '%s'.\n", argv[i]);
			return EXIT_FAILURE;
		} else {
			break;
		}
	}

	if (disk == NULL) {
		fprintf(stderr, "No disk specified after '--disk' or '-d' option.\n");
		return EXIT_FAILURE;
	}

	if (config == NULL) {
		fprintf(stderr, "No configuration file specified after '--config' or '-c' option.\n");
		return EXIT_FAILURE;
	}

	if (i >= argc) {
		fprintf(stderr, "No command specified (see '--help').\n");
		return EXIT_FAILURE;
	}

	argv = &argv[i];
	argc -= i;

	/* argv[0] should now point to a command. */

	const char *command = argv[0];

	argv = &argv[1];
	argc--;

	/* argv[0] is now the command arguments */

	if (strcmp(command, "init") == 0) {
		return pure64_init(config, disk, argc, argv);
	}

	struct pure64_util util;

	pure64_util_init(&util);

	int err = pure64_util_open_config(&util, config);
	if (err != 0) {
		fprintf(stderr, "Failed to open '%s': %s\n", config, pure64_strerror(err));
		pure64_util_done(&util);
		return EXIT_FAILURE;
	}

	err = pure64_util_open_disk(&util, disk);
	if (err != 0) {
		fprintf(stderr, "Failed to open '%s': %s\n", disk, pure64_strerror(err));
		pure64_util_done(&util);
		return EXIT_FAILURE;
	}

	int exit_code = EXIT_SUCCESS;

	if (strcmp(command, "cat") == 0) {
		exit_code = pure64_cat(&util, argc, argv);
	} else if (strcmp(command, "cp") == 0) {
		exit_code = pure64_cp(&util, argc, argv);
	} else if (strcmp(command, "ls") == 0) {
		exit_code = pure64_ls(&util, argc, argv);
	} else if (strcmp(command, "mkdir") == 0) {
		exit_code = pure64_mkdir(&util, argc, argv);
	} else {
		fprintf(stderr, "Unknown command '%s'.\n", command);
		pure64_util_done(&util);
		return EXIT_FAILURE;
	}

	if (exit_code != EXIT_SUCCESS) {
		pure64_util_done(&util);
		return EXIT_FAILURE;
	}

	err = pure64_util_save_disk(&util);
	if (err != 0) {
		fprintf(stderr, "Failed to save disk changes.\n");
		pure64_util_done(&util);
		return EXIT_FAILURE;
	}

	pure64_util_done(&util);

	return EXIT_SUCCESS;
}
Esempio n. 12
0
static void setup(int argc, char *argv[])
{
	const char *msg;
	msg = parse_opts(argc, argv, options, help);
	if (msg != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

	/* if client_num is not set, use num of processors */
	clients_num = sysconf(_SC_NPROCESSORS_ONLN);

	check_opt("a", aarg, &clients_num, 1);
	check_opt("r", rarg, &client_max_requests, 1);
	check_opt("R", Rarg, &server_max_requests, 1);
	check_opt("n", narg, &client_msg_size, 1);
	check_opt("N", Narg, &server_msg_size, 1);
	check_opt("q", qarg, &tfo_queue_size, 1);
	check_opt_l("T", Targ, &wait_timeout, 0L);

	if (!force_run)
		tst_require_root(NULL);

	if (!force_run && tst_kvercmp(3, 7, 0) < 0) {
		tst_brkm(TCONF, NULL,
			"Test must be run with kernel 3.7 or newer");
	}

	/* check tcp fast open knob */
	if (!force_run && access(tfo_cfg, F_OK) == -1)
		tst_brkm(TCONF, NULL, "Failed to find '%s'", tfo_cfg);

	if (!force_run) {
		SAFE_FILE_SCANF(NULL, tfo_cfg, "%d", &tfo_cfg_value);
		tst_resm(TINFO, "'%s' is %d", tfo_cfg, tfo_cfg_value);
	}

	tst_sig(FORK, DEF_HANDLER, cleanup);

	tst_resm(TINFO, "TCP %s is using %s TCP API.",
		(tcp_mode == TCP_SERVER) ? "server" : "client",
		(fastopen_api == TFO_ENABLED) ? "Fastopen" : "old");

	switch (tcp_mode) {
	case TCP_SERVER:
		tst_resm(TINFO, "max requests '%d'",
			server_max_requests);
		tcp.init	= server_init;
		tcp.run		= server_run;
		tcp.cleanup	= server_cleanup;
		tfo_bit_num = 2;
	break;
	case TCP_CLIENT:
		tst_resm(TINFO, "connection: %s:%s",
		server_addr, tcp_port);
		tst_resm(TINFO, "client max req: %d", client_max_requests);
		tst_resm(TINFO, "clients num: %d", clients_num);
		tst_resm(TINFO, "client msg size: %d", client_msg_size);
		tst_resm(TINFO, "server msg size: %d", server_msg_size);

		tcp.init	= client_init;
		tcp.run		= client_run;
		tcp.cleanup	= client_cleanup;
		tfo_bit_num = 1;
	break;
	}

	tfo_support = TFO_ENABLED == tfo_support;
	if (((tfo_cfg_value & tfo_bit_num) == tfo_bit_num) != tfo_support) {
		int value = (tfo_cfg_value & ~tfo_bit_num)
			| (tfo_support << (tfo_bit_num - 1));
		tst_resm(TINFO, "set '%s' to '%d'", tfo_cfg, value);
		SAFE_FILE_PRINTF(cleanup, tfo_cfg, "%d", value);
		tfo_cfg_changed = 1;
	}

	int reuse_value = 0;
	SAFE_FILE_SCANF(cleanup, tcp_tw_reuse, "%d", &reuse_value);
	if (!reuse_value) {
		SAFE_FILE_PRINTF(cleanup, tcp_tw_reuse, "1");
		tw_reuse_changed = 1;
		tst_resm(TINFO, "set '%s' to '1'", tcp_tw_reuse);
	}

	tst_resm(TINFO, "TFO support %s",
		(tfo_support) ? "enabled" : "disabled");

	tcp.init();
}
Esempio n. 13
0
int csr_start(int argc, char *argv[])
{

  int error;
  struct Parameter para;
  
  int N, NNZ;
  double *bvec, *xvec, *val;
  int *col, *ptr;
  double *Tval;
  int *Tcol, *Tptr;

  double *test;
  malloc_cuda_1d(10, test);
  free_cuda_1d(test);

  init_ver(&para);

  error = get_opt(argc, argv, &para);
  if(error_handle(error, (char*)"error in get_cmd")!=0)
    return -1;

  error = check_opt(&para);
  if(error_handle(error, (char*)"error in check_cmd")!=0)
    return -1;

  error = find_mat(&para);
  if(error_handle(error, (char*)"error in find_mat")!=0)
    return -1;

  show_opt(&para);

  error = set_openmp_thread(para.i_thread);
  if(error_handle(error, (char*)"error in set_openmp_thread")!=0)
    return -1;

  error = get_mat_head(&para, &N, &NNZ);
  if(error_handle(error, (char*)"error in get_mat_head")!=0)
    return -1;

  if( para.f_cuda == false )
  {
    bvec = malloc_1d(N);
    xvec = malloc_1d(N);

    val = malloc_1d(NNZ);
    col = malloc_1i(NNZ);
    ptr = malloc_1i(N+1);

    Tval=malloc_1d(NNZ);
    Tcol=malloc_1i(NNZ);
    Tptr=malloc_1i(N+1);
  }else{
    error_log((char*)"Cuda not done now");
    return -1;
  }


  error = get_mat_data(&para, col, ptr, val, bvec, xvec, N, NNZ);
  if(error_handle(error, (char*)"error in get_mat_data")!=0)
    return -1;

  //A^T
  Transpose_d(val, col, ptr, Tval, Tcol, Tptr, N, NNZ);

  error = outer_selecter(&para, bvec, xvec, val, col, ptr, Tval, Tcol, Tptr, N, NNZ);
  if(error_handle(error, (char*)"error in outer_selecter")!=0)
    return -1;

  show_opt(&para);

  if( para.f_cuda == false )
  {
    free_1d(bvec);
    free_1d(xvec);

    free_1d(val);
    free_1i(col);
    free_1i(ptr);

    free_1d(Tval);
    free_1i(Tcol);
    free_1i(Tptr);
  }else{
    error_log((char*)"Cuda not done now");
    return -1;
  }
  return 0;
}
Esempio n. 14
0
void
PolyRoseOptions::parse(int argc, char** argv)
{
  for (int i = 1; i < argc; ++i)
    {
      // Global Polyopt options.
      check_opt(argv[i], "--polyopt-verbose", pr_verbose = true);
      if (! strcmp (argv[i], "--polyopt-help"))
	print_help ();

      // Scop extraction options.
      check_opt(argv[i], "--polyopt-generic-scop-extractor",
		sc_generic_scop_extractor = true);
      check_opt(argv[i], "--polyopt-strict-scop-extractor",
		sc_strict_scop_extractor = true);
      check_opt(argv[i], "--polyopt-unsafe-scop-extractor",
		sc_unsafe_scop_extractor = true);
      sc_unsafe_scop_extractor = ! sc_strict_scop_extractor;
      check_opt(argv[i], "--polyopt-approximate-scop-extractor",
		sc_approximate_scop_extractor = true);
      check_opt(argv[i], "--polyopt-safe-math-func",
		sc_allow_math_func = true);
      check_opt(argv[i], "--polyopt-f2c-scop-extractor",
		sc_f2c_scop_extractor = true);
      check_opt(argv[i], "--polyopt-f2c-scop-extractor",
		sc_approximate_scop_extractor = true);
      check_opt(argv[i], "--polyopt-scop-extractor-verbose=1",
		sc_scop_extractor_verbose_level = 1);
      check_opt(argv[i], "--polyopt-scop-extractor-verbose=2",
		sc_scop_extractor_verbose_level = 2);
      check_opt(argv[i], "--polyopt-scop-extractor-verbose=3",
		sc_scop_extractor_verbose_level = 3);
      check_opt(argv[i], "--polyopt-scop-extractor-verbose=4",
		sc_scop_extractor_verbose_level = 4);
      check_opt(argv[i], "--polyopt-scop-extractor-verbose=5",
		sc_scop_extractor_verbose_level = 5);

      // Verbose option.
      check_opt(argv[i], "--polyopt-quiet",
		polyopt_quiet = true);

      // Dependence analysis.
      check_opt(argv[i], "--polyopt-scalar-privatization",
		polyopt_scalar_privatization = true);

      // Main driver option.
      check_opt(argv[i], "--polyopt-annotate-only",
		polyopt_annotate_only = true);
      check_opt(argv[i], "--polyopt-annotate-inner-loops",
		polyopt_annotate_inner_loops = true);
      check_opt(argv[i], "--polyopt-compute-ddv",
		polyopt_compute_ddv = true);
      check_opt(argv[i], "--polyopt-hullify-ddv",
		polyopt_hullify_ddv = true);
      check_opt(argv[i], "--polyopt-generate-pragmas",
		polyopt_generate_pragmas = true);


      // Main optimization primitives.
      check_opt(argv[i], "--polyopt-fixed-tiling",
		polyopt_fixed_tiling = true);
      check_opt(argv[i], "--polyopt-parametric-tiling",
		polyopt_parametric_tiling = true);
      check_opt(argv[i], "--polyopt-parallel-only",
		polyopt_parallel_only = true);

      // Codegen options.
      check_opt(argv[i], "--polyopt-codegen-use-past",
		polyopt_codegen_use_past = true);
      check_opt(argv[i], "--polyopt-codegen-use-clast",
		polyopt_codegen_use_past = false);

      check_opt(argv[i], "--polyopt-codegen-use-ptile",
		polyopt_codegen_use_ptile = true);
      check_opt(argv[i], "--polyopt-codegen-insert-ptile-api",
		polyopt_insert_ptile_api = true);

      // Global PoCC options.
      check_opt(argv[i], "--polyopt-pocc-verbose", pocc_verbose = true);

      check_opt(argv[i], "--polyopt-vectorizer", pocc_vectorizer = true);
      check_opt(argv[i], "--polyopt-storage-compaction",
		pocc_storage_compaction = true);

      // Pluto options.
      check_opt(argv[i], "--polyopt-pluto", pocc_pluto = true);
      check_opt(argv[i], "--polyopt-pluto-unroll", pocc_pluto_unroll = true);
      check_opt(argv[i], "--polyopt-pluto-ufactor",
		pocc_pluto_ufactor = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-pluto-parallel", pocc_pluto_parallel = true);
      check_opt(argv[i], "--polyopt-pluto-tile", pocc_pluto_tile = true);
      check_opt(argv[i], "--polyopt-pluto-rar", pocc_pluto_rar = true);


      check_opt(argv[i], "--polyopt-pluto-fuse-maxfuse", pocc_pluto_fuse = MAXIMAL_FUSE);
      check_opt(argv[i], "--polyopt-pluto-fuse-smartfuse", pocc_pluto_fuse = SMART_FUSE);
      check_opt(argv[i], "--polyopt-pluto-fuse-nofuse", pocc_pluto_fuse = NO_FUSE);
      // if (! strcmp (argv[i], "--polyopt-pluto-fuse"))
      // 	{
      // 	  ++i;
      // 	  if (! strcmp (argv[i], "maxfuse"))
      // 	    pocc_pluto_fuse = MAXIMAL_FUSE;
      // 	  else if (! strcmp (argv[i], "smartfuse"))
      // 	    pocc_pluto_fuse = SMART_FUSE;
      // 	  else if (! strcmp (argv[i], "nofuse"))
      // 	    pocc_pluto_fuse = NO_FUSE;
      // 	  pocc_pluto = 1;
      // 	}
      check_opt(argv[i], "--polyopt-pluto-polyunroll",
		pocc_pluto_polyunroll = true);
      check_opt(argv[i], "--polyopt-pluto-bee", pocc_pluto_bee = true);
      check_opt(argv[i], "--polyopt-pluto-prevector",
		pocc_pluto_prevector = true);
      check_opt(argv[i], "--polyopt-pluto-quiet", pocc_pluto_quiet = true);
      check_opt(argv[i], "--polyopt-pluto-context", pocc_pluto_context = true);
      check_opt(argv[i], "--polyopt-pluto-multipipe", pocc_pluto_multipipe = true);
      check_opt(argv[i], "--polyopt-pluto-l2tile", pocc_pluto_l2tile = true);
      check_opt(argv[i], "--polyopt-pluto-lastwriter",
		pocc_pluto_lastwriter = true);
      check_opt(argv[i], "--polyopt-pluto-scalpriv", pocc_pluto_scalpriv = true);
      check_opt(argv[i], "--polyopt-pluto-ft", pocc_pluto_ft = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-pluto-lt", pocc_pluto_lt = atoi (argv[++i]));

      // LetSee options.
      check_opt(argv[i], "--polyopt-letsee", pocc_letsee = true);
      if (! strcmp (argv[i], "--polyopt-letsee-traversal"))
	{
	  ++i;
	  if (! strcmp (argv[i], "exhaust"))
	    pocc_letsee_traversal = LS_HEURISTIC_EXHAUST;
	  else if (! strcmp (argv[i], "dh"))
	    pocc_letsee_traversal = LS_HEURISTIC_DH;
	  else if (! strcmp (argv[i], "random"))
	    pocc_letsee_traversal = LS_HEURISTIC_RANDOM;
	  else if (! strcmp (argv[i], "m1"))
	    pocc_letsee_traversal = LS_HEURISTIC_M1;
	  else if (! strcmp (argv[i], "skip"))
	    pocc_letsee_traversal = LS_HEURISTIC_SKIP;
	  //else if (! strcmp(opt_tab[POCC_OPT_LETSEE_TRAVERSAL], "ga"))
	  pocc_letsee = true;
	  continue;
	}
      if (! strcmp (argv[i], "--polyopt-letsee-space"))
	{
	  ++i;
	  if (! strcmp (argv[i], "precut"))
	    pocc_letsee_space = LS_TYPE_FS;
	  else if (! strcmp (argv[i], "schedule"))
	    pocc_letsee_space = LS_TYPE_MULTI;
	  pocc_letsee = true;
	  continue;
	}
      check_opt(argv[i], "--polyopt-letsee-normspace",
		pocc_letsee_normspace = true);
      check_opt(argv[i], "--polyopt-letsee-prune-precut",
		pocc_letsee_prune_precut = true);
      check_opt(argv[i], "--polyopt-letsee-backtrack-multi",
		pocc_letsee_backtrack_multi = true);
      check_opt(argv[i], "--polyopt-letsee-rtries",
		pocc_letsee_rtries = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-letsee-ilb",
		pocc_letsee_ilb = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-letsee-iUb",
		pocc_letsee_iUb = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-letsee-plb",
		pocc_letsee_plb = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-letsee-pUb",
		pocc_letsee_pUb = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-letsee-clb",
		pocc_letsee_clb = atoi (argv[++i]));
      check_opt(argv[i], "--polyopt-letsee-cUb",
		pocc_letsee_cUb = atoi (argv[++i]));
//       // Misc.
//       check_opt(argv[i], "--polyopt-compile-cmd",
// 		pocc_compile_cmd = strdup (argv[++i]));

      // If we are here, then we are parsing the filename now.
      pocc_input_file_name = strdup (argv[i]);
    }

  // Deal with generic options.
  if (polyopt_fixed_tiling)
    {
      polyopt_codegen_use_ptile = false;
      polyopt_insert_ptile_api = false;
      polyopt_generate_pragmas = true;
      pocc_pluto = true;
      pocc_pluto_parallel = true;
      pocc_pluto_tile = true;
      pocc_pluto_prevector = true;
    }
  if (polyopt_parametric_tiling)
    {
      polyopt_codegen_use_ptile = true;
      polyopt_insert_ptile_api = true;
      polyopt_generate_pragmas = true;
      pocc_pluto = true;
      pocc_pluto_parallel = true;
    }
  if (polyopt_parallel_only)
    {
      polyopt_codegen_use_ptile = false;
      polyopt_generate_pragmas = true;
      pocc_pluto = true;
      pocc_pluto_parallel = true;
    }
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    imageExtensions << "bmp" << "png" << "jpg" << "jpeg";
    //command-line version
    if(argc > 1)
    {
        int textureWidth = 512;
        int textureHeight = 512;
        bool merge = true;
        bool crop = true;
        int border = 1;
        int extrude = 0;
        bool rotate = false;
        bool recursion = true;
        bool square = false;
        bool autosize = false;
        int cropThreshold = 1;
        int autosizeThreshold = 80;
        int minTextureSizeX = 32;
        int minTextureSizeY = 32;
        int sortorder = 4;
        ImagePacker packer;
        mainPacker = &packer;
        QString outDir = QDir::currentPath();
        QString outFile = "atlas";
        for(int i = 1; i < argc; ++i)
        {
            if(check_opt("--help") || check_opt("-h") || check_opt("-?"))
            {
                printHelp();
            }
            else
                if(check_opt("-s") || check_opt("--size"))
                {
                    ++i;
                    if(i >= argc)
                    {
                        printHelp("Argument needed for option -s");
                    }
                    if(sscanf(argv[i], "%10dx%10d", &textureWidth, &textureHeight) != 2)
                    {
                        if(sscanf(argv[i], "%10d", &textureWidth) != 1)
                        {
                            printHelp("Wrong texture size format");
                        }
                        else
                        {
                            textureHeight = textureWidth;
                        }
                    }
                    printf("Setting texture size: %dx%d\n", textureWidth, textureHeight);
                }
                else
                    if(check_opt("-o") || check_opt("--out-file"))
                    {
                        ++i;
                        if(i >= argc)
                        {
                            printHelp("Argument needed for option -o");
                        }
                        QFileInfo info(argv[i]);
                        outFile = info.baseName();
                        outDir = info.absolutePath();
                    }
                    else
                        if(check_opt("--disable-merge"))
                        {
                            merge = false;
                        }
                        else
                            if(check_opt("--disable-crop"))
                            {
                                crop = false;
                            }
                            else
                                if(check_opt("--disable-recursion"))
                                {
                                    recursion = false;
                                }
                                else
                                    if(check_opt("--square"))
                                    {
                                        square = true;
                                    }
                                    else
                                        if(check_opt("--autosize-threshold"))
                                        {
                                            autosize = true;
                                            ++i;
                                            if(i >= argc)
                                            {
                                                printHelp("Argument needed for option --autosize-threshold");
                                            }
                                            if((sscanf(argv[i], "%10d", &autosizeThreshold) != 1) ||
                                                    (autosizeThreshold < 0) ||
                                                    (autosizeThreshold > 100))
                                            {
                                                printHelp("Wrong autosize threshold");
                                            }
                                        }
                                        else
                                            if(check_opt("--extrude-size"))
                                            {
                                                ++i;
                                                if(i >= argc)
                                                {
                                                    printHelp("Argument needed for option --extrude-size");
                                                }
                                                if((sscanf(argv[i], "%10d", &extrude) != 1) || (extrude < 0))
                                                {
                                                    printHelp("Wrong extrude size");
                                                }
                                            }
                                            else
                                                if(check_opt("--border-size"))
                                                {
                                                    ++i;
                                                    if(i >= argc)
                                                    {
                                                        printHelp("Argument needed for option --border-size");
                                                    }
                                                    if((sscanf(argv[i], "%10d", &border) != 1) || (border < 0))
                                                    {
                                                        printHelp("Wrong border size");
                                                    }
                                                }
                                                else
                                                    if(check_opt("--min-texture-size"))
                                                    {
                                                        ++i;
                                                        if(i >= argc)
                                                        {
                                                            printHelp("Argument needed for option -min-texture-size");
                                                        }
                                                        if(sscanf(argv[i], "%10dx%10d", &minTextureSizeX, &minTextureSizeY) != 2)
                                                        {
                                                            if(sscanf(argv[i], "%10d", &minTextureSizeX) != 1)
                                                            {
                                                                printHelp("Wrong texture size format");
                                                            }
                                                            else
                                                            {
                                                                minTextureSizeY = minTextureSizeX;
                                                            }
                                                        }
                                                    }
                                                    else
                                                        if(check_opt("--crop-threshold"))
                                                        {
                                                            ++i;
                                                            if(i >= argc)
                                                            {
                                                                printHelp("Argument needed for option --crop-threshold");
                                                            }
                                                            if((sscanf(argv[i], "%10d", &cropThreshold) != 1) ||
                                                                    (cropThreshold < 0) ||
                                                                    (cropThreshold > 255))
                                                            {
                                                                printHelp("Wrong crop threshold");
                                                            }
                                                        }
                                                        else
                                                            if(check_opt("--sort-order"))
                                                            {
                                                                ++i;
                                                                if(i >= argc)
                                                                {
                                                                    printHelp("Argument needed for option --sort-order");
                                                                }
                                                                if((sscanf(argv[i], "%10d", &sortorder) != 1) ||
                                                                        (sortorder < 0) ||
                                                                        (sortorder > 4))
                                                                {
                                                                    printHelp("Wrong sortorder must be from 0 to 4");
                                                                }
                                                            }
                                                            else
                                                                if(check_opt("--disable-border"))
                                                                {
                                                                    border = 0;
                                                                }
                                                                else
                                                                    if(check_opt("--enable-rotate"))
                                                                    {
                                                                        rotate = true;
                                                                    }
                                                                    //dir or file
                                                                    else
                                                                    {
                                                                        QFileInfo file(argv[i]);
                                                                        if(file.isFile())
                                                                        {
                                                                            packerData *data = new packerData;
                                                                            data->path = file.absoluteFilePath();
                                                                            data->file = file.fileName();
                                                                            packer.addItem(data->path, data);
                                                                        }
                                                                        else
                                                                            if(file.isDir())
                                                                            {
                                                                                topImageDir = file.absoluteFilePath();
                                                                                RecurseDirectory(file.absoluteFilePath(), recursion);
                                                                            }
                                                                    }
        }

        qDebug() << "Saving to dir" << outDir << "and file" << outFile;
        packer.sortOrder = sortorder;
        packer.border.t = 0;
        packer.border.l = 0;
        packer.border.r = border;
        packer.border.b = border;
        packer.extrude = extrude;
        packer.cropThreshold = crop ? cropThreshold : 0;
        packer.minFillRate = autosize ? autosizeThreshold : 0;
        packer.minTextureSizeX = minTextureSizeX;
        packer.minTextureSizeY = minTextureSizeY;
        packer.merge = merge;
        packer.mergeBF = false;
        packer.rotate = rotate;
        packer.square = square;
        packer.autosize = autosize;
        int heuristic = 1;

        QString outFormat("PNG");

        if(packer.images.size() == 0)
        {
            fprintf(stderr, "No images found, exitting\n");
            exit(1);
        }

        packer.pack(heuristic, textureWidth, textureHeight);

        QList<QImage> textures;
        for(int i = 0; i < packer.bins.size(); i++)
        {
            QImage texture(packer.bins.at(i).width(), packer.bins.at(i).height(),
                           QImage::Format_ARGB32);
            texture.fill(Qt::transparent);
            textures << texture;
        }
        for(int j = 0; j < textures.count(); j++)
        {
            QString outputFile = outDir;
            outputFile += QDir::separator();
            outputFile += outFile;
            if(textures.count() > 1)
            {
                outputFile += QString("_") + QString::number(j + 1);
            }
            outputFile += ".atlas";
            QString imgFile = outFile;
            if(textures.count() > 1)
            {
                imgFile += QString("_") + QString::number(j + 1);
            }
            imgFile += ".";
            imgFile += outFormat.toLower();

            QFile positionsFile(outputFile);
            if(!positionsFile.open(QIODevice::WriteOnly | QIODevice::Text))
            {
                fprintf(stderr, "Cannot create file %s", qPrintable(outputFile));
            }
            else
            {
                QTextStream out(&positionsFile);
                out << "textures: " << imgFile << "\n";
                for(int i = 0; i < packer.images.size(); i++)
                {
                    if(packer.images.at(i).textureId != j)
                    {
                        continue;
                    }
                    QPoint pos(packer.images.at(i).pos.x() + packer.border.l + packer.extrude,
                               packer.images.at(i).pos.y() + packer.border.t + packer.extrude);
                    QSize size, sizeOrig;
                    QRect crop;
                    sizeOrig = packer.images.at(i).size;
                    if(!packer.cropThreshold)
                    {
                        size = packer.images.at(i).size;
                        crop = QRect(0, 0, size.width(), size.height());
                    }
                    else
                    {
                        size = packer.images.at(i).crop.size();
                        crop = packer.images.at(i).crop;
                    }
                    if(packer.images.at(i).rotated)
                    {
                        size.transpose();
                        crop = QRect(crop.y(), crop.x(), crop.height(), crop.width());
                    }
                    out << (static_cast<packerData *>(packer.images.at(i).id))->file <<
                        "\t" <<
                        pos.x() << "\t" <<
                        pos.y() << "\t" <<
                        crop.width() << "\t" <<
                        crop.height() << "\t" <<
                        crop.x() << "\t" <<
                        crop.y() << "\t" <<
                        sizeOrig.width() << "\t" <<
                        sizeOrig.height() << "\t" <<
                        (packer.images.at(i).rotated ? "r" : "") << "\n";
                }
            }
        }

        for(int i = 0; i < packer.images.size(); i++)
        {
            qDebug() << "Processing" << (static_cast<packerData *>(packer.images.at(
                                             i).id))->file;
            if(packer.images.at(i).duplicateId != NULL && packer.merge)
            {
                continue;
            }
            QPoint pos(packer.images.at(i).pos.x() + packer.border.l,
                       packer.images.at(i).pos.y() + packer.border.t);
            QSize size;
            QRect crop;
            if(!packer.cropThreshold)
            {
                size = packer.images.at(i).size;
                crop = QRect(0, 0, size.width(), size.height());
            }
            else
            {
                size = packer.images.at(i).crop.size();
                crop = packer.images.at(i).crop;
            }
            QImage img;
            img = QImage((static_cast<packerData *>(packer.images.at(i).id))->path);
            if(packer.images.at(i).rotated)
            {
                QTransform myTransform;
                myTransform.rotate(90);
                img = img.transformed(myTransform);
                size.transpose();
                crop = QRect(packer.images.at(i).size.height() - crop.y() - crop.height(),
                             crop.x(), crop.height(), crop.width());
            }
            if(packer.images.at(i).textureId < packer.bins.size())
            {
                QPainter p(&textures.operator [](packer.images.at(i).textureId));

                if(packer.extrude)
                {
                    QColor color1 = QColor::fromRgba(img.pixel(crop.x(), crop.y()));
                    p.setPen(color1);
                    p.setBrush(color1);
                    if(packer.extrude == 1)
                    {
                        p.drawPoint(QPoint(pos.x(), pos.y()));
                    }
                    else
                    {
                        p.drawRect(QRect(pos.x(), pos.y(), packer.extrude - 1, packer.extrude - 1));
                    }

                    QColor color2 = QColor::fromRgba(img.pixel(crop.x(),
                                                     crop.y() + crop.height() - 1));
                    p.setPen(color2);
                    p.setBrush(color2);
                    if(packer.extrude == 1)
                    {
                        p.drawPoint(QPoint(pos.x(), pos.y() + crop.height() + packer.extrude));
                    }
                    else
                    {
                        p.drawRect(QRect(pos.x(), pos.y() + crop.height() + packer.extrude,
                                         packer.extrude - 1, packer.extrude - 1));
                    }

                    QColor color3 = QColor::fromRgba(img.pixel(crop.x() + crop.width() - 1,
                                                     crop.y()));
                    p.setPen(color3);
                    p.setBrush(color3);
                    if(packer.extrude == 1)
                    {
                        p.drawPoint(QPoint(pos.x() + crop.width() + packer.extrude, pos.y()));
                    }
                    else
                    {
                        p.drawRect(QRect(pos.x() + crop.width() + packer.extrude, pos.y(),
                                         packer.extrude - 1, packer.extrude - 1));
                    }

                    QColor color4 = QColor::fromRgba(img.pixel(crop.x() + crop.width() - 1,
                                                     crop.y() + crop.height() - 1));
                    p.setPen(color4);
                    p.setBrush(color4);
                    if(packer.extrude == 1)
                    {
                        p.drawPoint(QPoint(pos.x() + crop.width() + packer.extrude,
                                           pos.y() + crop.height() + packer.extrude));
                    }
                    else
                    {
                        p.drawRect(QRect(pos.x() + crop.width() + packer.extrude,
                                         pos.y() + crop.height() + packer.extrude, packer.extrude - 1,
                                         packer.extrude - 1));
                    }

                    p.drawImage(QRect(pos.x(), pos.y() + packer.extrude, packer.extrude,
                                      crop.height()), img, QRect(crop.x(), crop.y(), 1, crop.height()));
                    p.drawImage(QRect(pos.x() + crop.width() + packer.extrude,
                                      pos.y() + packer.extrude, packer.extrude, crop.height()), img,
                                QRect(crop.x() + crop.width() - 1, crop.y(), 1, crop.height()));

                    p.drawImage(QRect(pos.x() + packer.extrude, pos.y(), crop.width(),
                                      packer.extrude), img, QRect(crop.x(), crop.y(), crop.width(), 1));
                    p.drawImage(QRect(pos.x() + packer.extrude,
                                      pos.y() + crop.height() + packer.extrude, crop.width(), packer.extrude), img,
                                QRect(crop.x(), crop.y() + crop.height() - 1, crop.width(), 1));

                    p.drawImage(pos.x() + packer.extrude, pos.y() + packer.extrude, img, crop.x(),
                                crop.y(), crop.width(), crop.height());
                }
                else
                {
                    p.drawImage(pos.x(), pos.y(), img, crop.x(), crop.y(), crop.width(),
                                crop.height());
                }
            }
        }
        qint64 area = 0;
        for(int i = 0; i < textures.count(); i++)
        {
            area += textures.at(i).width() * textures.at(i).height();
        }
        float percent = (((float)packer.area / (float)area) * 100.0f);
        //        float percent2 = (float)(((float)packer.neededArea / (float)area) * 100.0f );
        printf("Atlas generated. %f%% filled, %d images missed, %d merged, %d KB\n",
               percent, packer.missingImages, packer.mergedImages, (int)((area * 4) / 1024));

        //        const char * format = qPrintable(outFormat);
        for(int i = 0; i < textures.count(); i++)
        {
            QString imgdirFile;
            imgdirFile = outDir;
            imgdirFile += QDir::separator();
            imgdirFile += outFile;
            if(textures.count() > 1)
            {
                imgdirFile += QString("_") + QString::number(i + 1);
            }
            imgdirFile += ".";
            imgdirFile += outFormat.toLower();
            textures.at(i).save(imgdirFile);
        }

        return 0;
    }

    QTranslator myTranslator;
    myTranslator.load("tile_" + QLocale::system().name(), "qm");
    a.installTranslator(&myTranslator);
    MainWindow w;
    w.show();


    return a.exec();
}