tool/CommandRegWrite.cpp
changeset 1804 742607c464c4
parent 1431 70e891465da1
child 1826 ec6223c3b7ec
--- a/tool/CommandRegWrite.cpp	Tue Feb 02 19:38:32 2010 +0100
+++ b/tool/CommandRegWrite.cpp	Fri Feb 05 10:29:47 2010 +0100
@@ -53,20 +53,20 @@
         << getBriefDescription() << endl
         << endl
         << "This command requires a single slave to be selected." << endl
-    	<< endl
+        << endl
         << "Arguments:" << endl
         << "  OFFSET  is the register address to write to." << endl
         << "  DATA    depends on whether a datatype was specified" << endl
-		<< "          with the --type option: If not, DATA must be" << endl
-		<< "          either a path to a file with data to write," << endl
-		<< "          or '-', which means, that data are read from" << endl
-		<< "          stdin. If a datatype was specified, VALUE is" << endl
-		<< "          interpreted respective to the given type." << endl
+        << "          with the --type option: If not, DATA must be" << endl
+        << "          either a path to a file with data to write," << endl
+        << "          or '-', which means, that data are read from" << endl
+        << "          stdin. If a datatype was specified, VALUE is" << endl
+        << "          interpreted respective to the given type." << endl
         << endl
         << "These are the valid data types:" << endl
         << "  int8, int16, int32, int64, uint8, uint16, uint32," << endl
         << "  uint64, string." << endl
-		<< endl
+        << endl
         << "Command-specific options:" << endl
         << "  --alias    -a <alias>" << endl
         << "  --position -p <pos>    Slave selection. See the help of" << endl
@@ -101,93 +101,93 @@
         throwInvalidUsageException(err);
     }
 
