Exemplo n.º 1
0
/* Here is perhaps the right place to mention that this code is
 * all in top-down order. Hence, |main| comes first.
 */
int
main(int argc, char *argv[]) {
  int ch;			/* used for |getopt| processing */
  wchar_t *tmp;
  size_t len;
  const char *src;

  (void) setlocale(LC_CTYPE, "");

  /* 1. Grok parameters. */

  while ((ch = getopt(argc, argv, "0123456789cd:hl:mnpst:w:")) != -1)
  switch(ch) {
    case 'c':
      centerP = 1;
      format_troff = 1;
      continue;
    case 'd':
      src = optarg;
      len = mbsrtowcs(NULL, &src, 0, NULL);
      if (len == (size_t)-1)
        err(EX_USAGE, "bad sentence-ending character set");
      tmp = XMALLOC((len + 1) * sizeof(wchar_t));
      mbsrtowcs(tmp, &src, len + 1, NULL);
      sentence_enders = tmp;
      continue;
    case 'l':
      output_tab_width
        = get_nonnegative(optarg, "output tab width must be non-negative", 1);
      continue;
    case 'm':
      grok_mail_headers = 1;
      continue;
    case 'n':
      format_troff = 1;
      continue;
    case 'p':
      allow_indented_paragraphs = 1;
      continue;
    case 's':
      coalesce_spaces_P = 1;
      continue;
    case 't':
      tab_width = get_positive(optarg, "tab width must be positive", 1);
      continue;
    case 'w':
      goal_length = get_positive(optarg, "width must be positive", 1);
      max_length = goal_length;
      continue;
    case '0': case '1': case '2': case '3': case '4': case '5':
    case '6': case '7': case '8': case '9':
    /* XXX  this is not a stylistically approved use of getopt() */
      if (goal_length==0) {
        char *p;
        p = argv[optind - 1];
        if (p[0] == '-' && p[1] == ch && !p[2])
             goal_length = get_positive(++p, "width must be nonzero", 1);
        else
             goal_length = get_positive(argv[optind]+1,
                 "width must be nonzero", 1);
        max_length = goal_length;
      }
      continue;
    case 'h': default:
      fprintf(stderr,
"usage:   fmt [-cmps] [-d chars] [-l num] [-t num]\n"
"             [-w width | -width | goal [maximum]] [file ...]\n"
"Options: -c     center each line instead of formatting\n"
"         -d <chars> double-space after <chars> at line end\n"
"         -l <n> turn each <n> spaces at start of line into a tab\n"
"         -m     try to make sure mail header lines stay separate\n"
"         -n     format lines beginning with a dot\n"
"         -p     allow indented paragraphs\n"
"         -s     coalesce whitespace inside lines\n"
"         -t <n> have tabs every <n> columns\n"
"         -w <n> set maximum width to <n>\n"
"         goal   set target width to goal\n");
      exit(ch=='h' ? 0 : EX_USAGE);
  }
  argc -= optind; argv += optind;

  /* [ goal [ maximum ] ] */

  if (argc>0 && goal_length==0
      && (goal_length=get_positive(*argv,"goal length must be positive", 0))
         != 0) {
    --argc; ++argv;
    if (argc>0
        && (max_length=get_positive(*argv,"max length must be positive", 0))
           != 0) {
      --argc; ++argv;
      if (max_length<goal_length)
        errx(EX_USAGE, "max length must be >= goal length");
    }
  }
  if (goal_length==0) goal_length = 65;
  if (max_length==0) max_length = goal_length+10;
  if (max_length >= SIZE_T_MAX / sizeof (wchar_t)) errx(EX_USAGE, "max length too large");
  /* really needn't be longer */
  output_buffer = XMALLOC((max_length+1) * sizeof(wchar_t));

  /* 2. Process files. */

  if (argc>0) {
    while (argc-->0) process_named_file(*argv++);
  }
  else {
    process_stream(stdin, "standard input");
  }

  /* We're done. */

  return n_errors ? EX_NOINPUT : 0;

}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	//first argument, lowercase, if any
	std::string fl = argc>1 ? lowercase(argv[1]) : "";
	if (argc<=2) {
		if (fl=="" || fl=="--help" || fl=="-h" || fl=="/?" || fl=="/help") {
			std::cerr << "usage:\t" << argv[0] << " graph [''|solution|'='|'%'|color]" << std::endl;
			std::cerr << "where:\t" << "graph and solution are filenames" << std::endl;
			std::cerr << "\tcolor is an id in the default color palette" << std::endl;
			return 1;
		}
		else {
			try {
				Graph graph = Graph::load(argv[1]);
				signal(SIGUSR1, Metaheuristic::dump_handler);
				std::vector<int> v(graph.succ.size(), 0);
				Solution sol(v, graph);
				sol.initSolution();
				//TODO adjust parameters here!
				float alpha = 0.9f;
				float temperature = 10.0f;
				float epsilon = 1.0f;
				int niter = 10;
				//std::cerr << "HIT before create recuit" << std::endl;
				Recuit meta(sol, alpha, niter, temperature, epsilon);
				//std::cerr << "HIT before starting recuit" << std::endl;
				//LocalSearch meta(graph);
				sol = meta.getSolution();
				sol.dump();
				if(sol.isAdmissible())
					std::cerr << "GOOD" << std::endl;
				return 0;
			}
			catch (GraphException& e) {
				std::cerr << "error: " << e.what() << std::endl;
				return 2;
			}
		}
	}
	else {
		try {
			Graph g = Graph::load(argv[1]);
#ifdef USE_SDL
			Solution *s = NULL;
			int pattern = get_positive(argv[2]);
			if (pattern == -1) {
				std::string a2 = argv[2];
				if (a2 == "=")
					pattern = -1;
				else if (a2 == "%")
					pattern = -2;
				else
					s = new Solution(Solution::load(a2, g));
			}
			if (!s)
				s = new Solution(Solution::load(g, pattern));
			ui_main(g, s);
			delete s;
#else
			g.dump();
#endif //USE_SDL
			return 0;
		}
		catch (SolutionException& e) {
			std::cerr << "error: " << e.what() << std::endl;
			return 3;
		}
		catch (GraphException& e) {
			std::cerr << "error: " << e.what() << std::endl;
			return 2;
		}
	}
}
Exemplo n.º 3
0
/* Here is perhaps the right place to mention that this code is
 * all in top-down order. Hence, |main| comes first.
 */
