Esempio n. 1
0
static AnjutaToken *
anjuta_token_find_target_property_position (AmpTargetNode *target,
                                            AnjutaTokenType type)
{
	AnjutaToken *pos = NULL;
	gboolean after = FALSE;
	GList *list;
	AmpGroupNode *group;
	AnjutaToken *makefile;

	group = AMP_GROUP_NODE (anjuta_project_node_parent_type (ANJUTA_PROJECT_NODE (target), ANJUTA_PROJECT_GROUP));

	/* Try to find a better position */

	/* 1. With the other properties of the target */
	list = amp_target_node_get_all_token (target);
	if (list != NULL)
	{
		GList *link;
		AnjutaTokenType best = 0;

		for (link = list; link != NULL; link = g_list_next (link))
		{
			AnjutaToken *token = (AnjutaToken *)link->data;
			AnjutaTokenType existing = anjuta_token_get_type (token);

			if ((existing < AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) || (existing > AM_TOKEN_LAST_ORDERED_TARGET_MACRO))
			{
				token = anjuta_token_list (token);
				if (token != NULL) existing = anjuta_token_get_type (token);
			}

			if ((existing >= AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) && (existing <= AM_TOKEN_LAST_ORDERED_TARGET_MACRO))
			{
				if (existing > type)
				{
					if ((best == 0) || ((existing - type) < best))
					{
						best = existing - type;
						pos = token;
						after = FALSE;
					}
				}
				else
				{
					if ((best == 0) || ((type -existing) < best))
					{
						best = type - existing;
						pos = token;
						after = TRUE;
					}
				}
			}
		}
		g_list_free (list);
	}


	/* 2. With properties of sibling targets */
	if (pos == NULL)
	{
		AnjutaProjectNode *prev = ANJUTA_PROJECT_NODE (target);
		AnjutaProjectNode *next = ANJUTA_PROJECT_NODE (target);
		AmpTargetNode *sibling;
		AnjutaTokenFile *makefile;
		AnjutaToken *target_list = NULL;
		GList *link;

		link = amp_target_node_get_token (target, ANJUTA_TOKEN_ARGUMENT);
		if ((link != NULL) && (link->data != NULL))
		{
			target_list = anjuta_token_list ((AnjutaToken *)link->data);
		}

		makefile = amp_group_node_get_make_token_file (group);

		if (makefile != NULL)
		{
			after = TRUE;
			while ((prev != NULL) || (next != NULL))
			{
				/* Find sibling */
				if (after)
				{
					while (prev != NULL)
					{
						prev = anjuta_project_node_prev_sibling (prev);
						if (anjuta_project_node_get_node_type (prev) == ANJUTA_PROJECT_TARGET) break;
					}
					sibling = AMP_TARGET_NODE (prev);
				}
				else
				{
					while (next != NULL)
					{
						next = anjuta_project_node_next_sibling (next);
						if (anjuta_project_node_get_node_type (next) == ANJUTA_PROJECT_TARGET) break;
					}
					sibling = AMP_TARGET_NODE (next);
				}
				list = sibling == NULL ? NULL : amp_target_node_get_all_token (sibling);

				/* Check that the target is in the same list */
				if ((list != NULL) && (target_list != NULL))
				{
					AnjutaToken *token;

					link = amp_target_node_get_token (sibling, ANJUTA_TOKEN_ARGUMENT);
					if ((link != NULL) && (link->data != NULL))
					{
						token = anjuta_token_list ((AnjutaToken *)link->data);
					}

					if ((token != NULL) && (target_list != token))
					{
						/* Target is in another list, do not use it, nor following ones */
						list = NULL;
						if (after)
						{
							prev = NULL;
						}
						else
						{
							next = NULL;
						}
					}
				}

				if (list != NULL)
				{
					gsize best = 0;

					for (link = list; link != NULL; link = g_list_next (link))
					{
						AnjutaToken *token = (AnjutaToken *)link->data;
						AnjutaTokenType existing = anjuta_token_get_type (token);

						if ((existing < AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) || (existing > AM_TOKEN_LAST_ORDERED_TARGET_MACRO))
						{
							token = anjuta_token_list (token);
							if (token != NULL) existing = anjuta_token_get_type (token);
						}

						if ((existing >= AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) && (existing <= AM_TOKEN_LAST_ORDERED_TARGET_MACRO))
						{
							gsize tpos;

							tpos = anjuta_token_file_get_token_position (makefile, token);

							if ((best == 0) ||
							    (after && (tpos > best)) ||
							    (!after && (tpos < best)))
							{
								pos = token;
								best = tpos;
							}
						}
					}
					g_list_free (list);
					list = NULL;

					if (best != 0) break;
				}

				after = after ? FALSE : TRUE;
			}
		}
	}


	/* 3. After target declaration */
	if (pos == NULL)
	{
		list = amp_target_node_get_token (AMP_TARGET_NODE (target), ANJUTA_TOKEN_ARGUMENT);
		if (list != NULL)
		{
			pos = (AnjutaToken *)list->data;
			if (pos != NULL)
			{
				pos = anjuta_token_list (pos);
				if (pos != NULL)
				{
					pos = anjuta_token_list (pos);
				}
			}
		}
		after = TRUE;
	}

	/* 4. At the end of the file */
	if (pos == NULL)
	{
		makefile = amp_group_node_get_makefile_token (group);

		for (pos = anjuta_token_first_item (makefile); (pos != NULL) && (anjuta_token_next_item (pos) != NULL); pos = anjuta_token_next_item (pos));

		after = TRUE;
	}

	/* 5. Create new file */
	if (pos == NULL)
	{
		/* Empty file */
		pos = anjuta_token_new_string (ANJUTA_TOKEN_COMMENT | ANJUTA_TOKEN_ADDED, "## Process this file with automake to produce Makefile.in\n");
		anjuta_token_append_child (makefile, pos);
		amp_group_node_update_makefile (group, pos);
	}


	/* Find end of line */
	if (after)
	{
		while (pos != NULL)
		{
			if (anjuta_token_get_type (pos) == ANJUTA_TOKEN_EOL) break;
			if (anjuta_token_next (pos) == NULL)
			{
				pos = anjuta_token_insert_token_list (after, pos,
					ANJUTA_TOKEN_EOL, "\n",
					NULL);

				break;
			}
			pos = anjuta_token_next (pos);
		}
	}

	pos = anjuta_token_insert_token_list (after, pos,
		    ANJUTA_TOKEN_EOL, "\n",
	    	NULL);
	pos = anjuta_token_insert_token_list (after, pos,
		    ANJUTA_TOKEN_EOL, "\n",
	    	NULL);
	amp_group_node_update_makefile (group, pos);


	return pos;
}
Esempio n. 2
0
AmpTargetNode*
amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const gchar *install, gint flags, AnjutaProjectNode *parent, GError **error)
{
	const gchar *basename;

	/* Check parent if present */
	if (parent != NULL)
	{
		if ((anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_GROUP) &&
		    (amp_group_node_get_makefile_token (AMP_GROUP_NODE (parent)) == NULL))
		{
			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
		               _("Target parent is not a valid group"));

			return NULL;
		}
	}

	/* Validate target name */
	if (!name || strlen (name) <= 0)
	{
		amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
		               _("Please specify target name"));
		return NULL;
	}
	{
		gboolean failed = FALSE;
		const gchar *ptr = name;
		while (*ptr) {
			if (!isalnum (*ptr) && *ptr != '.' && *ptr != '-' &&
			    *ptr != '_' && *ptr != '/')
				failed = TRUE;
			ptr++;
		}
		if (failed) {
			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
			               _("Target name can only contain alphanumeric, '_', '-', '/' or '.' characters"));
			return NULL;
		}
	}

	/* Skip eventual directory name */
	basename = strrchr (name, '/');
	basename = basename == NULL ? name : basename + 1;


	if ((type & ANJUTA_PROJECT_ID_MASK) == ANJUTA_PROJECT_SHAREDLIB) {
		if (strlen (basename) < 7 ||
		    strncmp (basename, "lib", strlen("lib")) != 0 ||
		    strcmp (&basename[strlen(basename) - 3], ".la") != 0) {
			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
			               _("Shared library target name must be of the form 'libxxx.la'"));
			return NULL;
		}
	}
	else if ((type & ANJUTA_PROJECT_ID_MASK) == ANJUTA_PROJECT_STATICLIB) {
		if (strlen (basename) < 6 ||
		    strncmp (basename, "lib", strlen("lib")) != 0 ||
		    strcmp (&basename[strlen(basename) - 2], ".a") != 0) {
			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
			               _("Static library target name must be of the form 'libxxx.a'"));
			return NULL;
		}
	}
	else if ((type & ANJUTA_PROJECT_ID_MASK) == ANJUTA_PROJECT_LT_MODULE) {
		if (strlen (basename) < 4 ||
		    strcmp (&basename[strlen(basename) - 3], ".la") != 0) {
			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
			               _("Module target name must be of the form 'xxx.la'"));
			return NULL;
		}
	}

	return amp_target_node_new (name, type, install, flags);
}
Esempio n. 3
0
static AnjutaToken *
anjuta_token_find_group_property_position (AmpGroupNode *group,
                                            AnjutaTokenType type)
{
	AnjutaToken *pos = NULL;
	gboolean after = FALSE;
	GList *list;
	AnjutaToken *makefile;


	/* Try to find a better position */

	/* 1. With the other properties of the group */
	list = amp_group_node_get_all_token (group);
	if (list != NULL)
	{
		GList *link;
		AnjutaTokenType best = 0;

		for (link = list; link != NULL; link = g_list_next (link))
		{
			AnjutaToken *token = (AnjutaToken *)link->data;
			AnjutaTokenType existing = anjuta_token_get_type (token);

			if ((existing < AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) || (existing > AM_TOKEN_LAST_ORDERED_TARGET_MACRO))
			{
				token = anjuta_token_list (token);
				if (token != NULL) existing = anjuta_token_get_type (token);
			}

			if ((existing >= AM_TOKEN_FIRST_ORDERED_TARGET_MACRO) && (existing <= AM_TOKEN_LAST_ORDERED_TARGET_MACRO))
			{
				if (existing > type)
				{
					if ((best == 0) || ((existing - type) < best))
					{
						best = existing - type;
						pos = token;
						after = FALSE;
					}
				}
				else
				{
					if ((best == 0) || ((type -existing) < best))
					{
						best = type - existing;
						pos = token;
						after = TRUE;
					}
				}
			}
		}
		g_list_free (list);
	}

	/* 2. At the end of the file */
	if (pos == NULL)
	{
		makefile = amp_group_node_get_makefile_token (group);

		for (pos = anjuta_token_first_item (makefile); (pos != NULL) && (anjuta_token_next_item (pos) != NULL); pos = anjuta_token_next_item (pos));

		after = TRUE;
	}

	/* makefile can be NULL for the root node if there isn't any corresponding Makefile.am */
	if (makefile != NULL)
	{
		/* 3. Create new file */
		if (pos == NULL)
		{
			/* Empty file */
			pos = anjuta_token_new_string (ANJUTA_TOKEN_COMMENT | ANJUTA_TOKEN_ADDED, "## Process this file with automake to produce Makefile.in\n");
			anjuta_token_append_child (makefile, pos);
			amp_group_node_update_makefile (group, pos);
		}

		/* Find end of line */
		if (after)
		{
			while (pos != NULL)
			{
				if (anjuta_token_get_type (pos) == ANJUTA_TOKEN_EOL) break;
				if (anjuta_token_next (pos) == NULL)
				{
					pos = anjuta_token_insert_token_list (after, pos,
						ANJUTA_TOKEN_EOL, "\n",
						NULL);

					break;
				}
				pos = anjuta_token_next (pos);
			}
		}

		pos = anjuta_token_insert_token_list (after, pos,
			    ANJUTA_TOKEN_EOL, "\n",
		    	NULL);
		pos = anjuta_token_insert_token_list (after, pos,
			    ANJUTA_TOKEN_EOL, "\n",
		    	NULL);
		amp_group_node_update_makefile (group, pos);
	}

	return pos;
}