当前位置:网站首页>CAPL script pair High level operation of INI configuration file

CAPL script pair High level operation of INI configuration file

2022-07-06 09:52:00 Ant soldier

Related articles

 Insert picture description here


Preface

  • In fact, we have mastered the configuration file , Reading and writing functions and methods of text files , If one INI The file has only a few key value pairs , So it's OK to use built-in functions , But when INI There are many key value pairs in the file , The way of reading and writing built-in functions one by one is very cumbersome , This section aims at this situation INI Upgrade the file by reading and writing , In order to read and write multi key value pairs quickly and conveniently
  • Demonstrate hardware and software environment Win10 x64 ; CANoe 11 SP2 x64

 Please add a picture description

 Please add a picture description

Explanation of batch reading code

  • 1️⃣ This demonstration is still based on bmw2.cfg , Add a new one Nework Node , Used for key trigger test

 Insert picture description here


  • 2️⃣ Below test.ini The file is our test file , There are many key value pairs

 Insert picture description here


  • 3️⃣ We follow our programming habits , The control statement is placed in IniAuto.can In file , Functions and global variables are placed in IniAuto.cin In file
  • IniAuto.can In the code :
/*@!Encoding:936*/
includes
{
    
  #include "IniAuto.cin"
}

on key 'u'
{
    
  write("*****press u***********");
  getKeyValueFromINI(Ini_data_path,var_Ini_Data); 
  if(1) //debug check values
  {
    
    int i ;
    write ("************************Debug data******************************");
    for(i = 0 ;i < var_Ini_Data.items ;i++)
    {
    
      write ("*******section:%s*******",var_Ini_Data.section[i]);
      write ("*******index:%d***keys:%s*******",i,var_Ini_Data.keys[i]);
      write ("*******index:%d***values:%s*******",i,var_Ini_Data.values[i]);
    }   
  } 
}


  • 4️⃣ The header file IniAuto.cin In the code :
/*@!Encoding:936*/

variables
{
      
    char Ini_data_path[100] = ".//TestModule//IniAutoCode//test.ini";  
    char Ini_data_path_out[100] = ".//TestModule//IniAutoCode//test_out.ini";  
    const int max_items = 200 ;
    const int max_keys_size = 50 ;
    const int max_values_size = 300 ;
  
    struct Ini_Data // record Ini data
  {
    
    int  items;
    char section[max_items][max_keys_size];
    char keys   [max_items][max_keys_size];
    char values [max_items][max_values_size];
 
  };
   struct Ini_Data var_Ini_Data;
}

/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  Read in bulk INI ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/
int getKeyValueFromINI(char FilePath[], struct Ini_Data Temp_var_Ini_Data)
{
     
  int i,j,glbHandle;
  char buffer[max_values_size];
  long section_find, key_find;
  char section_temp[max_keys_size];
  char keys_temp_1[max_keys_size]    ,keys_temp_2[max_keys_size];
  char values_temp_1[max_values_size],values_temp_2[max_values_size];
  
  glbHandle = OpenFileRead (FilePath,0);
  
  if (glbHandle!=0 )
  {
       
    write("Open file :%s passed.",FilePath);
    j = 0;
    
    while(fileGetStringSZ(buffer,elcount(buffer),glbHandle)!=0)   
    {
         
      section_find = strstr_regex(buffer, "\\[.*?\\]"); // Matching with regular expressions  []
      if(section_find != -1)
       {
    
        ClearCharArray(section_temp);
        substr_cpy (section_temp,buffer,1,strlen(buffer)-2,  elcount(buffer));// break off both ends , Get rid of []
        continue ; 
       }
      
      key_find = strstr(buffer, "=");     
      
      if(key_find != -1)
      {
    
        ClearCharArray(keys_temp_1);ClearCharArray(keys_temp_2); // Initialize the temporary string before using 
        substr_cpy (keys_temp_1,buffer,0,key_find,  elcount(buffer));// =  Before key
        remove_space(keys_temp_1,keys_temp_2);                 // Clear the space in the character transmission 
       
        ClearCharArray(values_temp_1);ClearCharArray(values_temp_2); 
        substr_cpy (values_temp_1,buffer,key_find+1,strlen(buffer) - key_find, elcount(buffer));//=  After value
        remove_space(values_temp_1,values_temp_2);

        strncpy (Temp_var_Ini_Data.section[j],section_temp  ,elcount(Temp_var_Ini_Data.section[j]));
        strncpy (Temp_var_Ini_Data.keys[j]   ,keys_temp_2   ,elcount(Temp_var_Ini_Data.keys[j]));
        strncpy (Temp_var_Ini_Data.values[j] ,values_temp_2 ,elcount(Temp_var_Ini_Data.values[j]));
        j++;  // index ++ 
      }           
    }  
    Temp_var_Ini_Data.items = j ;  //  Finally, count how many lines of data 
    fileClose (glbHandle);  
  }  
  else
  {
    
    write("Read file :%s failed.",FilePath);
    return 0; //failed 
  }
  return 1; //passed
}

