aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils
diff options
context:
space:
mode:
authorVladimir Chtchetkine <vchtchetkine@google.com>2012-01-23 12:43:30 -0800
committerVladimir Chtchetkine <vchtchetkine@google.com>2012-01-23 12:43:30 -0800
commit2ae501556f90647e40466b6a1948f6f7f51cf251 (patch)
treef662fec2f4852da7d8bdf5dc682fe62a288bc668 /android/utils
parentac7c3b18f8d3c472badd3f588565569eae53f2da (diff)
downloadexternal_qemu-2ae501556f90647e40466b6a1948f6f7f51cf251.zip
external_qemu-2ae501556f90647e40466b6a1948f6f7f51cf251.tar.gz
external_qemu-2ae501556f90647e40466b6a1948f6f7f51cf251.tar.bz2
Refactor camera parameters parsing routines
Move routines that pulls values out of the parameter string from camera-service.c to misc.c Those are general purpose routines that are also used in multitouch emulation, so it's better to keep it an an general utility file. Change-Id: I02978075b64b42ff07f5042cda770bbef8939e24
Diffstat (limited to 'android/utils')
-rw-r--r--android/utils/misc.c120
-rw-r--r--android/utils/misc.h57
2 files changed, 177 insertions, 0 deletions
diff --git a/android/utils/misc.c b/android/utils/misc.c
index 818ab78..80dc9a4 100644
--- a/android/utils/misc.c
+++ b/android/utils/misc.c
@@ -10,6 +10,7 @@
** GNU General Public License for more details.
*/
+#include "qemu-common.h"
#include "android/utils/misc.h"
#include "android/utils/stralloc.h"
#include "android/utils/debug.h"
@@ -17,6 +18,8 @@
#include <stdio.h>
#include <stdlib.h>
+#define E(...) derror(__VA_ARGS__)
+
extern void
print_tabular( const char** strings, int count,
const char* prefix, int width )
@@ -191,3 +194,120 @@ int2hex( uint8_t* hex, int len, int val )
while ( --len >= 0 )
*hex++ = hexchars[(val >> (len*4)) & 15];
}
+
+/** STRING PARAMETER PARSING
+ **/
+
+int
+strtoi(const char *nptr, char **endptr, int base)
+{
+ long val;
+
+ errno = 0;
+ val = strtol(nptr, endptr, base);
+ if (errno) {
+ return (val == LONG_MAX) ? INT_MAX : INT_MIN;
+ } else {
+ if (val == (int)val) {
+ return (int)val;
+ } else {
+ errno = ERANGE;
+ return val > 0 ? INT_MAX : INT_MIN;
+ }
+ }
+}
+
+int
+get_token_value(const char* params, const char* name, char* value, int val_size)
+{
+ const char* val_end;
+ int len = strlen(name);
+ const char* par_end = params + strlen(params);
+ const char* par_start = strstr(params, name);
+
+ /* Search for 'name=' */
+ while (par_start != NULL) {
+ /* Make sure that we're within the parameters buffer. */
+ if ((par_end - par_start) < len) {
+ par_start = NULL;
+ break;
+ }
+ /* Make sure that par_start starts at the beginning of <name>, and only
+ * then check for '=' value separator. */
+ if ((par_start == params || (*(par_start - 1) == ' ')) &&
+ par_start[len] == '=') {
+ break;
+ }
+ /* False positive. Move on... */
+ par_start = strstr(par_start + 1, name);
+ }
+ if (par_start == NULL) {
+ return -1;
+ }
+
+ /* Advance past 'name=', and calculate value's string length. */
+ par_start += len + 1;
+ val_end = strchr(par_start, ' ');
+ if (val_end == NULL) {
+ val_end = par_start + strlen(par_start);
+ }
+ len = val_end - par_start;
+
+ /* Check if fits... */
+ if ((len + 1) <= val_size) {
+ memcpy(value, par_start, len);
+ value[len] = '\0';
+ return 0;
+ } else {
+ return len + 1;
+ }
+}
+
+int
+get_token_value_alloc(const char* params, const char* name, char** value)
+{
+ char tmp;
+ int res;
+
+ /* Calculate size of string buffer required for the value. */
+ const int val_size = get_token_value(params, name, &tmp, 0);
+ if (val_size < 0) {
+ *value = NULL;
+ return val_size;
+ }
+
+ /* Allocate string buffer, and retrieve the value. */
+ *value = (char*)malloc(val_size);
+ if (*value == NULL) {
+ E("%s: Unable to allocated %d bytes for string buffer.",
+ __FUNCTION__, val_size);
+ return -2;
+ }
+ res = get_token_value(params, name, *value, val_size);
+ if (res) {
+ E("%s: Unable to retrieve value into allocated buffer.", __FUNCTION__);
+ free(*value);
+ *value = NULL;
+ }
+
+ return res;
+}
+
+int
+get_token_value_int(const char* params, const char* name, int* value)
+{
+ char val_str[64]; // Should be enough for all numeric values.
+ if (!get_token_value(params, name, val_str, sizeof(val_str))) {
+ errno = 0;
+ *value = strtoi(val_str, (char**)NULL, 10);
+ if (errno) {
+ E("%s: Value '%s' of the parameter '%s' in '%s' is not a decimal number.",
+ __FUNCTION__, val_str, name, params);
+ return -2;
+ } else {
+ return 0;
+ }
+ } else {
+ return -1;
+ }
+}
diff --git a/android/utils/misc.h b/android/utils/misc.h
index 0db1e28..0d943f7 100644
--- a/android/utils/misc.h
+++ b/android/utils/misc.h
@@ -64,4 +64,61 @@ extern int hex2int( const uint8_t* hex, int len );
/* encodes an integer 'val' into 'len' hexadecimal charaters into 'hex' */
extern void int2hex( uint8_t* hex, int len, int val );
+/** STRING PARAMETER PARSING
+ **/
+
+/* A strict 'int' version of the 'strtol'.
+ * This routine is implemented on top of the standard 'strtol' for 32/64 bit
+ * portability.
+ */
+extern int strtoi(const char *nptr, char **endptr, int base);
+
+/* Gets a parameter value out of the parameter string.
+ * Parameter format for this routine is as such:
+ * "<name1>=<value1> <name2>=<value2> ... <nameN>=<valueN>"
+ * I.e.:
+ * - Every parameter must have a name, and a value.
+ * - Name and value must be separated with '='.
+ * - No spaces are allowed around '=' separating name and value.
+ * - Parameters must be separated with a single ' ' character.
+ * - No '=' character is allowed in name and in value.
+ * Param:
+ * params - String, containing the parameters.
+ * name - Parameter name.
+ * value - Upon success contains value for the given parameter.
+ * val_size - Size of the 'value' string buffer.
+ * Return:
+ * 0 on success, -1 if requested parameter is not found, or (a positive) number
+ * of bytes, required to make a copy of the parameter's value if 'value' string
+ * was too small to contain it.
+ */
+extern int get_token_value(const char* params, const char* name, char* value, int val_size);
+
+/* Gets a parameter value out of the parameter string.
+ * This routine is similar to get_token_value, except it will always allocate
+ * a string buffer for the value.
+ * Param:
+ * params - String, containing the parameters.
+ * name - Parameter name.
+ * value - Upon success contains an allocated string containint the value for
+ * the given parameter. The caller is responsible for freeing the buffer
+ * returned in this parameter on success.
+ * Return:
+ * 0 on success, -1 if requested parameter is not found, or -2 on
+ * memory failure.
+ */
+extern int get_token_value_alloc(const char* params, const char* name, char** value);
+
+/* Gets an integer parameter value out of the parameter string.
+ * Param:
+ * params - String, containing the parameters. See comments to get_token_value
+ * routine on the parameters format.
+ * name - Parameter name. Parameter value must be a decimal number.
+ * value - Upon success contains integer value for the given parameter.
+ * Return:
+ * 0 on success, or -1 if requested parameter is not found, or -2 if parameter's
+ * format was bad (i.e. value was not a decimal number).
+ */
+extern int get_token_value_int(const char* params, const char* name, int* value);
+
#endif /* _ANDROID_UTILS_MISC_H */