midp_properties_dynamic.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 520 行 · 第 1/2 页
C
520 行
pcsl_string pathname; int fd = -1 ; char * errStr; /* Property file can be relative or at midp_home variable */ pcsl_string_cat(configRoot, name, &pathname); fd = storage_open(&errStr, &pathname, OPEN_READ); pcsl_string_free(&pathname); if (errStr != NULL) { REPORT_WARN2(LC_CORE, "Warning: could not open config file(%s): %s\n", pathname, errStr); storageFreeError(errStr); return 0; } /* Read through the file one line at a time */ if (parseConfig(fd, props) != 0) { return -1; } /* Close the storage handle */ storageClose(&errStr, fd); return 0;}/** * Finalizes a property set. * * @param props The property set to finalize */static voidfinalizeProps(Property* props) { Property *prop, *tmp; if (props != NULL) { for (prop = props ; prop ; prop = tmp ){ tmp = prop->next; midpFree((void*)prop->key); midpFree((void*)prop->value); midpFree(prop); } }}/** * Sets a property key to the specified value. * * @param props The property set in which to add/modify <tt>key</tt> * @param key The key to set * @param value The value to set <tt>key</tt> to */static voidsetProp(Property** props, const char* key , const char* value) { /* Try to find the property in the current pool. */ Property *p; for (p= *props; p; p = p->next) { if (strcmp(key, p->key) == 0) { midpFree((void*)p->value); p->value = midpStrdup(value); /* * if midpStrdup fails we will just return without setting * the value to anything other than NULL */ return; } } /* If the value is not defined, add it now */ p = (Property*)midpMalloc(sizeof(Property)); if (NULL != p){ p->next = *props ; p->key = midpStrdup(key); p->value = midpStrdup(value); if ((p->key == NULL) || (p->value == NULL)) { /* do nothing if there is no memory */ midpFree((void*)p->key); midpFree((void*)p->value); midpFree(p); return; } *props = p ; }}/** * Finds a property key and returns its value. * * @param prop The property set to search * @param key The key to search for * * @return The value associated with <tt>key</tt> if found, otherwise * <tt>NULL</tt> */static const char*findProp(Property* prop, const char* key) { Property* p; p = prop; while (p) { if (strcmp(key, p->key) == 0) { return (p->value); } p = p->next; } return (NULL);}/** * Initializes the configuration sub-system. * * @return <tt>0</tt> for success, otherwise a non-zero value */intinitializeConfig(void) { if (implementationProperties != NULL) { /* Already initialized. */ return 0; } if (initProps(&implementationProperties, &IMPL_PROPERTY_FILE, storage_get_config_root(INTERNAL_STORAGE_ID)) != 0) { return -1; } if (initProps(&applicationProperties, &APPL_PROPERTY_FILE, storage_get_config_root(INTERNAL_STORAGE_ID)) != 0) { finalizeConfig(); return -1; } /* * Make sure the configuration was specified, because * some older code requires it in the CLDC classes. */ if (getSystemProperty(DEFAULT_CONFIGURATION) == NULL) { setSystemProperty(DEFAULT_CONFIGURATION, DEFAULT_CLDC); } if (getSystemProperty(PROFILES_PROP_NAME) == NULL) { setSystemProperty(PROFILES_PROP_NAME, DEFAULT_PROFILE); } if (getSystemProperty(ENCODING_PROP_NAME) == NULL) { setSystemProperty(ENCODING_PROP_NAME, DEFAULT_CHARACTER_ENCODING); } return 0;}/** * Finalize the configuration subsystem by releasing all the * allocating memory buffers. This method should only be called by * midpFinalize or internally by initializeConfig. */voidfinalizeConfig(void) { finalizeProps(implementationProperties); implementationProperties = NULL; finalizeProps(applicationProperties); applicationProperties = NULL;}/** * Sets a property key to the specified value in the internal * property set. * * @param key The key to set * @param value The value to set <tt>key</tt> to */voidsetInternalProperty(const char* key , const char* value) { setProp(&implementationProperties, key, value);}/** * Gets the value of the specified property key in the internal * property set. If the key is not found in the internal property * set, the application property set is then searched. * * @param key The key to search for * * @return The value associated with <tt>key</tt> if found, otherwise * <tt>NULL</tt> */const char*getInternalProperty(const char* key) { const char *result; result = findProp(implementationProperties, key); if (NULL == result) { result = findProp(applicationProperties, key); } return result;}/** * Gets the value of the specified property key in the internal * property set. If the key is not found in the internal property * set, the application property set is then searched. If neither * search finds the specified key, a default value is returned. * * @param key The key to search for * @param def The default value to return if <tt>key</tt> is not found. * * @return The value associated with <tt>key</tt> if found, otherwise * <tt>def</tt> */const char*getInternalPropertyDefault(const char* key, const char* def) { const char *result; result = findProp(implementationProperties, key); if (NULL == result) { result = findProp(applicationProperties, key); } return (NULL == result) ? def : result;}/** * Sets a property key to the specified value in the application * property set. * * @param key The key to set * @param value The value to set <tt>key</tt> to */voidsetSystemProperty(const char* key , const char* value) { setProp(&applicationProperties, key, value);}/** * Gets the value of the specified property key in the application * property set. * * @param key The key to search for * * @return The value associated with <tt>key</tt> if found, otherwise * <tt>NULL</tt> */const char*getSystemProperty(const char* key) { const char *result; result = findProp(applicationProperties, key); if ((NULL == result) && (strcmp(key, "microedition.hostname") == 0)) { /* Get the local hostname from the native networking subsystem */ result = getLocalHostName(); } return result;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?