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!
Related
I have a large module that uses a very large input buffer, consisting of many structures which, in turn, contain other structures and in the end each structure has several variables.
Out of these hundreds of input variables, my module (standalone C entity) uses only a fraction.
I would like to know if there is a way to make a list that will contain only the variables used in my module (would be perfect if it contains the variable type and links to structure/s that contains it).
I tried Doxygen (1.8.5) but I could generate a doc with all input variables, only.
[Later EDIT]
I add an example code and the desired outcome:
#include <stdio.h>
typedef struct subS1{
unsigned char bIn1;
unsigned char bIn2;
} subS1;
typedef struct S1{
struct subS1 stMySubStruct1;
struct subS1 stMySubStruct2;
struct subS1 stMySubStruct3;
} MyInputStruct_t;
void Foo1(MyInputStruct_t *Input);
void Foo2(MyInputStruct_t *Input);
MyInputStruct_t stMyInputStruct = {{1, 2}, {0, 0}, {9, 6}}; // large input buffer
int main() {
Foo1(&stMyInputStruct); // call to my Module 'main' function
return 0;
}
void Foo1(MyInputStruct_t *Input)
{
if(Input->stMySubStruct1.bIn1 == 1)
{
printf("bIn1 = %d\n", Input->stMySubStruct1.bIn1); // stMySubStruct1.bIn1 is used (read or write)
}
Foo2(Input);
return;
}
void Foo2(MyInputStruct_t *Input)
{
if(Input->stMySubStruct3.bIn2 == 0)
{
printf("bIn2 = %d\n", Input->stMySubStruct3.bIn2); // stMySubStruct3.bIn2 is used (read or write)
}
return;
}
The list with just the used inputs for Foo1(): e.g
stMyInputStruct.stMySubStruct1.bIn1 -> is used in Foo1()
stMyInputStruct.stMySubStruct1.bIn2 -> is NOT used
...
stMyInputStruct.stMySubStruct3.bIn2 -> is used in Foo2()
This is just a five-minute hack to demonstrate what I mean, so take it with a grain of salt and for what it is.
So first I downloaded pycparser from https://github.com/eliben/pycparser/
Then I edit the C-generator from https://github.com/eliben/pycparser/blob/master/pycparser/c_generator.py
... adding two lines to the constructor-code (adding two vars struct_refs + struct_ref):
class CGenerator(object):
""" Uses the same visitor pattern as c_ast.NodeVisitor, but modified to
return a value from each visit method, using string accumulation in
generic_visit.
"""
def __init__(self, reduce_parentheses=False):
""" Constructs C-code generator
reduce_parentheses:
if True, eliminates needless parentheses on binary operators
"""
# Statements start with indentation of self.indent_level spaces, using
# the _make_indent method.
self.indent_level = 0
self.reduce_parentheses = reduce_parentheses
# newly added variables here
self.struct_refs = set()
self.struct_ref = None
Then I edit two visitor-functions, to make them save information about the struct-references they parse:
def visit_ID(self, n):
if self.struct_ref:
self.struct_refs.add(self.struct_ref + "->" + n.name)
return n.name
def visit_StructRef(self, n):
sref = self._parenthesize_unless_simple(n.name)
self.struct_ref = sref
self.struct_refs.add(sref)
res = sref + n.type + self.visit(n.field)
self.struct_ref = None
return res
Running this modified piece of Python script over your example code, collects this information:
>>> cgen.struct_refs
{'Input',
'Input->stMySubStruct1',
'Input->stMySubStruct1->bIn1',
'Input->stMySubStruct3',
'Input->stMySubStruct3->bIn2'}
So with a bit more work, it should be able to do the job more generally.
This of course breaks apart in the face of memcpy, struct-member-access-through-pointers etc.
You can also try exploiting structure in your code as well. E.g. If you always call your input-struct "Input", things gets easier.
I had a plan earlier tonight, and now it's turning into a total mess.....I think I'm missing some fundamental concept.
Here's the idea:
I have to send some stuff over uart that looks like this (for example):
rpmL.txt="10"
vbatt.txt="20.4"
info.txt="Low Temperature"
battIcon.pic='8'
Simple enough, right?
Now, I have a bunch of different things that all have that same sort of format, but the only things that change are:
rpmL, vbatt, etc.
txt (this can be txt or pic)
10, 20.4, Low Temperature, 8 (this can be an int or a float or a string or an enumeration...You'll see the enum below )
So, my idea was, just make a typedef'd struct that has all these things that can change in it, and then make another struct that holds a bunch of those typedef'd 'things'.
anyway, here's the code, and i just want to increment things in different ways depending on it's type:
screen.h
typedef enum _ScreenType{
T_INT,
T_FLOAT,
T_CHAR,
T_TEMP_ICON,
T_BATT_ICON
} ScreenType;
typedef enum _tempIcon{
TEMP_LOW = 0,
TEMP_MED,
TEMP_HIGH,
//Add More Elements Up Here
TEMP_MAX_ELEMENTS
}TempIcon;
typedef enum _battIcon{
BATT_ZERO = 0,
BATT_TWENTY_FIVE,
BATT_FIFTY,
BATT_SEVENTY_FIVE,
BATT_FULL,
//Add More Elements Up Here
BATT_MAX_ELEMENTS
}BattIcon;
// Jam everything together so we can just pass the memeber of the following struct to the LCD TX function
typedef struct _packedStringVars{
ScreenType ScreenType; // I think I need this to achieve some crude polymorphism later on...
void* var; //I think I should do this because this can be a float, int or char*...
char* label; //This is always a string...nothing special about this.
}packedStringVars;
typedef struct{
packedStringVars temp;
packedStringVars wattage;
packedStringVars battPercent;
packedStringVars rpmL;
packedStringVars rpmR;
packedStringVars vbat;
packedStringVars tempIcon;
packedStringVars battIcon;
}mainScreenVariables;
and here's the source:
screen.c
#include "screen.h"
#include ..........Everything Else that You Would Expect.......
//This is where I'm trying to 'initialize' the values...I was doing *(int*) and *(float*), etc., but that went nowhere...
mainScreenVariables screenData = {
.temp = {T_INT,0,"temp"},
.wattage = {T_INT,0,"watts"},
.rpmL = {T_INT,0,"rpmL"},
.rpmR = {T_INT,0,"rpmR"},
.vbat = {T_FLOAT,0,"vbat"},
.tempIcon = {T_TEMP_ICON,(TempIcon*)TEMP_LOW,"p1"},
.battIcon = {T_BATT_ICON,(BattIcon*)BATT_FULL,"battIcon"}
};
static void updateValues(void)
{
// As you can see, now i'm typecasting them as I'm using them, which feels all kinds of wrong and also, as it turns out, doesn't even work! surprise surprise....
*(int*)screenData.temp.var++;
*(int*)screenData.wattage.var++;
*(int*)screenData.battPercent.var++;
*(int*)screenData.rpmL.var++;
*(int*)screenData.rpmR.var++;
*(float*)screenData.vbat.var+=0.1;
*(TempIcon*)screenData.tempIcon.var++;
*(ScreenType*)screenData.battIcon.var++;
}
There's more code to make sure the enumerations don't grow beyond their bounds and so forth, but really, all I want to do is set this conceptual object, and send it to the the UART, with a function signature that looks like this:
void ForChristsSakesJustSendTheDamnString(screenData * dat);
I'm sure there's some better, idiomatic way to do this. This is totally out of control and just feels wrong.
Thanks!
I have NO idea if what i'm trying to accomplish is doable at all. I'm pretty new to C.
I have a struct that contains a pointer to another struct, that contains yet another pointer to another struct.
The last struct contains two pointers to structs called "leadingToCw" and "leadingToCcW". I also have a global String variable dir which can change from Cw to Ccw at any point in time. It looks someting like this:
#define concat(a,b) a##b
String dir = "Ccw"; // or "Cw"
struct Intersection {
struct Sector *incoming;
}
struct Sector {
struct Sector *leadingToCcw;
struct Sector *leadingToCw;
};
Now the thing i want to is call the leadingToCcw or leadingToCw depending on the "dir" string, so that the output would look like this:
Intersection->incoming->leadingToCcw
I've tried something like this:
Intersection->incoming->(dir == "Ccw" ? concat(leadingTo, Ccw) : concat(leadingTo, Cw))
but i get this error:
error: expected unqualified-id before '(' token
I don't know if it's worth mentioning but it is for an Arduino project :-)
I hope someone can help me, or kick me in the butt and tell me that it is not feasible!
Why don't use a usual condition?
dir == "Ccw"? Intersection->incoming->leadingToCcw : Intersection->incoming->leadingToCw
There is nothing like String in the C. You can use char * instead like below
char *dir = "Ccw";
if (strcmp(dir, "Ccw") == 0)
{
// Do whatever you want to do with Intersection->incoming->leadingToCcw
}
else if (strcmp(dir, "Cw") == 0)
{
// Do whatever you want to do with Intersection->incoming->leadingToCw
}
I am trying to pass a struct of 2D arrays and to do calculations on them.
typedef struct{
float X[80][2];
float Y[80][2];
float Z[80][2];
int T[80][2];
int K[80];
} STATS;
void MovingAverage(STATS *stat_array, int last_stat) {
//Average = Average(Prev) + (ValueToAverage/n) - (Average(Prev)/n)
stat_array->**X**[last_stat][0] = stat_array->**X**[last_stat][0] +
(stat_array->**X**[last_stat][1] / stat_array->T[last_stat][0]) -
(stat_array->**X**[last_stat][0] / stat_array->T[last_stat][0]);
}
calling the function:
MovingAverage(*stat_array, last_stat);
My question is:
how do I access in a generic way to X Y and Z inside MovingAverage function?
Edit:
void MovingAverage(STATS *stat_array, int last_stat, (char *(array_idx)) {
//Average = Average(Prev) + (ValueToAverage/n) - (Average(Prev)/n)
stat_array->**array_idx**[last_stat][0] =
stat_array->**array_idx**[last_stat][0] +
(stat_array->**array_idx**[last_stat][1] /
stat_array->T[last_stat][0]) -
(stat_array->**array_idx**[last_stat][0] /
stat_array->T[last_stat][0]);
}
I know it won't work, but just to demonstrate my willings,
Somebody here (not me) could probably come up with some preprocessor magic to do what you're asking, but that is a solution I would not pursue. I consider it bad practice since macros can quickly get hairy and tough to debug. You can't have "variables" inside your source code, if that makes sense. During the build procedure, one of the first things that runs is the preprocessor, which resolves all your macros. It then passes that source code to the compiler. The compiler is not going to do any text substitutions for you, it cranks on the source code it has. To achieve what you want, write a function that operates on the type you want, and call that function with all your types. I'd change your MovingAverage function to something like this:
void MovingAverage(float arr[80][2], const int T[80][2], int last_stat)
{
arr[last_stat][0] = ... // whatever calculation you want to do here
}
int main(void)
{
STATS stat_array;
int last_stat;
// .. initialize stat_array and last_stat
// now call MovingAverage with each of your 3 arrays
MovingAverage(stat_array.X, stat_array.T, last_stat);
MovingAverage(stat_array.Y, stat_array.T, last_stat);
MovingAverage(stat_array.Z, stat_array.T, last_stat);
...
return 0;
}
I am working on an embedded application where the device is controlled through a command interface. I mocked the command dispatcher in VC and had it working to my satisfaction; but when I then moved the code over to the embedded environment, I found out that the compiler has a broken implementation of pointer-to-func's.
Here's how I originally implemented the code (in VC):
/* Relevant parts of header file */
typedef struct command {
const char *code;
void *set_dispatcher;
void *get_dispatcher;
const char *_description;
} command_t;
#define COMMAND_ENTRY(label,dispatcher,description) {(const char*)label, &set_##dispatcher, &get_##dispatcher, (const char*)description}
/* Dispatcher data structure in the C file */
const command_t commands[] = {
COMMAND_ENTRY("DH", Dhcp, "DHCP (0=off, 1=on)"),
COMMAND_ENTRY("IP", Ip, "IP Address (192.168.1.205)"),
COMMAND_ENTRY("SM", Subnet, "Subunet Mask (255.255.255.0)"),
COMMAND_ENTRY("DR", DefaultRoute, "Default router (192.168.1.1)"),
COMMAND_ENTRY("UN", Username, "Web username"),
COMMAND_ENTRY("PW", Password, "Web password"),
...
}
/* After matching the received command string to the command "label", the command is dispatched */
if (pc->isGetter)
return ((get_fn_t)(commands[i].get_dispatcher))(pc);
else
return ((set_fn_t)(commands[i].set_dispatcher))(pc);
}
Without the use of function pointers, it seems like my only hope is to use switch()/case statements to call functions. But I'd like to avoid having to manually maintain a large switch() statement.
What I was thinking of doing is moving all the COMMAND_ENTRY lines into a separate include file. Then wraps that include file with varying #define and #undefines. Something like:
/* Create enum's labels */
#define COMMAND_ENTRY(label,dispatcher,description) SET_##dispatcher, GET_##dispatcher
typedef enum command_labels = {
#include "entries.cinc"
DUMMY_ENUM_ENTRY} command_labels_t;
#undefine COMMAND_ENTRY
/* Create command mapping table */
#define COMMAND_ENTRY(label,dispatcher,description) {(const char*)label, SET_##dispatcher, GET_##dispatcher, (const char*)description}
const command_t commands[] = {
#include "entries.cinc"
NULL /* dummy */ };
#undefine COMMAND_ENTRY
/*...*/
int command_dispatcher(command_labels_t dispatcher_id) {
/* Create dispatcher switch statement */
#define COMMAND_ENTRY(label,dispatcher,description) case SET_##dispatcher: return set_##dispatcher(pc); case GET_##dispatcher: return get_##dispatcher(pc);
switch(dispatcher_id) {
#include "entries.cinc"
default:
return NOT_FOUND;
}
#undefine COMMAND_ENTRY
}
Does anyone see a better way to handle this situation? Sadly, 'get another compiler' is not a viable option. :(
--- Edit to add:
Just to clarify, the particular embedded environment is broken in that the compiler is supposed to create a "function-pointer table" which is then used by the compiler to resolve calls to functions through a pointer. Unfortunately, the compiler is broken and doesn't generate a correct function-table.
So I don't have an easy way to extract the func address to invoke it.
--- Edit #2:
Ah, yes, the use of void *(set|get)_dispatcher was my attempt to see if the problem was with the typedefine of the func pointers. Originally, I had
typedef int (*set_fn_t)(cmdContext_t *pCmdCtx);
typedef int (*get_fn_t)(cmdContext_t *pCmdCtx);
typedef struct command {
const char *code;
set_fn_t set_dispatcher;
get_fn_t get_dispatcher;
const char *_description;
} command_t;
You should try changing your struct command so the function pointers have the actual type:
typedef struct command {
const char *code;
set_fn_t set_dispatcher;
get_fn_t get_dispatcher;
const char *_description;
} command_t;
Unfortunately, function pointers are not guaranteed to be able to convert to/from void pointers (that applies only to pointers to objects).
What's the embedded environment?
Given the information posted in the updates to the question, I see that it's really a bugged compiler.
I think that your proposed solution seems pretty reasonable - it's probably similar to what I would have come up with.
A function pointer isn't actually required to fit in a void*. You could check to make sure that the value you're calling is actually the address of the function. If not, use a function pointer type in the struct: either get_fn_t, or IIRC void(*)(void) is guaranteed to be compatible with any function pointer type.
Edit: OK, assuming that calling by value can't be made to work, I can't think of a neater way to do what you need than auto-generating the switch statement. You could maybe use an off-the-shelf ASP-style preprocessor mode for ruby/python/perl/php/whatever prior to the C preprocessor. Something like this:
switch(dispatcher_id) {
<% for c in commands %>
case SET_<% c.dispatcher %>: return set_<% c.dispatcher %>(pc);
case GET_<% c.dispatcher %>: return get_<% c.dispatcher %>(pc);
<% end %>
default:
return NOT_FOUND;
}
might be a bit more readable than the macro/include trick, but introducing a new tool and setting up the makefiles is probably not worth it for such a small amount of code. And the line numbers in the debug info won't relate to the file you think of as the source file unless you do extra work in your preprocessor to specify them.
Can you get the vendor to fix the compiler?
To what extent is the pointer-to-function broken?
If the compiler allows you to get the address of a function (I'm from C++, but &getenv is what I mean), you could wrap the calling convention stuff into assembler.
As said, I'm a C++ssie, but something in the way of
; function call
push [arg1]
push [arg2]
call [command+8] ; at the 4th location, the setter is stored
ret
If even that is broken, you could define an array of extern void* pointers which you define, again, in assembly.
try this syntax:
return (*((get_fn_t)commands[i].get_dispatcher))(pc);
It's been awhile since I've done C & function pointers, but I believe the original C syntax required the * when dereferencing function pointers but most compilers would let you get away without it.
Do you have access to the link map?
If so, maybe you can hack your way around the wonky function-pointer table:
unsigned long addr_get_dhcp = 0x1111111;
unsigned long addr_set_dhcp = 0x2222222; //make these unique numbers.
/* Relevant parts of header file */
typedef struct command {
const char *code;
unsigned long set_dispatcher;
unsigned long get_dispatcher;
const char *_description;
} command_t;
#define COMMAND_ENTRY(label,dispatcher,description) {(const char*)label,
addr_set_##dispatcher, addr_get_##dispatcher, (const char*)description}
Now compile, grab the relevant addresses from the link map, replace the constants, and recompile. Nothing should move, so the map ought to stay the same. (Making the original constants unique should prevent the compiler from collapsing identical values into one storage location. You may need a long long, depending on the architecture)
If the concept works, you could probably add a post-link step running a script to do the replacement automagically. Of course, this is just a theory, it may fail miserably.
Maybe, you need to look into the structure again:
typedef struct command {
const char *code;
void *set_dispatcher; //IMO, it does not look like a function pointer...
void *get_dispatcher; //more like a pointer to void
const char *_description;
} command_t;
Let say your dispatchers have the following similar function definition:
//a function pointer type definition
typedef int (*genericDispatcher)(int data);
Assume that the dispatchers are like below:
int set_DhcpDispatcher(int data) { return data; }
int get_DhcpDispatcher(int data) { return 2*data; }
So, the revised structure will be:
typedef struct command {
const char *code;
genericDispatcher set_dispatcher;
genericDispatcher get_dispatcher;
const char *_description;
} command_t;
Your macro will be:
#define COMMAND_ENTRY(label,dispatcher,description) \
{ (const char*)label, \
set_##dispatcher##Dispatcher, \
get_##dispatcher##Dispatcher, \
(const char*)description }
Then, you can set your array as usual:
int main(int argc, char **argv)
{
int value1 = 0, value2 = 0;
const command_t commands[] = {
COMMAND_ENTRY("DH", Dhcp, "DHCP (0=off, 1=on)")
};
value1 = commands[0].set_dispatcher(1);
value2 = commands[0].get_dispatcher(2);
printf("value1 = %d, value2 = %d", value1, value2);
return 0;
}
Correct me, if I am wrong somewhere... ;)