Ejemplo n.º 1
0
// Serialize/Deserialize
char *IoMemcached_serialize(IoMemcached *self, IoObject *locals, IoObject *object, size_t *size, uint32_t *flags) {
	char *cvalue;

	if(ISSEQ(object)) {
		*flags = _FLAG_SEQUENCE;
		*size = IOSEQ_LENGTH(object);
		cvalue = (char *) malloc(*size);
		strncpy(cvalue, CSTRING(object), *size);
	}
	else if(ISNUMBER(object)) {
		*flags = _FLAG_NUMBER;
		double cnumber = IoNumber_asDouble(object);
		cvalue = (char *) malloc(128 * sizeof(char));
		*size = snprintf(cvalue, 127, "%.16f", cnumber);
	}
	else if(ISNIL(object)) {
		*flags = _FLAG_NIL;
		*size = 3;
		cvalue = (char *) malloc(3 * sizeof(char));
		strncpy(cvalue, "nil", 3);
	}
	else if(ISBOOL(object)) {
		*flags = _FLAG_BOOLEAN;
		*size = 1;
		cvalue = (char *) malloc(sizeof(char));
		if(object == IOSTATE->ioTrue)  strncpy(cvalue, "1", 1);
		if(object == IOSTATE->ioFalse) strncpy(cvalue, "0", 1);
	}
	else {
		*flags = _FLAG_OBJECT;
		IoMessage *serialize = IoMessage_newWithName_(IOSTATE, IOSYMBOL("serialized"));
		IoSeq *serialized = IoMessage_locals_performOn_(serialize, locals, object);
		*size = IOSEQ_LENGTH(serialized);
		cvalue = (char *) malloc(*size);
		strncpy(cvalue, CSTRING(serialized), *size);
	}

	return cvalue;
}
Ejemplo n.º 2
0
Archivo: bask.c Proyecto: marc-q/bask
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;
}