lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


    Dear Sir
            I am writing to you regarding a problem we faced during our
use of the LUA language. We are using Lua to translate a text file to C code.
We have defined 4 blocks in our text file, and each block in it's turn
include many blocks (array of blocks) as below.

BLOCK_0 =
{
  GENERAL = {'Pattern', 'Pattern', 'Test pattern in start'},
  test_pattern = {'Patterna', 'aa55', 'UINT16', '2', '0', 'Testpattern'}
}

BLOCK_1 =
{
  GENERAL         ={'name', 'prompt', 'help'},
  specific_length ={ 'Length', '1082', 'UINT16', '2', '0', 'Help' },
}

BLOCK_2 =
{
  GENERAL       ={'Something', 'prompt', 'help'},
  Code          = { 'Code'      , '10'  , 'UINT16', '2', '0', 'Help' },
  Length        = { 'Length'    , '1084', 'UINT16', '2', '1', 'Help' },
  No_of_entries = { 'No_entries', '12'  , 'UINT8' , '1', '2', 'Help' }
}

BLOCK_3 =
{
  GENERAL            ={'Something else', 'prompt', 'help'},
  id                 = {'Id'                , 'RT'  , 'NNTS'  , '2',
'0', 'H'},
  bcd                = {'-'                 , '0101', 'BCD'   , '2',
'0', 'H'},
  length             = {'Length'            , '0007', 'UINT16', '2',
'1', 'H'},
  son_re             = {'SON RE'            , '80'  , 'UINT16', '2',
'2', 'H'},
  son_fe             = {'SON FE'            , '01'  , 'UINT16', '2',
'3', 'H'},
  sof_re             = {'SOF RE'            , '80'  , 'UINT16', '2',
'4', 'H'},
  sof_fe             = {'SOF RE'            , '01'  , 'UINT16', '2',
'5', 'H'},
  pll_clock_polarity = {'Pll Clock Polarity', '00'  , 'UINT16', '2',
'6', 'H'},
  pll_latch_polarity = {'Pll latch Polarity', '00'  , 'UINT16', '2',
'7', 'H'},
  prescale           = {'Prescale'          , '02'  , 'UINT16', '2',
'8', 'H'}
}
##########################END###########################

The problem we face is that we get a random segmentation of the array
elements. That is:

When we try to retrieve the keys in the order:
GENERAL, ID, BCD...

we get all keys, but (as far as we can tell) in a more or less random order...
when we wished to retrieve the keys in each block to save them into an array in
our C code. The function which perform this functionality is listed below.

{
  lua_getglobal  (l->lua, blockName);
  t = lua_gettop (l->lua);
  lua_pushnil (l->lua);

  key = (char **) malloc (sizeof (char *)*LUA_EEPROM_NO_PARAMS);
  memset (key, 0, sizeof (char *)*LUA_EEPROM_NO_PARAMS);
  c = 0;

  while (lua_next (l->lua,t) != 0)
    {
      str = (char *)lua_tostring (l->lua,-2);
      if (str != NULL)
 {
          if (strcmp (str, LUA_EEPROM_GENERAL_IDENT) != 0)
     {
       key[c] = strdup (str);
       c++;
     }
 }
 lua_pop (l->lua, 1);
}
##########################END##########################
If we where running the above given code on block_3 we would expect str to 
begiven the values:

GENERAL, ID, BCD...

But we wont get the values in that order, and thats our problem in a nut 
shell... We have read the manual quite thorougly, but as far as we can see we 
have not discovered anything that can cast any light on our problem.

So would you please inform us about the reason of this problem. As it is
very important for us to collect the key words in each block in the
right order (as they appear from top to bottom in the scriptfile) and not just 
to get those keys in a random index.

We wish to retrieve the keys from the C side rather than pushing values from LUA 
to the C-side. The reason for this, ahum, perhaps not recomendable way of doing 
things, is the fact that we wish to keep the LUA script as simple as possible 
due to the fact that our users simply do not wish to learn yet another language 
:-) The above script file resembles any other parameter file in our system, thus 
it can be considered to be 'simple' :-) And yes, We know, we do not have to make
a strdup in the above code... It will be removed when we have passed our 
experimental stage ;-)

If you have any ideas of what we are doing wrong, please give us a hint...

Thanks in advance and hope to hearing from you in the near future.

/Firas Baba Hami
/Frazze, Alias Joakim Hansson