int
main(int argc, char *argv[])
{
	int ch;			/* used for |getopt| processing */

	(void)setlocale(LC_CTYPE, "");

	/* 1. Grok parameters. */
	while ((ch = getopt(argc, argv, "0123456789cd:hl:mnpst:w:")) != -1) {
		switch (ch) {
		case 'c':
			centerP = 1;
			break;
		case 'd':
			sentence_enders = optarg;
			break;
		case 'l':
			output_tab_width
				= get_positive(optarg, "output tab width must be positive", 1);
			break;
		case 'm':
			grok_mail_headers = 1;
			break;
		case 'n':
			format_troff = 1;
			break;
		case 'p':
			allow_indented_paragraphs = 1;
			break;
		case 's':
			coalesce_spaces_P = 1;
			break;
		case 't':
			tab_width = get_positive(optarg, "tab width must be positive", 1);
			break;
		case 'w':
			goal_length = get_positive(optarg, "width must be positive", 1);
			max_length = goal_length;
			break;
		case '0': case '1': case '2': case '3': case '4': case '5':
		case '6': case '7': case '8': case '9':
			/* XXX  this is not a stylistically approved use of getopt() */
			if (goal_length == 0) {
				char *p;

				p = argv[optind - 1];
				if (p[0] == '-' && p[1] == ch && !p[2])
					goal_length = get_positive(++p, "width must be nonzero", 1);
				else
					goal_length = get_positive(argv[optind]+1,
							"width must be nonzero", 1);
				max_length = goal_length;
			}
			break;
		case 'h':
		default:
			usage();
			/* NOT REACHED */
		}
	}

	argc -= optind;
	argv += optind;

	/* [ goal [ maximum ] ] */
	if (argc > 0 && goal_length == 0 &&
	    (goal_length = get_positive(*argv,"goal length must be positive", 0)) != 0) {
		--argc;
		++argv;
		if (argc > 0 && (max_length = get_positive(*argv,"max length must be positive", 0)) != 0) {
			--argc;
			++argv;
			if (max_length < goal_length)
				errx(EX_USAGE, "max length must be >= goal length");
		}
	}

	if (goal_length == 0)
		goal_length = 65;
	if (max_length == 0)
		max_length = goal_length+10;
	output_buffer = XMALLOC(max_length+1);	/* really needn't be longer */

	/* 2. Process files. */

	if (argc > 0) {
		while (argc-- > 0)
			process_named_file(*argv++);
	} else {
		process_stream(stdin, "standard input");
	}

	/* We're done. */
	return n_errors ? EX_NOINPUT : 0;

}