socsvn commit: r253568 - soc2013/mattbw/backend

mattbw at FreeBSD.org mattbw at FreeBSD.org
Wed Jun 26 23:51:12 UTC 2013


Author: mattbw
Date: Wed Jun 26 23:51:12 2013
New Revision: 253568
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=253568

Log:
  start moving more generic string utilities out

Added:
  soc2013/mattbw/backend/utils.c
  soc2013/mattbw/backend/utils.h
Modified:
  soc2013/mattbw/backend/Makefile
  soc2013/mattbw/backend/db.c
  soc2013/mattbw/backend/iterate.c

Modified: soc2013/mattbw/backend/Makefile
==============================================================================
--- soc2013/mattbw/backend/Makefile	Wed Jun 26 23:28:59 2013	(r253567)
+++ soc2013/mattbw/backend/Makefile	Wed Jun 26 23:51:12 2013	(r253568)
@@ -2,7 +2,7 @@
 
 LIB=		pk_backend_pkgng
 SHLIB_MAJOR=	1
-SRCS=		pk-backend-pkgng.c groups.c db.c licenses.c iterate.c
+SRCS=		pk-backend-pkgng.c groups.c db.c licenses.c iterate.c utils.c
 SRCS+=		actions/get-details.c actions/get-files.c actions/get-repo-list.c
 
 LIBDIR=		/usr/local/lib/packagekit-backend

Modified: soc2013/mattbw/backend/db.c
==============================================================================
--- soc2013/mattbw/backend/db.c	Wed Jun 26 23:28:59 2013	(r253567)
+++ soc2013/mattbw/backend/db.c	Wed Jun 26 23:51:12 2013	(r253568)
@@ -26,8 +26,7 @@
 
 #include "db.h"			/* prototypes */
 #include "iterate.h"		/* pkg_func_ptr */
-
-static const char *null_if_empty(const char *in);
+#include "utils.h"		/* null_if_empty, split_id */
 
 static gboolean
 db_query_split(const gchar *name,
@@ -81,6 +80,8 @@
 	return success;
 }
 
+
+
 /*
  * Performs a package database query against a PackageID, and then hands the
  * matching results to an emitter function.
@@ -88,38 +89,32 @@
  * The exact type of query depends on the repository given.
  */
 gboolean
-db_query_with_id(const gchar *package_id,
+db_query_with_id(const gchar *id,
 		 PkBackend *backend,
 		 struct pkgdb *db,
 		 int load_flags,
 		 pkg_func_ptr emitter)
 {
-	gchar         **parts;
 	gboolean	success;
+	gchar         **strv;
+	const gchar    *name;
+	const gchar    *version;
+	const gchar    *arch;
+	const gchar    *data;
 
 	success = FALSE;
+	strv = NULL;
 
-	parts = pk_package_id_split(package_id);
-	if (parts == NULL)
+	success = split_id(id, &strv, &name, &version, &arch, &data);
+	if (success == FALSE)
 		pk_backend_error_code(backend,
 				      PK_ERROR_ENUM_PACKAGE_ID_INVALID,
 				      "invalid package id");
 	else {
-		/* Parts of the package ID */
-		const gchar    *name;
-		const gchar    *version;
-		const gchar    *arch;
-		const gchar    *data;
-
 		/*
-		 * Make really really REALLY sure that the PackageID fields
-		 * either have sensible data in them, or are NULL.
+		 * If we got a repository name, then we want to make sure it
+		 * corresponds to a real repository.
 		 */
-		name = null_if_empty(parts[PK_PACKAGE_ID_NAME]);
-		version = null_if_empty(parts[PK_PACKAGE_ID_VERSION]);
-		arch = null_if_empty(parts[PK_PACKAGE_ID_ARCH]);
-		data = null_if_empty(parts[PK_PACKAGE_ID_DATA]);
-
 		/*
 		 * If the PackageID has a repository specified (even if it's
 		 * "installed" i.e. currently installed package), running
@@ -151,7 +146,7 @@
 			pk_backend_error_code(backend,
 					    PK_ERROR_ENUM_PACKAGE_NOT_FOUND,
 					      "package not found");
-		g_strfreev(parts);
+		g_strfreev(strv);
 	}
 
 	return success;
@@ -196,15 +191,3 @@
 					     emitter);
 	return success;
 }
-
-/*
- * If the input pointer points to a string that is empty, return the null
- * pointer; else pass through the pointer unmolested.
- * 
- * This does NOT free anything!
- */
-static const char *
-null_if_empty(const char *in)
-{
-	return (in != NULL && strlen(in) == 0) ? NULL : in;
-}

Modified: soc2013/mattbw/backend/iterate.c
==============================================================================
--- soc2013/mattbw/backend/iterate.c	Wed Jun 26 23:28:59 2013	(r253567)
+++ soc2013/mattbw/backend/iterate.c	Wed Jun 26 23:51:12 2013	(r253568)
@@ -27,20 +27,20 @@
 
 #include "db.h"			/* open_remote_db */
 #include "hash_traverse.h"	/* HASH_FOR */
+#include "utils.h"		/* string_match */
 
 static gboolean
 try_id_match(struct pkg *pkg, const gchar *name, const gchar *version, const
 	     gchar *arch, const gchar *data, gchar **match_id);
 
-static gboolean	string_match(const char *left, const char *right);
 
 gboolean
 iterate_id_matches(struct pkgdb_it *iterator,
 		   PkBackend *backend,
-		   const gchar *name,
-		   const gchar *version,
-		   const gchar *arch,
-		   const gchar *data,
+		   const char *name,
+		   const char *version,
+		   const char *arch,
+		   const char *data,
 		   int fetch_flags,
 		   pkg_func_ptr iterate_f)
 {
@@ -86,8 +86,8 @@
 }
 
 static gboolean
-try_id_match(struct pkg *pkg, const gchar *name, const gchar *version, const
-	     gchar *arch, const gchar *data, gchar **match_id)
+try_id_match(struct pkg *pkg, const char *name, const char *version, const
+	     char *arch, const char *data, char **match_id)
 {
 	const char     *p_arch;
 	const char     *p_data;
@@ -130,24 +130,6 @@
 }
 
 /*
- * Checks two strings with strcmp and emits TRUE if they match. If either
- * string is NULL, emit TRUE as well (this is so that missing PackageID
- * elements trigger matches).
- */
-static gboolean
-string_match(const char *left, const char *right)
-{
-	int		result;
-
-	if (left == NULL || right == NULL)
-		result = TRUE;
-	else
-		result = (strcmp(left, right) == 0 ? TRUE : FALSE);
-
-	return result;
-}
-
-/*
  * Iterates over a set of PackageIDs provided for this job with a function.
  * 
  * This provides each iterating function call with an open database connection

Added: soc2013/mattbw/backend/utils.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/utils.c	Wed Jun 26 23:51:12 2013	(r253568)
@@ -0,0 +1,95 @@
+/*-
+ *
+ * Copyright (C) 2013 Matt Windsor <mattbw at FreeBSD.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/* String/ID utilities. */
+
+#include <string.h>
+
+#include <glib.h>
+#include "pk-backend.h"		/* pk_package_id_split */
+
+#include "utils.h"		/* prototypes */
+
+/*
+ * If the input pointer points to a string that is empty, return the null
+ * pointer; else pass through the pointer unmolested.
+ * 
+ * This does NOT free anything!
+ */
+const char     *
+null_if_empty(const char *in)
+{
+	return (in != NULL && strlen(in) == 0) ? NULL : in;
+}
+
+/*
+ * Splits a PackageID into its constituent parts, and returns both the split
+ * string vector (to be freed with g_strfreev later) and four pointers to its
+ * constituents (or NULL where they are empty strings).
+ * 
+ * Returns FALSE on failure.
+ */
+gboolean
+split_id(const char *id,
+	 char ***strv_p,
+	 const char **name_p,
+	 const char **version_p,
+	 const char **arch_p,
+	 const char **data_p)
+{
+	int		success;
+	char          **strv;
+
+	success = FALSE;
+	strv = pk_package_id_split(id);
+	if (strv != NULL) {
+		/*
+		 * Make really really REALLY sure that the PackageID fields
+		 * either have sensible data in them, or are NULL.
+		 */
+		*name_p = null_if_empty(strv[PK_PACKAGE_ID_NAME]);
+		*version_p = null_if_empty(strv[PK_PACKAGE_ID_VERSION]);
+		*arch_p = null_if_empty(strv[PK_PACKAGE_ID_ARCH]);
+		*data_p = null_if_empty(strv[PK_PACKAGE_ID_DATA]);
+
+		*strv_p = strv;
+		success = TRUE;
+	}
+	return success;
+}
+
+/*
+ * Checks two strings with strcmp and emits TRUE if they match. If either
+ * string is NULL, emit TRUE as well (this is so that missing PackageID
+ * elements trigger matches).
+ */
+gboolean
+string_match(const char *left, const char *right)
+{
+	int		result;
+
+	if (left == NULL || right == NULL)
+		result = TRUE;
+	else
+		result = (strcmp(left, right) == 0 ? TRUE : FALSE);
+
+	return result;
+}

Added: soc2013/mattbw/backend/utils.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/utils.h	Wed Jun 26 23:51:12 2013	(r253568)
@@ -0,0 +1,38 @@
+/*-
+ *
+ * Copyright (C) 2013 Matt Windsor <mattbw at FreeBSD.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _PKGNG_BACKEND_UTILS_H_
+#define _PKGNG_BACKEND_UTILS_H_
+
+#include <glib.h>		/* gboolean */
+
+const char     *null_if_empty(const char *in);
+gboolean
+split_id(const char *id,
+	 char ***strv_p,
+	 const char **name_p,
+	 const char **version_p,
+	 const char **arch_p,
+	 const char **data_p);
+gboolean	string_match(const char *left, const char *right);
+
+
+#endif				/* !_PKGNG_BACKEND_DETAILS_H_ */


More information about the svn-soc-all mailing list