changeset 706:066b90ad9925

cache_find_location(): use g_build_filename() and move redundant code to new functions.
author zas_
date Tue, 20 May 2008 22:47:13 +0000
parents a7009e2c4a0b
children 41c17c66e63b
files src/cache.c
diffstat 1 files changed, 26 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/cache.c	Tue May 20 22:22:11 2008 +0000
+++ b/src/cache.c	Tue May 20 22:47:13 2008 +0000
@@ -679,11 +679,31 @@
 	return path;
 }
 
+static gchar *cache_build_path_local(const gchar *source, const gchar *cache_local, const gchar *cache_ext)
+{
+	gchar *path;
+	gchar *base = remove_level_from_path(source);
+	gchar *name = g_strconcat(filename_from_path(source), cache_ext, NULL);
+	path = g_build_filename(base, cache_local, name, NULL);
+	g_free(name);
+	g_free(base);
+	
+	return path;
+}
+
+static gchar *cache_build_path_rc(const gchar *source, const gchar *cache_rc, const gchar *cache_ext)
+{
+	gchar *path;
+	gchar *name = g_strconcat(source, cache_ext, NULL);
+	path = g_build_filename(homedir(), cache_rc, name, NULL);
+	g_free(name);
+
+	return path;
+}
+
 gchar *cache_find_location(CacheType type, const gchar *source)
 {
 	gchar *path;
-	const gchar *name;
-	gchar *base;
 	const gchar *cache_rc;
 	const gchar *cache_local;
 	const gchar *cache_ext;
@@ -693,9 +713,6 @@
 
 	cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
 
-	name = filename_from_path(source);
-	base = remove_level_from_path(source);
-
 	if (type == CACHE_TYPE_METADATA)
 		{
 		prefer_local = options->enable_metadata_dirs;
@@ -707,11 +724,11 @@
 
 	if (prefer_local)
 		{
-		path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
+		path = cache_build_path_local(source, cache_local, cache_ext);
 		}
 	else
 		{
-		path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
+		path = cache_build_path_rc(source, cache_rc, cache_ext);
 		}
 
 	if (!isfile(path))
@@ -721,11 +738,11 @@
 		/* try the opposite method if not found */
 		if (!prefer_local)
 			{
-			path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
+			path = cache_build_path_local(source, cache_local, cache_ext);
 			}
 		else
 			{
-			path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
+			path = cache_build_path_rc(source, cache_rc, cache_ext);
 			}
 
 		if (!isfile(path))
@@ -735,8 +752,6 @@
 			}
 		}
 
-	g_free(base);
-
 	return path;
 }