diff options
author | David 'Digit' Turner <digit@android.com> | 2011-02-01 17:48:37 +0100 |
---|---|---|
committer | David 'Digit' Turner <digit@android.com> | 2011-02-01 19:19:12 +0100 |
commit | 622f1530c8a6badfbcaf8c59976348678fbe248b (patch) | |
tree | 3847a94f8f82f0fd81ef69f3e04f02c72fee7ade /android/utils | |
parent | f4d4ca164dea606cee27e1f68d4742a3c57b0954 (diff) | |
download | external_qemu-622f1530c8a6badfbcaf8c59976348678fbe248b.zip external_qemu-622f1530c8a6badfbcaf8c59976348678fbe248b.tar.gz external_qemu-622f1530c8a6badfbcaf8c59976348678fbe248b.tar.bz2 |
Generate temporary hardware.ini from configuration settings.
This patch generates a temporary hardware.ini from the hardware
configuration. The idea is to move as much hw config info as possible
to a single file that the core can read.
Other patches will change how various config info is passed to the
core, from command-line options to the generated hardware.ini.
Diffstat (limited to 'android/utils')
-rw-r--r-- | android/utils/ini.c | 143 | ||||
-rw-r--r-- | android/utils/ini.h | 19 | ||||
-rw-r--r-- | android/utils/system.h | 13 |
3 files changed, 134 insertions, 41 deletions
diff --git a/android/utils/ini.c b/android/utils/ini.c index 317d233..1a1449c 100644 --- a/android/utils/ini.c +++ b/android/utils/ini.c @@ -62,8 +62,33 @@ iniFile_alloc( void ) } static void -iniFile_addPair( IniFile* i, const char* key, int keyLen, - const char* value, int valueLen ) +iniPair_init( IniPair* pair, const char* key, int keyLen, + const char* value, int valueLen ) +{ + AARRAY_NEW(pair->key, keyLen + valueLen + 2); + memcpy(pair->key, key, keyLen); + pair->key[keyLen] = 0; + + pair->value = pair->key + keyLen + 1; + memcpy(pair->value, value, valueLen); + pair->value[valueLen] = 0; +} + +static void +iniPair_replaceValue( IniPair* pair, const char* value ) +{ + char* key = pair->key; + int keyLen = strlen(key); + int valueLen = strlen(value); + + iniPair_init(pair, key, keyLen, value, valueLen); + AFREE(key); +} + +static void +iniFile_addPair( IniFile* i, + const char* key, int keyLen, + const char* value, int valueLen ) { IniPair* pair; @@ -76,53 +101,39 @@ iniFile_addPair( IniFile* i, const char* key, int keyLen, } pair = i->pairs + i->numPairs; - - AARRAY_NEW(pair->key, keyLen + valueLen + 2); - memcpy(pair->key, key, keyLen); - pair->key[keyLen] = 0; - - pair->value = pair->key + keyLen + 1; - memcpy(pair->value, value, valueLen); - pair->value[valueLen] = 0; + iniPair_init(pair, key, keyLen, value, valueLen); i->numPairs += 1; } -const char* -iniFile_getValue( IniFile* i, const char* key ) +static IniPair* +iniFile_getPair( IniFile* i, const char* key ) { if (i && key) { int nn; for (nn = 0; nn < i->numPairs; nn++) { if (!strcmp(i->pairs[nn].key,key)) - return i->pairs[nn].value; + return &i->pairs[nn]; } } return NULL; } -int -iniFile_getPairCount( IniFile* i ) +const char* +iniFile_getValue( IniFile* i, const char* key ) { - return i ? i->numPairs : 0; + IniPair* pair = iniFile_getPair(i, key); + if (pair) + return pair->value; + else + return NULL; } -void -iniFile_getPair( IniFile* i, - int index, - const char* *pKey, - const char* *pValue ) +int +iniFile_getPairCount( IniFile* i ) { - const char* key = NULL; - const char* value = NULL; - - if (i && index >= 0 && index < i->numPairs) { - key = i->pairs[index].key; - value = i->pairs[index].value; - } - *pKey = key; - *pValue = value; + return i ? i->numPairs : 0; } /* NOTE: we avoid using <ctype.h> functions to avoid locale-specific @@ -415,3 +426,75 @@ iniFile_getInt64( IniFile* f, const char* key, int64_t defaultValue ) return value; } +void +iniFile_setValue( IniFile* f, const char* key, const char* value ) +{ + IniPair* pair; + + if (f == NULL || key == NULL || value == NULL) + return; + + pair = iniFile_getPair(f, key); + if (pair != NULL) { + iniPair_replaceValue(pair, value); + } else { + iniFile_addPair(f, key, strlen(key), value, strlen(value)); + } +} + +void +iniFile_setInteger( IniFile* f, const char* key, int value ) +{ + char temp[16]; + snprintf(temp, sizeof temp, "%d", value); + iniFile_setValue(f, key, temp); +} + +void +iniFile_setInt64( IniFile* f, const char* key, int64_t value ) +{ + char temp[32]; + snprintf(temp, sizeof temp, "%" PRId64, value); + iniFile_setValue(f, key, temp); +} + +void +iniFile_setDouble( IniFile* f, const char* key, double value ) +{ + char temp[32]; + snprintf(temp, sizeof temp, "%g", value); + iniFile_setValue(f, key, temp); +} + +void +iniFile_setBoolean( IniFile* f, const char* key, int value ) +{ + iniFile_setValue(f, key, value ? "yes" : "no"); +} + +void +iniFile_setDiskSize( IniFile* f, const char* key, int64_t size ) +{ + char temp[32]; + int64_t divisor = 0; + char suffix = '\0'; + + if (size >= 0) { + if (!(size % 1024)) { + suffix = 'k'; + divisor = 1024; + } else if (!(size % 1024*1024)) { + divisor = 1024*1024; + suffix = 'm'; + } else if (!(size % 1024*1024*1024LL)) { + divisor = 1024*1024*1024; + suffix = 'g'; + } + } + if (divisor) { + snprintf(temp, sizeof temp, "%" PRId64 "%c", size/divisor, suffix); + } else { + snprintf(temp, sizeof temp, "%" PRId64, size); + } + iniFile_setValue(f, key, temp); +} diff --git a/android/utils/ini.h b/android/utils/ini.h index 83d2027..77d760f 100644 --- a/android/utils/ini.h +++ b/android/utils/ini.h @@ -43,17 +43,6 @@ void iniFile_free( IniFile* f ); /* returns the number of (key.value) pairs in an IniFile */ int iniFile_getPairCount( IniFile* f ); -/* return a specific (key,value) pair from an IniFile. - * if the index is not correct, both '*pKey' and '*pValue' will be - * set to NULL. - * - * you should probably use iniFile_getValue() and its variants instead - */ -void iniFile_getPair( IniFile* f, - int index, - const char* *pKey, - const char* *pValue ); - /* returns the value of a given key from an IniFile. * NULL if the key is not assigned in the corresponding configuration file */ @@ -96,6 +85,14 @@ int iniFile_getBoolean( IniFile* f, const char* key, const char* defaultV */ int64_t iniFile_getDiskSize( IniFile* f, const char* key, const char* defaultValue ); +/* These functions are used to set values in an IniFile */ +void iniFile_setValue( IniFile* f, const char* key, const char* value ); +void iniFile_setInteger( IniFile* f, const char* key, int value ); +void iniFile_setInt64( IniFile* f, const char* key, int64_t value ); +void iniFile_setDouble( IniFile* f, const char* key, double value ); +void iniFile_setBoolean( IniFile* f, const char* key, int value ); +void iniFile_setDiskSize( IniFile* f, const char* key, int64_t size ); + /* */ #endif /* _ANDROID_UTILS_INI_H */ diff --git a/android/utils/system.h b/android/utils/system.h index 5053786..c8163c6 100644 --- a/android/utils/system.h +++ b/android/utils/system.h @@ -14,6 +14,7 @@ #include <string.h> #include <stdint.h> +#include <inttypes.h> /* for PRId64 et al. */ #include "android/utils/assert.h" /* internal helpers */ @@ -161,6 +162,18 @@ extern void restore_sigalrm( signal_state_t *state ); extern void sleep_ms( int timeout ); +/** FORMATTING int64_t in printf() statements + ** + ** Normally defined in <inttypes.h> except on Windows and maybe others. + **/ + +#ifndef PRId64 +# define PRId64 "lld" +#endif +#ifndef PRIx64 +# define PRIx64 "llx" +#endif + /* */ #endif /* _ANDROID_UTILS_SYSTEM_H */ |