1

I have been trying to find a way to use macro define strings as their values during run time. My test scenario is like this

In user-defined file 'data.txt'

Reg1 0x12345678

In test.c

extern write(uint64_t addr, uint32_t value);

main(){
  FILE * reg_data;
  char reg_name[256];
  uint32_t value;

  reg_data = fopen("data.txt", "r");
  if(reg_data == NULL){
    print_log("Cannot open file data.txt\n");
    exit(-1);
  }
  while(fscanf(reg_data, "%s %x \n", reg_name, value) != EOF){
    write((uint64_t)reg_name, value);
  }     
}

In another included file 'head.h'

#define reg1 0x400000000

So I want my write call to translate to write(0x400000000, 0x12345678) However since these #define are replaced at compile time it is not taking effect. Also since I have a typecast on string it passes compilation and elaboration substituting int equivalent of the string.

Is there any way to accomplish this? The main requirement is user generated file has define macro strings and not actual address values.

5
  • your main() signature is wrong, it can be int main(), but not just main() without any return type. Commented Feb 25, 2015 at 0:32
  • I have a typecast on string it passes compilation and elaboration substituting int equivalent of the string What? Commented Feb 25, 2015 at 0:38
  • use strtol() and pass base 16 to it. And also, write() is a standard function and it takes 2 int's Commented Feb 25, 2015 at 0:39
  • I can't guess what you really need, because what you are asking for is time travel. Whatever you get from a file is at runtime, long after any compile-time things like #define have vanished. What is it you are really trying to do? Process the file in some way, producing output in some form? Please try to be specific, and try not to talk about language or details--just what you want to do. Commented Feb 25, 2015 at 0:58
  • Reg1 doesn't equal reg1. Commented Aug 21, 2015 at 10:45

1 Answer 1

1

What you need is a lookup table that has the name of the register and the corresponding integer value. Here's a sample lookup table that has entries for two registers

#define reg1 0x40000000
#define reg2 0x40000010

typedef struct
{
    const char *name;
    uint64_t value;
}
    stLookup;

static stLookup lookup[] =
{
    {  "reg1", reg1  },
    {  "reg2", reg2  },
    {   NULL , 0     }
};

Then you need a function that takes a string, finds the corresponding entry in the table, and returns the value. The sample function below will return the value, or 0 if the name is not found in the table.

uint64_t addrForName( const char *name )
{
    stLookup *lptr;

    for ( lptr = lookup; lptr->name != NULL; lptr++ )
        if ( strcmp( lptr->name, name ) == 0 )
            break;

    return( lptr->value );
}

You can call the function like this

uint64_t addr = addrForName( reg_name );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.