-	if (getDataType().empty()) {
-		if (args[1] == "-") {
-			loadRegData(&data, cin);
-		} else {
-			file.open(args[1].c_str(), ifstream::in | ifstream::binary);
-			if (file.fail()) {
-				err << "Failed to open '" << args[1] << "'!";
-				throwCommandException(err);
-			}
-			loadRegData(&data, file);
-			file.close();
-		}
-	} else {
-		stringstream strValue;
-		const DataType *dataType = findDataType(getDataType());
+    if (getDataType().empty()) {
+        if (args[1] == "-") {
+            loadRegData(&data, cin);
+        } else {
+            file.open(args[1].c_str(), ifstream::in | ifstream::binary);
+            if (file.fail()) {
+                err << "Failed to open '" << args[1] << "'!";
+                throwCommandException(err);
+            }
+            loadRegData(&data, file);
+            file.close();
+        }
+    } else {
+        stringstream strValue;
+        const DataType *dataType = findDataType(getDataType());
 
         if (!dataType) {
             err << "Invalid data type '" << getDataType() << "'!";
             throwInvalidUsageException(err);
         }
 
-		if (dataType->byteSize) {
-			data.length = dataType->byteSize;
-			data.data = new uint8_t[data.length];
-		}
-
-		strValue << args[1];
-		strValue >> resetiosflags(ios::basefield); // guess base from prefix
-		strValue.exceptions(ios::failbit);
-
-		try {
-			if (string(dataType->name) == "int8") {
-				int16_t val; // uint8_t is interpreted as char
-				strValue >> val;
-				if (val > 127 || val < -128)
-					throw ios::failure("Value out of range");
-				*data.data = (int8_t) val;
-			} else if (string(dataType->name) == "int16") {
-				int16_t val;
-				strValue >> val;
-				*(int16_t *) data.data = cpu_to_le16(val);
-			} else if (string(dataType->name) == "int32") {
-				int32_t val;
-				strValue >> val;
-				*(int32_t *) data.data = cpu_to_le32(val);
-			} else if (string(dataType->name) == "int64") {
-				int64_t val;
-				strValue >> val;
-				*(int64_t *) data.data = cpu_to_le64(val);
-			} else if (string(dataType->name) == "uint8") {
-				uint16_t val; // uint8_t is interpreted as char
-				strValue >> val;
-				if (val > 0xff)
-					throw ios::failure("Value out of range");
-				*data.data = (uint8_t) val;
-			} else if (string(dataType->name) == "uint16") {
-				uint16_t val;
-				strValue >> val;
-				*(uint16_t *) data.data = cpu_to_le16(val);
-			} else if (string(dataType->name) == "uint32") {
-				uint32_t val;
-				strValue >> val;
-				*(uint32_t *) data.data = cpu_to_le32(val);
-			} else if (string(dataType->name) == "uint64") {
-				uint64_t val;
-				strValue >> val;
-				*(uint64_t *) data.data = cpu_to_le64(val);
-			} else if (string(dataType->name) == "string" ||
-					string(dataType->name) == "octet_string") {
-				data.length = strValue.str().size();
-				if (!data.length) {
-					err << "Zero-size string now allowed!";
-					throwCommandException(err);
-				}
-				data.data = new uint8_t[data.length];
-				strValue >> (char *) data.data;
-			} else {
-				err << "Invalid data type " << dataType->name;
-				throwCommandException(err);
-			}
-		} catch (ios::failure &e) {
-			delete [] data.data;
-			err << "Invalid value argument '" << args[1]
-				<< "' for type '" << dataType->name << "'!";
-			throwInvalidUsageException(err);
-		}
-	}
+        if (dataType->byteSize) {
+            data.length = dataType->byteSize;
+            data.data = new uint8_t[data.length];
+        }
+
+        strValue << args[1];
+        strValue >> resetiosflags(ios::basefield); // guess base from prefix
+        strValue.exceptions(ios::failbit);
+
+        try {
+            if (string(dataType->name) == "int8") {
+                int16_t val; // uint8_t is interpreted as char
+                strValue >> val;
+                if (val > 127 || val < -128)
+                    throw ios::failure("Value out of range");
+                *data.data = (int8_t) val;
+            } else if (string(dataType->name) == "int16") {
+                int16_t val;
+                strValue >> val;
+                *(int16_t *) data.data = cpu_to_le16(val);
+            } else if (string(dataType->name) == "int32") {
+                int32_t val;
+                strValue >> val;
+                *(int32_t *) data.data = cpu_to_le32(val);
+            } else if (string(dataType->name) == "int64") {
+                int64_t val;
+                strValue >> val;
+                *(int64_t *) data.data = cpu_to_le64(val);
+            } else if (string(dataType->name) == "uint8") {
+                uint16_t val; // uint8_t is interpreted as char
+                strValue >> val;
+                if (val > 0xff)
+                    throw ios::failure("Value out of range");
+                *data.data = (uint8_t) val;
+            } else if (string(dataType->name) == "uint16") {
+                uint16_t val;
+                strValue >> val;
+                *(uint16_t *) data.data = cpu_to_le16(val);
+            } else if (string(dataType->name) == "uint32") {
+                uint32_t val;
+                strValue >> val;
+                *(uint32_t *) data.data = cpu_to_le32(val);
+            } else if (string(dataType->name) == "uint64") {
+                uint64_t val;
+                strValue >> val;
+                *(uint64_t *) data.data = cpu_to_le64(val);
+            } else if (string(dataType->name) == "string" ||
+                    string(dataType->name) == "octet_string") {
+                data.length = strValue.str().size();
+                if (!data.length) {
+                    err << "Zero-size string now allowed!";
+                    throwCommandException(err);
+                }
+                data.data = new uint8_t[data.length];
+                strValue >> (char *) data.data;
+            } else {
+                err << "Invalid data type " << dataType->name;
+                throwCommandException(err);
+            }
+        } catch (ios::failure &e) {
+            delete [] data.data;
+            err << "Invalid value argument '" << args[1]
+                << "' for type '" << dataType->name << "'!";
+            throwInvalidUsageException(err);
+        }
+    }
 
     if ((uint32_t) data.offset + data.length > 0xffff) {
         err << "Offset and length exceeding 64k!";