📄 mpeg4ip_config_set.h
字号:
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is MPEG4IP. * * The Initial Developer of the Original Code is Cisco Systems Inc. * Portions created by Cisco Systems Inc. are * Copyright (C) Cisco Systems Inc. 2000, 2001. All Rights Reserved. * * Contributor(s): * Dave Mackie dmackie@cisco.com * Bill May wmay@cisco.com */#ifndef __CONFIG_SET_H__#define __CONFIG_SET_H__#include <mpeg4ip.h>#ifndef CONFIG_SAFETY#define CONFIG_SAFETY 1#endiftypedef u_int32_t config_integer_t;typedef u_int32_t config_index_t;enum ConfigException { CONFIG_ERR_INAME, CONFIG_ERR_TYPE, CONFIG_ERR_MEMORY,};// TBD type specific exception info and printing utilityclass CConfigException {public: CConfigException(ConfigException e) { type = e; } ConfigException type;};#define CONFIG_MAX_STRLEN 255// TBD weld this in, and throw exceptiontypedef enum ConfigType { CONFIG_TYPE_UNDEFINED, CONFIG_TYPE_INTEGER, CONFIG_TYPE_BOOL, CONFIG_TYPE_STRING, CONFIG_TYPE_FLOAT} ConfigType;union UConfigValue { UConfigValue(void) { m_svalue = NULL; } UConfigValue(config_integer_t ivalue) { m_ivalue = ivalue; } UConfigValue(bool bvalue) { m_bvalue = bvalue; } UConfigValue(const char *svalue) { m_svalue = svalue; } UConfigValue(char* svalue) { m_svalue = svalue; } UConfigValue(float fvalue) { m_fvalue = fvalue; } config_integer_t m_ivalue; bool m_bvalue; const char* m_svalue; float m_fvalue;};#ifdef _WIN32#define SConfigVariableDeclare class#else#define SConfigVariableDeclare struct#endifSConfigVariableDeclare SConfigVariable {#ifdef _WIN32 public: SConfigVariable(config_index_t *iName, const char* sName, ConfigType type, UConfigValue defaultValue, const char *help = NULL, UConfigValue value = (config_integer_t)0) { m_iName = iName; m_sName = sName; m_type = type; m_defaultValue = defaultValue; m_helpString = help; m_value = value; };#endif config_index_t *m_iName; const char* m_sName; ConfigType m_type; UConfigValue m_defaultValue; const char* m_helpString; UConfigValue m_value; const char* ToAscii() { static char sBuf[CONFIG_MAX_STRLEN+3]; switch (m_type) { case CONFIG_TYPE_INTEGER: sprintf(sBuf, "%d", m_value.m_ivalue); return sBuf; case CONFIG_TYPE_BOOL: sprintf(sBuf, "%d", m_value.m_bvalue); return sBuf; case CONFIG_TYPE_STRING: if(m_value.m_svalue == NULL) { sprintf(sBuf, "\"\""); return sBuf; } if (strchr(m_value.m_svalue, ' ')) { sBuf[0] = '"'; strncpy(&sBuf[1], m_value.m_svalue, CONFIG_MAX_STRLEN); strcpy(&sBuf[ MIN(strlen(m_value.m_svalue), CONFIG_MAX_STRLEN)+1], "\""); } return m_value.m_svalue; case CONFIG_TYPE_FLOAT: sprintf(sBuf, "%f", m_value.m_fvalue); return sBuf; default: return ""; } } bool FromAscii(const char* s) { switch (m_type) { case CONFIG_TYPE_INTEGER: return (sscanf(s, " %i ", &m_value.m_ivalue) == 1); case CONFIG_TYPE_BOOL: // OPTION could add "yes/no", "true/false" if (sscanf(s, " %u ", &m_value.m_ivalue) != 1) { return false; } m_value.m_bvalue = m_value.m_ivalue ? true : false; return true; case CONFIG_TYPE_STRING: // N.B. assuming m_svalue has been alloc'ed { size_t len = strlen(s); CHECK_AND_FREE(m_value.m_svalue); if (*s == '"' && s[len] == '"') { char *newvalue = strdup(s + 1); newvalue[len - 1] = '\0'; m_value.m_svalue = newvalue; } else { m_value.m_svalue = strdup(s); } if (m_value.m_svalue == NULL) { throw new CConfigException(CONFIG_ERR_MEMORY); } return true; } case CONFIG_TYPE_FLOAT: return (sscanf(s, " %f ", &m_value.m_fvalue) == 1); default: return false; } } void SetToDefault(void) { switch (m_type) { case CONFIG_TYPE_INTEGER: m_value.m_ivalue = m_defaultValue.m_ivalue; break; case CONFIG_TYPE_BOOL: m_value.m_bvalue = m_defaultValue.m_bvalue; break; case CONFIG_TYPE_STRING: CHECK_AND_FREE(m_value.m_svalue); if (m_defaultValue.m_svalue == NULL) { m_value.m_svalue = NULL; } else { m_value.m_svalue = strdup(m_defaultValue.m_svalue); if (m_value.m_svalue == NULL) { throw new CConfigException(CONFIG_ERR_MEMORY); } } break; case CONFIG_TYPE_FLOAT: m_value.m_fvalue = m_defaultValue.m_fvalue; break; default: break; } } bool IsValueDefault(void) { switch (m_type) { case CONFIG_TYPE_INTEGER: return m_value.m_ivalue == m_defaultValue.m_ivalue; case CONFIG_TYPE_BOOL: return m_value.m_bvalue == m_defaultValue.m_bvalue; case CONFIG_TYPE_STRING: if (m_defaultValue.m_svalue == NULL && m_value.m_svalue == NULL) return true; if (m_defaultValue.m_svalue == NULL) return false; if (m_value.m_svalue == NULL) return false; return (strcmp(m_value.m_svalue, m_defaultValue.m_svalue) == 0); case CONFIG_TYPE_FLOAT: return m_value.m_fvalue == m_defaultValue.m_fvalue; default: return false; } } void CleanUpConfig(void) { if (m_type == CONFIG_TYPE_STRING) { CHECK_AND_FREE(m_value.m_svalue); } }};struct SUnknownConfigVariable { struct SUnknownConfigVariable *next; SConfigVariable *value;};class CConfigSet {public: CConfigSet(const SConfigVariable* variables, config_index_t numVariables, const char* defaultFileName) { uint32_t size; m_debug = false; m_numVariables = numVariables; size = sizeof(SConfigVariable) * numVariables; m_variables = (SConfigVariable*)malloc(size); memcpy(m_variables, variables, size); m_fileName = defaultFileName == NULL ? NULL : strdup(defaultFileName); SetToDefaults(); m_unknown_head = NULL; }; ~CConfigSet() { CHECK_AND_FREE(m_fileName); for (config_index_t i = 0; i < m_numVariables; i++) { m_variables[i].CleanUpConfig(); } free(m_variables); m_variables = NULL; SUnknownConfigVariable *ptr = m_unknown_head; while (ptr != NULL) { m_unknown_head = ptr->next; CHECK_AND_FREE(ptr->value->m_sName); CHECK_AND_FREE(ptr->value->m_value.m_svalue); free(ptr->value); free(ptr); ptr = m_unknown_head; } } void InitializeIndexes(void) { for (config_index_t ix = 0; ix < m_numVariables; ix++) { *m_variables[ix].m_iName = ix; } } void AddConfigVariables (const SConfigVariable* vars, config_index_t numVariables) { config_index_t start = m_numVariables; uint32_t size = sizeof(SConfigVariable) * (m_numVariables + numVariables); m_variables = (SConfigVariable*)realloc(m_variables, size); memcpy(&m_variables[m_numVariables], vars, numVariables * sizeof(SConfigVariable)); m_numVariables += numVariables; SetToDefaults(start); } const char* GetFileName() { return m_fileName; } void SetFileName(const char *name) { CHECK_AND_FREE(m_fileName); m_fileName = strdup(name); }; inline void CheckIName(config_index_t iName) { if (iName >= m_numVariables) { fprintf(stderr, "config variable index failure - try %u max %u\n", iName, m_numVariables); fflush(stderr); throw new CConfigException(CONFIG_ERR_INAME); } if (*m_variables[iName].m_iName != iName) { fprintf(stderr, "config variable index mismatch %u should %u\n", iName, *m_variables[iName].m_iName); fflush(stderr); throw new CConfigException(CONFIG_ERR_INAME); } } inline void CheckIntegerType(config_index_t iName) { if (m_variables[iName].m_type != CONFIG_TYPE_INTEGER) { fprintf(stderr, "config type mismatch on %s - shouldn't be integer\n", m_variables[iName].m_sName); fflush(stderr); throw new CConfigException(CONFIG_ERR_TYPE); } } inline void CheckBoolType(config_index_t iName) { if (m_variables[iName].m_type != CONFIG_TYPE_BOOL) { fprintf(stderr, "config type mismatch on %s - shouldn't be bool\n", m_variables[iName].m_sName); fflush(stderr); throw new CConfigException(CONFIG_ERR_TYPE); } } inline void CheckStringType(config_index_t iName) { if (m_variables[iName].m_type != CONFIG_TYPE_STRING) { fprintf(stderr, "config type mismatch on %s - shouldn't be string\n", m_variables[iName].m_sName); fflush(stderr); throw new CConfigException(CONFIG_ERR_TYPE); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -