Beispiel #1
0
int main (int argc, char* argv[])
{
	int optc, optindex;
	short filter, pact, ppri, pday, pmonth, pyear, pfin;
	char padded[T_S_ADDED], pdue[T_S_DUE], pfinished[T_S_FINISHED], pproject[T_S_PROJECT], pdescription[T_S_DESCRIPTION];
	bask_core tcore;
	bask_theme btheme;
	bask_priority* bprioritys = NULL;
	bask_filter bfilter;
	struct bask_task* first = NULL;
	
	struct option long_options[] = {
		 {"help", no_argument, 0, B_CMD_ARG_HELP},
		 {"about", no_argument, 0, B_CMD_ARG_ABOUT},
		 {"day", required_argument, 0, B_CMD_ARG_DAY},
		 {"month", required_argument, 0, B_CMD_ARG_MONTH},
		 {"year", required_argument, 0, B_CMD_ARG_YEAR},
		 {"due", required_argument, 0, B_CMD_ARG_DUE},
		 {"today", no_argument, 0, B_CMD_ARG_TODAY},
		 {0,0,0,0}
	};
	
	optindex = filter = tcore.priority_min = tcore.priority_max = 0;
	ppri = pact = pday = pmonth = pyear = pfin = -1;
	
	strcpy (padded, "");
	strcpy (pdue, "");
	strcpy (pfinished, "");
	strcpy (pproject, "");
	strcpy (pdescription, "");
	
	strcpy (tcore.path_baskpath, secure_getenv("HOME"));
	
	if (tcore.path_baskpath == NULL || *tcore.path_baskpath == '\0')
	{
		errors_homedirnotgot ();
		exit (EXIT_FAILURE);
	}

	strcat (tcore.path_baskpath, "/.local/share/bask/");
	
	bask_get_baskpath (&tcore, tcore.path_baskconf, BASKCONFFILE);
	bask_get_baskpath (&tcore, tcore.path_basktheme, BASKTHEMEFILE);
	bask_get_baskpath (&tcore, tcore.path_baskbin, BASKBINFILE);
	
	config_init (&tcore);
	
	/* NOTE: These are cmd's that don't need the tasks data, so it wont be loaded. */
	if (argc <= 1)
	{
		usage ();
		exit (EXIT_FAILURE);
	}
	else if (argc == 2)
	{
		if (utils_streq (argv[optind], B_CMD_HELP) == 0)
		{
			print_help ();
			exit (EXIT_SUCCESS);
		}
		else if (utils_streq (argv[optind], B_CMD_ABOUT) == 0)
		{
			print_about ();
			exit (EXIT_SUCCESS);
		}
		else if (utils_streq (argv[optind], B_CMD_INIT) == 0)
		{
			bask_init_local (&tcore);
			exit (EXIT_SUCCESS);
		}
	}
	else if (argc == 3)
	{
		if (utils_streq (argv[optind], B_CMD_INIT) == 0)
		{
			if (utils_streq (argv[optind+1], B_CMD_BASKCONF) == 0)
			{
				/* Saving the default values and exit. */
				config_save (&tcore);
				exit (EXIT_SUCCESS);
			}
			else if (utils_streq (argv[optind+1], B_CMD_BASKBIN) == 0)
			{
				bask_init_baskbin (&tcore);
				exit (EXIT_SUCCESS);
			}
			else if (utils_streq (argv[optind+1], B_CMD_BASKTHEME) == 0)
			{
				bask_init_basktheme (&tcore);
				exit (EXIT_SUCCESS);
			}
		}
	}
	
	config_load (&tcore);
	ui_theme_load (&tcore, &btheme);
	
	/* Default Prioritys */
	priority_create (&tcore, &bprioritys, 0, btheme.color_normal, "L", "Normal");
	priority_create (&tcore, &bprioritys, 1, btheme.color_important, "I", "Important");
	priority_create (&tcore, &bprioritys, 2, btheme.color_today, "T", "Today");
	priority_create (&tcore, &bprioritys, 3, btheme.color_critical, "C", "Critical");
	
	while ((optc = getopt_long (argc, argv, "p:P:a:D:F:A:s:fh", long_options, &optindex)) != -1)
	{
		switch (optc)
		{
			case 'p':
				ppri = priority_get_idfromstr (&bprioritys, optarg);
				break;
			case 'P':
				if (strlen (optarg) < sizeof (pproject))
				{
					strcpy (pproject, optarg);
				}
				break;
			case 'a':
				if (utils_atos (&pact, optarg) == -2 || ISBOOL (pact) != TRUE)
				{
					errors_outofrange_int ("-a", FALSE, TRUE);
				}
				break;
			case 'D':
				if (strlen (optarg) < sizeof (pdescription))
				{
					strcpy (pdescription, optarg);
				}
				break;
			case 'F':
				time_get_str_described (pfinished, sizeof (pfinished), optarg);
				break;
			case 'A':
				time_get_str_described (padded, sizeof (padded), optarg);
				break;
			case 's':
				if (utils_atos (&pfin, optarg) == -2 || ISBOOL (pfin) != TRUE)
				{
					errors_outofrange_int ("-s", FALSE, TRUE);
				}
				break;
			case 'f':
				filter = TRUE;
				break;
			case B_CMD_ARG_HELP:
				print_help ();
				exit (EXIT_SUCCESS);
				break;
			case B_CMD_ARG_ABOUT:
				print_about ();
				exit (EXIT_SUCCESS);
				break;
			case B_CMD_ARG_DAY:
				if (utils_atos (&pday, optarg) == -2 || pday < 1 || pday > 31)
				{
					errors_outofrange_int ("--day", 1, 31);
				}
				break;
			case B_CMD_ARG_MONTH:
				if (utils_atos (&pmonth, optarg) == -2 || pmonth < 1 || pmonth > 12)
				{
					errors_outofrange_int ("--month", 1, 12);
				}
				break;
			case B_CMD_ARG_YEAR:
				if (utils_atos (&pyear, optarg) == -2)
				{
					errors_outofrange_int ("--year", SHRT_MIN, SHRT_MAX);
				}
				break;
			case B_CMD_ARG_DUE:
				time_get_str_described (pdue, sizeof (pdue), optarg);
				break;
			case B_CMD_ARG_TODAY:
				/* TODO: Improve this. */
				if (time_get_str (padded, sizeof (padded)) == 0)
				{
					pday = (short) time_get_day (padded);
					pmonth = (short) time_get_month (padded);
					pyear = (short) time_get_year (padded);
				}
				break;
			default:
				break;
		}
	}
	
	if (filter == TRUE)
	{
		filter_init (&bfilter, pact, pfin, ppri, pday, pmonth, pyear);
	}
	else
	{
		filter_init (&bfilter, TRUE, -1, -1, -1, -1, -1);
	}
	
	bask_init (&tcore, &first);
	
	if (optind > 1)
	{
		if (argc-optind == 1)
		{
			if (utils_streq (argv[optind], B_CMD_LIST) == 0)
			{
				view_tasklist (&tcore, &btheme, &bprioritys, &first, &bfilter);
			}
			else if (utils_streq (argv[optind], B_CMD_SUMMARY) == 0)
			{
				view_summary (&tcore, &btheme, &first, &bfilter);
			}
			else if (utils_streq (argv[optind], B_CMD_HISTORY) == 0)
			{
				view_history (&tcore, &btheme, &first, &bfilter);
			}
			else
			{
				usage ();
			}
		}
		else if (argc-optind == 2)
		{
			if (utils_streq (argv[optind], B_CMD_MOD) == 0)
			{
				task_modificate_cmd (&tcore, &first, atoi (argv[optind+1]), pact, -1, ppri, padded, pdue, pfinished, pproject, pdescription);
			}
			else if (utils_streq (argv[optind], B_CMD_SEARCH) == 0)
			{
				search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+1], BVIEW_TASKLIST);
			}
			else
			{
				usage ();
			}
		}
		else if (argc-optind == 3)
		{
			if (utils_streq (argv[optind], B_CMD_SEARCH) == 0)
			{
				if (utils_streq (argv[optind+1], B_CMD_TASKLIST) == 0)
				{
					search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+2], BVIEW_TASKLIST);
				}
				else if (utils_streq (argv[optind+1], B_CMD_SUMMARY) == 0)
				{
					search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+2], BVIEW_SUMMARY);
				}
				else if (utils_streq (argv[optind+1], B_CMD_DETAILED) == 0)
				{
					search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+2], BVIEW_DETAILED);
				}
				else
				{
					usage ();
				}
			}
		}
		else
		{
			usage ();
		}
	}
	else if (argc == 2)
	{
		if (utils_streq (argv[optind], B_CMD_LIST) == 0)
		{
			view_tasklist (&tcore, &btheme, &bprioritys, &first, &bfilter);
		}
		else if (utils_streq (argv[optind], B_CMD_SUMMARY) == 0)
		{
			view_summary (&tcore, &btheme, &first, &bfilter);
		}
		else if (utils_streq (argv[optind], B_CMD_HISTORY) == 0)
		{
			/* This allows History to use all tasks including the hidden ones. */
			filter_init (&bfilter, -1, -1, -1, -1, -1, -1);
			view_history (&tcore, &btheme, &first, &bfilter);
		}
		else
		{
			usage ();
		}
	}
	else if (argc == 3)
	{
		if (utils_streq (argv[optind], B_CMD_FINISH) == 0)
		{
			task_finish_cmd (&tcore, &first, atoi (argv[optind+1]));
		}
		else if (utils_streq (argv[optind], B_CMD_REMOVE) == 0)
		{
			task_remove_cmd (&tcore, &first, atoi (argv[optind+1]));
		}
		else if (utils_streq (argv[optind], B_CMD_SEARCH) == 0)
		{
			search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+1], BVIEW_TASKLIST);
		}
		else if (utils_streq (argv[optind], B_CMD_STOP) == 0)
		{
			task_deactivate_cmd (&tcore, &first, atoi (argv[optind+1]));
		}
		else if (utils_streq (argv[optind], B_CMD_SHOW) == 0)
		{
			view_single (&tcore, &first, &bfilter, atoi (argv[optind+1]), FALSE);
		}
		else if (utils_streq (argv[optind], B_CMD_EXPORT) == 0)
		{
			export_baskbin_cmd (&tcore, &first, argv[optind+1]);
		}
		else if (utils_streq (argv[optind], B_CMD_IMPORT) == 0)
		{
			import_baskbin_cmd (&tcore, &first, argv[optind+1]);
		}
		else if (utils_streq (argv[optind], B_CMD_CONFIG) == 0)
		{
			config_set_str_cmd (&tcore, argv[optind+1]);
		}
		else
		{
			usage ();
		}
	}
	else if (argc == 4)
	{
		if (utils_streq (argv[optind], B_CMD_SEARCH) == 0)
		{
			if (utils_streq (argv[optind+1], B_CMD_TASKLIST) == 0)
			{
				search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+2], BVIEW_TASKLIST);
			}
			else if (utils_streq (argv[optind+1], B_CMD_SUMMARY) == 0)
			{
				search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+2], BVIEW_SUMMARY);
			}
			else if (utils_streq (argv[optind+1], B_CMD_DETAILED) == 0)
			{
				search_view (&tcore, &btheme, &bprioritys, &bfilter, &first, argv[optind+2], BVIEW_DETAILED);
			}
			else
			{
				usage ();
			}
		}
		else if (utils_streq (argv[optind], B_CMD_EXPORT) == 0)
		{
			if (utils_streq (argv[optind+1], B_CMD_BASKBIN) == 0)
			{
				export_baskbin_cmd (&tcore, &first, argv[optind+2]);
			}
			else if (utils_streq (argv[optind+1], B_CMD_CSV) == 0)
			{
				export_csv_cmd (&tcore, &first, argv[optind+2]);
			}
			else if (utils_streq (argv[optind+1], B_CMD_ICAL) == 0)
			{
				export_ical_cmd (&tcore, &first, argv[optind+2]);
			}
			else if (utils_streq (argv[optind+1], B_CMD_WEB) == 0)
			{
				export_web (&tcore, &first, argv[optind+2]);
			}
			else
			{
				usage ();
			}
		}
		else if (utils_streq (argv[optind], B_CMD_IMPORT) == 0)
		{
			if (utils_streq (argv[optind+1], B_CMD_BASKBIN) == 0)
			{
				import_baskbin_cmd (&tcore, &first, argv[optind+2]);
			}
			else if (utils_streq (argv[optind+1], B_CMD_CSV) == 0)
			{
				import_csv_cmd (&tcore, &first, argv[optind+2]);
			}
			else if (utils_streq (argv[optind+1], B_CMD_ICAL) == 0)
			{
				import_ical_cmd (&tcore, &first, argv[optind+2]);
			}
			else
			{
				usage ();
			}
		}
		else if (utils_streq (argv[optind], B_CMD_DUE) == 0)
		{
			time_get_str_described (pdue, sizeof (pdue), argv[optind+2]);
			task_due_cmd (&tcore, &first, atoi (argv[optind+1]), pdue);
		}
		else
		{
			usage ();
		}
	}
	else if (argc == 5)
	{	
		if (utils_streq (argv[optind], B_CMD_ADD) == 0)
		{
			task_create_cmd (&tcore, &first, priority_get_idfromstr (&bprioritys, argv[optind+1]), argv[optind+2], argv[optind+3]);
		}
		else
		{
			usage ();
		}
	}
	else
	{
		usage ();
	}
	
	config_save (&tcore);
	bask_unload (&bprioritys, &first);
	return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
  char *dictionary_file = "en";	/* if not told otherwise, assume we're using english */
  const char *input_file = NULL;
  char *output_file = NULL;

  FILE *input_stream = stdin;	/*by default read from stdin */
  FILE *output_stream = stdout;	/*by default read from stdout */

  OtsArticle *Art;

  int sumPercent = 20;	 	/* if not told otherwise highlight 20% of the  document */

  int c,n_args=0;

  int html = FALSE;
  int keywords = FALSE;
  int about = FALSE;
  int version = FALSE;
	
  const char *const *args=NULL;

	GOptionContext *context = NULL;
	GError *error = NULL;
	
	const GOptionEntry options[] = {
		{"ratio"   , 'r', 0, G_OPTION_ARG_INT   , &sumPercent, "summarization % [default = 20%]", "<int>"},
		{"dic"     , 'd', 0, G_OPTION_ARG_STRING, &dictionary_file, "dictionary to use", "<string>"},
		{"out"     , 'o', 0, G_OPTION_ARG_STRING, &output_file, "output file [default = stdout]", "<string>"},
		{"html"    , 'h', 0, G_OPTION_ARG_NONE  , &html, "output as html", NULL},
		{"keywords", 'k', 0, G_OPTION_ARG_NONE  , &keywords, "only output keywords", NULL},
		{"about"   , 'a', 0, G_OPTION_ARG_NONE  , &about, "only output the summary", NULL},
		{"version" , 'v', 0, G_OPTION_ARG_NONE  , &version, "show version information", NULL},
		{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, "[file.txt | stdin]" },
		{NULL}
  };

	context = g_option_context_new(" - Open Text Summarizer");
	g_option_context_add_main_entries(context, options, NULL);

        /* Parse command line */
        if (!g_option_context_parse (context, &argc, &argv, &error))
        {
                g_option_context_free (context);

                g_print ("%s\n", error->message);
                g_error_free (error);
                exit (1);
        }

	/* print version number */
  if (version)
    {
		printf("%s\n", PACKAGE_STRING);
		g_option_context_free(context);
      exit (0);
    }
  
  if(args==NULL) 
    {
       printf("\nInvalid number of arguments. Use --help to see options\n");
       exit(1);
    }
  if (args)
    while (args[n_args] != NULL)
      n_args++;

  if (n_args > 1)
    {
		g_option_context_free(context);
      return 1;
    }

	if (n_args == 1 && g_file_test (args[0], G_FILE_TEST_EXISTS) == TRUE)
    input_file = args[0];

  if (input_file)
    {
      input_stream = fopen (input_file, "r");
      if (!input_stream)
	{
			g_option_context_free(context);
	  perror ("Couldn't load input file");
	  return 1;
	}
    }

  if (output_file)
    {
      output_stream = fopen (output_file, "w");
      if (!output_stream)
	{
	  if (input_file)
	    fclose (input_stream);
			g_option_context_free(context);
	  perror ("Couldn't load output file");
	  return 1;
	}
    }

  Art = ots_new_article ();
	
  if (!ots_load_xml_dictionary (Art, dictionary_file))
    {
   
      ots_free_article (Art);
		if (input_file)
			fclose(input_stream);
		if (output_file)
			fclose(output_stream);
		g_option_context_free(context);
      perror ("Couldn't load dictionary");
      return 1;
    }


  ots_parse_file (input_stream, Art);	/* read article from stdin , put it in struct Article */
  ots_grade_doc (Art);					/* grade each sentence (how relevent is it to the text) */

/*
  int i;

  for (i=0;i<1000000;i++)
  {

    printf("\n word = %s ", ots_word_in_list(Art->ImpWords,i));

  }
*/

  ots_highlight_doc (Art, sumPercent);	/* highlight what we are going to print 0% - 100% of the words */


	if (html)
		ots_print_HTML(output_stream, Art);	/* print article in html form */
	else if (keywords)
		print_keywords(Art, input_file); 
	else if (about)
		print_about(output_stream, Art);
	else
		ots_print_doc(output_stream, Art);	/* print article in text */

  ots_free_article (Art);
 

  if (input_file)
    fclose (input_stream);

  if (output_file)
    fclose (output_stream);

  return 0;
}