On Monday 31 March 2008, Phil Dibowitz wrote:
> Now that Stephen's patch has been merged, did you want to re-submit your
> patch against the latest CVS? In theory it should make your patch a bit
> smaller...

Yup - piece of cake now, with all that \0 termination or not stuff gone.
Makes some functions almost redundant.
I compiled the patch and did a few tests with up- and downloading files
from/to the remote that looked ok.
Added also debug output of the Harmony code string in learn_ir_code.
Maybe read_from_file should eventually even be moved to binaryfile?

Andreas
Index: libconcord/libconcord.cpp
===================================================================
RCS file: /cvsroot/concordance/concordance/libconcord/libconcord.cpp,v
retrieving revision 1.18
diff -u -3 -p -u -p -r1.18 libconcord.cpp
--- libconcord/libconcord.cpp	30 Mar 2008 23:59:48 -0000	1.18
+++ libconcord/libconcord.cpp	1 Apr 2008 07:43:06 -0000
@@ -320,6 +320,37 @@ void delete_blob(uint8_t *ptr)
 	delete[] ptr;
 }
 
+/*
+ * Common routine to read contents of file named *file_name into
+ * byte buffer **out. Get size from file and return out[size] 
+ * as read from file.
+ */
+
+int read_from_file(char *file_name, uint8_t **out, uint32_t *size)
+{
+	binaryinfile file;
+
+	if (file_name == NULL) {
+		debug("Empty file_name");
+		return LC_ERROR_OS_FILE;
+	}
+
+	if (file.open(file_name) != 0) {
+		debug("Failed to open %s", file_name);
+		return LC_ERROR_OS_FILE;
+	}
+
+	*size = file.getlength();
+	*out = new uint8_t[*size];
+	file.read(*out, *size);
+
+	if (file.close() != 0) {
+		debug("Failed to close %s\n", file_name);
+		return LC_ERROR_OS_FILE;
+	}
+	return 0;
+}
+
 
 /*
  * GENERAL REMOTE STUFF
@@ -444,26 +475,17 @@ int post_postconfig(uint8_t *data, uint3
 
 int post_connect_test_success(char *file_name)
 {
+	uint32_t size;
+	uint8_t *buf;
+	int err = 0;
 	/*
 	 * If we arrived, we can talk to the remote - so if it's
 	 * just a connectivity test, tell the site we succeeded
 	 */
-	binaryinfile file;
-	if (file.open(file_name) != 0) {
-		return LC_ERROR_OS_FILE;
-	}
-
-	const uint32_t size = file.getlength();
-	uint8_t * const buf = new uint8_t[size];
-	file.read(buf, size);
-
-	Post(buf, size, "POSTOPTIONS", ri, true);
-
-	if (file.close() != 0) {
-		return LC_ERROR_OS_FILE;
+	if ( (err = read_from_file(file_name, &buf, &size)) == 0 ) {
+		Post(buf, size, "POSTOPTIONS", ri, true);
 	}
-
-	return 0;
+	return err;
 }
 
 int get_time()
@@ -544,23 +566,7 @@ int write_config_to_remote(uint8_t *in, 
 
 int read_config_from_file(char *file_name, uint8_t **out, uint32_t *size)
 {
-	binaryinfile file;
-
-	if (file.open(file_name) != 0) {
-		debug("Failed to open %s", file_name);
-		return LC_ERROR_OS_FILE;
-	}
-
-	*size = file.getlength();
-	*out = new uint8_t[*size];
-	file.read(*out, *size);
-
-	if (file.close() != 0) {
-		debug("Failed to close %s", file_name);
-		return LC_ERROR_OS_FILE;
-	}
-
-	return 0;
+	return read_from_file(file_name, out, size);
 }
 
 int write_config_to_file(uint8_t *in, uint32_t size, char *file_name,
@@ -787,23 +793,7 @@ int write_safemode_to_file(uint8_t *in, 
 
 int read_safemode_from_file(char *file_name, uint8_t **out, uint32_t *size)
 {
-	binaryinfile file;
-
-	if (file.open(file_name) != 0) {
-		debug("Failed to open %s", file_name);
-		return LC_ERROR_OS_FILE;
-	}
-
-	*size = file.getlength();
-	*out = new uint8_t[*size];
-	file.read(*out, *size);
-
-	if (file.close() != 0) {
-		debug("Failed to close %s", file_name);
-		return LC_ERROR_OS_FILE;
-	}
-
-	return 0;
+	return read_from_file(file_name, out, size);
 }
 
 /*
@@ -1106,23 +1096,7 @@ int extract_firmware_binary(uint8_t *xml
 	
 int read_firmware_from_file(char *file_name, uint8_t **out, uint32_t *size, int binary)
 {
-	binaryinfile file;
-
-	if (file.open(file_name) != 0) {
-		debug("Failed to open %s", file_name);
-		return LC_ERROR_OS_FILE;
-	}
-
-	*size = file.getlength();
-	*out = new uint8_t[*size];
-	file.read(*out, *size);
-
-	if (file.close() != 0) {
-		debug("Failed to close %s", file_name);
-		return LC_ERROR_OS_FILE;
-	}
-
-	return 0;
+	return read_from_file(file_name, out, size);
 }
 
 /*
@@ -1133,15 +1107,11 @@ int learn_ir_commands(char *file_name, i
 	int err;
 
 	if (file_name) {
-		binaryinfile file;
-		if (file.open(file_name)) {
-			return LC_ERROR_OS_FILE;
-		}
-		uint32_t size = file.getlength();
-		uint8_t * const x = new uint8_t[size];
-		file.read(x, size);
-		if (file.close() != 0) {
-			return LC_ERROR_OS_FILE;
+		uint32_t size = 0;
+		uint8_t *x = NULL;
+		err = read_from_file(file_name, &x, &size);
+		if (err != 0) {
+			return err;
 		}
 
 		uint8_t *t = x;
@@ -1161,10 +1131,11 @@ int learn_ir_commands(char *file_name, i
 
 		string ls;
 		rmt->LearnIR(&ls);
-		//printf("%s\n",ls.c_str());
+		debug("Learned code: %s",ls.c_str());
 
-		if (post)
+		if (post) {
 			Post(x, size, "POSTOPTIONS", ri, true, &ls, &keyname);
+		}
 	} else {
 		rmt->LearnIR();
 	}
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
concordance-devel mailing list
concordance-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/concordance-devel

Reply via email to