07-25-2012 03:42 PM
How does Ini_NthItemName sort the items?
I have an Ini file called "vatsplatforms.ini"
(This is one of the sections)
[cfg_lm]
SBC_BootstrapC = "SBC-Bootstrap Node B 5798-2098"
SBC_BootstrapD = "SBC-Bootstrap Node A 5798-2099"
SBC_Control = "SBC-Control 5982-2079"
SBC_Digital_Data = "SBC-Digital Data 5798-2089"
SBC_Baseband_FPGA = "SBC-Baseband FPGA 5798-2280"
SBC_PMC_1553 = "SBC-PMC-1553 1553PMC3"
RSM_Bootstrap = "RSM-Bootstrap 5798-1699"
RSM_FPGA = "RSM-FPGA 5798-1650"
RSM_Control = "RSM-Control 5798-1689"
RSM_BIT_DSP = "RSM-BIT DSP 5798-1989"
RPC1_Bootstrap = "RPC1-Bootstrap 5798-1099"
RPC1_Control = "RPC1-Control 5982-1059"
RPC1_Tuning_FPGA = "RPC1-Tuning FPGA 5798-1160"
RPC1_Demodulation_FPGA = "RPC1-Demodulation FPGA 5798-1170"
ADM_OTPI = "ADM-OTPI 5798-1978"
ADM_FPGA = "ADM-FPGA 5798-1850"
I 1st set up to read the ini file
/* Set up the pathName for the .ini file */
GetProjectDir (dirName);
MakePathname (dirName, "vatsplatforms.ini", pathName);
/* create object for holding the value/tag pairs */
iniText = Ini_New (TRUE); /* TRUE for automatic sorting */
/* read in the tag/value pairs */
Ini_ReadFromFile (iniText, pathName);
Then I read # of items
numItems = Ini_NumberOfItems(iniText, "cfg_lm"); // numItems = 16
I then read the 1st item
Ini_NthItemName(iniText, "cfg_lm", numItems, &tagName);
Sometimes the 1st item is SBC_PMC_1553, sometimes it is ADM_FPGA.
Why isn't the 1st item read SBC_BootstrapC?
I have changed the TRUE for automatic sorting to FALSE and no difference.
Is there something I am missing?
Thanks!
John W.
07-26-2012 02:27 AM - edited 07-26-2012 02:31 AM
As far as I can see, the problem is that Ini_NthItemName () uses the list as is and not the sorted one. You can check it by verifying in the source code for the function.
I see no solution to this problem apart a modification to IniFile instrument.
I suppose the function could be modified as follows:
int CVIFUNC Ini_NthItemName(IniText theIniText, const char *sectionName, int index, char **itemName) { IniSection section; IniEntry entry = NULL; int numItems; int result = FALSE; if( !itemName ) return UIENullPointerPassed; section = Ini_FindSection(theIniText, sectionName); if (section) { numItems = (int)ListNumItems(section->entryList); if (index > 0 && index <= numItems) { // Start of modified part if (theIniText->internallySortedList) /* optimization */ ListGetItem(section->internallySortedEntryList, &entry, index); else // End of modified part ListGetItem(section->entryList, &entry, index); *itemName = entry->name; result = TRUE; } } return result; }
Before doing so, make a copy of source and lib file, next compile the modified code (you may want to create a new Ini_NthItemNameEx function instead of directly modifying the existing one).
The above code is just a temptative code not tested: let's see if somebody from NI confirms my opinion.
In case you run this way, keep in mind that you may need to apply the same modification again on any new release of the instrument unless NI modifies it. You may want to add this as a suggestion to the CVI Idea Exchange board too.
07-26-2012 07:11 AM
For a test I did the following:
numItems = Ini_NumberOfItems(iniText, "cfg_lm");
i = 1;
while (i <= numItems)
{
Ini_NthItemName(iniText, "cfg_lm", i, &tagName);
i ++;
}
The order it came out as is:
1 - ADM_FPGA
2 - ADM_OTPI
3 - RPC1_Bootstrap
4 - RPC1_Control
5 - RPC1_Demodulation_FPGA
6 - RPC1_Tuning_FPGA
7 - RSM_BIT_DSP
8 - RSM_Bootstrap
9 - RSM_Control
10 - RSM_FPGA
11 - SBC_Baseband_FPGA
12 - SBC_BootstrapC
13 - SBC_BootstrapD
14 - SBC_Control
15 - SBC_Digital_Data
16 - SBC_PMC_1553
So it looks like it sort the items alphabetically. There is no way to sort in the order they appear in the ini file?
Thanks!
John W.
07-26-2012 01:44 PM
@jwinterb wrote:
Then I read # of items
numItems = Ini_NumberOfItems(iniText, "cfg_lm"); // numItems = 16
I then read the 1st item
Ini_NthItemName(iniText, "cfg_lm", numItems, &tagName);
Sometimes the 1st item is SBC_PMC_1553, sometimes it is ADM_FPGA.
When you make the following call to Ini_NthItemName:
Ini_NthItemName(iniText, "cfg_lm", numItems, &tagName);
According to the Ini_NthItemName LabWindows/CVI 2010 Help, the third argument is the 1-based item index. Your comment indicates that you believe you are reading the first item, but you are actually reading the last. This is due the the fact that numItems is 16, and that is the item index you are requesting.
In light of this, the behavior you are seeing makes sense. When automatic sorting is true, the last item will be SBC_PMC_1553. When automatic sorting is false, it will be the last item in that .ini group's section (cfg_lm), which in your case is ADM_FPGA.
To answer your question, by default the function returns items in the order they appear in the file. If you want the first item, you should change your method call to:
Ini_NthItemName(iniText, "cfg_lm", 1, &tagName);