tags:

views:

67

answers:

3

Hello

I have to classes, an Executer with these methods:

  1. Executer()
  2. struct Execute(string s)
  3. Lookup(string name, int module, int num, ...)

and a Parser:

  1. Parser()
  2. struct Parse(string s)

The Exectuers Execute method calls the Parsers Parse method. The Parser then chucks the string into smaller bits (it explodes the string on the ;-sign) and returns a struct to the Execute method. This struct it uses to call the Lookup method. The struct that the Parse returns holds some standard information:

  1. An command name
  2. A senderId (a username, a mac address and a password)
  3. A variable number of arguments

And that is my problem. The Lookup method take variable arguments, but how do I handle the the hand over of these variable arguments by the struct? Im not an expert in C and C++. Should I mass the two classes togheter? So the Parser method could call the Execute method, sparing the struct away. Or maybe there is a way of parsing an unknown variable of arguments at runtime? By some sort of array?

EDIT I cant use the STL library from C++. I only use the C++ class and virtual feature. Im writing to an compiler where Im restricted to use almost all of the C libraries + the magic skills of C++ (virtual and class). SOory for not telling that right away.

EDIT 2 Im writing code to an embedded system and thereby using avr-gcc to compile my code. Thats why I cant use STL. The avr-gcc doesnt support this.

+1  A: 

Use std::vector<> or a simular container that can hold an arbitrary number of entries.

struct {
  std::string commandName;
  sender_t senderId;
  std::vector<arg_t> arguments;
};

Edit: oh, you can't use std::vector. In that case: use an array and store the length:

struct {
  const char* commandName;
  sender_t senderId;
  int argumentCount;
  int maxArgumentCount; // you might not need this
  arg_t* arguments; // pointer to array of (at least) argumentCount elements.
};

Use malloc() or new() to create the array for the arguments.

I would suggest to wrap the argumentCount, maxArgumentCount and arguments in a separate class, which can handle the malloc/new and free/delete as well. This will make it easier to prevent memory leaks.

In the end, you'll have written your own vector_of_arg_t class, so maybe have a look at some basic vector implementation. There must be tutorials on that on the web.

Sjoerd
Oh. Im sorry :( I have tagged the wrong way. I use the virtual and class C++ features. I have forgotten to write that I cant use the C++ STL library. This is because of compiler restrictions. I have updated my question.
mslot
@mslot Huh? "They" don't want you to use well-tested, optimized code but prefer you to reinvent the wheel? Strange...
Sjoerd
Embedded system. Im using the avr-gcc compiler so I cant use the neat C++ features other than class and virtual.
mslot
@mslot oh well, sometimes you don't get what you want. I've updated my answer to reflect that situation.
Sjoerd
Sjoerd you gave me some code and thats way I have checked your answer.
mslot
Note after "revelation" this is for embedded system: dynamic allocation might be a bad idea, prefer memory pools or static buffer.
MaR
+1  A: 

You could declare your Lookup method as follows:

void Lookup(string name, int module, int num, std::vector<std::string> &args);

By storing the variable arguments in an args array, you can have as many as you want.

Greg Hewgill
As I have edited, I cant use the STL library. I have updated my question. But maybe I could do this with a standard C like array?
mslot
Yes, you certainly could. Assuming you're allowed to call `malloc()`, that is.
Greg Hewgill
Aaah Im allowed to do that!! In my Parse method I call malloc, return the pointer in the struct and parse the pointer, or what the array contains, to the Lookup function.
mslot
I have given you +1 for telling me the right way in the comment. I havent checked it because Sjoerd have given me some code to chew on.
mslot
+1  A: 

See

Q: How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)?

A: In general, you cannot. Ideally, you should provide a version of that other function which accepts a va_list pointer.

Suppose you want to write a faterror function which will print a fatal error message, then exit. You might like to write it in terms of the error function of question 15.5:

    void faterror(const char *fmt, ...)
    {
        error(fmt, what goes here? );
        exit(EXIT_FAILURE);
    }

but it's not obvious how to hand faterror's arguments off to error.

<snip>

Read on at

http://c-faq.com/varargs/handoff.html

bradgonesurfing
+1 for the link!! I have googled this alot before asking here, and I didnt found this. Thanks.
mslot
It's quite obscure. I only remembered it cause I was forced into a position like the OP a long time ago. It's messy to write vararg C functions and even messier to do handoff.
bradgonesurfing