• Create Account

## Saving data, according to list

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1BytePtr  Members

106
Like
0Likes
Like

Posted 03 November 2012 - 03:03 AM

Hi.

Im working on my script compiler. All works fine, but at the saving part i got some problems.
I need some ideas from other people.

What i have is a list of ID's (identifications) of commands used in script.
The list is built as the commands are parsed from script (i mean, the commands ID's are put in same order into list, as they are found in script). This is important.

Almost each command has it's own struct which must be saved to compiled script in same order as they are found in source of script.

ID's are just int's, for ex: "cls" = 5 "begin" = 20, "end" = 25, "print" = 30 etc.
So for example i have script like:
begin
cls
print "hello"
print "good bye"
end


As you see from example, print is used twice: so command id's list for this script will be like this:
20
5
30
30
25


5 commands. Each of these commands structs must be saved to file, in same order.
The filling of structs is done earlier in parsing stage.

What i have now is just a for_loop, that goes thru ID's, for ex: 0 to 4, in this loop i check what ID i have currently, with "switch" statement.

In each "case" in "switch" i also loop through nr_of_current_command_id's", for example above, i need to loop 2 times, because i have 2 "print" commands.

It kinda works but because of first loop, it writes data too many times.

If i have 1 "print" command, it writes actually 2 of them. If i have 2 "print" commands, it writes 4 of them, etc.
Not good.

Any ideas how i could get rid of the main loop?
Im heavily using std::vector and std::map and iterators, because they are so good and neat.

I thought about something with std::map and go through it and if i find specific ID, i will save it.
But what about order of saving?

Im completely lost here.

I hope somebody understands my problem.
TIA for any help.

### #2RulerOfNothing  Members

1369
Like
0Likes
Like

Posted 03 November 2012 - 03:32 AM

Why do you need a "number of current command ids"? Can't you just loop through repeated commands normally?

### #3BytePtr  Members

106
Like
0Likes
Like

Posted 03 November 2012 - 03:38 AM

I just loop through them, they are saved in specific order, same order as i use the "fwrite" in my CPP source.
But how i would know in advance in what order the commands will be in the script source?
Impossible.

For ex: script
cls
print "test 1"
print "test 2"


I have to write "cls" struct once, and then "print" twice.
In same order. The order could be anything. Because its compiler. And user can write this in any order.

Edited by BytePtr, 03 November 2012 - 03:39 AM.

### #4BytePtr  Members

106
Like
0Likes
Like

Posted 03 November 2012 - 02:48 PM

Seems that i solved this problem, thanks to "boost.any"

http://www.boost.org....html#id1028457
http://sandfly.net.n...art-6-boostany/

But sometimes, very rarely, you actually want to store a whole bunch of messy, unrelated types in a container without trying to ram them into some sort of class hierarchy. Parsers are a good example of this.

Exactly my case! Parser & compiler.

At parsing stage i just "push_back" my structs into custom vector:
using boost::any_cast;
typedef std::vector< boost::any > AnyVector;
AnyVector values;


Later when saving, i just cast them back, get my values and save.
  for ( std::vector< boost::any >::const_iterator p = parser->tab->values.begin();
p != parser->tab->values.end();
++p )
{
if ( p->type() == typeid(int) )
{
cout << "ID: " << any_cast<int>(*p) << endl;
}
if ( p->type() == typeid(MY_STRUCT) )
{
// ....
fwrite(mystruct, ...);
}
}
etc etc...


Each struct is saved correctly with correct nr of them.
Just tested and it works.

Edited by BytePtr, 03 November 2012 - 02:52 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.