From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
04-18-2019 07:48 AM - edited 04-18-2019 08:06 AM
Dear All,
please imagine you have 2 versions of an ini file containing application settings. One is the new and extended version of the other one. I want to port the common part from the older file in to the new one.
So for example we have
new version:
[section]
old tag = 0
new tag = 0
old version:
[section]
old tag = 1
the desired final version should be
[section]
old tag = 1
new tag = 0
To achieved the goal I done what is following:
Created a template ini file that it is basically the new version with all tags getting a default value, 0 suits ok for this example
Then I loaded two IniText and read data from files, one contains the template and one contains the older file.
After that I got the sections in the template file and for each section the items under it.
For each section/tag couple in the template I did a Ini_GetData in the older ini file since I want to deal with raw bytes to remain data agnostic.
If the data has been found in the older ini I wrote it in the template ini file. At the end of the cycle the template file got saved on the disk so the porting should be happily concluded.
But!
The Ini_GetData, despite it returned 1 (i.e. success) give NULL data and 0 as data size.
Do you guys know what I'm doing wrong? Below there is the source code I've written as implementation of what explained just above.
Please be sympathetic with my code coz it is in early stage...It will be improved once I got a working version,
Thanks and regards
static void s_portTestParameterFile( const char * fileName, const char * plainSourcePath, const char * portedPath ) { int sectionsNum; char * sectionName; int itemsNum; char * itemName; char sourceTestParamPath[MAX_PATHNAME_LEN]; char portedTestParamPath[MAX_PATHNAME_LEN]; unsigned char *data; size_t dataSize; IniText templateText = 0; IniText sourceText = 0; if( fileName == NULL || plainSourcePath == NULL || portedPath == NULL ) { printf( "Porting test parameter file: INVALID PARAMETERS!\n"); return; } printf( "\nPorting test parameter file: %s ", fileName ); // load the template ini file templateText = Ini_New( FALSE ); if( templateText == 0 ) goto Error; if( Ini_ReadFromFile( templateText, templatePath ) < 0 ) goto Error; // load the test parameters file to port if( MakePathname( plainSourcePath, fileName, sourceTestParamPath ) < 0 ) goto Error; sourceText = Ini_New( FALSE ); if( sourceText == 0 ) goto Error; if( Ini_ReadFromFile( sourceText, sourceTestParamPath ) < 0 ) goto Error; // get the number of sections in the template name sectionsNum = Ini_NumberOfSections( templateText ); // scan the sections and port the items for( int i = 1; i <= sectionsNum; i ++ ) { if( Ini_NthSectionName( templateText, i, §ionName ) ) { // get the number of item in the section itemsNum = Ini_NumberOfItems(templateText, sectionName ); for( int j = 1; j < itemsNum; j ++ ) { if( Ini_NthItemName(templateText, sectionName, j, &itemName ) ) { if( (Ini_GetData( sourceText, sectionName, itemName, &data, &dataSize ) ) { Ini_PutData( templateText, sectionName, itemName, data, dataSize ); free( data ); } } } } } // write the result in the ported directory if( MakePathname( portedPath, fileName, portedTestParamPath) == 0 ) Ini_WriteToFile( templateText, portedTestParamPath ); s_disposeIniFiles( templateText, sourceText ); printf("[OK]"); return; Error: s_disposeIniFiles( templateText, sourceText ); printf("[FAILED]" ); return; }