/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  Remove spaces from strings  ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/
void remove_space(char input_c[],char out_c[]) 
{
    
  
  int i,j ;
  j=0;
  
  for(i = 0; i< strlen(input_c);i++)
  {
    
    if (input_c[i] != ' ')
    {
    
      out_c[j] = input_c[i];
      j++;
    }
  } 
}
/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  String initialization  ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/
void ClearCharArray(char arrIn[])
{
    
	int i, length;
  length = strlen(arrIn);
  for(i=length;i>=0;i--){
    
   arrIn[i]=0x00; 
  }
}


  • 5️⃣ The header file IniAuto.cin The definition of "bad" in
    explain : Because it involves batch operation , Everything is broken up , All key/value All data types of are treated as string operations , In the actual code , If there is an integer floating point , Please perform format conversion again

 Insert picture description here


  • 6️⃣ The header file IniAuto.cin in getKeyValueFromINI Comments on functions

 Insert picture description here


  • 7️⃣ Key ’u‘, Look at the test results :

  • We have ini Key value pairs of are converted to CAPl Array type of , In this way, no matter how we read in batches in the next step , rewrite , Delete

 Insert picture description here

Explanation of batch writing code

  • 1️⃣ After we read it , The possible operation is rewriting , Delete , add to , Even new operations , After this series of operations , We have to save it to the original INI Of documents .

  • 2️⃣ We follow our programming habits , The control statement is placed in IniAuto.can In file , Functions and global variables are placed in IniAuto.cin In file

  • IniAuto.can Added code in :

/*@!Encoding:936*/
on key 'm'
{
    
  write("*****press m***********");
  
  SetKeyValueToINI(Ini_data_path_out,var_Ini_Data);
}

  • 3️⃣ The header file IniAuto.cin Code added in :
/*@!Encoding:936*/

/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  Batch write Key/value  To INI ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/
int SetKeyValueToINI(char FilePath[], struct Ini_Data Temp_var_Ini_Data)
{
     
  long retVal;
  int i,j,glbHandle;
  char buffer[max_values_size];
  char section_temp[max_items][max_keys_size];
  char tempText[max_values_size];

  
  ClearCharArray(section_temp);
  glbHandle = OpenFileWrite(FilePath,0); //  write file , In the form of overwriting the source file 
  
  if (glbHandle!=0 )
  {
       
    write("Open file :%s passed.",FilePath);
    j = 0 ;
    for(i=0;i< Temp_var_Ini_Data.items ; i++)
    {
    
                  
      if((strncmp(Temp_var_Ini_Data.section[i],"",strlen(Temp_var_Ini_Data.section[i])) !=0)&&
         (strncmp(Temp_var_Ini_Data.keys[i],   "",strlen(Temp_var_Ini_Data.keys[i]))    !=0)) //section、key  The value is not empty. , write in 
        
      {
    
        /* section_temp Is a temporary string array , Used to store what has been written section,  Before each write, check whether it has been written , If you haven't written , Just add it to this prime Group , If found in this array , No longer write Ini 了  */
        retVal = SeachValueInArrary(Temp_var_Ini_Data.section[i],section_temp );
        
        if (retVal== -1)
        {
           
          snprintf(tempText, elcount(tempText), "\n[%s]\n", Temp_var_Ini_Data.section[i]);
          filePutString (tempText, elcount(tempText),glbHandle);       
          strncpy (section_temp[j],Temp_var_Ini_Data.section[i]  ,elcount(section_temp[j]));
          j++ ;
        }

        snprintf(tempText, elcount(tempText), "%s = %s\n", Temp_var_Ini_Data.keys[i],Temp_var_Ini_Data.values[i]); // Write key value pairs 
        filePutString (tempText, elcount(tempText),glbHandle); 
      }
    }      
      
    fileClose (glbHandle);  
  }  
  else
  {
    
    write("Write file :%s failed.",FilePath);
    return 0; //failed 
  }
  return 1; //passed
}

/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  Find the specified value in a  ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/
long SeachValueInArrary(char target[] ,char source[][])
{
    
  
  int i ;
  
  for(i= 0;i<elcount(source);i++)
  {
    
    if(strncmp(target,source[i],strlen(target)) ==0 )
    {
    
      //write("Seached value %s in the arrary and the index is %d",target,i);
      return i;
    }
  } 
  return -1 ;
}


  • 4️⃣ Let's press the button again ’u‘, Read out INI After the document , Don't do anything , However, it will be pressed ’m‘, Look, the test results have been written in

 Insert picture description here

to update INI File key value pair

  • 1️⃣ After we read it , The possible operation is to rewrite one of the key value pairs ,

  • 2️⃣ We follow our programming habits , The control statement is placed in IniAuto.can In file , Functions and global variables are placed in IniAuto.cin In file

  • IniAuto.can Added code in :

on key 'i' // rewrite key
{
    

  write("*****press %c***********",this);
  updateINIvalue(0,"Tester ","Runer","",var_Ini_Data,Ini_data_path_out); //  Rewrite the first key value pair key, The index value is 0, hold Tester Change to Runer
  
}

on key 'p' // rewrite value
{
    

  write("*****press %c***********",this);
  updateINIvalue(0,"Tester","Tester","https://blog.csdn.net/qq_34414530",var_Ini_Data,Ini_data_path_out); //  Rewrite the first key value pair value, The index value is 0,
  
}

on key 'k' // rewrite value
{
    

  write("*****press %c***********",this);
  updateINIvalue(0,"Tester","Runer","https://blog.csdn.net/qq_34414530",var_Ini_Data,Ini_data_path_out); //  Yes key and value Operate at the same time 
  
}

  • 3️⃣ The header file IniAuto.cin Code added in :

/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  Change key value pairs , Imperfect , Exception capture code needs to be improved  ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/

void updateINIvalue(long index ,char section[],char keys[],char values[],struct Ini_Data Temp_var_Ini_Data,char FilePath[])
{
         
  if(index < Temp_var_Ini_Data.items)
  {
    
    if(strncmp(section,"",strlen(section)) !=0)//  It's not empty , Just write 
    {
    
      strncpy (Temp_var_Ini_Data.section[index],section  ,elcount(Temp_var_Ini_Data.section[index]));
    }
    if(strncmp(keys,"",strlen(keys)) !=0)//  It's not empty , Just write 
    {
    
      strncpy (Temp_var_Ini_Data.keys[index]   ,keys     ,elcount(Temp_var_Ini_Data.keys[index]));
    }
    if(strncmp(values,"",strlen(values)) !=0)//  It's not empty , Just write 
    {
    
      strncpy (Temp_var_Ini_Data.values[index] ,values   ,elcount(Temp_var_Ini_Data.values[index]));
    }
     
        
     SetKeyValueToINI(FilePath, Temp_var_Ini_Data); // preservation 
  }
  else
  {
    
    write("index out of range.");
  }
}



  • 4️⃣ Let's take a look at the test results

Press the key first ’u‘ Read raw INI, However, it will be pressed ’i‘, rewrite Key value :

 Insert picture description here


Press the key first ’u‘ Read raw INI, However, it will be pressed p, rewrite value value :
 Insert picture description here


Press the key first ’u‘ Read raw INI, However, it will be pressed k, rewrite key/value value :
 Insert picture description here

Delete INI File key value pair

  • 1️⃣ After we read it , The possible operation is to rewrite and delete a key value pair ,

  • 2️⃣ We follow our programming habits , The control statement is placed in IniAuto.can In file , Functions and global variables are placed in IniAuto.cin In file

  • IniAuto.can Added code in :

on key 'h' // Delete key value pair 
{
    

  write("*****press %c***********",this);
  deleteINIItem(3,var_Ini_Data,Ini_data_path_out); //  Delete Peed = 20.5
}

  • 3️⃣ The header file IniAuto.cin Code added in :
/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  Delete key value pair , Here delete according to the index , You can also reload functions according to key Value or value Value to delete   Of course, you can also overload functions , Pass in the index array , Go to batch delete ,  We will continue to improve later  ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/

void deleteINIItem(long index ,struct Ini_Data Temp_var_Ini_Data,char FilePath[])
{
         
  if(index < Temp_var_Ini_Data.items)
  {
    
    strncpy (Temp_var_Ini_Data.section[index],""  ,elcount(Temp_var_Ini_Data.section[index]));  
    strncpy (Temp_var_Ini_Data.keys[index]   ,""  ,elcount(Temp_var_Ini_Data.keys[index]));
    strncpy (Temp_var_Ini_Data.values[index] ,""  ,elcount(Temp_var_Ini_Data.values[index]));
        
    SetKeyValueToINI(FilePath, Temp_var_Ini_Data); // preservation 
  }
  else
  {
    
    write("index out of range.");
  }
}


  • 4️⃣ Let's take a look at the test results

Press the key first ’u‘ Read raw INI, However, it will be pressed ’h‘, Delete Peed = 20.5:

 Insert picture description here

increase INI File key value pair

  • 1️⃣ After we read it , The possible operation is to add a key value pair

  • 2️⃣ We follow our programming habits , The control statement is placed in IniAuto.can In file , Functions and global variables are placed in IniAuto.cin In file

  • IniAuto.can Added code in :

on key 'j' // Add key value to 
{
    

  write("*****press %c***********",this);
  appendINIItem("Number","place","shanghai",var_Ini_Data,Ini_data_path_out); //  stay [Number]  Add one next place = shanghai,
}

on key 'g' // Add key value to , new section
{
    

  write("*****press %c***********",this);
  appendINIItem("Position","place","shanghai",var_Ini_Data,Ini_data_path_out); //  stay [Number]  Add one next place = shanghai,
}

  • 3️⃣ The header file IniAuto.cin Code added in :
/*************************************************************************************************** ---------------------------------------------------------------------------------------------------- Revision history: Date 2022-2-18 Author mayixiaobing  Add key value to , The following code supports adding new section, If you want to be in the existing section ---------------------------------------------------------------------------------------------------- ***************************************************************************************************/

void appendINIItem(char section[],char keys[],char values[],struct Ini_Data Temp_var_Ini_Data,char FilePath[])
{
        
  long retIndex ;
  long items;
  long i ;
                    
  if((strncmp(section,"",strlen(section)) !=0)&&
     (strncmp(keys   ,"",strlen(keys))    !=0)) //section、key  The value is not empty. , write in  
  {
    
      
    items = Temp_var_Ini_Data.items ;  
    
    retIndex = SeachValueInArrary(section,Temp_var_Ini_Data.section);    
      
    if (retIndex == -1)// If it's new section, Add 
    {
                
      strncpy (Temp_var_Ini_Data.section[items],section  ,elcount(Temp_var_Ini_Data.section[items]));
      strncpy (Temp_var_Ini_Data.keys[items]   ,keys     ,elcount(Temp_var_Ini_Data.keys[items]));
      strncpy (Temp_var_Ini_Data.values[items] ,values   ,elcount(Temp_var_Ini_Data.values[items]));  

    }
    else // If it is already section, From the index , Move backward 
    {
    
      for(i= items;i > retIndex ;i--)
      {
    
        strncpy (Temp_var_Ini_Data.section[i],Temp_var_Ini_Data.section[i-1]  ,elcount(Temp_var_Ini_Data.section[i]));
        strncpy (Temp_var_Ini_Data.keys[i]   ,Temp_var_Ini_Data.keys[i-1]     ,elcount(Temp_var_Ini_Data.keys[i]));
        strncpy (Temp_var_Ini_Data.values[i] ,Temp_var_Ini_Data.values[i-1]   ,elcount(Temp_var_Ini_Data.values[i]));       
      } 
      strncpy (Temp_var_Ini_Data.section[retIndex],section  ,elcount(Temp_var_Ini_Data.section[retIndex]));
      strncpy (Temp_var_Ini_Data.keys[retIndex]   ,keys     ,elcount(Temp_var_Ini_Data.keys[retIndex]));
      strncpy (Temp_var_Ini_Data.values[retIndex] ,values   ,elcount(Temp_var_Ini_Data.values[retIndex]));  
    }
    
            
    Temp_var_Ini_Data.items = items + 1;    
    SetKeyValueToINI(FilePath, Temp_var_Ini_Data); // preservation 
  }
}

  • 4️⃣ Let's take a look at the test results

Press the key first ’u‘ Read raw INI, However, it will be pressed ’j‘, Existing section New plus one key/value:

 Insert picture description here


Press the key first ’u‘ Read raw INI, However, it will be pressed ’g‘, newly build section as well as key/value:

 Insert picture description here

newly build INI file

  • 1️⃣ We can also directly create a key value pair file , No need to read in advance

  • 2️⃣ We follow our programming habits , The control statement is placed in IniAuto.can In file , Functions and global variables are placed in IniAuto.cin In file

  • IniAuto.can Added code in , Here are two new parameters :

on key 'f' // newly build ini file 
{
    

  write("*****press %c***********",this);
  var_Ini_Data.items = 2;
  strncpy (var_Ini_Data.section[0],"test"  ,elcount(var_Ini_Data.section[0]));
  strncpy (var_Ini_Data.keys[0]   ,"para1" ,elcount(var_Ini_Data.keys[0]));
  strncpy (var_Ini_Data.values[0] ,"1234"  ,elcount(var_Ini_Data.values[0]));
  
  strncpy (var_Ini_Data.section[1],"test"  ,elcount(var_Ini_Data.section[0]));
  strncpy (var_Ini_Data.keys[1]   ,"para2" ,elcount(var_Ini_Data.keys[0]));
  strncpy (var_Ini_Data.values[1] ,"4567"  ,elcount(var_Ini_Data.values[0]));
  
  SetKeyValueToINI(Ini_data_path_out,var_Ini_Data); 
}

  • 3️⃣ The header file IniAuto.cin Code added in :
 No new .cin Code 

  • 4️⃣ Let's take a look at the test results

 Insert picture description here

 Please add a picture description
23

summary

 Please add a picture description

CSDN Source download


7

  • Have the most simple life , The furthest dream , Even if it's freezing tomorrow , Lu Yao's horse died !

  • Wechat partners can pay attention Langge on-board diagnosis , A small circle in the industry , In the group SkyDrive data , Source code , There are all kinds of gods Free time communication technology , Talk about job opportunities .

  • If this blog is helpful to you , please “ give the thumbs-up ” “ Comment on ”“ Collection ” One key, three links Oh ! It's not easy to code words , Everyone's support is my driving force to stick to it .
    18
原网站

版权声明
本文为[Ant soldier]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060903481340.html