📄 epath_utils.c
字号:
/* Parse entity path into a string */ for (i--; i >= 0; i--) { guint num_digits, work_instance_num; /* Validate and convert data */ work_instance_num = epathptr->Entry[i].EntityLocation; for (num_digits = 1; (work_instance_num = work_instance_num/10) > 0; num_digits++); if (num_digits > OH_MAX_LOCATION_DIGITS) { dbg("Instance value too big"); rtncode = -1; goto CLEANUP; } memset(instance_str, 0, OH_MAX_LOCATION_DIGITS + 1); err = snprintf(instance_str, OH_MAX_LOCATION_DIGITS + 1, "%d", epathptr->Entry[i].EntityLocation); /* Find string for current entity type */ tidx = entitytype2index(epathptr->Entry[i].EntityType); if ( tidx >= 0 ) type_str = eshort_names[tidx]; else { /* String for entity type not found. */ err = snprintf(type_str_buffer, 20, "%d", epathptr->Entry[i].EntityType); type_str = type_str_buffer; } strcount = strcount + strlen(EPATHSTRING_START_DELIMITER) + strlen(type_str) + strlen(EPATHSTRING_VALUE_DELIMITER) + strlen(instance_str) + strlen(EPATHSTRING_END_DELIMITER); if (strcount > strsize - 1) { dbg("Not enough space allocated for string"); rtncode = -1; goto CLEANUP; } catstr = g_strconcat(EPATHSTRING_START_DELIMITER, type_str, EPATHSTRING_VALUE_DELIMITER, instance_str, EPATHSTRING_END_DELIMITER, NULL); strcat(tmpstr, catstr); g_free(catstr); } rtncode = strcount; /* Write string */ memset(epathstr, 0 , strsize); strcpy(epathstr, tmpstr); CLEANUP: g_free(instance_str); g_free(tmpstr); return(rtncode);} /* End entitypath2string *//** * ep_init * @ep: Pointer to SaHpiEntityPathT structure to initialize. * * Initializes the given entity path to all {ROOT,0} elements. * * Returns: void. **/ void ep_init(SaHpiEntityPathT *ep) { int i; if (!ep) return; for (i = 0; i < SAHPI_MAX_ENTITY_PATH; i++) { ep->Entry[i].EntityType = SAHPI_ENT_ROOT; ep->Entry[i].EntityLocation = 0; } }/** * ep_concat * @dest: IN,OUT Left-hand entity path. Gets appended with @append. * @append: IN Right-hand entity path. Pointer entity path to be appended. * * Concatenate two entity path structures (SaHpiEntityPathT). * @dest is assumed to be the least significant entity path in the operation. * append will be truncated into @dest, if it doesn't fit completely in the space * that @dest has available relative to SAHPI_MAX_ENTITY_PATH. * * Returns: 0 on Success, -1 if dest is NULL. **/int ep_concat(SaHpiEntityPathT *dest, const SaHpiEntityPathT *append){ unsigned int i, j; if (!dest) return -1; if (!append) return 0; for (i = 0; i < SAHPI_MAX_ENTITY_PATH; i++) { if (dest->Entry[i].EntityType == SAHPI_ENT_ROOT) { break; } } for (j = 0; i < SAHPI_MAX_ENTITY_PATH; i++) { dest->Entry[i].EntityLocation = append->Entry[j].EntityLocation; dest->Entry[i].EntityType = append->Entry[j].EntityType; if (append->Entry[j].EntityType == SAHPI_ENT_ROOT) break; j++; } return 0;}/** * validate_ep * @ep: Pointer to an SaHpiEntityPathT structure. * * This will check the entity path to make sure it does not contain * any invalid entity types in it up to the root element if it has it. * * Returns: 0 if entity path is valid, -1 otherwise. **/int validate_ep(const SaHpiEntityPathT *ep){ int check = 0; int i; for (i = 0; i < SAHPI_MAX_ENTITY_PATH; i++) { if ( entitytype2index(ep->Entry[i].EntityType) < 0 && ep->Entry[i].EntityType > 255) { check = -1; break; } else if (ep->Entry[i].EntityType == SAHPI_ENT_ROOT) break; } return check;}/** * set_ep_instance * @ep: Pointer to entity path to work on * @et: entity type to look for * @ei: entity instance to set when entity type is found * * Set an instance number in the entity path given at the first * position (from least significant to most) the specified entity type is found. * * Returns: 0 on Success, -1 if the entity type was not found. **/int set_ep_instance(SaHpiEntityPathT *ep, SaHpiEntityTypeT et, SaHpiEntityLocationT ei){ int i; int retval = -1; if (!ep) return retval; for (i = 0; i < SAHPI_MAX_ENTITY_PATH; i++) { if (ep->Entry[i].EntityType == et) { ep->Entry[i].EntityLocation = ei; retval = 0; break; } else if (ep->Entry[i].EntityType == SAHPI_ENT_ROOT) { break; } } return retval;}/** * ep_cmp * @ep1: Pointer to entity path struct * @ep2: Pointer to entity path struct * * Compare two entity paths up to their root element. * To be equal, they must have the same number of elements and each element * (type and instance pair) must be equal to the corresponding element * in the other entity path. * * Returns: 0 if equal, -1 if not. **/int ep_cmp(const SaHpiEntityPathT *ep1, const SaHpiEntityPathT *ep2){ unsigned int i, j; if ((!ep1) || (!ep2)) { dbg("ep_cmp error - null pointer\n"); return -1; } for ( i = 0; i < SAHPI_MAX_ENTITY_PATH; i++ ) { if (ep1->Entry[i].EntityType == SAHPI_ENT_ROOT) { i++; break; } } for ( j = 0; j < SAHPI_MAX_ENTITY_PATH; j++ ) { if (ep2->Entry[j].EntityType == SAHPI_ENT_ROOT) { j++; break; } } if ( i != j ) { /* dbg("ep1 element count %d != ep2 %d\n", i, j); */ return -1; } for ( i = 0; i < j; i++ ) { if (ep1->Entry[i].EntityType != ep2->Entry[i].EntityType || ep1->Entry[i].EntityLocation != ep2->Entry[i].EntityLocation) { /* dbg("Entity element %d: EP1 {%d,%d} != EP2 {%d,%d}", i, ep1->Entry[i].EntityType, ep1->Entry[i].EntityLocation, ep2->Entry[i].EntityType, ep2->Entry[i].EntityLocation); */ return -1; } } return 0;}/** * print_ep * @ep: Pointer to entity path stucture. * * Print the string form of an entity path structure. * * Returns: 0 on Success, -1 on Failure. **/int print_ep(const SaHpiEntityPathT *ep){ const int buffer_size = 512; gchar epstr[buffer_size]; int len; memset(epstr,0,buffer_size); if (!ep) { dbg("Error: null pointer \n"); return -1; } len = entitypath2string(ep, epstr, sizeof(epstr)); if (len < 0) return -1; /* Entity path with a sole root element will be an empty string */ printf("Entity Path=\"%s\"\n", epstr); return 0;}/********************************************************************** * oh_derive_string: * @ep - Pointer to entity's HPI SaHpiEntityPathT. * @str - Un-normalized character string. * * This function "normalizes" a string (such as an SNMP OID) * based on entity path. Starting from the end of @str, this routine * replaces the letter 'x', with the last instance number of entity path, * the process is repeated until all 'x' are replaced by an instance number. * For example, * * @str = ".1.3.6.1.4.1.2.3.x.2.22.1.5.1.1.5.x" * @ep = {SAHPI_ENT_CHASSIS, 51}{SAHPI_ENT_SBC_BLADE, 3} * * Returns a normalized string of ".1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.3". * * If @str does not contain any 'x' characters, this routine still * allocates memory and returns a "normalized" string. In this case, * the normalized string is identical to @str. * * Note! * Caller of this routine MUST g_free() the returned normalized string * when finished with it. * * Returns: * Pointer to normalize string - Normal case. * NULL - Error. **********************************************************************/gchar * oh_derive_string(SaHpiEntityPathT *ep, const gchar *str){ gchar *new_str = NULL, *str_walker = NULL; gchar **fragments = NULL, **str_nodes = NULL; guint num_epe, num_blanks, str_strlen = 0; guint total_num_digits, i, work_instance_num, num_digits; if (!ep || !str) return(NULL); for (num_epe = 0; ep->Entry[num_epe].EntityType != SAHPI_ENT_ROOT && num_epe < SAHPI_MAX_ENTITY_PATH; num_epe++); /* trace("Number of elements in entity path: %d", num_epe); */ if (num_epe == 0) { dbg("Entity Path is null."); return(NULL); } if ((str_strlen = strlen(str)) == 0) return(NULL); /* Str is zero length */ if (!strrchr(str, OH_DERIVE_BLANK_CHAR)) return(g_strdup(str)); /* Nothing to replace */ for (num_blanks=0, i=0; i<str_strlen; i++) { if (str[i] == OH_DERIVE_BLANK_CHAR) num_blanks++; } /* trace("Number of blanks in str: %d, %s", num_blanks, str); */ if (num_blanks > num_epe) { dbg("Number of replacments=%d > entity path elements=%d", num_blanks, num_epe); return(NULL); } fragments = g_strsplit(str, OH_DERIVE_BLANK_STR, - 1); if (!fragments) { dbg("Cannot split string"); goto CLEANUP; } str_nodes = g_malloc0((num_blanks + 1) * sizeof(gchar **)); if (!str_nodes) { dbg("Out of memory."); goto CLEANUP; } total_num_digits = 0; for (i=0; i<num_blanks; i++) { work_instance_num = ep->Entry[num_blanks-1-i].EntityLocation; for (num_digits = 1; (work_instance_num = work_instance_num/10) > 0; num_digits++); str_nodes[i] = g_malloc0((num_digits+1) * sizeof(gchar)); if (!str_nodes[i]) {dbg("Out of memory."); goto CLEANUP;} snprintf(str_nodes[i], (num_digits + 1) * sizeof(gchar), "%d", ep->Entry[num_blanks - 1 - i].EntityLocation); /* trace("Instance number: %s", str_nodes[i]); */ total_num_digits = total_num_digits + num_digits; } new_str = g_malloc0((str_strlen-num_blanks + total_num_digits + 1) * sizeof(gchar)); if (!new_str) { dbg("Out of memory."); goto CLEANUP; } str_walker = new_str; for (i=0; fragments[i]; i++) { str_walker = strcpy(str_walker, fragments[i]); str_walker = str_walker + strlen(fragments[i]); if (str_nodes[i]) { str_walker = strcpy(str_walker, str_nodes[i]); /* trace("Instance number: %s", str_nodes[i]); */ str_walker = str_walker + strlen(str_nodes[i]); } /* trace("New str: %s", new_str); */ }CLEANUP: g_strfreev(fragments); g_strfreev(str_nodes); return(new_str);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -