简体   繁体   中英

Reading array size from file

I have a program which should adjust number of elements to number of devices it is working with. I have a config *.txt file that contains some parameters that allows users who don't know programming language to adjust program to their needs.

For example till now everything have been handled like this. In header file:

enum 
{
    // number of input and output channels
    kMaxInputChannels = 8,
    kMaxOutputChannels = 8
};

typedef struct AudioDriverSettings
{
   (...)
   ASIOBufferInfo bufferInfos[kMaxInputChannels + kMaxOutputChannels]; 
   ASIOChannelInfo channelInfos[kMaxInputChannels + kMaxOutputChannels];
   (...)

} AudioDriverSettings;

typedef struct AudioFileConfig
{
   (...)
   int inputId[kMaxInputChannels];
   int outputId[kMaxOutputChannels];
   bool shouldMixInput[kMaxInputChannels];
   bool shouldRecordChannel[kMaxInputChannels];
   (...)

} AudioFileConfig;

In *.txt there are variables:

NUM_CHANNELS_IN             8
NUM_CHANNELS_OUT            8

And on program start I am reading it and writing to variable:

if (!strcmp(tmp_str, "NUM_CHANNELS_IN")) 
        NUM_CHANNELS_IN = atoi(token);
if (!strcmp(tmp_str, "NUM_CHANNELS_OUT")) 
        NUM_CHANNELS_OUT = atoi(token);

I would like to get effect as below but variable needs to be const so it isn't working.

int NUM_CHANNELS_IN;
int NUM_CHANNELS_OUT;

typedef struct AudioDriverSettings
{
   (...)
   ASIOBufferInfo bufferInfos[NUM_CHANNELS_IN + NUM_CHANNELS_OUT]; 
   ASIOChannelInfo channelInfos[NUM_CHANNELS_IN + NUM_CHANNELS_OUT];
   (...)

} AudioDriverSettings;

typedef struct AudioFileConfig
{
   (...)
   int inputId[NUM_CHANNELS_IN];
   int outputId[NUM_CHANNELS_OUT];
   bool shouldMixInput[NUM_CHANNELS_IN];
   bool shouldRecordChannel[NUM_CHANNELS_IN];
   (...)

} AudioFileConfig;

Is there any simple way to handle it?

If this is C, you need to allocate your arrays dynamically:

ASIOBufferInfo *bufferInfos;
...
bufferInfos = malloc(sizeof(ASIOBufferInfo) * (NUM_CHANNELS_IN + NUM_CHANNELS_OUT));

If this is C++, use the std::vector class:

std::vector<ASIOBufferInfo> bufferInfos;
...
bufferInfos.reserve(NUM_CHANNELS_IN + NUM_CHANNELS_OUT);

and then push_back to the vector . Or:

std::vector<ASIOBufferInfo> bufferInfos(NUM_CHANNELS_IN + NUM_CHANNELS_OUT);

and then just access the elements like bufferInfos.at(i) .

I Believe the answer you are looking for is to use pointers. By changing from:

int NUM_CHANNELS_IN; int NUM_CHANNELS_OUT;

to: int *NUM_CHANNELS_IN; int *NUM_CHANNELS_OUT;

You will be able to pass your variable out of functions etc. I cannot say the exact changes you will need to make to your code, you may need to brush up on the proper syntax for pointers, But i believe this is the simplest way to achieve what you are trying to do if I have correctly interpreted your question.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM