From c71075ca84d896c650dd0b65c7664149ed51dcec Mon Sep 17 00:00:00 2001 From: "nicolas.zilio@hotmail.fr" <> Date: Tue, 15 Sep 2015 11:36:20 +0200 Subject: addition of other remarks, cleaning in progress --- pcilib/unit.h | 4 +- pcilib/views.c | 233 ++++++++++++++++++++------------------------------------- pcilib/xml.c | 3 +- 3 files changed, 85 insertions(+), 155 deletions(-) (limited to 'pcilib') diff --git a/pcilib/unit.h b/pcilib/unit.h index f9991f1..4a99f5b 100644 --- a/pcilib/unit.h +++ b/pcilib/unit.h @@ -3,6 +3,8 @@ #include "pcilib.h" +#define PCILIB_MAX_TRANSFORMS_PER_UNIT 16 + typedef struct pcilib_unit_s pcilib_unit_t; typedef struct pcilib_transform_unit_s pcilib_transform_unit_t; @@ -19,7 +21,7 @@ struct pcilib_transform_unit_s{ */ struct pcilib_unit_s{ char* name; - pcilib_transform_unit_t* other_units; + pcilib_transform_unit_t other_units[PCILIB_MAX_TRANSFORMS_PER_UNIT]; }; /** diff --git a/pcilib/views.c b/pcilib/views.c index 2a0969c..86675f5 100644 --- a/pcilib/views.c +++ b/pcilib/views.c @@ -8,73 +8,6 @@ #include #include "unit.h" -/** - * - * replace a substring within a string by another - * @param[in] txt - the string to be modified - *@param[in] before - the substring in the string that will be replaced - *@param[in] after - the new value of before substring - *@return the modified txt string - */ -static char* -pcilib_view_formula_replace (const char *txt, const char *before, const char *after) -{ - const char *pos; - char *return_txt; - size_t pos_return_txt; - size_t len; - size_t allocated_size; - - /*get the first occurence of before. then we need one time to be out of the loop to correctly set the diverses varaibles (malloc instead of realloc notably)*/ - pos = strstr (txt, before); - - if (pos == NULL) - { - pcilib_warning("problem with a formula"); - } - - /* get the position of this occurence*/ - len = (size_t)pos - (size_t)txt; - allocated_size = len + strlen (after) + 1; - return_txt = malloc (allocated_size); - pos_return_txt = 0; - - /* we copy there the in a newly allocated string the start of txt before the "before" occurence, and then we copy after instead*/ - strncpy (return_txt + pos_return_txt, txt, len); - pos_return_txt += len; - txt = pos + strlen (before); - - len = strlen (after); - strncpy (return_txt + pos_return_txt, after, len); - pos_return_txt += len; - - /* we then iterate with the same principle to all occurences of before*/ - pos = strstr (txt, before); - while (pos != NULL) - { - len = (size_t)pos - (size_t)txt; - allocated_size += len + strlen (after); - return_txt = (char *)realloc (return_txt, allocated_size); - - strncpy (return_txt + pos_return_txt, txt, len); - pos_return_txt += len; - - txt = pos + strlen (before); - - len = strlen (after); - strncpy (return_txt + pos_return_txt, after, len); - pos_return_txt += len; - pos = strstr (txt, before); - } - /* put the rest of txt string at the end*/ - len = strlen (txt) + 1; - allocated_size += len; - return_txt = realloc (return_txt, allocated_size); - strncpy (return_txt + pos_return_txt, txt, len); - - return return_txt; -} - /** * function used to get the substring of a string s, from the starting and ending indexes * @param[in] s string containing the substring we want to extract. @@ -110,62 +43,6 @@ pcilib_view_str_sub (const char *s, unsigned int start, unsigned int end) } -/** - * get the bank name associated with a register name - */ -static const char* -pcilib_view_get_bank_from_reg_name(pcilib_t* ctx,char* reg_name){ - int k; - for(k=0;ctx->registers[k].bits;k++){ - if(!(strcasecmp(reg_name,ctx->registers[k].name))){ - return ctx->banks[pcilib_find_register_bank_by_addr(ctx,ctx->registers[k].bank)].name; - } - } - return NULL; -} - -/** - * replace plain registers name in a formula by their value - */ -static char* -pcilib_view_compute_plain_registers(pcilib_t* ctx, char* formula, int direction){ - int j,k; - char *substr, *substr2; - char temp[66]; - pcilib_register_value_t value; - - /*we get recursively all registers of string , and if they are not equel to '@reg', then we get their value and put it in formula*/ - for(j=0;jbase_unit.name))){ @@ -362,8 +291,8 @@ int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pci return PCILIB_ERROR_MEMORY; } strncpy(formula,((pcilib_formula_t*)params)->read_formula,strlen(((pcilib_formula_t*)params)->read_formula)); - pcilib_view_apply_formula(ctx,formula,*regval,&value,0); - return value; + pcilib_view_apply_formula(ctx,formula,regval); + return 0; } for(j=0; ((pcilib_view_t*)viewval)->base_unit.other_units[j].name;j++){ @@ -375,16 +304,16 @@ int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pci return PCILIB_ERROR_MEMORY; } strncpy(formula,((pcilib_formula_t*)params)->read_formula,strlen(((pcilib_formula_t*)params)->read_formula)); - pcilib_view_apply_formula(ctx,formula, *regval,&value,0); + pcilib_view_apply_formula(ctx,formula, regval); pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.other_units[j],unit,&value); - return value; + return 0; } } }else if(view2reg==1){ if(!(strcasecmp(unit, ((pcilib_view_t*)viewval)->base_unit.name))){ formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->write_formula)); strncpy(formula,((pcilib_formula_t*)params)->write_formula,strlen(((pcilib_formula_t*)params)->write_formula)); - pcilib_view_apply_formula(ctx,formula,*regval,&value,1); + pcilib_view_apply_formula(ctx,formula,regval); return 0; } @@ -394,13 +323,13 @@ int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pci formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->write_formula)); strncpy(formula,((pcilib_formula_t*)params)->write_formula,strlen((( pcilib_formula_t*)params)->write_formula)); pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.other_units[j],unit,&value); - pcilib_view_apply_formula(ctx,formula,*regval,&value,1); - *regval=value; + pcilib_view_apply_formula(ctx,formula,regval); /* we maybe need some error checking there , like temp_value >min and properties; value=(char*)attr->children->content; desc.name=value; - desc.other_units=malloc(sizeof(pcilib_transform_unit_t)); for (cur = node->children; cur != NULL; cur = cur->next) { if (!cur->children) continue; @@ -520,7 +520,6 @@ pcilib_xml_create_unit(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, x if (!value || !attr) continue; if (!strcasecmp(name, "convert_unit")) { - desc.other_units=realloc(desc.other_units,(i+1)*sizeof(pcilib_transform_unit_t)); desc.other_units[i].name=value2; desc.other_units[i].transform_formula=value; i++; -- cgit v1.2.3