changeset 18247:97671eb4991e

Avoid some extra nesting.
author Richard Laager <rlaager@wiktel.com>
date Sun, 24 Jun 2007 23:37:53 +0000
parents e236b3bd4542
children ab761acbc614
files libpurple/plugins/log_reader.c
diffstat 1 files changed, 143 insertions(+), 152 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/plugins/log_reader.c	Sun Jun 24 22:20:15 2007 +0000
+++ b/libpurple/plugins/log_reader.c	Sun Jun 24 23:37:53 2007 +0000
@@ -1751,18 +1751,25 @@
 	const char *logdir;
 	PurplePlugin *plugin;
 	PurplePluginProtocolInfo *prpl_info;
-	const char *buddy_name;
 	char *username;
 	char *filename;
 	char *path;
 	gsize length;
 	GError *error = NULL;
-	gchar *contents = NULL;
+	char *contents = NULL;
+	struct qip_logger_data *data = NULL;
+	char *utf8_string = NULL;
+	struct tm prev_tm;
+	gboolean prev_tm_init = FALSE;
+	char *c;
+	char *start_log;
+	char *escaped;
+	int offset = 0;
 
 	g_return_val_if_fail(sn != NULL, list);
 	g_return_val_if_fail(account != NULL, list);
 
-	/* QIP is ICQ messenger. Should we add prpl-aim? */
+	/* QIP only supports ICQ. */
 	if (strcmp(account->protocol_id, "prpl-icq"))
 		return list;
 
@@ -1780,171 +1787,155 @@
 	if (!prpl_info->list_icon)
 		return NULL;
 
-	buddy_name = g_strdup(purple_normalize(account, sn));
-
 	username = g_strdup(purple_normalize(account, account->username));
-	
-	filename = g_strdup_printf("%s.txt", buddy_name);
-	path = g_build_filename(
-		logdir, username, "History", filename, NULL);
-		
+	filename = g_strdup_printf("%s.txt", purple_normalize(account, sn));
+	path = g_build_filename(logdir, username, "History", filename, NULL);
+	g_free(username);
+	g_free(filename);
+
 	purple_debug_info("QIP logger list", "Reading %s\n", path);
 	
 	error = NULL;
 	if (!g_file_get_contents(path, &contents, &length, &error)) {
-		if (error) {
-			purple_debug_error("QIP logger list",
-			                   "Couldn't read file %s \n", path);
-
-			g_error_free(error);
-		}
-	} else if (contents) {
-		struct qip_logger_data *data = NULL;
-		gchar * utf8_string = NULL;
-
-		purple_debug_info("QIP logger list", "File %s is found\n", filename);
+		purple_debug_error("QIP logger list",
+		                   "Couldn't read file %s: %s \n", path, error->message);
+		g_error_free(error);
+
+		g_free(path);
+		return list;
+	}
+
+	g_return_val_if_fail(contents != NULL, list);
+
+	purple_debug_info("QIP logger list", "File %s is found\n", path);
+		
+	/* Convert file contents from Cp1251 to UTF-8 codeset */
+	error = NULL;
+	if (!(utf8_string = g_convert(contents, length, "UTF-8", "Cp1251", NULL, NULL, &error))) {
+		purple_debug_error("QIP logger list",
+		                   "Couldn't convert file %s to UTF-8: %s\n", path, error->message);
+		g_error_free(error);
+
+		g_free(path);
+		g_free(contents);
+		return list;
+	}
+	
+	g_free(contents);
+	contents = g_markup_escape_text(utf8_string, -1);
+	g_free(utf8_string);
+	
+	c = contents;
+	start_log = contents;
+	while (*c) {
+		if (purple_str_has_prefix(c, QIP_LOG_IN_MESSAGE_ESC) || 
+		    purple_str_has_prefix(c, QIP_LOG_OUT_MESSAGE_ESC)) {
+
+			gchar *new_line = c;
 			
-		/* We should convert file contents from Cp1251 to UTF-8 codeset */
-		error = NULL;
-		if (!(utf8_string = g_convert(contents, length, "UTF-8", "Cp1251", NULL, NULL, &error))) {
-			if (error) {
-				purple_debug_error("QIP logger list",
-				                   "Couldn't convert file %s to UTF-8\n", filename);
-				g_error_free(error);
-			}
-		} else {
-			struct tm prev_tm;
-			gboolean prev_tm_init = FALSE;
-			gchar *c;
-			gchar *start_log;
-			gchar *escaped;
-			int offset = 0;
-			
-			purple_debug_info("QIP logger list",
-			                  "File %s converted successfully\n", filename);
-			
-			g_free(contents);
-			escaped = g_markup_escape_text(utf8_string, -1);
-			contents = escaped;
-		
-			c = contents;
-			start_log = contents;
-			while (*c) {
-				if (purple_str_has_prefix(c, QIP_LOG_IN_MESSAGE_ESC) || 
-					purple_str_has_prefix(c, QIP_LOG_OUT_MESSAGE_ESC)) {
-					gchar *new_line = c;
-
-					purple_debug_info("QIP logger list",
-					                  "Find message\n", filename);
-					
-					/* find EOL */
-					c = strstr(c, "\n");
+			/* find EOL */
+			c = strstr(c, "\n");
+			c++;
+
+			/* Find the last '(' character. */
+			if ((tmp = strstr(c, "\n")) != NULL)
+				c = g_strrstr(tmp, "(");
+			else {
+				while (*c)
 					c++;
-					/* searching '(' character from the end of the line */
-					c = strstr(c, "\n");
-					while (*c && *c != '(')
-						--c;
-
-					if (*c == '(') {
-						const char *timestamp = ++c;
-						struct tm tm;
-						purple_debug_info("QIP logger list", "Timestap found\n");
-						
-						/*  Parse the time, day, month and year  */
-						if (sscanf(timestamp, "%u:%u:%u %u/%u/%u",
-							&tm.tm_hour, &tm.tm_min, &tm.tm_sec,
-							&tm.tm_mday, &tm.tm_mon, &tm.tm_year) != 6) {
-								purple_debug_error("QIP logger list",
-								                   "Parsing timestamp error\n");
-						} else {
-							/* cos month of year in [0,11] */
-							tm.tm_mon -= 1; 
-							/* cos years since 1900  */
-							tm.tm_year -= 1900;
-
-							/* Let the C library deal with
-							* daylight savings time. */
-							tm.tm_isdst = -1;
-
-							if (!prev_tm_init) {
-								prev_tm = tm;
-								prev_tm_init = TRUE;
-								
-							} else {
-								double time_diff = 0;
-								
-								time_diff = difftime(mktime(&tm), mktime(&prev_tm));
-
-								if (time_diff > QIP_LOG_TIMEOUT) {
-									PurpleLog *log;
-									
-									/* filling data */
-									data = g_new0(
-										struct qip_logger_data, 1);
-									data->path = g_strdup(path);
-									data->length = new_line - start_log;
-									data->offset = offset;
-									offset += data->length;
-									
-									purple_debug_error("QIP logger list",
-									                   "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset);
-
-									/* XXX: Look into this later... Should we pass in a struct tm? */
-									log = purple_log_new(PURPLE_LOG_IM,
-										sn, account, NULL, mktime(&prev_tm), NULL);
-									
-									log->logger = qip_logger;
-									log->logger_data = data;
-									
-									list = g_list_append(list, log);
-									
-									prev_tm = tm;
-									start_log = new_line;
-								}
-							}
+				c--;
+				c = g_strrstr(c, "(");
+			}
+
+			if (c != NULL) {
+				const char *timestamp = ++c;
+				struct tm tm;
+				
+				/*  Parse the time, day, month and year  */
+				if (sscanf(timestamp, "%u:%u:%u %u/%u/%u",
+				           &tm.tm_hour, &tm.tm_min, &tm.tm_sec,
+				           &tm.tm_mday, &tm.tm_mon, &tm.tm_year) != 6) {
+
+						purple_debug_error("QIP logger list",
+						                   "Parsing timestamp error\n");
+				} else {
+					tm.tm_mon -= 1; 
+					tm.tm_year -= 1900;
+
+					/* Let the C library deal with
+					 * daylight savings time. */
+					tm.tm_isdst = -1;
+
+					if (!prev_tm_init) {
+						prev_tm = tm;
+						prev_tm_init = TRUE;	
+					} else {
+						double time_diff = difftime(mktime(&tm), mktime(&prev_tm));
+
+						if (time_diff > QIP_LOG_TIMEOUT) {
+							PurpleLog *log;
 							
-							/* find EOF */
-							c = strstr(c, "\n");
-							c++;
+							/* filling data */
+							data = g_new0(struct qip_logger_data, 1);
+							data->path = g_strdup(path);
+							data->length = new_line - start_log;
+							data->offset = offset;
+							offset += data->length;
+							
+							purple_debug_error("QIP logger list",
+							                   "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset);
+
+							/* XXX: Look into this later... Should we pass in a struct tm? */
+							log = purple_log_new(PURPLE_LOG_IM, sn, account,
+							                     NULL, mktime(&prev_tm), NULL);
+							
+							log->logger = qip_logger;
+							log->logger_data = data;
+							
+							list = g_list_append(list, log);
+							
+							prev_tm = tm;
+							start_log = new_line;
 						}
 					}
-				} else {
+					
+					/* find EOF */
 					c = strstr(c, "\n");
 					c++;
 				}
 			}
-			
-			/* adding last log */
-			if (prev_tm_init) {
-				PurpleLog *log;
-			
-				/* filling data */
-				data = g_new0(
-				struct qip_logger_data, 1);
-				data->path = g_strdup(path);
-				data->length = c - start_log;
-				data->offset = offset;
-				offset += data->length;
-				purple_debug_error("QIP logger list",
-				                   "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset);
-
-				/* XXX: Look into this later... Should we pass in a struct tm? */
-				log = purple_log_new(PURPLE_LOG_IM, sn, account,
-				                     NULL, mktime(&prev_tm), NULL);
-
-				log->logger = qip_logger;
-				log->logger_data = data;
-
-				list = g_list_append(list, log);
-			}
+		} else {
+			c = strstr(c, "\n");
+			c++;
 		}
-		g_free(contents); 
-	} 
-
-	g_free(username);
+	}
+	
+	/* adding last log */
+	if (prev_tm_init) {
+		PurpleLog *log;
+	
+		/* filling data */
+		data = g_new0(
+		struct qip_logger_data, 1);
+		data->path = g_strdup(path);
+		data->length = c - start_log;
+		data->offset = offset;
+		offset += data->length;
+		purple_debug_error("QIP logger list",
+		                   "Creating log: path = (%s); length = (%d); offset = (%d)\n", data->path, data->length, data->offset);
+
+		/* XXX: Look into this later... Should we pass in a struct tm? */
+		log = purple_log_new(PURPLE_LOG_IM, sn, account,
+		                     NULL, mktime(&prev_tm), NULL);
+
+		log->logger = qip_logger;
+		log->logger_data = data;
+
+		list = g_list_append(list, log);
+	}
+
+	g_free(contents);
 	g_free(path);
-	g_free(filename);
-
 	return list;
 }