Example #1
0
char*	ug_attachment_alloc (unsigned int* stamp)
{
	GString*		gstr;
	guint			length;
	guint			count;
	guint			value;

	if (attachment_dir == NULL)
		ug_attachment_init (NULL);
	// create new attachment dir
	gstr = g_string_new (attachment_dir);
	g_string_append_c (gstr, G_DIR_SEPARATOR);
	length = gstr->len;
	for (count = 0;  count < 30;  count++) {
		value = g_random_int ();
		if (value) {
			g_string_truncate (gstr, length);
			g_string_append_printf (gstr, "%x", value);
			if (ug_create_dir (gstr->str) == 0) {
				*stamp = value;
				ug_attachment_ref (value);
				return g_string_free (gstr, FALSE);
			}
		}
	}

	g_string_free (gstr, TRUE);
	return NULL;
}
Example #2
0
int  ug_create_dir_all (const char* dir, int len)
{
	const char*   dir_end;
	const char*   element_end;	// path element
	char*         element_os;

	if (len == -1)
		len = strlen (dir);
	dir_end = dir + len;
	element_end = dir;

	for (;;) {
		// skip directory separator "\\\\" or "//"
		for (;  element_end < dir_end;  element_end++) {
			if (*element_end != UG_DIR_SEPARATOR)
				break;
		}
		if (element_end == dir_end)
			return 0;
		// get directory name [dir, element_end)
		for (;  element_end < dir_end;  element_end++) {
			if (*element_end == UG_DIR_SEPARATOR)
				break;
		}
		element_os = (char*) ug_malloc (element_end - dir + 1);
		element_os[element_end - dir] = 0;
		strncpy (element_os, dir, element_end - dir);

		if (element_os == NULL)
			break;
		if (ug_create_dir (element_os) == -1) {
			if (ug_file_is_exist (element_os) == FALSE) {
				ug_free (element_os);
				return -1;
			}
		}
		ug_free (element_os);
	}
	return -1;
}